blob: 224b8263745649a61abfdf89d439a7ce7f206d56 [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;
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
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000157 DeclTy *CategoryType = Actions.ObjcStartCatInterface(atLoc,
158 nameId, nameLoc,
159 categoryId, categoryLoc,
160 &ProtocolRefs[0],
161 ProtocolRefs.size());
162
163 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000164
Steve Naroff0bbffd82007-08-22 16:35:03 +0000165 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000166 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000167 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000168 return 0;
169 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000170 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000171 return 0;
172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000176
Steve Narofffb367882007-08-20 21:31:48 +0000177 if (Tok.getKind() == tok::colon) { // a super class is specified.
178 ConsumeToken();
179 if (Tok.getKind() != tok::identifier) {
180 Diag(Tok, diag::err_expected_ident); // missing super class name.
181 return 0;
182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000187 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000188 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000189 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000190 return 0;
191 }
Steve Naroff304ed392007-09-05 23:30:30 +0000192 DeclTy *ClsType = Actions.ObjcStartClassInterface(atLoc, nameId, nameLoc,
193 superClassId, superClassLoc, &ProtocolRefs[0],
194 ProtocolRefs.size(), attrList);
195
Steve Narofffb367882007-08-20 21:31:48 +0000196 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000197 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000198
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000199 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000200
201 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000202 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000203 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000204 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000205 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000206 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000207 return 0;
208}
209
210/// objc-interface-decl-list:
211/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000212/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000213/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000214/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000215/// objc-interface-decl-list declaration
216/// objc-interface-decl-list ';'
217///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000218/// objc-method-requirement: [OBJC2]
219/// @required
220/// @optional
221///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000222void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
223 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000224 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000225 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000226 while (1) {
227 if (Tok.getKind() == tok::at) {
228 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000229 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000230
231 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000232 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000233 } else if (ocKind == tok::objc_required) { // protocols only
234 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000235 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000236 if (contextKey != tok::objc_protocol)
237 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000238 } else if (ocKind == tok::objc_optional) { // protocols only
239 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000240 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000241 if (contextKey != tok::objc_protocol)
242 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000243 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000244 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000245 continue;
246 } else {
247 Diag(Tok, diag::err_objc_illegal_interface_qual);
248 ConsumeToken();
249 }
250 }
251 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000252 DeclTy *methodPrototype = ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000253 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000254 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
255 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000256 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000257 continue;
258 }
259 if (Tok.getKind() == tok::semi)
260 ConsumeToken();
261 else if (Tok.getKind() == tok::eof)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000262 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000263 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000264 // FIXME: as the name implies, this rule allows function definitions.
265 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000266 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000267 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000268 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000269 /// Insert collected methods declarations into the @interface object.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000270 Actions.ObjcAddMethodsToClass(interfaceDecl,&allMethods[0],allMethods.size());
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000271 return;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000272}
273
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000274/// Parse property attribute declarations.
275///
276/// property-attr-decl: '(' property-attrlist ')'
277/// property-attrlist:
278/// property-attribute
279/// property-attrlist ',' property-attribute
280/// property-attribute:
281/// getter '=' identifier
282/// setter '=' identifier ':'
283/// readonly
284/// readwrite
285/// assign
286/// retain
287/// copy
288/// nonatomic
289///
290void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
291 SourceLocation loc = ConsumeParen(); // consume '('
292 while (isObjCPropertyAttribute()) {
293 const IdentifierInfo *II = Tok.getIdentifierInfo();
294 // getter/setter require extra treatment.
295 if (II == ObjcPropertyAttrs[objc_getter] ||
296 II == ObjcPropertyAttrs[objc_setter]) {
297 // skip getter/setter part.
298 SourceLocation loc = ConsumeToken();
299 if (Tok.getKind() == tok::equal) {
300 loc = ConsumeToken();
301 if (Tok.getKind() == tok::identifier) {
302 if (II == ObjcPropertyAttrs[objc_setter]) {
303 loc = ConsumeToken(); // consume method name
304 if (Tok.getKind() != tok::colon) {
305 Diag(loc, diag::err_expected_colon);
306 SkipUntil(tok::r_paren,true,true);
307 break;
308 }
309 }
310 }
311 else {
312 Diag(loc, diag::err_expected_ident);
313 SkipUntil(tok::r_paren,true,true);
314 break;
315 }
316 }
317 else {
318 Diag(loc, diag::err_objc_expected_equal);
319 SkipUntil(tok::r_paren,true,true);
320 break;
321 }
322 }
323 ConsumeToken(); // consume last attribute token
324 if (Tok.getKind() == tok::comma) {
325 loc = ConsumeToken();
326 continue;
327 }
328 if (Tok.getKind() == tok::r_paren)
329 break;
330 Diag(loc, diag::err_expected_rparen);
331 SkipUntil(tok::semi);
332 return;
333 }
334 if (Tok.getKind() == tok::r_paren)
335 ConsumeParen();
336 else {
337 Diag(loc, diag::err_objc_expected_property_attr);
338 SkipUntil(tok::r_paren); // recover from error inside attribute list
339 }
340}
341
342/// Main routine to parse property declaration.
343///
344/// @property property-attr-decl[opt] property-component-decl ';'
345///
346void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
347 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
348 "ParseObjCPropertyDecl(): Expected @property");
349 ConsumeToken(); // the "property" identifier
350 // Parse property attribute list, if any.
351 if (Tok.getKind() == tok::l_paren) {
352 // property has attribute list.
353 ParseObjCPropertyAttribute(0/*FIXME*/);
354 }
355 // Parse declaration portion of @property.
356 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
357 ParseStructDeclaration(interfaceDecl, PropertyDecls);
358 if (Tok.getKind() == tok::semi)
359 ConsumeToken();
360 else {
361 Diag(Tok, diag::err_expected_semi_decl_list);
362 SkipUntil(tok::r_brace, true, true);
363 }
Chris Lattner4b009652007-07-25 00:24:17 +0000364}
Steve Narofffb367882007-08-20 21:31:48 +0000365
Steve Naroff81f1bba2007-09-06 21:24:23 +0000366/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000367/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000368/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000369///
370/// objc-instance-method: '-'
371/// objc-class-method: '+'
372///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000373/// objc-method-attributes: [OBJC2]
374/// __attribute__((deprecated))
375///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000376Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
377 tok::ObjCKeywordKind MethodImplKind) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000378 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
379 "expected +/-");
380
381 tok::TokenKind methodType = Tok.getKind();
382 SourceLocation methodLoc = ConsumeToken();
383
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000384 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000385 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000386 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000387 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000388}
389
390/// objc-selector:
391/// identifier
392/// one of
393/// enum struct union if else while do for switch case default
394/// break continue return goto asm sizeof typeof __alignof
395/// unsigned long const short volatile signed restrict _Complex
396/// in out inout bycopy byref oneway int char float double void _Bool
397///
398IdentifierInfo *Parser::ParseObjCSelector() {
399 tok::TokenKind tKind = Tok.getKind();
400 IdentifierInfo *II = 0;
401
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000402 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
403 tKind == tok::kw___alignof ||
Steve Naroff0bbffd82007-08-22 16:35:03 +0000404 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000405 II = Tok.getIdentifierInfo();
406 ConsumeToken();
407 }
408 return II;
409}
410
Steve Naroffa8ee2262007-08-22 23:18:22 +0000411/// objc-type-qualifier: one of
412/// in out inout bycopy byref oneway
413///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000414bool Parser::isObjCTypeQualifier() {
415 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000416 const IdentifierInfo *II = Tok.getIdentifierInfo();
417 for (unsigned i = 0; i < objc_NumQuals; ++i)
418 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000419 }
420 return false;
421}
422
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000423/// property-attrlist: one of
424/// readonly getter setter assign retain copy nonatomic
425///
426bool Parser::isObjCPropertyAttribute() {
427 if (Tok.getKind() == tok::identifier) {
428 const IdentifierInfo *II = Tok.getIdentifierInfo();
429 for (unsigned i = 0; i < objc_NumAttrs; ++i)
430 if (II == ObjcPropertyAttrs[i]) return true;
431 }
432 return false;
433}
434
Steve Naroff0bbffd82007-08-22 16:35:03 +0000435/// objc-type-name:
436/// '(' objc-type-qualifiers[opt] type-name ')'
437/// '(' objc-type-qualifiers[opt] ')'
438///
439/// objc-type-qualifiers:
440/// objc-type-qualifier
441/// objc-type-qualifiers objc-type-qualifier
442///
Steve Naroff304ed392007-09-05 23:30:30 +0000443Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000444 assert(Tok.getKind() == tok::l_paren && "expected (");
445
446 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Naroff304ed392007-09-05 23:30:30 +0000447 TypeTy *Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000448
Steve Naroffa8ee2262007-08-22 23:18:22 +0000449 while (isObjCTypeQualifier())
450 ConsumeToken();
451
Steve Naroff0bbffd82007-08-22 16:35:03 +0000452 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000453 Ty = ParseTypeName();
454 // FIXME: back when Sema support is in place...
455 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000456 }
457 if (Tok.getKind() != tok::r_paren) {
458 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000459 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000460 }
461 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000462 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000463}
464
465/// objc-method-decl:
466/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000467/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000468/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000469/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000470///
471/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000472/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000473/// objc-keyword-selector objc-keyword-decl
474///
475/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000476/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
477/// objc-selector ':' objc-keyword-attributes[opt] identifier
478/// ':' objc-type-name objc-keyword-attributes[opt] identifier
479/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000480///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000481/// objc-parmlist:
482/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000483///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000484/// objc-parms:
485/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000486///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000487/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000488/// , ...
489///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000490/// objc-keyword-attributes: [OBJC2]
491/// __attribute__((unused))
492///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000493Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc,
494 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000495
Steve Naroff304ed392007-09-05 23:30:30 +0000496 TypeTy *ReturnType = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000497 AttributeList *methodAttrs = 0;
Steve Naroff304ed392007-09-05 23:30:30 +0000498
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000499 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000500 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000501 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000502 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000503
Steve Naroff253118b2007-09-17 20:25:27 +0000504 llvm::SmallVector<ObjcKeywordDecl, 12> KeyInfo;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000505
506 if (Tok.getKind() == tok::colon) {
Steve Naroff304ed392007-09-05 23:30:30 +0000507
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000508 while (1) {
Steve Naroff253118b2007-09-17 20:25:27 +0000509 ObjcKeywordDecl KeyInfoDecl;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000510 KeyInfoDecl.SelectorName = selIdent;
Steve Naroff304ed392007-09-05 23:30:30 +0000511
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000512 // Each iteration parses a single keyword argument.
513 if (Tok.getKind() != tok::colon) {
514 Diag(Tok, diag::err_expected_colon);
515 break;
516 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000517 KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000518 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000519 KeyInfoDecl.TypeInfo = ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000520
521 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000522 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000523 KeyInfoDecl.AttrList = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000524
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000525 if (Tok.getKind() != tok::identifier) {
526 Diag(Tok, diag::err_expected_ident); // missing argument name.
527 break;
528 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000529 KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000530 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000531
Steve Naroff253118b2007-09-17 20:25:27 +0000532 // Rather than call out to the actions, package up the info locally,
533 // like we do for Declarator.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000534 KeyInfo.push_back(KeyInfoDecl);
Steve Naroff253118b2007-09-17 20:25:27 +0000535
536 // Check for another keyword selector.
Steve Naroff304ed392007-09-05 23:30:30 +0000537 selIdent = ParseObjCSelector();
538 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000539 break;
540 // We have a selector or a colon, continue parsing.
541 }
542 // Parse the (optional) parameter list.
543 while (Tok.getKind() == tok::comma) {
544 ConsumeToken();
545 if (Tok.getKind() == tok::ellipsis) {
546 ConsumeToken();
547 break;
548 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000549 // Parse the c-style argument declaration-specifier.
550 DeclSpec DS;
551 ParseDeclarationSpecifiers(DS);
552 // Parse the declarator.
553 Declarator ParmDecl(DS, Declarator::PrototypeContext);
554 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000555 }
Steve Naroff304ed392007-09-05 23:30:30 +0000556 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000557 // If attributes exist after the method, parse them.
558 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
559 methodAttrs = ParseAttributes();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000560 return Actions.ObjcBuildMethodDeclaration(mLoc, mType,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000561 ReturnType,
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000562 &KeyInfo[0], KeyInfo.size(),
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000563 methodAttrs, MethodImplKind);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000564 } else if (!selIdent) {
565 Diag(Tok, diag::err_expected_ident); // missing selector name.
566 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000567 // If attributes exist after the method, parse them.
568 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
569 methodAttrs = ParseAttributes();
570
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000571 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
572 selIdent, methodAttrs,
573 MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000574}
575
Steve Narofffb367882007-08-20 21:31:48 +0000576/// objc-protocol-refs:
577/// '<' identifier-list '>'
578///
Steve Naroff304ed392007-09-05 23:30:30 +0000579bool Parser::ParseObjCProtocolReferences(
580 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000581 assert(Tok.getKind() == tok::less && "expected <");
582
583 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000584
585 while (1) {
586 if (Tok.getKind() != tok::identifier) {
587 Diag(Tok, diag::err_expected_ident);
588 SkipUntil(tok::greater);
589 return true;
590 }
591 ProtocolRefs.push_back(Tok.getIdentifierInfo());
592 ConsumeToken();
593
594 if (Tok.getKind() != tok::comma)
595 break;
596 ConsumeToken();
597 }
598 // Consume the '>'.
599 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
600}
601
602/// objc-class-instance-variables:
603/// '{' objc-instance-variable-decl-list[opt] '}'
604///
605/// objc-instance-variable-decl-list:
606/// objc-visibility-spec
607/// objc-instance-variable-decl ';'
608/// ';'
609/// objc-instance-variable-decl-list objc-visibility-spec
610/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
611/// objc-instance-variable-decl-list ';'
612///
613/// objc-visibility-spec:
614/// @private
615/// @protected
616/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000617/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000618///
619/// objc-instance-variable-decl:
620/// struct-declaration
621///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000622void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000623 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000624 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000625 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
626 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000627
628 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000629
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000630 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000631 // While we still have something to read, read the instance variables.
632 while (Tok.getKind() != tok::r_brace &&
633 Tok.getKind() != tok::eof) {
634 // Each iteration of this loop reads one objc-instance-variable-decl.
635
636 // Check for extraneous top-level semicolon.
637 if (Tok.getKind() == tok::semi) {
638 Diag(Tok, diag::ext_extra_struct_semi);
639 ConsumeToken();
640 continue;
641 }
642 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000643 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
644 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000645 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000646 case tok::objc_private:
647 case tok::objc_public:
648 case tok::objc_protected:
649 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000650 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000651 ConsumeToken();
652 continue;
653 default:
654 Diag(Tok, diag::err_objc_illegal_visibility_spec);
655 ConsumeToken();
656 continue;
657 }
658 }
659 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000660 for (unsigned i = 0; i < IvarDecls.size(); i++) {
661 AllIvarDecls.push_back(IvarDecls[i]);
662 AllVisibilities.push_back(visibility);
663 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000664 IvarDecls.clear();
665
Steve Naroffc4474992007-08-21 21:17:12 +0000666 if (Tok.getKind() == tok::semi) {
667 ConsumeToken();
668 } else if (Tok.getKind() == tok::r_brace) {
669 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
670 break;
671 } else {
672 Diag(Tok, diag::err_expected_semi_decl_list);
673 // Skip to end of block or statement
674 SkipUntil(tok::r_brace, true, true);
675 }
676 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000677 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff0acc9c92007-09-15 18:49:24 +0000678 Actions.ActOnFields(LBraceLoc, interfaceDecl,
679 &AllIvarDecls[0], AllIvarDecls.size(),
680 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000681 }
Steve Naroffc4474992007-08-21 21:17:12 +0000682 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
683 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000684}
Steve Narofffb367882007-08-20 21:31:48 +0000685
686/// objc-protocol-declaration:
687/// objc-protocol-definition
688/// objc-protocol-forward-reference
689///
690/// objc-protocol-definition:
691/// @protocol identifier
692/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000693/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000694/// @end
695///
696/// objc-protocol-forward-reference:
697/// @protocol identifier-list ';'
698///
699/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000700/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000701/// semicolon in the first alternative if objc-protocol-refs are omitted.
702
Steve Naroff72f17fb2007-08-22 22:17:26 +0000703Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000704 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000705 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
706 ConsumeToken(); // the "protocol" identifier
707
708 if (Tok.getKind() != tok::identifier) {
709 Diag(Tok, diag::err_expected_ident); // missing protocol name.
710 return 0;
711 }
712 // Save the protocol name, then consume it.
713 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
714 SourceLocation nameLoc = ConsumeToken();
715
716 if (Tok.getKind() == tok::semi) { // forward declaration.
717 ConsumeToken();
718 return 0; // FIXME: add protocolName
719 }
720 if (Tok.getKind() == tok::comma) { // list of forward declarations.
721 // Parse the list of forward declarations.
722 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
723 ProtocolRefs.push_back(protocolName);
724
725 while (1) {
726 ConsumeToken(); // the ','
727 if (Tok.getKind() != tok::identifier) {
728 Diag(Tok, diag::err_expected_ident);
729 SkipUntil(tok::semi);
730 return 0;
731 }
732 ProtocolRefs.push_back(Tok.getIdentifierInfo());
733 ConsumeToken(); // the identifier
734
735 if (Tok.getKind() != tok::comma)
736 break;
737 }
738 // Consume the ';'.
739 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
740 return 0;
741 return 0; // FIXME
742 }
743 // Last, and definitely not least, parse a protocol declaration.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000744 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000745 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000746 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000747 return 0;
748 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000749
750 DeclTy *ProtoType = Actions.ObjcStartProtoInterface(AtLoc,
751 protocolName, nameLoc,
752 &ProtocolRefs[0],
753 ProtocolRefs.size());
754 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000755
756 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000757 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000758 ConsumeToken(); // the "end" identifier
759 return 0;
760 }
761 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000762 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000763}
Steve Narofffb367882007-08-20 21:31:48 +0000764
765/// objc-implementation:
766/// objc-class-implementation-prologue
767/// objc-category-implementation-prologue
768///
769/// objc-class-implementation-prologue:
770/// @implementation identifier objc-superclass[opt]
771/// objc-class-instance-variables[opt]
772///
773/// objc-category-implementation-prologue:
774/// @implementation identifier ( identifier )
775
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000776Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
777 SourceLocation atLoc) {
778 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
779 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
780 ConsumeToken(); // the "implementation" identifier
781
782 if (Tok.getKind() != tok::identifier) {
783 Diag(Tok, diag::err_expected_ident); // missing class or category name.
784 return 0;
785 }
786 // We have a class or category name - consume it.
787 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
788
789 if (Tok.getKind() == tok::l_paren) {
790 // we have a category implementation.
791 SourceLocation lparenLoc = ConsumeParen();
792 SourceLocation categoryLoc, rparenLoc;
793 IdentifierInfo *categoryId = 0;
794
795 if (Tok.getKind() == tok::identifier) {
796 categoryId = Tok.getIdentifierInfo();
797 categoryLoc = ConsumeToken();
798 } else {
799 Diag(Tok, diag::err_expected_ident); // missing category name.
800 return 0;
801 }
802 if (Tok.getKind() != tok::r_paren) {
803 Diag(Tok, diag::err_expected_rparen);
804 SkipUntil(tok::r_paren, false); // don't stop at ';'
805 return 0;
806 }
807 rparenLoc = ConsumeParen();
808 return 0;
809 }
810 // We have a class implementation
811 if (Tok.getKind() == tok::colon) {
812 // We have a super class
813 ConsumeToken();
814 if (Tok.getKind() != tok::identifier) {
815 Diag(Tok, diag::err_expected_ident); // missing super class name.
816 return 0;
817 }
818 ConsumeToken(); // Consume super class name
819 }
820 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000821 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000822
Steve Narofffb367882007-08-20 21:31:48 +0000823 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000824}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000825Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
826 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
827 "ParseObjCAtEndDeclaration(): Expected @end");
828 ConsumeToken(); // the "end" identifier
829
Steve Narofffb367882007-08-20 21:31:48 +0000830 return 0;
831}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000832
833/// compatibility-alias-decl:
834/// @compatibility_alias alias-name class-name ';'
835///
836Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
837 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
838 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
839 ConsumeToken(); // consume compatibility_alias
840 if (Tok.getKind() != tok::identifier) {
841 Diag(Tok, diag::err_expected_ident);
842 return 0;
843 }
844 ConsumeToken(); // consume alias-name
845 if (Tok.getKind() != tok::identifier) {
846 Diag(Tok, diag::err_expected_ident);
847 return 0;
848 }
849 ConsumeToken(); // consume class-name;
850 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000851 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000852 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000853}
854
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000855/// property-synthesis:
856/// @synthesize property-ivar-list ';'
857///
858/// property-ivar-list:
859/// property-ivar
860/// property-ivar-list ',' property-ivar
861///
862/// property-ivar:
863/// identifier
864/// identifier '=' identifier
865///
866Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
867 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
868 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
869 SourceLocation loc = ConsumeToken(); // consume dynamic
870 if (Tok.getKind() != tok::identifier) {
871 Diag(Tok, diag::err_expected_ident);
872 return 0;
873 }
874 while (Tok.getKind() == tok::identifier) {
875 ConsumeToken(); // consume property name
876 if (Tok.getKind() == tok::equal) {
877 // property '=' ivar-name
878 ConsumeToken(); // consume '='
879 if (Tok.getKind() != tok::identifier) {
880 Diag(Tok, diag::err_expected_ident);
881 break;
882 }
883 ConsumeToken(); // consume ivar-name
884 }
885 if (Tok.getKind() != tok::comma)
886 break;
887 ConsumeToken(); // consume ','
888 }
889 if (Tok.getKind() != tok::semi)
890 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
891 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000892}
893
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000894/// property-dynamic:
895/// @dynamic property-list
896///
897/// property-list:
898/// identifier
899/// property-list ',' identifier
900///
901Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
902 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
903 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
904 SourceLocation loc = ConsumeToken(); // consume dynamic
905 if (Tok.getKind() != tok::identifier) {
906 Diag(Tok, diag::err_expected_ident);
907 return 0;
908 }
909 while (Tok.getKind() == tok::identifier) {
910 ConsumeToken(); // consume property name
911 if (Tok.getKind() != tok::comma)
912 break;
913 ConsumeToken(); // consume ','
914 }
915 if (Tok.getKind() != tok::semi)
916 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
917 return 0;
918}
919
Steve Naroff81f1bba2007-09-06 21:24:23 +0000920/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000921///
922void Parser::ParseObjCInstanceMethodDefinition() {
923 assert(Tok.getKind() == tok::minus &&
924 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000925 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000926 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000927 // parse optional ';'
928 if (Tok.getKind() == tok::semi)
929 ConsumeToken();
930
931 if (Tok.getKind() != tok::l_brace) {
932 Diag (Tok, diag::err_expected_lbrace);
933 return;
934 }
935
936 StmtResult FnBody = ParseCompoundStatementBody();
937}
938
Steve Naroff81f1bba2007-09-06 21:24:23 +0000939/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000940///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000941void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000942 assert(Tok.getKind() == tok::plus &&
943 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000944 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000945 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000946 // parse optional ';'
947 if (Tok.getKind() == tok::semi)
948 ConsumeToken();
949 if (Tok.getKind() != tok::l_brace) {
950 Diag (Tok, diag::err_expected_lbrace);
951 return;
952 }
953
954 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +0000955}
Anders Carlssona66cad42007-08-21 17:43:55 +0000956
957Parser::ExprResult Parser::ParseObjCExpression() {
958 SourceLocation AtLoc = ConsumeToken(); // the "@"
959
960 switch (Tok.getKind()) {
961 case tok::string_literal: // primary-expression: string-literal
962 case tok::wide_string_literal:
963 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000964 default:
965 break;
966 }
967
968 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +0000969 case tok::objc_encode:
970 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +0000971 case tok::objc_protocol:
972 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +0000973 default:
974 Diag(AtLoc, diag::err_unexpected_at);
975 SkipUntil(tok::semi);
976 break;
977 }
978
979 return 0;
980}
981
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000982/// objc-message-expr:
983/// '[' objc-receiver objc-message-args ']'
984///
985/// objc-receiver:
986/// expression
987/// class-name
988/// type-name
989///
990/// objc-message-args:
991/// objc-selector
992/// objc-keywordarg-list
993///
994/// objc-keywordarg-list:
995/// objc-keywordarg
996/// objc-keywordarg-list objc-keywordarg
997///
998/// objc-keywordarg:
999/// selector-name[opt] ':' objc-keywordexpr
1000///
1001/// objc-keywordexpr:
1002/// nonempty-expr-list
1003///
1004/// nonempty-expr-list:
1005/// assignment-expression
1006/// nonempty-expr-list , assignment-expression
1007///
1008Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001009 assert(Tok.getKind() == tok::l_square && "'[' expected");
1010 SourceLocation Loc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001011 IdentifierInfo *ReceiverName = 0;
1012 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001013 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001014 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001015 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1016 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001017 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001018 } else {
1019 ExprResult Res = ParseAssignmentExpression();
1020 if (Res.isInvalid) {
1021 SkipUntil(tok::identifier);
1022 return Res;
1023 }
1024 ReceiverExpr = Res.Val;
1025 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001026 // Parse objc-selector
1027 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff253118b2007-09-17 20:25:27 +00001028 llvm::SmallVector<ObjcKeywordMessage, 12> KeyInfo;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001029 if (Tok.getKind() == tok::colon) {
1030 while (1) {
1031 // Each iteration parses a single keyword argument.
Steve Naroff253118b2007-09-17 20:25:27 +00001032 ObjcKeywordMessage KeyInfoMess;
1033 KeyInfoMess.SelectorName = selIdent;
1034
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001035 if (Tok.getKind() != tok::colon) {
1036 Diag(Tok, diag::err_expected_colon);
1037 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001038 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001039 }
Steve Naroff253118b2007-09-17 20:25:27 +00001040 KeyInfoMess.ColonLoc = ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001041 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001042 ExprResult Res = ParseAssignmentExpression();
1043 if (Res.isInvalid) {
1044 SkipUntil(tok::identifier);
1045 return Res;
1046 }
1047 // We have a valid expression.
1048 KeyInfoMess.KeywordExpr = Res.Val;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001049
Steve Naroff253118b2007-09-17 20:25:27 +00001050 // Rather than call out to the actions, package up the info locally,
1051 // like we do for Declarator.
1052 KeyInfo.push_back(KeyInfoMess);
1053
1054 // Check for another keyword selector.
1055 selIdent = ParseObjCSelector();
1056 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001057 break;
1058 // We have a selector or a colon, continue parsing.
1059 }
1060 // Parse the, optional, argument list, comma separated.
1061 while (Tok.getKind() == tok::comma) {
1062 ConsumeToken();
1063 /// Parse the expression after ','
1064 ParseAssignmentExpression();
1065 }
1066 } else if (!selIdent) {
1067 Diag(Tok, diag::err_expected_ident); // missing selector name.
1068 SkipUntil(tok::semi);
1069 return 0;
1070 }
1071 if (Tok.getKind() != tok::r_square) {
1072 Diag(Tok, diag::err_expected_rsquare);
1073 SkipUntil(tok::semi);
1074 return 0;
1075 }
1076 ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001077
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001078 if (KeyInfo.size()) {
1079 // We've just parsed a keyword message.
1080 if (ReceiverName)
1081 return Actions.ActOnKeywordMessage(ReceiverName,
1082 &KeyInfo[0], KeyInfo.size());
1083 return Actions.ActOnKeywordMessage(ReceiverExpr,
1084 &KeyInfo[0], KeyInfo.size());
1085 }
1086 // We've just parsed a unary message (a message with no arguments).
Steve Naroff253118b2007-09-17 20:25:27 +00001087 if (ReceiverName)
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001088 return Actions.ActOnUnaryMessage(ReceiverName, selIdent);
1089 return Actions.ActOnUnaryMessage(ReceiverExpr, selIdent);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001090}
1091
Anders Carlssona66cad42007-08-21 17:43:55 +00001092Parser::ExprResult Parser::ParseObjCStringLiteral() {
1093 ExprResult Res = ParseStringLiteralExpression();
1094
1095 if (Res.isInvalid) return Res;
1096
1097 return Actions.ParseObjCStringLiteral(Res.Val);
1098}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001099
1100/// objc-encode-expression:
1101/// @encode ( type-name )
1102Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001103 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001104
1105 SourceLocation EncLoc = ConsumeToken();
1106
1107 if (Tok.getKind() != tok::l_paren) {
1108 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1109 return true;
1110 }
1111
1112 SourceLocation LParenLoc = ConsumeParen();
1113
1114 TypeTy *Ty = ParseTypeName();
1115
Anders Carlsson92faeb82007-08-23 15:31:37 +00001116 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001117
1118 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001119 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001120}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001121
1122/// objc-protocol-expression
1123/// @protocol ( protocol-name )
1124
1125Parser::ExprResult Parser::ParseObjCProtocolExpression()
1126{
1127 SourceLocation ProtoLoc = ConsumeToken();
1128
1129 if (Tok.getKind() != tok::l_paren) {
1130 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1131 return true;
1132 }
1133
1134 SourceLocation LParenLoc = ConsumeParen();
1135
1136 if (Tok.getKind() != tok::identifier) {
1137 Diag(Tok, diag::err_expected_ident);
1138 return true;
1139 }
1140
1141 // FIXME: Do something with the protocol name
1142 ConsumeToken();
1143
Anders Carlsson92faeb82007-08-23 15:31:37 +00001144 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001145
1146 // FIXME
1147 return 0;
1148}