blob: e3c5f727d41ca670df5253fe73e68751f0cdc7ce [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallVector.h"
18using namespace clang;
19
20
21/// ParseExternalDeclaration:
22/// external-declaration: [C99 6.9]
23/// [OBJC] objc-class-definition
24/// [OBJC] objc-class-declaration [TODO]
25/// [OBJC] objc-alias-declaration [TODO]
26/// [OBJC] objc-protocol-definition [TODO]
27/// [OBJC] objc-method-definition [TODO]
28/// [OBJC] '@' 'end' [TODO]
Steve Narofffb367882007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff87c329f2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000037 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000039 case tok::objc_implementation:
Steve Naroff81f1bba2007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000041 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000043 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000052 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Narofffb367882007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000065 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000066 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000068 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
Chris Lattner4b009652007-07-25 00:24:17 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
Chris Lattnera1d2bb72007-10-09 17:51:17 +000073 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000074 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000081 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000082
Steve Naroffb4dfe362007-10-02 22:39:18 +000083 return Actions.ActOnForwardClassDeclaration(CurScope, atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000084 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Steve Narofffb367882007-08-20 21:31:48 +000087///
88/// objc-interface:
89/// objc-class-interface-attributes[opt] objc-class-interface
90/// objc-category-interface
91///
92/// objc-class-interface:
93/// '@' 'interface' identifier objc-superclass[opt]
94/// objc-protocol-refs[opt]
95/// objc-class-instance-variables[opt]
96/// objc-interface-decl-list
97/// @end
98///
99/// objc-category-interface:
100/// '@' 'interface' identifier '(' identifier[opt] ')'
101/// objc-protocol-refs[opt]
102/// objc-interface-decl-list
103/// @end
104///
105/// objc-superclass:
106/// ':' identifier
107///
108/// objc-class-interface-attributes:
109/// __attribute__((visibility("default")))
110/// __attribute__((visibility("hidden")))
111/// __attribute__((deprecated))
112/// __attribute__((unavailable))
113/// __attribute__((objc_exception)) - used by NSException on 64-bit
114///
115Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
116 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000121 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000122 Diag(Tok, diag::err_expected_ident); // missing class or category name.
123 return 0;
124 }
125 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000129 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000133 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
149 // Next, we need to check for any protocol references.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000150 if (Tok.is(tok::less)) {
Steve Naroff304ed392007-09-05 23:30:30 +0000151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000152 return 0;
153 }
154 if (attrList) // categories don't support attributes.
155 Diag(Tok, diag::err_objc_no_attributes_on_category);
156
Steve Naroff25aace82007-10-03 21:00:46 +0000157 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(CurScope, atLoc,
158 nameId, nameLoc, categoryId, categoryLoc,
159 &ProtocolRefs[0], ProtocolRefs.size());
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000160
161 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000162
Steve Naroff0bbffd82007-08-22 16:35:03 +0000163 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000164 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000165 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000166 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000167 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000168 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000169 return 0;
170 }
171 // Parse a class interface.
172 IdentifierInfo *superClassId = 0;
173 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000174
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000175 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000176 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000177 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000178 Diag(Tok, diag::err_expected_ident); // missing super class name.
179 return 0;
180 }
181 superClassId = Tok.getIdentifierInfo();
182 superClassLoc = ConsumeToken();
183 }
184 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000185 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000186 if (Tok.is(tok::less)) {
Steve Naroff304ed392007-09-05 23:30:30 +0000187 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000188 return 0;
189 }
Steve Naroff25aace82007-10-03 21:00:46 +0000190 DeclTy *ClsType = Actions.ActOnStartClassInterface(CurScope,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000191 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000192 superClassId, superClassLoc, &ProtocolRefs[0],
193 ProtocolRefs.size(), attrList);
194
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff81f1bba2007-09-06 21:24:23 +0000196 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000197
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000199
200 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000201 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000202 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000203 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000204 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000206 return 0;
207}
208
209/// objc-interface-decl-list:
210/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000211/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000212/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000213/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000214/// objc-interface-decl-list declaration
215/// objc-interface-decl-list ';'
216///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000217/// objc-method-requirement: [OBJC2]
218/// @required
219/// @optional
220///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000221void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
222 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000223 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000224 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000225 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000226 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000227 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000228 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000229
230 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000231 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 } else if (ocKind == tok::objc_required) { // protocols only
233 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000234 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000235 if (contextKey != tok::objc_protocol)
236 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000237 } else if (ocKind == tok::objc_optional) { // protocols only
238 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000239 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000240 if (contextKey != tok::objc_protocol)
241 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000242 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000243 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000244 continue;
245 } else {
246 Diag(Tok, diag::err_objc_illegal_interface_qual);
247 ConsumeToken();
248 }
249 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000250 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
251 DeclTy *methodPrototype =
252 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000253 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000254 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
255 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000256 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000257 continue;
258 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000259 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000260 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000261 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000262 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000263 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000264 // FIXME: as the name implies, this rule allows function definitions.
265 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000266 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000267 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000268 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000269 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000270 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl,
271 &allMethods[0], allMethods.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000272}
273
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000274/// Parse property attribute declarations.
275///
276/// property-attr-decl: '(' property-attrlist ')'
277/// property-attrlist:
278/// property-attribute
279/// property-attrlist ',' property-attribute
280/// property-attribute:
281/// getter '=' identifier
282/// setter '=' identifier ':'
283/// readonly
284/// readwrite
285/// assign
286/// retain
287/// copy
288/// nonatomic
289///
290void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
291 SourceLocation loc = ConsumeParen(); // consume '('
292 while (isObjCPropertyAttribute()) {
293 const IdentifierInfo *II = Tok.getIdentifierInfo();
294 // getter/setter require extra treatment.
295 if (II == ObjcPropertyAttrs[objc_getter] ||
296 II == ObjcPropertyAttrs[objc_setter]) {
297 // skip getter/setter part.
298 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000299 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000300 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000301 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000302 if (II == ObjcPropertyAttrs[objc_setter]) {
303 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000304 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000305 Diag(loc, diag::err_expected_colon);
306 SkipUntil(tok::r_paren,true,true);
307 break;
308 }
309 }
310 }
311 else {
312 Diag(loc, diag::err_expected_ident);
313 SkipUntil(tok::r_paren,true,true);
314 break;
315 }
316 }
317 else {
318 Diag(loc, diag::err_objc_expected_equal);
319 SkipUntil(tok::r_paren,true,true);
320 break;
321 }
322 }
323 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000324 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000325 loc = ConsumeToken();
326 continue;
327 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000328 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000329 break;
330 Diag(loc, diag::err_expected_rparen);
331 SkipUntil(tok::semi);
332 return;
333 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000334 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000335 ConsumeParen();
336 else {
337 Diag(loc, diag::err_objc_expected_property_attr);
338 SkipUntil(tok::r_paren); // recover from error inside attribute list
339 }
340}
341
342/// Main routine to parse property declaration.
343///
344/// @property property-attr-decl[opt] property-component-decl ';'
345///
346void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
347 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
348 "ParseObjCPropertyDecl(): Expected @property");
349 ConsumeToken(); // the "property" identifier
350 // Parse property attribute list, if any.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000351 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000352 // property has attribute list.
353 ParseObjCPropertyAttribute(0/*FIXME*/);
354 }
355 // Parse declaration portion of @property.
356 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
357 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000358 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000359 ConsumeToken();
360 else {
361 Diag(Tok, diag::err_expected_semi_decl_list);
362 SkipUntil(tok::r_brace, true, true);
363 }
Chris Lattner4b009652007-07-25 00:24:17 +0000364}
Steve Narofffb367882007-08-20 21:31:48 +0000365
Steve Naroff81f1bba2007-09-06 21:24:23 +0000366/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000367/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000368/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000369///
370/// objc-instance-method: '-'
371/// objc-class-method: '+'
372///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000373/// objc-method-attributes: [OBJC2]
374/// __attribute__((deprecated))
375///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000376Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
377 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000378 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000379
380 tok::TokenKind methodType = Tok.getKind();
381 SourceLocation methodLoc = ConsumeToken();
382
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000383 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000384 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000385 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000386 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000387}
388
389/// objc-selector:
390/// identifier
391/// one of
392/// enum struct union if else while do for switch case default
393/// break continue return goto asm sizeof typeof __alignof
394/// unsigned long const short volatile signed restrict _Complex
395/// in out inout bycopy byref oneway int char float double void _Bool
396///
397IdentifierInfo *Parser::ParseObjCSelector() {
Chris Lattnerd031a452007-10-07 02:00:24 +0000398 switch (Tok.getKind()) {
399 default:
400 return 0;
401 case tok::identifier:
402 case tok::kw_typeof:
403 case tok::kw___alignof:
404 case tok::kw_auto:
405 case tok::kw_break:
406 case tok::kw_case:
407 case tok::kw_char:
408 case tok::kw_const:
409 case tok::kw_continue:
410 case tok::kw_default:
411 case tok::kw_do:
412 case tok::kw_double:
413 case tok::kw_else:
414 case tok::kw_enum:
415 case tok::kw_extern:
416 case tok::kw_float:
417 case tok::kw_for:
418 case tok::kw_goto:
419 case tok::kw_if:
420 case tok::kw_inline:
421 case tok::kw_int:
422 case tok::kw_long:
423 case tok::kw_register:
424 case tok::kw_restrict:
425 case tok::kw_return:
426 case tok::kw_short:
427 case tok::kw_signed:
428 case tok::kw_sizeof:
429 case tok::kw_static:
430 case tok::kw_struct:
431 case tok::kw_switch:
432 case tok::kw_typedef:
433 case tok::kw_union:
434 case tok::kw_unsigned:
435 case tok::kw_void:
436 case tok::kw_volatile:
437 case tok::kw_while:
438 case tok::kw__Bool:
439 case tok::kw__Complex:
440 IdentifierInfo *II = Tok.getIdentifierInfo();
441 ConsumeToken();
442 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000443 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000444}
445
Steve Naroffa8ee2262007-08-22 23:18:22 +0000446/// objc-type-qualifier: one of
447/// in out inout bycopy byref oneway
448///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000449bool Parser::isObjCTypeQualifier() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000450 if (Tok.is(tok::identifier)) {
Chris Lattner32352462007-08-29 22:54:08 +0000451 const IdentifierInfo *II = Tok.getIdentifierInfo();
452 for (unsigned i = 0; i < objc_NumQuals; ++i)
453 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000454 }
455 return false;
456}
457
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000458/// property-attrlist: one of
459/// readonly getter setter assign retain copy nonatomic
460///
461bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000462 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000463 const IdentifierInfo *II = Tok.getIdentifierInfo();
464 for (unsigned i = 0; i < objc_NumAttrs; ++i)
465 if (II == ObjcPropertyAttrs[i]) return true;
466 }
467 return false;
468}
469
Steve Naroff0bbffd82007-08-22 16:35:03 +0000470/// objc-type-name:
471/// '(' objc-type-qualifiers[opt] type-name ')'
472/// '(' objc-type-qualifiers[opt] ')'
473///
474/// objc-type-qualifiers:
475/// objc-type-qualifier
476/// objc-type-qualifiers objc-type-qualifier
477///
Steve Naroff304ed392007-09-05 23:30:30 +0000478Parser::TypeTy *Parser::ParseObjCTypeName() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000479 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000480
481 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000482 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000483
Steve Naroffa8ee2262007-08-22 23:18:22 +0000484 while (isObjCTypeQualifier())
485 ConsumeToken();
486
Steve Naroff0bbffd82007-08-22 16:35:03 +0000487 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000488 Ty = ParseTypeName();
489 // FIXME: back when Sema support is in place...
490 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000491 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000492 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000493 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000494 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000495 }
496 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000497 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000498}
499
500/// objc-method-decl:
501/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000502/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000503/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000504/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000505///
506/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000507/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000508/// objc-keyword-selector objc-keyword-decl
509///
510/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000511/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
512/// objc-selector ':' objc-keyword-attributes[opt] identifier
513/// ':' objc-type-name objc-keyword-attributes[opt] identifier
514/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000515///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000516/// objc-parmlist:
517/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000518///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000519/// objc-parms:
520/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000521///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000522/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000523/// , ...
524///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000525/// objc-keyword-attributes: [OBJC2]
526/// __attribute__((unused))
527///
Steve Naroff4ed9d662007-09-27 14:38:14 +0000528Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType,
529 SourceLocation mLoc,
530 tok::ObjCKeywordKind MethodImplKind)
531{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000532 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000533 TypeTy *ReturnType = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000534 if (Tok.is(tok::l_paren))
Steve Naroff304ed392007-09-05 23:30:30 +0000535 ReturnType = ParseObjCTypeName();
Chris Lattnerd031a452007-10-07 02:00:24 +0000536
537 IdentifierInfo *SelIdent = ParseObjCSelector();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000538 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000539 if (!SelIdent) {
540 Diag(Tok, diag::err_expected_ident); // missing selector name.
541 // FIXME: this creates a unary selector with a null identifier, is this
542 // ok?? Maybe we should skip to the next semicolon or something.
543 }
544
545 // If attributes exist after the method, parse them.
546 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000547 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000548 MethodAttrs = ParseAttributes();
549
550 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
551 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
552 0, 0, MethodAttrs, MethodImplKind);
553 }
Steve Naroff304ed392007-09-05 23:30:30 +0000554
Steve Naroff4ed9d662007-09-27 14:38:14 +0000555 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
556 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
557 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000558
559 Action::TypeTy *TypeInfo;
560 while (1) {
561 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000562
Chris Lattnerd031a452007-10-07 02:00:24 +0000563 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000564 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000565 Diag(Tok, diag::err_expected_colon);
566 break;
567 }
568 ConsumeToken(); // Eat the ':'.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000569 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000570 TypeInfo = ParseObjCTypeName();
571 else
572 TypeInfo = 0;
573 KeyTypes.push_back(TypeInfo);
Steve Naroff304ed392007-09-05 23:30:30 +0000574
Chris Lattnerd031a452007-10-07 02:00:24 +0000575 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000576 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000577 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000578
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000579 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000580 Diag(Tok, diag::err_expected_ident); // missing argument name.
581 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000582 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000583 ArgNames.push_back(Tok.getIdentifierInfo());
584 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000585
Chris Lattnerd031a452007-10-07 02:00:24 +0000586 // Check for another keyword selector.
587 SelIdent = ParseObjCSelector();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000588 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000589 break;
590 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000591 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000592
593 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000594 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000595 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000596 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000597 ConsumeToken();
598 break;
599 }
600 // Parse the c-style argument declaration-specifier.
601 DeclSpec DS;
602 ParseDeclarationSpecifiers(DS);
603 // Parse the declarator.
604 Declarator ParmDecl(DS, Declarator::PrototypeContext);
605 ParseDeclarator(ParmDecl);
606 }
607
608 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000609 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000610 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000611 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000612 MethodAttrs = ParseAttributes();
613
614 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
615 &KeyIdents[0]);
Steve Naroffb4dfe362007-10-02 22:39:18 +0000616 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000617 &KeyTypes[0], &ArgNames[0],
618 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000619}
620
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000621/// CmpProtocolVals - Comparison predicate for sorting protocols.
622static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
623 const IdentifierInfo* const& rhs) {
624 return strcmp(lhs->getName(), rhs->getName()) < 0;
625}
626
Steve Narofffb367882007-08-20 21:31:48 +0000627/// objc-protocol-refs:
628/// '<' identifier-list '>'
629///
Steve Naroff304ed392007-09-05 23:30:30 +0000630bool Parser::ParseObjCProtocolReferences(
631 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000632 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000633
634 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000635
636 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000637 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000638 Diag(Tok, diag::err_expected_ident);
639 SkipUntil(tok::greater);
640 return true;
641 }
642 ProtocolRefs.push_back(Tok.getIdentifierInfo());
643 ConsumeToken();
644
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000645 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000646 break;
647 ConsumeToken();
648 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000649
650 // Sort protocols, keyed by name.
651 // Later on, we remove duplicates.
652 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
653
654 // Make protocol names unique.
655 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
656 ProtocolRefs.end());
657
Steve Narofffb367882007-08-20 21:31:48 +0000658 // Consume the '>'.
659 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
660}
661
662/// objc-class-instance-variables:
663/// '{' objc-instance-variable-decl-list[opt] '}'
664///
665/// objc-instance-variable-decl-list:
666/// objc-visibility-spec
667/// objc-instance-variable-decl ';'
668/// ';'
669/// objc-instance-variable-decl-list objc-visibility-spec
670/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
671/// objc-instance-variable-decl-list ';'
672///
673/// objc-visibility-spec:
674/// @private
675/// @protected
676/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000677/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000678///
679/// objc-instance-variable-decl:
680/// struct-declaration
681///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000682void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000683 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000684 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000685 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
686 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000687
688 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000689
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000690 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000691 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000692 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000693 // Each iteration of this loop reads one objc-instance-variable-decl.
694
695 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000696 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000697 Diag(Tok, diag::ext_extra_struct_semi);
698 ConsumeToken();
699 continue;
700 }
701 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000702 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000703 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000704 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000705 case tok::objc_private:
706 case tok::objc_public:
707 case tok::objc_protected:
708 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000709 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000710 ConsumeToken();
711 continue;
712 default:
713 Diag(Tok, diag::err_objc_illegal_visibility_spec);
714 ConsumeToken();
715 continue;
716 }
717 }
718 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000719 for (unsigned i = 0; i < IvarDecls.size(); i++) {
720 AllIvarDecls.push_back(IvarDecls[i]);
721 AllVisibilities.push_back(visibility);
722 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000723 IvarDecls.clear();
724
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000725 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000726 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000727 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000728 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
729 break;
730 } else {
731 Diag(Tok, diag::err_expected_semi_decl_list);
732 // Skip to end of block or statement
733 SkipUntil(tok::r_brace, true, true);
734 }
735 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000736 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000737 Actions.ActOnFields(CurScope, LBraceLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000738 &AllIvarDecls[0], AllIvarDecls.size(),
739 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000740 }
Steve Naroffc4474992007-08-21 21:17:12 +0000741 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
742 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000743}
Steve Narofffb367882007-08-20 21:31:48 +0000744
745/// objc-protocol-declaration:
746/// objc-protocol-definition
747/// objc-protocol-forward-reference
748///
749/// objc-protocol-definition:
750/// @protocol identifier
751/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000752/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000753/// @end
754///
755/// objc-protocol-forward-reference:
756/// @protocol identifier-list ';'
757///
758/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000759/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000760/// semicolon in the first alternative if objc-protocol-refs are omitted.
761
Steve Naroff72f17fb2007-08-22 22:17:26 +0000762Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000763 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000764 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
765 ConsumeToken(); // the "protocol" identifier
766
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000767 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000768 Diag(Tok, diag::err_expected_ident); // missing protocol name.
769 return 0;
770 }
771 // Save the protocol name, then consume it.
772 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
773 SourceLocation nameLoc = ConsumeToken();
774
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000775 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000776 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000777 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000778 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000779 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000780 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000781 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000782 ProtocolRefs.push_back(protocolName);
783
784 while (1) {
785 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000786 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000787 Diag(Tok, diag::err_expected_ident);
788 SkipUntil(tok::semi);
789 return 0;
790 }
791 ProtocolRefs.push_back(Tok.getIdentifierInfo());
792 ConsumeToken(); // the identifier
793
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000794 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000795 break;
796 }
797 // Consume the ';'.
798 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
799 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000800 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000801 if (ProtocolRefs.size() > 0)
Steve Naroffb4dfe362007-10-02 22:39:18 +0000802 return Actions.ActOnForwardProtocolDeclaration(CurScope, AtLoc,
803 &ProtocolRefs[0],
804 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000805 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000806 if (Tok.is(tok::less)) {
Steve Naroff304ed392007-09-05 23:30:30 +0000807 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000808 return 0;
809 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000810
Steve Naroff25aace82007-10-03 21:00:46 +0000811 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(CurScope, AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000812 protocolName, nameLoc,
813 &ProtocolRefs[0],
814 ProtocolRefs.size());
815 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000816
817 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000818 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000819 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000820 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000821 }
822 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000823 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000824}
Steve Narofffb367882007-08-20 21:31:48 +0000825
826/// objc-implementation:
827/// objc-class-implementation-prologue
828/// objc-category-implementation-prologue
829///
830/// objc-class-implementation-prologue:
831/// @implementation identifier objc-superclass[opt]
832/// objc-class-instance-variables[opt]
833///
834/// objc-category-implementation-prologue:
835/// @implementation identifier ( identifier )
836
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000837Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
838 SourceLocation atLoc) {
839 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
840 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
841 ConsumeToken(); // the "implementation" identifier
842
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000843 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000844 Diag(Tok, diag::err_expected_ident); // missing class or category name.
845 return 0;
846 }
847 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000848 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000849 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
850
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000851 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000852 // we have a category implementation.
853 SourceLocation lparenLoc = ConsumeParen();
854 SourceLocation categoryLoc, rparenLoc;
855 IdentifierInfo *categoryId = 0;
856
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000857 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000858 categoryId = Tok.getIdentifierInfo();
859 categoryLoc = ConsumeToken();
860 } else {
861 Diag(Tok, diag::err_expected_ident); // missing category name.
862 return 0;
863 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000864 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000865 Diag(Tok, diag::err_expected_rparen);
866 SkipUntil(tok::r_paren, false); // don't stop at ';'
867 return 0;
868 }
869 rparenLoc = ConsumeParen();
Steve Naroff25aace82007-10-03 21:00:46 +0000870 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(CurScope,
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000871 atLoc, nameId, nameLoc, categoryId,
872 categoryLoc);
873 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000874 }
875 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000876 SourceLocation superClassLoc;
877 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000878 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000879 // We have a super class
880 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000881 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000882 Diag(Tok, diag::err_expected_ident); // missing super class name.
883 return 0;
884 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000885 superClassId = Tok.getIdentifierInfo();
886 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000887 }
Steve Naroff25aace82007-10-03 21:00:46 +0000888 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(CurScope,
889 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000890 superClassId, superClassLoc);
891
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000892 if (Tok.is(tok::l_brace))
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000893 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000894
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000895 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000896}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000897Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
898 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
899 "ParseObjCAtEndDeclaration(): Expected @end");
900 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000901 if (ObjcImpDecl) {
902 // Checking is not necessary except that a parse error might have caused
903 // @implementation not to have been parsed to completion and ObjcImpDecl
904 // could be 0.
905 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000906 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
907 &AllImplMethods[0],AllImplMethods.size());
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000908 ObjcImpDecl = 0;
909 AllImplMethods.clear();
910 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000911
Steve Narofffb367882007-08-20 21:31:48 +0000912 return 0;
913}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000914
915/// compatibility-alias-decl:
916/// @compatibility_alias alias-name class-name ';'
917///
918Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
919 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
920 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
921 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000922 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000923 Diag(Tok, diag::err_expected_ident);
924 return 0;
925 }
926 ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000927 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000928 Diag(Tok, diag::err_expected_ident);
929 return 0;
930 }
931 ConsumeToken(); // consume class-name;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000932 if (Tok.isNot(tok::semi))
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000933 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000934 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000935}
936
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000937/// property-synthesis:
938/// @synthesize property-ivar-list ';'
939///
940/// property-ivar-list:
941/// property-ivar
942/// property-ivar-list ',' property-ivar
943///
944/// property-ivar:
945/// identifier
946/// identifier '=' identifier
947///
948Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
949 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
950 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
951 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000952 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000953 Diag(Tok, diag::err_expected_ident);
954 return 0;
955 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000956 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000957 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000958 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000959 // property '=' ivar-name
960 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000961 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000962 Diag(Tok, diag::err_expected_ident);
963 break;
964 }
965 ConsumeToken(); // consume ivar-name
966 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000967 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000968 break;
969 ConsumeToken(); // consume ','
970 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000971 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000972 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
973 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000974}
975
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000976/// property-dynamic:
977/// @dynamic property-list
978///
979/// property-list:
980/// identifier
981/// property-list ',' identifier
982///
983Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
984 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
985 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
986 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000987 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000988 Diag(Tok, diag::err_expected_ident);
989 return 0;
990 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000991 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000992 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000993 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000994 break;
995 ConsumeToken(); // consume ','
996 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000997 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000998 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
999 return 0;
1000}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001001
1002/// objc-throw-statement:
1003/// throw expression[opt];
1004///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001005Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001006 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001007 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001008 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001009 if (Res.isInvalid) {
1010 SkipUntil(tok::semi);
1011 return 0;
1012 }
1013 }
1014 return 0;
1015}
1016
1017/// objc-try-catch-statement:
1018/// @try compound-statement objc-catch-list[opt]
1019/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1020///
1021/// objc-catch-list:
1022/// @catch ( parameter-declaration ) compound-statement
1023/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1024/// catch-parameter-declaration:
1025/// parameter-declaration
1026/// '...' [OBJC2]
1027///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001028Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001029 bool catch_or_finally_seen = false;
1030 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001031 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001032 Diag (Tok, diag::err_expected_lbrace);
1033 return 0;
1034 }
1035 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001036 while (Tok.is(tok::at)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001037 ConsumeToken();
1038 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1039 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001040 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001041 ConsumeParen();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001042 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001043 DeclSpec DS;
1044 ParseDeclarationSpecifiers(DS);
1045 // Parse the parameter-declaration.
1046 // FIXME: BlockContext may not be the right context!
1047 Declarator ParmDecl(DS, Declarator::BlockContext);
1048 ParseDeclarator(ParmDecl);
1049 }
1050 else
1051 ConsumeToken(); // consume '...'
1052 ConsumeParen();
1053 StmtResult CatchMody = ParseCompoundStatementBody();
1054 }
1055 else {
1056 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1057 return 0;
1058 }
1059 catch_or_finally_seen = true;
1060 }
1061 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1062 ConsumeToken(); // consume finally
1063 StmtResult FinallyBody = ParseCompoundStatementBody();
1064 catch_or_finally_seen = true;
1065 break;
1066 }
1067 }
1068 if (!catch_or_finally_seen)
1069 Diag(atLoc, diag::err_missing_catch_finally);
1070 return 0;
1071}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001072
Steve Naroff81f1bba2007-09-06 21:24:23 +00001073/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001074///
1075void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001076 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1077
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001078 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001079 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001080 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001081 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001082 ConsumeToken();
1083
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001084 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001085 Diag (Tok, diag::err_expected_lbrace);
1086 return;
1087 }
1088
1089 StmtResult FnBody = ParseCompoundStatementBody();
1090}
1091
Steve Naroff81f1bba2007-09-06 21:24:23 +00001092/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001093///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001094void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001095 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001096 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001097 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001098 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001099 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001100 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001101 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001102 Diag (Tok, diag::err_expected_lbrace);
1103 return;
1104 }
1105
1106 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001107}
Anders Carlssona66cad42007-08-21 17:43:55 +00001108
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001109Parser::ExprResult Parser::ParseObjCExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001110
1111 switch (Tok.getKind()) {
1112 case tok::string_literal: // primary-expression: string-literal
1113 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001114 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001115 default:
1116 break;
1117 }
1118
1119 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001120 case tok::objc_encode:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001121 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001122 case tok::objc_protocol:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001123 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression());
Anders Carlssona66cad42007-08-21 17:43:55 +00001124 default:
1125 Diag(AtLoc, diag::err_unexpected_at);
1126 SkipUntil(tok::semi);
1127 break;
1128 }
1129
1130 return 0;
1131}
1132
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001133/// objc-message-expr:
1134/// '[' objc-receiver objc-message-args ']'
1135///
1136/// objc-receiver:
1137/// expression
1138/// class-name
1139/// type-name
1140///
1141/// objc-message-args:
1142/// objc-selector
1143/// objc-keywordarg-list
1144///
1145/// objc-keywordarg-list:
1146/// objc-keywordarg
1147/// objc-keywordarg-list objc-keywordarg
1148///
1149/// objc-keywordarg:
1150/// selector-name[opt] ':' objc-keywordexpr
1151///
1152/// objc-keywordexpr:
1153/// nonempty-expr-list
1154///
1155/// nonempty-expr-list:
1156/// assignment-expression
1157/// nonempty-expr-list , assignment-expression
1158///
1159Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001160 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001161 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001162 IdentifierInfo *ReceiverName = 0;
1163 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001164 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001165 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001166 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1167 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001168 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001169 } else {
1170 ExprResult Res = ParseAssignmentExpression();
1171 if (Res.isInvalid) {
1172 SkipUntil(tok::identifier);
1173 return Res;
1174 }
1175 ReceiverExpr = Res.Val;
1176 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001177 // Parse objc-selector
1178 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff4ed9d662007-09-27 14:38:14 +00001179
1180 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1181 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1182
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001183 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001184 while (1) {
1185 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001186 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001187
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001188 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001189 Diag(Tok, diag::err_expected_colon);
1190 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001191 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001192 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001193 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001194 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001195 ExprResult Res = ParseAssignmentExpression();
1196 if (Res.isInvalid) {
1197 SkipUntil(tok::identifier);
1198 return Res;
1199 }
1200 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001201 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001202
1203 // Check for another keyword selector.
1204 selIdent = ParseObjCSelector();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001205 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001206 break;
1207 // We have a selector or a colon, continue parsing.
1208 }
1209 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001210 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001211 ConsumeToken();
1212 /// Parse the expression after ','
1213 ParseAssignmentExpression();
1214 }
1215 } else if (!selIdent) {
1216 Diag(Tok, diag::err_expected_ident); // missing selector name.
1217 SkipUntil(tok::semi);
1218 return 0;
1219 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001220
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001221 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001222 Diag(Tok, diag::err_expected_rsquare);
1223 SkipUntil(tok::semi);
1224 return 0;
1225 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001226 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001227
Steve Narofff9e80db2007-10-05 18:42:47 +00001228 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001229 if (nKeys == 0)
1230 KeyIdents.push_back(selIdent);
1231 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1232
1233 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001234 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001235 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1236 &KeyExprs[0]);
1237 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1238 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001239}
1240
Anders Carlssona66cad42007-08-21 17:43:55 +00001241Parser::ExprResult Parser::ParseObjCStringLiteral() {
1242 ExprResult Res = ParseStringLiteralExpression();
1243
1244 if (Res.isInvalid) return Res;
1245
1246 return Actions.ParseObjCStringLiteral(Res.Val);
1247}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001248
1249/// objc-encode-expression:
1250/// @encode ( type-name )
1251Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001252 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001253
1254 SourceLocation EncLoc = ConsumeToken();
1255
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001256 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001257 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1258 return true;
1259 }
1260
1261 SourceLocation LParenLoc = ConsumeParen();
1262
1263 TypeTy *Ty = ParseTypeName();
1264
Anders Carlsson92faeb82007-08-23 15:31:37 +00001265 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001266
1267 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001268 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001269}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001270
1271/// objc-protocol-expression
1272/// @protocol ( protocol-name )
1273
1274Parser::ExprResult Parser::ParseObjCProtocolExpression()
1275{
1276 SourceLocation ProtoLoc = ConsumeToken();
1277
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001278 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001279 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1280 return true;
1281 }
1282
1283 SourceLocation LParenLoc = ConsumeParen();
1284
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001285 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001286 Diag(Tok, diag::err_expected_ident);
1287 return true;
1288 }
1289
1290 // FIXME: Do something with the protocol name
1291 ConsumeToken();
1292
Anders Carlsson92faeb82007-08-23 15:31:37 +00001293 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001294
1295 // FIXME
1296 return 0;
1297}