blob: c55768704dfa1eee5138cabb8a75206c8feab8df [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) {
65 if (Tok.getKind() != tok::identifier) {
66 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
73 if (Tok.getKind() != tok::comma)
74 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
121 if (Tok.getKind() != tok::identifier) {
122 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
Steve Naroffa7f62782007-08-23 19:56:30 +0000129 if (Tok.getKind() == 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).
Steve Narofffb367882007-08-20 21:31:48 +0000136 if (Tok.getKind() == tok::identifier) {
137 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 }
143 if (Tok.getKind() != tok::r_paren) {
144 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.
150 if (Tok.getKind() == 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
Steve Narofffb367882007-08-20 21:31:48 +0000166 return 0;
167 }
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
Steve Narofffb367882007-08-20 21:31:48 +0000175 if (Tok.getKind() == tok::colon) { // a super class is specified.
176 ConsumeToken();
177 if (Tok.getKind() != tok::identifier) {
178 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;
Steve Narofffb367882007-08-20 21:31:48 +0000186 if (Tok.getKind() == 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
Steve Narofffb367882007-08-20 21:31:48 +0000195 if (Tok.getKind() == 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) {
226 if (Tok.getKind() == tok::at) {
227 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 }
250 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000251 DeclTy *methodPrototype = ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000252 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000253 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
254 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000255 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000256 continue;
257 }
258 if (Tok.getKind() == tok::semi)
259 ConsumeToken();
260 else if (Tok.getKind() == tok::eof)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000261 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000262 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000263 // FIXME: as the name implies, this rule allows function definitions.
264 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000265 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000266 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000267 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000268 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000269 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl,
270 &allMethods[0], allMethods.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000271}
272
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000273/// Parse property attribute declarations.
274///
275/// property-attr-decl: '(' property-attrlist ')'
276/// property-attrlist:
277/// property-attribute
278/// property-attrlist ',' property-attribute
279/// property-attribute:
280/// getter '=' identifier
281/// setter '=' identifier ':'
282/// readonly
283/// readwrite
284/// assign
285/// retain
286/// copy
287/// nonatomic
288///
289void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
290 SourceLocation loc = ConsumeParen(); // consume '('
291 while (isObjCPropertyAttribute()) {
292 const IdentifierInfo *II = Tok.getIdentifierInfo();
293 // getter/setter require extra treatment.
294 if (II == ObjcPropertyAttrs[objc_getter] ||
295 II == ObjcPropertyAttrs[objc_setter]) {
296 // skip getter/setter part.
297 SourceLocation loc = ConsumeToken();
298 if (Tok.getKind() == tok::equal) {
299 loc = ConsumeToken();
300 if (Tok.getKind() == tok::identifier) {
301 if (II == ObjcPropertyAttrs[objc_setter]) {
302 loc = ConsumeToken(); // consume method name
303 if (Tok.getKind() != tok::colon) {
304 Diag(loc, diag::err_expected_colon);
305 SkipUntil(tok::r_paren,true,true);
306 break;
307 }
308 }
309 }
310 else {
311 Diag(loc, diag::err_expected_ident);
312 SkipUntil(tok::r_paren,true,true);
313 break;
314 }
315 }
316 else {
317 Diag(loc, diag::err_objc_expected_equal);
318 SkipUntil(tok::r_paren,true,true);
319 break;
320 }
321 }
322 ConsumeToken(); // consume last attribute token
323 if (Tok.getKind() == tok::comma) {
324 loc = ConsumeToken();
325 continue;
326 }
327 if (Tok.getKind() == tok::r_paren)
328 break;
329 Diag(loc, diag::err_expected_rparen);
330 SkipUntil(tok::semi);
331 return;
332 }
333 if (Tok.getKind() == tok::r_paren)
334 ConsumeParen();
335 else {
336 Diag(loc, diag::err_objc_expected_property_attr);
337 SkipUntil(tok::r_paren); // recover from error inside attribute list
338 }
339}
340
341/// Main routine to parse property declaration.
342///
343/// @property property-attr-decl[opt] property-component-decl ';'
344///
345void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
346 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
347 "ParseObjCPropertyDecl(): Expected @property");
348 ConsumeToken(); // the "property" identifier
349 // Parse property attribute list, if any.
350 if (Tok.getKind() == tok::l_paren) {
351 // property has attribute list.
352 ParseObjCPropertyAttribute(0/*FIXME*/);
353 }
354 // Parse declaration portion of @property.
355 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
356 ParseStructDeclaration(interfaceDecl, PropertyDecls);
357 if (Tok.getKind() == tok::semi)
358 ConsumeToken();
359 else {
360 Diag(Tok, diag::err_expected_semi_decl_list);
361 SkipUntil(tok::r_brace, true, true);
362 }
Chris Lattner4b009652007-07-25 00:24:17 +0000363}
Steve Narofffb367882007-08-20 21:31:48 +0000364
Steve Naroff81f1bba2007-09-06 21:24:23 +0000365/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000366/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000367/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000368///
369/// objc-instance-method: '-'
370/// objc-class-method: '+'
371///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000372/// objc-method-attributes: [OBJC2]
373/// __attribute__((deprecated))
374///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000375Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
376 tok::ObjCKeywordKind MethodImplKind) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000377 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
378 "expected +/-");
379
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() {
398 tok::TokenKind tKind = Tok.getKind();
399 IdentifierInfo *II = 0;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000400 switch (tKind) {
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 II = Tok.getIdentifierInfo();
441 ConsumeToken();
442 default:
443 break;
444 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000445
Steve Naroff0bbffd82007-08-22 16:35:03 +0000446 return II;
447}
448
Steve Naroffa8ee2262007-08-22 23:18:22 +0000449/// objc-type-qualifier: one of
450/// in out inout bycopy byref oneway
451///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000452bool Parser::isObjCTypeQualifier() {
453 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000454 const IdentifierInfo *II = Tok.getIdentifierInfo();
455 for (unsigned i = 0; i < objc_NumQuals; ++i)
456 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000457 }
458 return false;
459}
460
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000461/// property-attrlist: one of
462/// readonly getter setter assign retain copy nonatomic
463///
464bool Parser::isObjCPropertyAttribute() {
465 if (Tok.getKind() == tok::identifier) {
466 const IdentifierInfo *II = Tok.getIdentifierInfo();
467 for (unsigned i = 0; i < objc_NumAttrs; ++i)
468 if (II == ObjcPropertyAttrs[i]) return true;
469 }
470 return false;
471}
472
Steve Naroff0bbffd82007-08-22 16:35:03 +0000473/// objc-type-name:
474/// '(' objc-type-qualifiers[opt] type-name ')'
475/// '(' objc-type-qualifiers[opt] ')'
476///
477/// objc-type-qualifiers:
478/// objc-type-qualifier
479/// objc-type-qualifiers objc-type-qualifier
480///
Steve Naroff304ed392007-09-05 23:30:30 +0000481Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000482 assert(Tok.getKind() == tok::l_paren && "expected (");
483
484 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000485 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000486
Steve Naroffa8ee2262007-08-22 23:18:22 +0000487 while (isObjCTypeQualifier())
488 ConsumeToken();
489
Steve Naroff0bbffd82007-08-22 16:35:03 +0000490 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000491 Ty = ParseTypeName();
492 // FIXME: back when Sema support is in place...
493 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000494 }
495 if (Tok.getKind() != tok::r_paren) {
496 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000497 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000498 }
499 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000500 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000501}
502
Steve Naroff5b82d952007-09-28 23:39:26 +0000503unsigned Selector::getNumArgs() const {
504 unsigned IIF = getIdentifierInfoFlag();
505 if (IIF == ZeroArg)
506 return 0;
507 if (IIF == OneArg)
508 return 1;
509 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
510 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
511 return SI->getNumArgs();
512}
513
514IdentifierInfo *Selector::getIdentifierInfoForSlot(unsigned argIndex) {
515 IdentifierInfo *II = getAsIdentifierInfo();
516 if (II) {
517 assert(((argIndex == 0) || (argIndex == 1)) && "illegal keyword index");
518 return II;
519 }
520 // We point to a MultiKeywordSelector (pointer doesn't contain any flags).
521 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
522 return SI->getIdentifierInfoForSlot(argIndex);
523}
524
525char *MultiKeywordSelector::getName(llvm::SmallVectorImpl<char> &methodName) {
526 methodName[0] = '\0';
527 keyword_iterator KeyIter = keyword_begin();
528 for (unsigned int i = 0; i < NumArgs; i++) {
529 if (KeyIter[i]) {
Steve Naroff96db8562007-10-02 02:01:22 +0000530 unsigned KeyLen = KeyIter[i]->getLength();
Steve Naroff5b82d952007-09-28 23:39:26 +0000531 methodName.append(KeyIter[i]->getName(), KeyIter[i]->getName()+KeyLen);
532 }
533 methodName.push_back(':');
534 }
535 methodName.push_back('\0');
536 return &methodName[0];
537}
538
539char *Selector::getName(llvm::SmallVectorImpl<char> &methodName) {
540 methodName[0] = '\0';
541 IdentifierInfo *II = getAsIdentifierInfo();
542 if (II) {
Steve Naroff96db8562007-10-02 02:01:22 +0000543 unsigned NameLen = II->getLength();
Steve Naroff5b82d952007-09-28 23:39:26 +0000544 methodName.append(II->getName(), II->getName()+NameLen);
545 if (getNumArgs() == 1)
546 methodName.push_back(':');
547 methodName.push_back('\0');
548 } else { // We have a multiple keyword selector (no embedded flags).
549 MultiKeywordSelector *SI = reinterpret_cast<MultiKeywordSelector *>(InfoPtr);
550 SI->getName(methodName);
551 }
552 return &methodName[0];
553}
554
Steve Naroff6cb1d362007-09-28 22:22:11 +0000555Selector Parser::ObjcGetUnarySelector(IdentifierInfo *unarySel)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000556{
Steve Naroff6cb1d362007-09-28 22:22:11 +0000557 return Selector(unarySel, 0);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000558}
559
Steve Naroff6cb1d362007-09-28 22:22:11 +0000560Selector Parser::ObjcGetKeywordSelector(
561 llvm::SmallVectorImpl<IdentifierInfo *> &IIV)
562{
563 if (IIV.size() == 1)
564 return Selector(IIV[0], 1);
565
566 llvm::FoldingSet<MultiKeywordSelector> &SelTab = PP.getSelectorTable();
567
Steve Naroff4ed9d662007-09-27 14:38:14 +0000568 // Unique selector, to guarantee there is one per name.
569 llvm::FoldingSetNodeID ID;
Steve Naroff6cb1d362007-09-28 22:22:11 +0000570 MultiKeywordSelector::Profile(ID, &IIV[0], IIV.size());
Steve Naroff4ed9d662007-09-27 14:38:14 +0000571
572 void *InsertPos = 0;
Steve Naroff6cb1d362007-09-28 22:22:11 +0000573 if (MultiKeywordSelector *SI = SelTab.FindNodeOrInsertPos(ID, InsertPos)) {
574 return Selector(SI);
575 }
576 // MultiKeywordSelector objects are not allocated with new because they have a
Steve Naroff4ed9d662007-09-27 14:38:14 +0000577 // variable size array (for parameter types) at the end of them.
Steve Naroff6cb1d362007-09-28 22:22:11 +0000578 MultiKeywordSelector *SI =
579 (MultiKeywordSelector*)malloc(sizeof(MultiKeywordSelector) +
580 IIV.size()*sizeof(IdentifierInfo *));
581 new (SI) MultiKeywordSelector(IIV.size(), &IIV[0]);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000582 SelTab.InsertNode(SI, InsertPos);
Steve Naroff6cb1d362007-09-28 22:22:11 +0000583 return Selector(SI);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000584}
585
Steve Naroff0bbffd82007-08-22 16:35:03 +0000586/// objc-method-decl:
587/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000588/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000589/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000590/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000591///
592/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000593/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000594/// objc-keyword-selector objc-keyword-decl
595///
596/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000597/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
598/// objc-selector ':' objc-keyword-attributes[opt] identifier
599/// ':' objc-type-name objc-keyword-attributes[opt] identifier
600/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000601///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000602/// objc-parmlist:
603/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000604///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000605/// objc-parms:
606/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000607///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000608/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000609/// , ...
610///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000611/// objc-keyword-attributes: [OBJC2]
612/// __attribute__((unused))
613///
Steve Naroff4ed9d662007-09-27 14:38:14 +0000614Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType,
615 SourceLocation mLoc,
616 tok::ObjCKeywordKind MethodImplKind)
617{
Steve Naroff304ed392007-09-05 23:30:30 +0000618 TypeTy *ReturnType = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000619 AttributeList *methodAttrs = 0;
Steve Naroff304ed392007-09-05 23:30:30 +0000620
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000621 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000622 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000623 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000624 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000625
Steve Naroff4ed9d662007-09-27 14:38:14 +0000626 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
627 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
628 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
629
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000630 if (Tok.getKind() == tok::colon) {
Steve Naroff4ed9d662007-09-27 14:38:14 +0000631 Action::TypeTy *TypeInfo;
Steve Naroff304ed392007-09-05 23:30:30 +0000632
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000633 while (1) {
Steve Naroff4ed9d662007-09-27 14:38:14 +0000634 KeyIdents.push_back(selIdent);
Steve Naroff304ed392007-09-05 23:30:30 +0000635
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000636 // Each iteration parses a single keyword argument.
637 if (Tok.getKind() != tok::colon) {
638 Diag(Tok, diag::err_expected_colon);
639 break;
640 }
Steve Naroff4ed9d662007-09-27 14:38:14 +0000641 ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000642 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Steve Naroff4ed9d662007-09-27 14:38:14 +0000643 TypeInfo = ParseObjCTypeName();
644 else
645 TypeInfo = 0;
646 KeyTypes.push_back(TypeInfo);
647
Steve Naroff72f17fb2007-08-22 22:17:26 +0000648 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000649 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000650 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000651
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000652 if (Tok.getKind() != tok::identifier) {
653 Diag(Tok, diag::err_expected_ident); // missing argument name.
654 break;
655 }
Steve Naroff4ed9d662007-09-27 14:38:14 +0000656 ArgNames.push_back(Tok.getIdentifierInfo());
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000657 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000658
Steve Naroff253118b2007-09-17 20:25:27 +0000659 // Check for another keyword selector.
Steve Naroff304ed392007-09-05 23:30:30 +0000660 selIdent = ParseObjCSelector();
661 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000662 break;
663 // We have a selector or a colon, continue parsing.
664 }
665 // Parse the (optional) parameter list.
666 while (Tok.getKind() == tok::comma) {
667 ConsumeToken();
668 if (Tok.getKind() == tok::ellipsis) {
669 ConsumeToken();
670 break;
671 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000672 // Parse the c-style argument declaration-specifier.
673 DeclSpec DS;
674 ParseDeclarationSpecifiers(DS);
675 // Parse the declarator.
676 Declarator ParmDecl(DS, Declarator::PrototypeContext);
677 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000678 }
Steve Naroff304ed392007-09-05 23:30:30 +0000679 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000680 // If attributes exist after the method, parse them.
681 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
682 methodAttrs = ParseAttributes();
Steve Naroff4ed9d662007-09-27 14:38:14 +0000683
Steve Naroff6cb1d362007-09-28 22:22:11 +0000684 Selector Sel = ObjcGetKeywordSelector(KeyIdents);
Steve Naroffb4dfe362007-10-02 22:39:18 +0000685 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
686 &KeyTypes[0], &ArgNames[0],
687 methodAttrs, MethodImplKind);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000688 } else if (!selIdent) {
689 Diag(Tok, diag::err_expected_ident); // missing selector name.
690 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000691 // If attributes exist after the method, parse them.
692 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
693 methodAttrs = ParseAttributes();
694
Steve Naroff6cb1d362007-09-28 22:22:11 +0000695 Selector Sel = ObjcGetUnarySelector(selIdent);
Steve Naroffb4dfe362007-10-02 22:39:18 +0000696 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
697 0, 0, methodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000698}
699
Steve Narofffb367882007-08-20 21:31:48 +0000700/// objc-protocol-refs:
701/// '<' identifier-list '>'
702///
Steve Naroff304ed392007-09-05 23:30:30 +0000703bool Parser::ParseObjCProtocolReferences(
704 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000705 assert(Tok.getKind() == tok::less && "expected <");
706
707 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000708
709 while (1) {
710 if (Tok.getKind() != tok::identifier) {
711 Diag(Tok, diag::err_expected_ident);
712 SkipUntil(tok::greater);
713 return true;
714 }
715 ProtocolRefs.push_back(Tok.getIdentifierInfo());
716 ConsumeToken();
717
718 if (Tok.getKind() != tok::comma)
719 break;
720 ConsumeToken();
721 }
722 // Consume the '>'.
723 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
724}
725
726/// objc-class-instance-variables:
727/// '{' objc-instance-variable-decl-list[opt] '}'
728///
729/// objc-instance-variable-decl-list:
730/// objc-visibility-spec
731/// objc-instance-variable-decl ';'
732/// ';'
733/// objc-instance-variable-decl-list objc-visibility-spec
734/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
735/// objc-instance-variable-decl-list ';'
736///
737/// objc-visibility-spec:
738/// @private
739/// @protected
740/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000741/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000742///
743/// objc-instance-variable-decl:
744/// struct-declaration
745///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000746void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000747 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000748 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000749 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
750 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000751
752 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000753
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000754 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000755 // While we still have something to read, read the instance variables.
756 while (Tok.getKind() != tok::r_brace &&
757 Tok.getKind() != tok::eof) {
758 // Each iteration of this loop reads one objc-instance-variable-decl.
759
760 // Check for extraneous top-level semicolon.
761 if (Tok.getKind() == tok::semi) {
762 Diag(Tok, diag::ext_extra_struct_semi);
763 ConsumeToken();
764 continue;
765 }
766 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000767 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
768 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000769 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000770 case tok::objc_private:
771 case tok::objc_public:
772 case tok::objc_protected:
773 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000774 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000775 ConsumeToken();
776 continue;
777 default:
778 Diag(Tok, diag::err_objc_illegal_visibility_spec);
779 ConsumeToken();
780 continue;
781 }
782 }
783 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000784 for (unsigned i = 0; i < IvarDecls.size(); i++) {
785 AllIvarDecls.push_back(IvarDecls[i]);
786 AllVisibilities.push_back(visibility);
787 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000788 IvarDecls.clear();
789
Steve Naroffc4474992007-08-21 21:17:12 +0000790 if (Tok.getKind() == tok::semi) {
791 ConsumeToken();
792 } else if (Tok.getKind() == tok::r_brace) {
793 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
794 break;
795 } else {
796 Diag(Tok, diag::err_expected_semi_decl_list);
797 // Skip to end of block or statement
798 SkipUntil(tok::r_brace, true, true);
799 }
800 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000801 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000802 Actions.ActOnFields(CurScope, LBraceLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000803 &AllIvarDecls[0], AllIvarDecls.size(),
804 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000805 }
Steve Naroffc4474992007-08-21 21:17:12 +0000806 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
807 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000808}
Steve Narofffb367882007-08-20 21:31:48 +0000809
810/// objc-protocol-declaration:
811/// objc-protocol-definition
812/// objc-protocol-forward-reference
813///
814/// objc-protocol-definition:
815/// @protocol identifier
816/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000817/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000818/// @end
819///
820/// objc-protocol-forward-reference:
821/// @protocol identifier-list ';'
822///
823/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000824/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000825/// semicolon in the first alternative if objc-protocol-refs are omitted.
826
Steve Naroff72f17fb2007-08-22 22:17:26 +0000827Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000828 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000829 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
830 ConsumeToken(); // the "protocol" identifier
831
832 if (Tok.getKind() != tok::identifier) {
833 Diag(Tok, diag::err_expected_ident); // missing protocol name.
834 return 0;
835 }
836 // Save the protocol name, then consume it.
837 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
838 SourceLocation nameLoc = ConsumeToken();
839
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000840 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
841 if (Tok.getKind() == tok::semi) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000842 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000843 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000844 }
845 if (Tok.getKind() == tok::comma) { // list of forward declarations.
846 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000847 ProtocolRefs.push_back(protocolName);
848
849 while (1) {
850 ConsumeToken(); // the ','
851 if (Tok.getKind() != tok::identifier) {
852 Diag(Tok, diag::err_expected_ident);
853 SkipUntil(tok::semi);
854 return 0;
855 }
856 ProtocolRefs.push_back(Tok.getIdentifierInfo());
857 ConsumeToken(); // the identifier
858
859 if (Tok.getKind() != tok::comma)
860 break;
861 }
862 // Consume the ';'.
863 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
864 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000865 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000866 if (ProtocolRefs.size() > 0)
Steve Naroffb4dfe362007-10-02 22:39:18 +0000867 return Actions.ActOnForwardProtocolDeclaration(CurScope, AtLoc,
868 &ProtocolRefs[0],
869 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000870 // Last, and definitely not least, parse a protocol declaration.
871 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000872 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000873 return 0;
874 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000875
Steve Naroff25aace82007-10-03 21:00:46 +0000876 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(CurScope, AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000877 protocolName, nameLoc,
878 &ProtocolRefs[0],
879 ProtocolRefs.size());
880 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000881
882 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000883 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000884 ConsumeToken(); // the "end" identifier
885 return 0;
886 }
887 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000888 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000889}
Steve Narofffb367882007-08-20 21:31:48 +0000890
891/// objc-implementation:
892/// objc-class-implementation-prologue
893/// objc-category-implementation-prologue
894///
895/// objc-class-implementation-prologue:
896/// @implementation identifier objc-superclass[opt]
897/// objc-class-instance-variables[opt]
898///
899/// objc-category-implementation-prologue:
900/// @implementation identifier ( identifier )
901
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000902Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
903 SourceLocation atLoc) {
904 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
905 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
906 ConsumeToken(); // the "implementation" identifier
907
908 if (Tok.getKind() != tok::identifier) {
909 Diag(Tok, diag::err_expected_ident); // missing class or category name.
910 return 0;
911 }
912 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000913 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000914 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
915
916 if (Tok.getKind() == tok::l_paren) {
917 // we have a category implementation.
918 SourceLocation lparenLoc = ConsumeParen();
919 SourceLocation categoryLoc, rparenLoc;
920 IdentifierInfo *categoryId = 0;
921
922 if (Tok.getKind() == tok::identifier) {
923 categoryId = Tok.getIdentifierInfo();
924 categoryLoc = ConsumeToken();
925 } else {
926 Diag(Tok, diag::err_expected_ident); // missing category name.
927 return 0;
928 }
929 if (Tok.getKind() != tok::r_paren) {
930 Diag(Tok, diag::err_expected_rparen);
931 SkipUntil(tok::r_paren, false); // don't stop at ';'
932 return 0;
933 }
934 rparenLoc = ConsumeParen();
Steve Naroff25aace82007-10-03 21:00:46 +0000935 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(CurScope,
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000936 atLoc, nameId, nameLoc, categoryId,
937 categoryLoc);
938 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000939 }
940 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000941 SourceLocation superClassLoc;
942 IdentifierInfo *superClassId = 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000943 if (Tok.getKind() == tok::colon) {
944 // We have a super class
945 ConsumeToken();
946 if (Tok.getKind() != tok::identifier) {
947 Diag(Tok, diag::err_expected_ident); // missing super class name.
948 return 0;
949 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000950 superClassId = Tok.getIdentifierInfo();
951 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000952 }
Steve Naroff25aace82007-10-03 21:00:46 +0000953 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(CurScope,
954 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000955 superClassId, superClassLoc);
956
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000957 if (Tok.getKind() == tok::l_brace)
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000958 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000959
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000960 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000961}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000962Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
963 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
964 "ParseObjCAtEndDeclaration(): Expected @end");
965 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000966 if (ObjcImpDecl) {
967 // Checking is not necessary except that a parse error might have caused
968 // @implementation not to have been parsed to completion and ObjcImpDecl
969 // could be 0.
970 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000971 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
972 &AllImplMethods[0],AllImplMethods.size());
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000973 ObjcImpDecl = 0;
974 AllImplMethods.clear();
975 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000976
Steve Narofffb367882007-08-20 21:31:48 +0000977 return 0;
978}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000979
980/// compatibility-alias-decl:
981/// @compatibility_alias alias-name class-name ';'
982///
983Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
984 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
985 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
986 ConsumeToken(); // consume compatibility_alias
987 if (Tok.getKind() != tok::identifier) {
988 Diag(Tok, diag::err_expected_ident);
989 return 0;
990 }
991 ConsumeToken(); // consume alias-name
992 if (Tok.getKind() != tok::identifier) {
993 Diag(Tok, diag::err_expected_ident);
994 return 0;
995 }
996 ConsumeToken(); // consume class-name;
997 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000998 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000999 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001000}
1001
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001002/// property-synthesis:
1003/// @synthesize property-ivar-list ';'
1004///
1005/// property-ivar-list:
1006/// property-ivar
1007/// property-ivar-list ',' property-ivar
1008///
1009/// property-ivar:
1010/// identifier
1011/// identifier '=' identifier
1012///
1013Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1014 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1015 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1016 SourceLocation loc = ConsumeToken(); // consume dynamic
1017 if (Tok.getKind() != tok::identifier) {
1018 Diag(Tok, diag::err_expected_ident);
1019 return 0;
1020 }
1021 while (Tok.getKind() == tok::identifier) {
1022 ConsumeToken(); // consume property name
1023 if (Tok.getKind() == tok::equal) {
1024 // property '=' ivar-name
1025 ConsumeToken(); // consume '='
1026 if (Tok.getKind() != tok::identifier) {
1027 Diag(Tok, diag::err_expected_ident);
1028 break;
1029 }
1030 ConsumeToken(); // consume ivar-name
1031 }
1032 if (Tok.getKind() != tok::comma)
1033 break;
1034 ConsumeToken(); // consume ','
1035 }
1036 if (Tok.getKind() != tok::semi)
1037 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1038 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001039}
1040
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001041/// property-dynamic:
1042/// @dynamic property-list
1043///
1044/// property-list:
1045/// identifier
1046/// property-list ',' identifier
1047///
1048Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1049 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1050 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1051 SourceLocation loc = ConsumeToken(); // consume dynamic
1052 if (Tok.getKind() != tok::identifier) {
1053 Diag(Tok, diag::err_expected_ident);
1054 return 0;
1055 }
1056 while (Tok.getKind() == tok::identifier) {
1057 ConsumeToken(); // consume property name
1058 if (Tok.getKind() != tok::comma)
1059 break;
1060 ConsumeToken(); // consume ','
1061 }
1062 if (Tok.getKind() != tok::semi)
1063 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1064 return 0;
1065}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001066
1067/// objc-throw-statement:
1068/// throw expression[opt];
1069///
1070Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation &atLoc) {
1071 ConsumeToken(); // consume throw
1072 if (Tok.getKind() != tok::semi) {
1073 ExprResult Res = ParseAssignmentExpression();
1074 if (Res.isInvalid) {
1075 SkipUntil(tok::semi);
1076 return 0;
1077 }
1078 }
1079 return 0;
1080}
1081
1082/// objc-try-catch-statement:
1083/// @try compound-statement objc-catch-list[opt]
1084/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1085///
1086/// objc-catch-list:
1087/// @catch ( parameter-declaration ) compound-statement
1088/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1089/// catch-parameter-declaration:
1090/// parameter-declaration
1091/// '...' [OBJC2]
1092///
1093Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation &atLoc) {
1094 bool catch_or_finally_seen = false;
1095 ConsumeToken(); // consume try
1096 if (Tok.getKind() != tok::l_brace) {
1097 Diag (Tok, diag::err_expected_lbrace);
1098 return 0;
1099 }
1100 StmtResult TryBody = ParseCompoundStatementBody();
1101 while (Tok.getKind() == tok::at) {
1102 ConsumeToken();
1103 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1104 SourceLocation catchLoc = ConsumeToken(); // consume catch
1105 if (Tok.getKind() == tok::l_paren) {
1106 ConsumeParen();
1107 if (Tok.getKind() != tok::ellipsis) {
1108 DeclSpec DS;
1109 ParseDeclarationSpecifiers(DS);
1110 // Parse the parameter-declaration.
1111 // FIXME: BlockContext may not be the right context!
1112 Declarator ParmDecl(DS, Declarator::BlockContext);
1113 ParseDeclarator(ParmDecl);
1114 }
1115 else
1116 ConsumeToken(); // consume '...'
1117 ConsumeParen();
1118 StmtResult CatchMody = ParseCompoundStatementBody();
1119 }
1120 else {
1121 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1122 return 0;
1123 }
1124 catch_or_finally_seen = true;
1125 }
1126 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1127 ConsumeToken(); // consume finally
1128 StmtResult FinallyBody = ParseCompoundStatementBody();
1129 catch_or_finally_seen = true;
1130 break;
1131 }
1132 }
1133 if (!catch_or_finally_seen)
1134 Diag(atLoc, diag::err_missing_catch_finally);
1135 return 0;
1136}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001137
Steve Naroff81f1bba2007-09-06 21:24:23 +00001138/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001139///
1140void Parser::ParseObjCInstanceMethodDefinition() {
1141 assert(Tok.getKind() == tok::minus &&
1142 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001143 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001144 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001145 // parse optional ';'
1146 if (Tok.getKind() == tok::semi)
1147 ConsumeToken();
1148
1149 if (Tok.getKind() != tok::l_brace) {
1150 Diag (Tok, diag::err_expected_lbrace);
1151 return;
1152 }
1153
1154 StmtResult FnBody = ParseCompoundStatementBody();
1155}
1156
Steve Naroff81f1bba2007-09-06 21:24:23 +00001157/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001158///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001159void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001160 assert(Tok.getKind() == tok::plus &&
1161 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001162 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001163 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001164 // parse optional ';'
1165 if (Tok.getKind() == tok::semi)
1166 ConsumeToken();
1167 if (Tok.getKind() != tok::l_brace) {
1168 Diag (Tok, diag::err_expected_lbrace);
1169 return;
1170 }
1171
1172 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001173}
Anders Carlssona66cad42007-08-21 17:43:55 +00001174
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001175Parser::ExprResult Parser::ParseObjCExpression(SourceLocation &AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001176
1177 switch (Tok.getKind()) {
1178 case tok::string_literal: // primary-expression: string-literal
1179 case tok::wide_string_literal:
1180 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001181 default:
1182 break;
1183 }
1184
1185 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001186 case tok::objc_encode:
1187 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001188 case tok::objc_protocol:
1189 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001190 default:
1191 Diag(AtLoc, diag::err_unexpected_at);
1192 SkipUntil(tok::semi);
1193 break;
1194 }
1195
1196 return 0;
1197}
1198
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001199/// objc-message-expr:
1200/// '[' objc-receiver objc-message-args ']'
1201///
1202/// objc-receiver:
1203/// expression
1204/// class-name
1205/// type-name
1206///
1207/// objc-message-args:
1208/// objc-selector
1209/// objc-keywordarg-list
1210///
1211/// objc-keywordarg-list:
1212/// objc-keywordarg
1213/// objc-keywordarg-list objc-keywordarg
1214///
1215/// objc-keywordarg:
1216/// selector-name[opt] ':' objc-keywordexpr
1217///
1218/// objc-keywordexpr:
1219/// nonempty-expr-list
1220///
1221/// nonempty-expr-list:
1222/// assignment-expression
1223/// nonempty-expr-list , assignment-expression
1224///
1225Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001226 assert(Tok.getKind() == tok::l_square && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001227 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001228 IdentifierInfo *ReceiverName = 0;
1229 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001230 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001231 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001232 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1233 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001234 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001235 } else {
1236 ExprResult Res = ParseAssignmentExpression();
1237 if (Res.isInvalid) {
1238 SkipUntil(tok::identifier);
1239 return Res;
1240 }
1241 ReceiverExpr = Res.Val;
1242 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001243 // Parse objc-selector
1244 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff4ed9d662007-09-27 14:38:14 +00001245
1246 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1247 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1248
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001249 if (Tok.getKind() == tok::colon) {
1250 while (1) {
1251 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001252 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001253
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001254 if (Tok.getKind() != tok::colon) {
1255 Diag(Tok, diag::err_expected_colon);
1256 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001257 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001258 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001259 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001260 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001261 ExprResult Res = ParseAssignmentExpression();
1262 if (Res.isInvalid) {
1263 SkipUntil(tok::identifier);
1264 return Res;
1265 }
1266 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001267 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001268
1269 // Check for another keyword selector.
1270 selIdent = ParseObjCSelector();
1271 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001272 break;
1273 // We have a selector or a colon, continue parsing.
1274 }
1275 // Parse the, optional, argument list, comma separated.
1276 while (Tok.getKind() == tok::comma) {
1277 ConsumeToken();
1278 /// Parse the expression after ','
1279 ParseAssignmentExpression();
1280 }
1281 } else if (!selIdent) {
1282 Diag(Tok, diag::err_expected_ident); // missing selector name.
1283 SkipUntil(tok::semi);
1284 return 0;
1285 }
1286 if (Tok.getKind() != tok::r_square) {
1287 Diag(Tok, diag::err_expected_rsquare);
1288 SkipUntil(tok::semi);
1289 return 0;
1290 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001291 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001292
Steve Naroff4ed9d662007-09-27 14:38:14 +00001293 if (KeyIdents.size()) {
Steve Naroff6cb1d362007-09-28 22:22:11 +00001294 Selector sel = ObjcGetKeywordSelector(KeyIdents);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001295 // We've just parsed a keyword message.
1296 if (ReceiverName)
Steve Naroff6cb1d362007-09-28 22:22:11 +00001297 return Actions.ActOnClassMessage(ReceiverName, sel, LBracloc, RBracloc,
Steve Naroff4ed9d662007-09-27 14:38:14 +00001298 &KeyExprs[0]);
Steve Naroff6cb1d362007-09-28 22:22:11 +00001299 return Actions.ActOnInstanceMessage(ReceiverExpr, sel, LBracloc, RBracloc,
Steve Naroff4ed9d662007-09-27 14:38:14 +00001300 &KeyExprs[0]);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001301 }
Steve Naroff6cb1d362007-09-28 22:22:11 +00001302 Selector sel = ObjcGetUnarySelector(selIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001303
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001304 // We've just parsed a unary message (a message with no arguments).
Steve Naroff253118b2007-09-17 20:25:27 +00001305 if (ReceiverName)
Steve Naroff6cb1d362007-09-28 22:22:11 +00001306 return Actions.ActOnClassMessage(ReceiverName, sel, LBracloc, RBracloc, 0);
1307 return Actions.ActOnInstanceMessage(ReceiverExpr, sel, LBracloc, RBracloc, 0);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001308}
1309
Anders Carlssona66cad42007-08-21 17:43:55 +00001310Parser::ExprResult Parser::ParseObjCStringLiteral() {
1311 ExprResult Res = ParseStringLiteralExpression();
1312
1313 if (Res.isInvalid) return Res;
1314
1315 return Actions.ParseObjCStringLiteral(Res.Val);
1316}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001317
1318/// objc-encode-expression:
1319/// @encode ( type-name )
1320Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001321 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001322
1323 SourceLocation EncLoc = ConsumeToken();
1324
1325 if (Tok.getKind() != tok::l_paren) {
1326 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1327 return true;
1328 }
1329
1330 SourceLocation LParenLoc = ConsumeParen();
1331
1332 TypeTy *Ty = ParseTypeName();
1333
Anders Carlsson92faeb82007-08-23 15:31:37 +00001334 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001335
1336 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001337 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001338}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001339
1340/// objc-protocol-expression
1341/// @protocol ( protocol-name )
1342
1343Parser::ExprResult Parser::ParseObjCProtocolExpression()
1344{
1345 SourceLocation ProtoLoc = ConsumeToken();
1346
1347 if (Tok.getKind() != tok::l_paren) {
1348 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1349 return true;
1350 }
1351
1352 SourceLocation LParenLoc = ConsumeParen();
1353
1354 if (Tok.getKind() != tok::identifier) {
1355 Diag(Tok, diag::err_expected_ident);
1356 return true;
1357 }
1358
1359 // FIXME: Do something with the protocol name
1360 ConsumeToken();
1361
Anders Carlsson92faeb82007-08-23 15:31:37 +00001362 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001363
1364 // FIXME
1365 return 0;
1366}