blob: dd9ab2e152ee05b07b9df54730e028fd2825d2d8 [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 Naroff81f1bba2007-09-06 21:24:23 +000083 return Actions.ObjcClassDeclaration(CurScope, atLoc,
84 &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;
133
Steve Naroffa7f62782007-08-23 19:56:30 +0000134 // For ObjC2, the category name is optional (not an error).
Steve Narofffb367882007-08-20 21:31:48 +0000135 if (Tok.getKind() == tok::identifier) {
136 categoryId = Tok.getIdentifierInfo();
137 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000138 } else if (!getLang().ObjC2) {
139 Diag(Tok, diag::err_expected_ident); // missing category name.
140 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000141 }
142 if (Tok.getKind() != tok::r_paren) {
143 Diag(Tok, diag::err_expected_rparen);
144 SkipUntil(tok::r_paren, false); // don't stop at ';'
145 return 0;
146 }
147 rparenLoc = ConsumeParen();
148 // Next, we need to check for any protocol references.
149 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000150 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
151 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
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000157 ParseObjCInterfaceDeclList(0, tok::objc_not_keyword/*FIXME*/);
Steve Narofffb367882007-08-20 21:31:48 +0000158
Steve Naroff0bbffd82007-08-22 16:35:03 +0000159 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000160 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000161 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000162 return 0;
163 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000164 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000165 return 0;
166 }
167 // Parse a class interface.
168 IdentifierInfo *superClassId = 0;
169 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000170
Steve Narofffb367882007-08-20 21:31:48 +0000171 if (Tok.getKind() == tok::colon) { // a super class is specified.
172 ConsumeToken();
173 if (Tok.getKind() != tok::identifier) {
174 Diag(Tok, diag::err_expected_ident); // missing super class name.
175 return 0;
176 }
177 superClassId = Tok.getIdentifierInfo();
178 superClassLoc = ConsumeToken();
179 }
180 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000181 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000182 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000183 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000184 return 0;
185 }
Steve Naroff304ed392007-09-05 23:30:30 +0000186 DeclTy *ClsType = Actions.ObjcStartClassInterface(atLoc, nameId, nameLoc,
187 superClassId, superClassLoc, &ProtocolRefs[0],
188 ProtocolRefs.size(), attrList);
189
Steve Narofffb367882007-08-20 21:31:48 +0000190 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000191 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000192
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000193 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000194
195 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000196 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000197 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000198 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000199 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000200 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000201 return 0;
202}
203
204/// objc-interface-decl-list:
205/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000206/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000207/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000208/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000209/// objc-interface-decl-list declaration
210/// objc-interface-decl-list ';'
211///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000212/// objc-method-requirement: [OBJC2]
213/// @required
214/// @optional
215///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000216void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
217 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000218 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000219 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000220 while (1) {
221 if (Tok.getKind() == tok::at) {
222 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000223 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000224
225 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000226 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000227 } else if (ocKind == tok::objc_required) { // protocols only
228 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000229 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000230 if (contextKey != tok::objc_protocol)
231 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 } else if (ocKind == tok::objc_optional) { // 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_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000237 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000238 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000239 continue;
240 } else {
241 Diag(Tok, diag::err_objc_illegal_interface_qual);
242 ConsumeToken();
243 }
244 }
245 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000246 DeclTy *methodPrototype = ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000247 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000248 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
249 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000250 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000251 continue;
252 }
253 if (Tok.getKind() == tok::semi)
254 ConsumeToken();
255 else if (Tok.getKind() == tok::eof)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000256 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000257 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000258 // FIXME: as the name implies, this rule allows function definitions.
259 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000260 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000261 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000262 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000263 /// Insert collected methods declarations into the @interface object.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000264 Actions.ObjcAddMethodsToClass(interfaceDecl,&allMethods[0],allMethods.size());
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000265 return;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000266}
267
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000268/// Parse property attribute declarations.
269///
270/// property-attr-decl: '(' property-attrlist ')'
271/// property-attrlist:
272/// property-attribute
273/// property-attrlist ',' property-attribute
274/// property-attribute:
275/// getter '=' identifier
276/// setter '=' identifier ':'
277/// readonly
278/// readwrite
279/// assign
280/// retain
281/// copy
282/// nonatomic
283///
284void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
285 SourceLocation loc = ConsumeParen(); // consume '('
286 while (isObjCPropertyAttribute()) {
287 const IdentifierInfo *II = Tok.getIdentifierInfo();
288 // getter/setter require extra treatment.
289 if (II == ObjcPropertyAttrs[objc_getter] ||
290 II == ObjcPropertyAttrs[objc_setter]) {
291 // skip getter/setter part.
292 SourceLocation loc = ConsumeToken();
293 if (Tok.getKind() == tok::equal) {
294 loc = ConsumeToken();
295 if (Tok.getKind() == tok::identifier) {
296 if (II == ObjcPropertyAttrs[objc_setter]) {
297 loc = ConsumeToken(); // consume method name
298 if (Tok.getKind() != tok::colon) {
299 Diag(loc, diag::err_expected_colon);
300 SkipUntil(tok::r_paren,true,true);
301 break;
302 }
303 }
304 }
305 else {
306 Diag(loc, diag::err_expected_ident);
307 SkipUntil(tok::r_paren,true,true);
308 break;
309 }
310 }
311 else {
312 Diag(loc, diag::err_objc_expected_equal);
313 SkipUntil(tok::r_paren,true,true);
314 break;
315 }
316 }
317 ConsumeToken(); // consume last attribute token
318 if (Tok.getKind() == tok::comma) {
319 loc = ConsumeToken();
320 continue;
321 }
322 if (Tok.getKind() == tok::r_paren)
323 break;
324 Diag(loc, diag::err_expected_rparen);
325 SkipUntil(tok::semi);
326 return;
327 }
328 if (Tok.getKind() == tok::r_paren)
329 ConsumeParen();
330 else {
331 Diag(loc, diag::err_objc_expected_property_attr);
332 SkipUntil(tok::r_paren); // recover from error inside attribute list
333 }
334}
335
336/// Main routine to parse property declaration.
337///
338/// @property property-attr-decl[opt] property-component-decl ';'
339///
340void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
341 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
342 "ParseObjCPropertyDecl(): Expected @property");
343 ConsumeToken(); // the "property" identifier
344 // Parse property attribute list, if any.
345 if (Tok.getKind() == tok::l_paren) {
346 // property has attribute list.
347 ParseObjCPropertyAttribute(0/*FIXME*/);
348 }
349 // Parse declaration portion of @property.
350 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
351 ParseStructDeclaration(interfaceDecl, PropertyDecls);
352 if (Tok.getKind() == tok::semi)
353 ConsumeToken();
354 else {
355 Diag(Tok, diag::err_expected_semi_decl_list);
356 SkipUntil(tok::r_brace, true, true);
357 }
Chris Lattner4b009652007-07-25 00:24:17 +0000358}
Steve Narofffb367882007-08-20 21:31:48 +0000359
Steve Naroff81f1bba2007-09-06 21:24:23 +0000360/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000361/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000362/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000363///
364/// objc-instance-method: '-'
365/// objc-class-method: '+'
366///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000367/// objc-method-attributes: [OBJC2]
368/// __attribute__((deprecated))
369///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000370Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
371 tok::ObjCKeywordKind MethodImplKind) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000372 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
373 "expected +/-");
374
375 tok::TokenKind methodType = Tok.getKind();
376 SourceLocation methodLoc = ConsumeToken();
377
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000378 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000379 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000380 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000381 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000382}
383
384/// objc-selector:
385/// identifier
386/// one of
387/// enum struct union if else while do for switch case default
388/// break continue return goto asm sizeof typeof __alignof
389/// unsigned long const short volatile signed restrict _Complex
390/// in out inout bycopy byref oneway int char float double void _Bool
391///
392IdentifierInfo *Parser::ParseObjCSelector() {
393 tok::TokenKind tKind = Tok.getKind();
394 IdentifierInfo *II = 0;
395
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000396 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
397 tKind == tok::kw___alignof ||
Steve Naroff0bbffd82007-08-22 16:35:03 +0000398 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000399 II = Tok.getIdentifierInfo();
400 ConsumeToken();
401 }
402 return II;
403}
404
Steve Naroffa8ee2262007-08-22 23:18:22 +0000405/// objc-type-qualifier: one of
406/// in out inout bycopy byref oneway
407///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000408bool Parser::isObjCTypeQualifier() {
409 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000410 const IdentifierInfo *II = Tok.getIdentifierInfo();
411 for (unsigned i = 0; i < objc_NumQuals; ++i)
412 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000413 }
414 return false;
415}
416
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000417/// property-attrlist: one of
418/// readonly getter setter assign retain copy nonatomic
419///
420bool Parser::isObjCPropertyAttribute() {
421 if (Tok.getKind() == tok::identifier) {
422 const IdentifierInfo *II = Tok.getIdentifierInfo();
423 for (unsigned i = 0; i < objc_NumAttrs; ++i)
424 if (II == ObjcPropertyAttrs[i]) return true;
425 }
426 return false;
427}
428
Steve Naroff0bbffd82007-08-22 16:35:03 +0000429/// objc-type-name:
430/// '(' objc-type-qualifiers[opt] type-name ')'
431/// '(' objc-type-qualifiers[opt] ')'
432///
433/// objc-type-qualifiers:
434/// objc-type-qualifier
435/// objc-type-qualifiers objc-type-qualifier
436///
Steve Naroff304ed392007-09-05 23:30:30 +0000437Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000438 assert(Tok.getKind() == tok::l_paren && "expected (");
439
440 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Naroff304ed392007-09-05 23:30:30 +0000441 TypeTy *Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000442
Steve Naroffa8ee2262007-08-22 23:18:22 +0000443 while (isObjCTypeQualifier())
444 ConsumeToken();
445
Steve Naroff0bbffd82007-08-22 16:35:03 +0000446 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000447 Ty = ParseTypeName();
448 // FIXME: back when Sema support is in place...
449 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000450 }
451 if (Tok.getKind() != tok::r_paren) {
452 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000453 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000454 }
455 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000456 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000457}
458
459/// objc-method-decl:
460/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000461/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000462/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000463/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000464///
465/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000466/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000467/// objc-keyword-selector objc-keyword-decl
468///
469/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000470/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
471/// objc-selector ':' objc-keyword-attributes[opt] identifier
472/// ':' objc-type-name objc-keyword-attributes[opt] identifier
473/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000474///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000475/// objc-parmlist:
476/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000477///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000478/// objc-parms:
479/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000480///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000481/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000482/// , ...
483///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000484/// objc-keyword-attributes: [OBJC2]
485/// __attribute__((unused))
486///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000487Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc,
488 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000489
Steve Naroff304ed392007-09-05 23:30:30 +0000490 TypeTy *ReturnType = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000491 AttributeList *methodAttrs = 0;
Steve Naroff304ed392007-09-05 23:30:30 +0000492
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000493 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000494 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000495 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000496 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000497
Steve Naroff253118b2007-09-17 20:25:27 +0000498 llvm::SmallVector<ObjcKeywordDecl, 12> KeyInfo;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000499
500 if (Tok.getKind() == tok::colon) {
Steve Naroff304ed392007-09-05 23:30:30 +0000501
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000502 while (1) {
Steve Naroff253118b2007-09-17 20:25:27 +0000503 ObjcKeywordDecl KeyInfoDecl;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000504 KeyInfoDecl.SelectorName = selIdent;
Steve Naroff304ed392007-09-05 23:30:30 +0000505
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000506 // Each iteration parses a single keyword argument.
507 if (Tok.getKind() != tok::colon) {
508 Diag(Tok, diag::err_expected_colon);
509 break;
510 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000511 KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000512 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000513 KeyInfoDecl.TypeInfo = ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000514
515 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000516 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000517 KeyInfoDecl.AttrList = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000518
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000519 if (Tok.getKind() != tok::identifier) {
520 Diag(Tok, diag::err_expected_ident); // missing argument name.
521 break;
522 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000523 KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000524 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000525
Steve Naroff253118b2007-09-17 20:25:27 +0000526 // Rather than call out to the actions, package up the info locally,
527 // like we do for Declarator.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000528 KeyInfo.push_back(KeyInfoDecl);
Steve Naroff253118b2007-09-17 20:25:27 +0000529
530 // Check for another keyword selector.
Steve Naroff304ed392007-09-05 23:30:30 +0000531 selIdent = ParseObjCSelector();
532 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000533 break;
534 // We have a selector or a colon, continue parsing.
535 }
536 // Parse the (optional) parameter list.
537 while (Tok.getKind() == tok::comma) {
538 ConsumeToken();
539 if (Tok.getKind() == tok::ellipsis) {
540 ConsumeToken();
541 break;
542 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000543 // Parse the c-style argument declaration-specifier.
544 DeclSpec DS;
545 ParseDeclarationSpecifiers(DS);
546 // Parse the declarator.
547 Declarator ParmDecl(DS, Declarator::PrototypeContext);
548 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000549 }
Steve Naroff304ed392007-09-05 23:30:30 +0000550 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000551 // If attributes exist after the method, parse them.
552 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
553 methodAttrs = ParseAttributes();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000554 return Actions.ObjcBuildMethodDeclaration(mLoc, mType,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000555 ReturnType,
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000556 &KeyInfo[0], KeyInfo.size(),
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000557 methodAttrs, MethodImplKind);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000558 } else if (!selIdent) {
559 Diag(Tok, diag::err_expected_ident); // missing selector name.
560 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000561 // If attributes exist after the method, parse them.
562 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
563 methodAttrs = ParseAttributes();
564
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000565 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
566 selIdent, methodAttrs,
567 MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000568}
569
Steve Narofffb367882007-08-20 21:31:48 +0000570/// objc-protocol-refs:
571/// '<' identifier-list '>'
572///
Steve Naroff304ed392007-09-05 23:30:30 +0000573bool Parser::ParseObjCProtocolReferences(
574 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000575 assert(Tok.getKind() == tok::less && "expected <");
576
577 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000578
579 while (1) {
580 if (Tok.getKind() != tok::identifier) {
581 Diag(Tok, diag::err_expected_ident);
582 SkipUntil(tok::greater);
583 return true;
584 }
585 ProtocolRefs.push_back(Tok.getIdentifierInfo());
586 ConsumeToken();
587
588 if (Tok.getKind() != tok::comma)
589 break;
590 ConsumeToken();
591 }
592 // Consume the '>'.
593 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
594}
595
596/// objc-class-instance-variables:
597/// '{' objc-instance-variable-decl-list[opt] '}'
598///
599/// objc-instance-variable-decl-list:
600/// objc-visibility-spec
601/// objc-instance-variable-decl ';'
602/// ';'
603/// objc-instance-variable-decl-list objc-visibility-spec
604/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
605/// objc-instance-variable-decl-list ';'
606///
607/// objc-visibility-spec:
608/// @private
609/// @protected
610/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000611/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000612///
613/// objc-instance-variable-decl:
614/// struct-declaration
615///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000616void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000617 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000618 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000619 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
620 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000621
622 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000623
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000624 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000625 // While we still have something to read, read the instance variables.
626 while (Tok.getKind() != tok::r_brace &&
627 Tok.getKind() != tok::eof) {
628 // Each iteration of this loop reads one objc-instance-variable-decl.
629
630 // Check for extraneous top-level semicolon.
631 if (Tok.getKind() == tok::semi) {
632 Diag(Tok, diag::ext_extra_struct_semi);
633 ConsumeToken();
634 continue;
635 }
636 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000637 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
638 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000639 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000640 case tok::objc_private:
641 case tok::objc_public:
642 case tok::objc_protected:
643 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000644 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000645 ConsumeToken();
646 continue;
647 default:
648 Diag(Tok, diag::err_objc_illegal_visibility_spec);
649 ConsumeToken();
650 continue;
651 }
652 }
653 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000654 for (unsigned i = 0; i < IvarDecls.size(); i++) {
655 AllIvarDecls.push_back(IvarDecls[i]);
656 AllVisibilities.push_back(visibility);
657 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000658 IvarDecls.clear();
659
Steve Naroffc4474992007-08-21 21:17:12 +0000660 if (Tok.getKind() == tok::semi) {
661 ConsumeToken();
662 } else if (Tok.getKind() == tok::r_brace) {
663 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
664 break;
665 } else {
666 Diag(Tok, diag::err_expected_semi_decl_list);
667 // Skip to end of block or statement
668 SkipUntil(tok::r_brace, true, true);
669 }
670 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000671 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff0acc9c92007-09-15 18:49:24 +0000672 Actions.ActOnFields(LBraceLoc, interfaceDecl,
673 &AllIvarDecls[0], AllIvarDecls.size(),
674 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000675 }
Steve Naroffc4474992007-08-21 21:17:12 +0000676 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
677 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000678}
Steve Narofffb367882007-08-20 21:31:48 +0000679
680/// objc-protocol-declaration:
681/// objc-protocol-definition
682/// objc-protocol-forward-reference
683///
684/// objc-protocol-definition:
685/// @protocol identifier
686/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000687/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000688/// @end
689///
690/// objc-protocol-forward-reference:
691/// @protocol identifier-list ';'
692///
693/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000694/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000695/// semicolon in the first alternative if objc-protocol-refs are omitted.
696
Steve Naroff72f17fb2007-08-22 22:17:26 +0000697Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000698 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000699 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
700 ConsumeToken(); // the "protocol" identifier
701
702 if (Tok.getKind() != tok::identifier) {
703 Diag(Tok, diag::err_expected_ident); // missing protocol name.
704 return 0;
705 }
706 // Save the protocol name, then consume it.
707 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
708 SourceLocation nameLoc = ConsumeToken();
709
710 if (Tok.getKind() == tok::semi) { // forward declaration.
711 ConsumeToken();
712 return 0; // FIXME: add protocolName
713 }
714 if (Tok.getKind() == tok::comma) { // list of forward declarations.
715 // Parse the list of forward declarations.
716 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
717 ProtocolRefs.push_back(protocolName);
718
719 while (1) {
720 ConsumeToken(); // the ','
721 if (Tok.getKind() != tok::identifier) {
722 Diag(Tok, diag::err_expected_ident);
723 SkipUntil(tok::semi);
724 return 0;
725 }
726 ProtocolRefs.push_back(Tok.getIdentifierInfo());
727 ConsumeToken(); // the identifier
728
729 if (Tok.getKind() != tok::comma)
730 break;
731 }
732 // Consume the ';'.
733 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
734 return 0;
735 return 0; // FIXME
736 }
737 // Last, and definitely not least, parse a protocol declaration.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000738 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000739 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000740 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000741 return 0;
742 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000743
744 DeclTy *ProtoType = Actions.ObjcStartProtoInterface(AtLoc,
745 protocolName, nameLoc,
746 &ProtocolRefs[0],
747 ProtocolRefs.size());
748 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000749
750 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000751 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000752 ConsumeToken(); // the "end" identifier
753 return 0;
754 }
755 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000756 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000757}
Steve Narofffb367882007-08-20 21:31:48 +0000758
759/// objc-implementation:
760/// objc-class-implementation-prologue
761/// objc-category-implementation-prologue
762///
763/// objc-class-implementation-prologue:
764/// @implementation identifier objc-superclass[opt]
765/// objc-class-instance-variables[opt]
766///
767/// objc-category-implementation-prologue:
768/// @implementation identifier ( identifier )
769
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000770Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
771 SourceLocation atLoc) {
772 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
773 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
774 ConsumeToken(); // the "implementation" identifier
775
776 if (Tok.getKind() != tok::identifier) {
777 Diag(Tok, diag::err_expected_ident); // missing class or category name.
778 return 0;
779 }
780 // We have a class or category name - consume it.
781 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
782
783 if (Tok.getKind() == tok::l_paren) {
784 // we have a category implementation.
785 SourceLocation lparenLoc = ConsumeParen();
786 SourceLocation categoryLoc, rparenLoc;
787 IdentifierInfo *categoryId = 0;
788
789 if (Tok.getKind() == tok::identifier) {
790 categoryId = Tok.getIdentifierInfo();
791 categoryLoc = ConsumeToken();
792 } else {
793 Diag(Tok, diag::err_expected_ident); // missing category name.
794 return 0;
795 }
796 if (Tok.getKind() != tok::r_paren) {
797 Diag(Tok, diag::err_expected_rparen);
798 SkipUntil(tok::r_paren, false); // don't stop at ';'
799 return 0;
800 }
801 rparenLoc = ConsumeParen();
802 return 0;
803 }
804 // We have a class implementation
805 if (Tok.getKind() == tok::colon) {
806 // We have a super class
807 ConsumeToken();
808 if (Tok.getKind() != tok::identifier) {
809 Diag(Tok, diag::err_expected_ident); // missing super class name.
810 return 0;
811 }
812 ConsumeToken(); // Consume super class name
813 }
814 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000815 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000816
Steve Narofffb367882007-08-20 21:31:48 +0000817 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000818}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000819Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
820 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
821 "ParseObjCAtEndDeclaration(): Expected @end");
822 ConsumeToken(); // the "end" identifier
823
Steve Narofffb367882007-08-20 21:31:48 +0000824 return 0;
825}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000826
827/// compatibility-alias-decl:
828/// @compatibility_alias alias-name class-name ';'
829///
830Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
831 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
832 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
833 ConsumeToken(); // consume compatibility_alias
834 if (Tok.getKind() != tok::identifier) {
835 Diag(Tok, diag::err_expected_ident);
836 return 0;
837 }
838 ConsumeToken(); // consume alias-name
839 if (Tok.getKind() != tok::identifier) {
840 Diag(Tok, diag::err_expected_ident);
841 return 0;
842 }
843 ConsumeToken(); // consume class-name;
844 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000845 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000846 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000847}
848
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000849/// property-synthesis:
850/// @synthesize property-ivar-list ';'
851///
852/// property-ivar-list:
853/// property-ivar
854/// property-ivar-list ',' property-ivar
855///
856/// property-ivar:
857/// identifier
858/// identifier '=' identifier
859///
860Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
861 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
862 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
863 SourceLocation loc = ConsumeToken(); // consume dynamic
864 if (Tok.getKind() != tok::identifier) {
865 Diag(Tok, diag::err_expected_ident);
866 return 0;
867 }
868 while (Tok.getKind() == tok::identifier) {
869 ConsumeToken(); // consume property name
870 if (Tok.getKind() == tok::equal) {
871 // property '=' ivar-name
872 ConsumeToken(); // consume '='
873 if (Tok.getKind() != tok::identifier) {
874 Diag(Tok, diag::err_expected_ident);
875 break;
876 }
877 ConsumeToken(); // consume ivar-name
878 }
879 if (Tok.getKind() != tok::comma)
880 break;
881 ConsumeToken(); // consume ','
882 }
883 if (Tok.getKind() != tok::semi)
884 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
885 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000886}
887
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000888/// property-dynamic:
889/// @dynamic property-list
890///
891/// property-list:
892/// identifier
893/// property-list ',' identifier
894///
895Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
896 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
897 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
898 SourceLocation loc = ConsumeToken(); // consume dynamic
899 if (Tok.getKind() != tok::identifier) {
900 Diag(Tok, diag::err_expected_ident);
901 return 0;
902 }
903 while (Tok.getKind() == tok::identifier) {
904 ConsumeToken(); // consume property name
905 if (Tok.getKind() != tok::comma)
906 break;
907 ConsumeToken(); // consume ','
908 }
909 if (Tok.getKind() != tok::semi)
910 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
911 return 0;
912}
913
Steve Naroff81f1bba2007-09-06 21:24:23 +0000914/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000915///
916void Parser::ParseObjCInstanceMethodDefinition() {
917 assert(Tok.getKind() == tok::minus &&
918 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000919 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000920 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000921 // parse optional ';'
922 if (Tok.getKind() == tok::semi)
923 ConsumeToken();
924
925 if (Tok.getKind() != tok::l_brace) {
926 Diag (Tok, diag::err_expected_lbrace);
927 return;
928 }
929
930 StmtResult FnBody = ParseCompoundStatementBody();
931}
932
Steve Naroff81f1bba2007-09-06 21:24:23 +0000933/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000934///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000935void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000936 assert(Tok.getKind() == tok::plus &&
937 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000938 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000939 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000940 // parse optional ';'
941 if (Tok.getKind() == tok::semi)
942 ConsumeToken();
943 if (Tok.getKind() != tok::l_brace) {
944 Diag (Tok, diag::err_expected_lbrace);
945 return;
946 }
947
948 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +0000949}
Anders Carlssona66cad42007-08-21 17:43:55 +0000950
951Parser::ExprResult Parser::ParseObjCExpression() {
952 SourceLocation AtLoc = ConsumeToken(); // the "@"
953
954 switch (Tok.getKind()) {
955 case tok::string_literal: // primary-expression: string-literal
956 case tok::wide_string_literal:
957 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000958 default:
959 break;
960 }
961
962 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000963 case tok::objc_encode:
964 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000965 case tok::objc_protocol:
966 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000967 default:
968 Diag(AtLoc, diag::err_unexpected_at);
969 SkipUntil(tok::semi);
970 break;
971 }
972
973 return 0;
974}
975
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000976/// objc-message-expr:
977/// '[' objc-receiver objc-message-args ']'
978///
979/// objc-receiver:
980/// expression
981/// class-name
982/// type-name
983///
984/// objc-message-args:
985/// objc-selector
986/// objc-keywordarg-list
987///
988/// objc-keywordarg-list:
989/// objc-keywordarg
990/// objc-keywordarg-list objc-keywordarg
991///
992/// objc-keywordarg:
993/// selector-name[opt] ':' objc-keywordexpr
994///
995/// objc-keywordexpr:
996/// nonempty-expr-list
997///
998/// nonempty-expr-list:
999/// assignment-expression
1000/// nonempty-expr-list , assignment-expression
1001///
1002Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001003 assert(Tok.getKind() == tok::l_square && "'[' expected");
1004 SourceLocation Loc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001005 IdentifierInfo *ReceiverName = 0;
1006 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001007 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001008 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001009 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1010 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001011 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001012 } else {
1013 ExprResult Res = ParseAssignmentExpression();
1014 if (Res.isInvalid) {
1015 SkipUntil(tok::identifier);
1016 return Res;
1017 }
1018 ReceiverExpr = Res.Val;
1019 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001020 // Parse objc-selector
1021 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff253118b2007-09-17 20:25:27 +00001022 llvm::SmallVector<ObjcKeywordMessage, 12> KeyInfo;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001023 if (Tok.getKind() == tok::colon) {
1024 while (1) {
1025 // Each iteration parses a single keyword argument.
Steve Naroff253118b2007-09-17 20:25:27 +00001026 ObjcKeywordMessage KeyInfoMess;
1027 KeyInfoMess.SelectorName = selIdent;
1028
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001029 if (Tok.getKind() != tok::colon) {
1030 Diag(Tok, diag::err_expected_colon);
1031 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001032 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001033 }
Steve Naroff253118b2007-09-17 20:25:27 +00001034 KeyInfoMess.ColonLoc = ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001035 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001036 ExprResult Res = ParseAssignmentExpression();
1037 if (Res.isInvalid) {
1038 SkipUntil(tok::identifier);
1039 return Res;
1040 }
1041 // We have a valid expression.
1042 KeyInfoMess.KeywordExpr = Res.Val;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001043
Steve Naroff253118b2007-09-17 20:25:27 +00001044 // Rather than call out to the actions, package up the info locally,
1045 // like we do for Declarator.
1046 KeyInfo.push_back(KeyInfoMess);
1047
1048 // Check for another keyword selector.
1049 selIdent = ParseObjCSelector();
1050 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001051 break;
1052 // We have a selector or a colon, continue parsing.
1053 }
1054 // Parse the, optional, argument list, comma separated.
1055 while (Tok.getKind() == tok::comma) {
1056 ConsumeToken();
1057 /// Parse the expression after ','
1058 ParseAssignmentExpression();
1059 }
1060 } else if (!selIdent) {
1061 Diag(Tok, diag::err_expected_ident); // missing selector name.
1062 SkipUntil(tok::semi);
1063 return 0;
1064 }
1065 if (Tok.getKind() != tok::r_square) {
1066 Diag(Tok, diag::err_expected_rsquare);
1067 SkipUntil(tok::semi);
1068 return 0;
1069 }
1070 ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001071
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001072 if (KeyInfo.size()) {
1073 // We've just parsed a keyword message.
1074 if (ReceiverName)
1075 return Actions.ActOnKeywordMessage(ReceiverName,
1076 &KeyInfo[0], KeyInfo.size());
1077 return Actions.ActOnKeywordMessage(ReceiverExpr,
1078 &KeyInfo[0], KeyInfo.size());
1079 }
1080 // We've just parsed a unary message (a message with no arguments).
Steve Naroff253118b2007-09-17 20:25:27 +00001081 if (ReceiverName)
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001082 return Actions.ActOnUnaryMessage(ReceiverName, selIdent);
1083 return Actions.ActOnUnaryMessage(ReceiverExpr, selIdent);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001084}
1085
Anders Carlssona66cad42007-08-21 17:43:55 +00001086Parser::ExprResult Parser::ParseObjCStringLiteral() {
1087 ExprResult Res = ParseStringLiteralExpression();
1088
1089 if (Res.isInvalid) return Res;
1090
1091 return Actions.ParseObjCStringLiteral(Res.Val);
1092}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001093
1094/// objc-encode-expression:
1095/// @encode ( type-name )
1096Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001097 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001098
1099 SourceLocation EncLoc = ConsumeToken();
1100
1101 if (Tok.getKind() != tok::l_paren) {
1102 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1103 return true;
1104 }
1105
1106 SourceLocation LParenLoc = ConsumeParen();
1107
1108 TypeTy *Ty = ParseTypeName();
1109
Anders Carlsson92faeb82007-08-23 15:31:37 +00001110 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001111
1112 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001113 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001114}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001115
1116/// objc-protocol-expression
1117/// @protocol ( protocol-name )
1118
1119Parser::ExprResult Parser::ParseObjCProtocolExpression()
1120{
1121 SourceLocation ProtoLoc = ConsumeToken();
1122
1123 if (Tok.getKind() != tok::l_paren) {
1124 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1125 return true;
1126 }
1127
1128 SourceLocation LParenLoc = ConsumeParen();
1129
1130 if (Tok.getKind() != tok::identifier) {
1131 Diag(Tok, diag::err_expected_ident);
1132 return true;
1133 }
1134
1135 // FIXME: Do something with the protocol name
1136 ConsumeToken();
1137
Anders Carlsson92faeb82007-08-23 15:31:37 +00001138 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001139
1140 // FIXME
1141 return 0;
1142}