blob: cd78797e2ecd6fdbf426935a6e0c845555430abb [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallVector.h"
18using namespace clang;
19
20
21/// ParseExternalDeclaration:
22/// external-declaration: [C99 6.9]
23/// [OBJC] objc-class-definition
24/// [OBJC] objc-class-declaration [TODO]
25/// [OBJC] objc-alias-declaration [TODO]
26/// [OBJC] objc-protocol-definition [TODO]
27/// [OBJC] objc-method-definition [TODO]
28/// [OBJC] '@' 'end' [TODO]
Steve Narofffb367882007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff87c329f2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000037 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000039 case tok::objc_implementation:
Steve Naroff81f1bba2007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000041 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000043 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000052 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Narofffb367882007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
65 if (Tok.getKind() != tok::identifier) {
66 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000068 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
Chris Lattner4b009652007-07-25 00:24:17 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
73 if (Tok.getKind() != tok::comma)
74 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000081 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000082
Steve Naroffb4dfe362007-10-02 22:39:18 +000083 return Actions.ActOnForwardClassDeclaration(CurScope, atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000084 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Steve Narofffb367882007-08-20 21:31:48 +000087///
88/// objc-interface:
89/// objc-class-interface-attributes[opt] objc-class-interface
90/// objc-category-interface
91///
92/// objc-class-interface:
93/// '@' 'interface' identifier objc-superclass[opt]
94/// objc-protocol-refs[opt]
95/// objc-class-instance-variables[opt]
96/// objc-interface-decl-list
97/// @end
98///
99/// objc-category-interface:
100/// '@' 'interface' identifier '(' identifier[opt] ')'
101/// objc-protocol-refs[opt]
102/// objc-interface-decl-list
103/// @end
104///
105/// objc-superclass:
106/// ':' identifier
107///
108/// objc-class-interface-attributes:
109/// __attribute__((visibility("default")))
110/// __attribute__((visibility("hidden")))
111/// __attribute__((deprecated))
112/// __attribute__((unavailable))
113/// __attribute__((objc_exception)) - used by NSException on 64-bit
114///
115Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
116 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
121 if (Tok.getKind() != tok::identifier) {
122 Diag(Tok, diag::err_expected_ident); // missing class or category name.
123 return 0;
124 }
125 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Steve Naroffa7f62782007-08-23 19:56:30 +0000129 if (Tok.getKind() == tok::l_paren) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000133 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Steve Narofffb367882007-08-20 21:31:48 +0000136 if (Tok.getKind() == tok::identifier) {
137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
143 if (Tok.getKind() != tok::r_paren) {
144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
149 // Next, we need to check for any protocol references.
150 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000152 return 0;
153 }
154 if (attrList) // categories don't support attributes.
155 Diag(Tok, diag::err_objc_no_attributes_on_category);
156
Steve Naroff25aace82007-10-03 21:00:46 +0000157 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(CurScope, atLoc,
158 nameId, nameLoc, categoryId, categoryLoc,
159 &ProtocolRefs[0], ProtocolRefs.size());
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000160
161 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000162
Steve Naroff0bbffd82007-08-22 16:35:03 +0000163 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000164 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000165 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000166 return 0;
167 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000168 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000169 return 0;
170 }
171 // Parse a class interface.
172 IdentifierInfo *superClassId = 0;
173 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000174
Steve Narofffb367882007-08-20 21:31:48 +0000175 if (Tok.getKind() == tok::colon) { // a super class is specified.
176 ConsumeToken();
177 if (Tok.getKind() != tok::identifier) {
178 Diag(Tok, diag::err_expected_ident); // missing super class name.
179 return 0;
180 }
181 superClassId = Tok.getIdentifierInfo();
182 superClassLoc = ConsumeToken();
183 }
184 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000185 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000186 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000187 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000188 return 0;
189 }
Steve Naroff25aace82007-10-03 21:00:46 +0000190 DeclTy *ClsType = Actions.ActOnStartClassInterface(CurScope,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000191 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000192 superClassId, superClassLoc, &ProtocolRefs[0],
193 ProtocolRefs.size(), attrList);
194
Steve Narofffb367882007-08-20 21:31:48 +0000195 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000196 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000197
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000199
200 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000201 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000202 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000203 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000204 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000206 return 0;
207}
208
209/// objc-interface-decl-list:
210/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000211/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000212/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000213/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000214/// objc-interface-decl-list declaration
215/// objc-interface-decl-list ';'
216///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000217/// objc-method-requirement: [OBJC2]
218/// @required
219/// @optional
220///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000221void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
222 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000223 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000224 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000225 while (1) {
226 if (Tok.getKind() == tok::at) {
227 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000228 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000229
230 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000231 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 } else if (ocKind == tok::objc_required) { // protocols only
233 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000234 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000235 if (contextKey != tok::objc_protocol)
236 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000237 } else if (ocKind == tok::objc_optional) { // protocols only
238 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000239 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000240 if (contextKey != tok::objc_protocol)
241 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000242 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000243 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000244 continue;
245 } else {
246 Diag(Tok, diag::err_objc_illegal_interface_qual);
247 ConsumeToken();
248 }
249 }
250 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000251 DeclTy *methodPrototype = ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000252 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000253 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
254 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000255 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000256 continue;
257 }
258 if (Tok.getKind() == tok::semi)
259 ConsumeToken();
260 else if (Tok.getKind() == tok::eof)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000261 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000262 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000263 // FIXME: as the name implies, this rule allows function definitions.
264 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000265 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000266 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000267 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000268 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000269 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl,
270 &allMethods[0], allMethods.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000271}
272
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000273/// Parse property attribute declarations.
274///
275/// property-attr-decl: '(' property-attrlist ')'
276/// property-attrlist:
277/// property-attribute
278/// property-attrlist ',' property-attribute
279/// property-attribute:
280/// getter '=' identifier
281/// setter '=' identifier ':'
282/// readonly
283/// readwrite
284/// assign
285/// retain
286/// copy
287/// nonatomic
288///
289void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
290 SourceLocation loc = ConsumeParen(); // consume '('
291 while (isObjCPropertyAttribute()) {
292 const IdentifierInfo *II = Tok.getIdentifierInfo();
293 // getter/setter require extra treatment.
294 if (II == ObjcPropertyAttrs[objc_getter] ||
295 II == ObjcPropertyAttrs[objc_setter]) {
296 // skip getter/setter part.
297 SourceLocation loc = ConsumeToken();
298 if (Tok.getKind() == tok::equal) {
299 loc = ConsumeToken();
300 if (Tok.getKind() == tok::identifier) {
301 if (II == ObjcPropertyAttrs[objc_setter]) {
302 loc = ConsumeToken(); // consume method name
303 if (Tok.getKind() != tok::colon) {
304 Diag(loc, diag::err_expected_colon);
305 SkipUntil(tok::r_paren,true,true);
306 break;
307 }
308 }
309 }
310 else {
311 Diag(loc, diag::err_expected_ident);
312 SkipUntil(tok::r_paren,true,true);
313 break;
314 }
315 }
316 else {
317 Diag(loc, diag::err_objc_expected_equal);
318 SkipUntil(tok::r_paren,true,true);
319 break;
320 }
321 }
322 ConsumeToken(); // consume last attribute token
323 if (Tok.getKind() == tok::comma) {
324 loc = ConsumeToken();
325 continue;
326 }
327 if (Tok.getKind() == tok::r_paren)
328 break;
329 Diag(loc, diag::err_expected_rparen);
330 SkipUntil(tok::semi);
331 return;
332 }
333 if (Tok.getKind() == tok::r_paren)
334 ConsumeParen();
335 else {
336 Diag(loc, diag::err_objc_expected_property_attr);
337 SkipUntil(tok::r_paren); // recover from error inside attribute list
338 }
339}
340
341/// Main routine to parse property declaration.
342///
343/// @property property-attr-decl[opt] property-component-decl ';'
344///
345void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
346 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
347 "ParseObjCPropertyDecl(): Expected @property");
348 ConsumeToken(); // the "property" identifier
349 // Parse property attribute list, if any.
350 if (Tok.getKind() == tok::l_paren) {
351 // property has attribute list.
352 ParseObjCPropertyAttribute(0/*FIXME*/);
353 }
354 // Parse declaration portion of @property.
355 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
356 ParseStructDeclaration(interfaceDecl, PropertyDecls);
357 if (Tok.getKind() == tok::semi)
358 ConsumeToken();
359 else {
360 Diag(Tok, diag::err_expected_semi_decl_list);
361 SkipUntil(tok::r_brace, true, true);
362 }
Chris Lattner4b009652007-07-25 00:24:17 +0000363}
Steve Narofffb367882007-08-20 21:31:48 +0000364
Steve Naroff81f1bba2007-09-06 21:24:23 +0000365/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000366/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000367/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000368///
369/// objc-instance-method: '-'
370/// objc-class-method: '+'
371///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000372/// objc-method-attributes: [OBJC2]
373/// __attribute__((deprecated))
374///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000375Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
376 tok::ObjCKeywordKind MethodImplKind) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000377 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
378 "expected +/-");
379
380 tok::TokenKind methodType = Tok.getKind();
381 SourceLocation methodLoc = ConsumeToken();
382
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000383 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000384 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000385 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000386 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000387}
388
389/// objc-selector:
390/// identifier
391/// one of
392/// enum struct union if else while do for switch case default
393/// break continue return goto asm sizeof typeof __alignof
394/// unsigned long const short volatile signed restrict _Complex
395/// in out inout bycopy byref oneway int char float double void _Bool
396///
397IdentifierInfo *Parser::ParseObjCSelector() {
Chris Lattnerd031a452007-10-07 02:00:24 +0000398 switch (Tok.getKind()) {
399 default:
400 return 0;
401 case tok::identifier:
402 case tok::kw_typeof:
403 case tok::kw___alignof:
404 case tok::kw_auto:
405 case tok::kw_break:
406 case tok::kw_case:
407 case tok::kw_char:
408 case tok::kw_const:
409 case tok::kw_continue:
410 case tok::kw_default:
411 case tok::kw_do:
412 case tok::kw_double:
413 case tok::kw_else:
414 case tok::kw_enum:
415 case tok::kw_extern:
416 case tok::kw_float:
417 case tok::kw_for:
418 case tok::kw_goto:
419 case tok::kw_if:
420 case tok::kw_inline:
421 case tok::kw_int:
422 case tok::kw_long:
423 case tok::kw_register:
424 case tok::kw_restrict:
425 case tok::kw_return:
426 case tok::kw_short:
427 case tok::kw_signed:
428 case tok::kw_sizeof:
429 case tok::kw_static:
430 case tok::kw_struct:
431 case tok::kw_switch:
432 case tok::kw_typedef:
433 case tok::kw_union:
434 case tok::kw_unsigned:
435 case tok::kw_void:
436 case tok::kw_volatile:
437 case tok::kw_while:
438 case tok::kw__Bool:
439 case tok::kw__Complex:
440 IdentifierInfo *II = Tok.getIdentifierInfo();
441 ConsumeToken();
442 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000443 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000444}
445
Steve Naroffa8ee2262007-08-22 23:18:22 +0000446/// objc-type-qualifier: one of
447/// in out inout bycopy byref oneway
448///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000449bool Parser::isObjCTypeQualifier() {
450 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000451 const IdentifierInfo *II = Tok.getIdentifierInfo();
452 for (unsigned i = 0; i < objc_NumQuals; ++i)
453 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000454 }
455 return false;
456}
457
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000458/// property-attrlist: one of
459/// readonly getter setter assign retain copy nonatomic
460///
461bool Parser::isObjCPropertyAttribute() {
462 if (Tok.getKind() == tok::identifier) {
463 const IdentifierInfo *II = Tok.getIdentifierInfo();
464 for (unsigned i = 0; i < objc_NumAttrs; ++i)
465 if (II == ObjcPropertyAttrs[i]) return true;
466 }
467 return false;
468}
469
Steve Naroff0bbffd82007-08-22 16:35:03 +0000470/// objc-type-name:
471/// '(' objc-type-qualifiers[opt] type-name ')'
472/// '(' objc-type-qualifiers[opt] ')'
473///
474/// objc-type-qualifiers:
475/// objc-type-qualifier
476/// objc-type-qualifiers objc-type-qualifier
477///
Steve Naroff304ed392007-09-05 23:30:30 +0000478Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000479 assert(Tok.getKind() == tok::l_paren && "expected (");
480
481 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000482 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000483
Steve Naroffa8ee2262007-08-22 23:18:22 +0000484 while (isObjCTypeQualifier())
485 ConsumeToken();
486
Steve Naroff0bbffd82007-08-22 16:35:03 +0000487 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000488 Ty = ParseTypeName();
489 // FIXME: back when Sema support is in place...
490 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000491 }
492 if (Tok.getKind() != tok::r_paren) {
493 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000494 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000495 }
496 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000497 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000498}
499
500/// objc-method-decl:
501/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000502/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000503/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000504/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000505///
506/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000507/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000508/// objc-keyword-selector objc-keyword-decl
509///
510/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000511/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
512/// objc-selector ':' objc-keyword-attributes[opt] identifier
513/// ':' objc-type-name objc-keyword-attributes[opt] identifier
514/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000515///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000516/// objc-parmlist:
517/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000518///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000519/// objc-parms:
520/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000521///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000522/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000523/// , ...
524///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000525/// objc-keyword-attributes: [OBJC2]
526/// __attribute__((unused))
527///
Steve Naroff4ed9d662007-09-27 14:38:14 +0000528Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType,
529 SourceLocation mLoc,
530 tok::ObjCKeywordKind MethodImplKind)
531{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000532 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000533 TypeTy *ReturnType = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000534 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000535 ReturnType = ParseObjCTypeName();
Chris Lattnerd031a452007-10-07 02:00:24 +0000536
537 IdentifierInfo *SelIdent = ParseObjCSelector();
538 if (Tok.getKind() != tok::colon) {
539 if (!SelIdent) {
540 Diag(Tok, diag::err_expected_ident); // missing selector name.
541 // FIXME: this creates a unary selector with a null identifier, is this
542 // ok?? Maybe we should skip to the next semicolon or something.
543 }
544
545 // If attributes exist after the method, parse them.
546 AttributeList *MethodAttrs = 0;
547 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
548 MethodAttrs = ParseAttributes();
549
550 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
551 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
552 0, 0, MethodAttrs, MethodImplKind);
553 }
Steve Naroff304ed392007-09-05 23:30:30 +0000554
Steve Naroff4ed9d662007-09-27 14:38:14 +0000555 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
556 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
557 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000558
559 Action::TypeTy *TypeInfo;
560 while (1) {
561 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000562
Chris Lattnerd031a452007-10-07 02:00:24 +0000563 // Each iteration parses a single keyword argument.
564 if (Tok.getKind() != tok::colon) {
565 Diag(Tok, diag::err_expected_colon);
566 break;
567 }
568 ConsumeToken(); // Eat the ':'.
569 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
570 TypeInfo = ParseObjCTypeName();
571 else
572 TypeInfo = 0;
573 KeyTypes.push_back(TypeInfo);
Steve Naroff304ed392007-09-05 23:30:30 +0000574
Chris Lattnerd031a452007-10-07 02:00:24 +0000575 // If attributes exist before the argument name, parse them.
576 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
577 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000578
Chris Lattnerd031a452007-10-07 02:00:24 +0000579 if (Tok.getKind() != tok::identifier) {
580 Diag(Tok, diag::err_expected_ident); // missing argument name.
581 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000582 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000583 ArgNames.push_back(Tok.getIdentifierInfo());
584 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000585
Chris Lattnerd031a452007-10-07 02:00:24 +0000586 // Check for another keyword selector.
587 SelIdent = ParseObjCSelector();
588 if (!SelIdent && Tok.getKind() != tok::colon)
589 break;
590 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000591 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000592
593 // Parse the (optional) parameter list.
594 while (Tok.getKind() == tok::comma) {
595 ConsumeToken();
596 if (Tok.getKind() == tok::ellipsis) {
597 ConsumeToken();
598 break;
599 }
600 // Parse the c-style argument declaration-specifier.
601 DeclSpec DS;
602 ParseDeclarationSpecifiers(DS);
603 // Parse the declarator.
604 Declarator ParmDecl(DS, Declarator::PrototypeContext);
605 ParseDeclarator(ParmDecl);
606 }
607
608 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000609 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000610 AttributeList *MethodAttrs = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000611 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Chris Lattnerd031a452007-10-07 02:00:24 +0000612 MethodAttrs = ParseAttributes();
613
614 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
615 &KeyIdents[0]);
Steve Naroffb4dfe362007-10-02 22:39:18 +0000616 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000617 &KeyTypes[0], &ArgNames[0],
618 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000619}
620
Steve Narofffb367882007-08-20 21:31:48 +0000621/// objc-protocol-refs:
622/// '<' identifier-list '>'
623///
Steve Naroff304ed392007-09-05 23:30:30 +0000624bool Parser::ParseObjCProtocolReferences(
625 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000626 assert(Tok.getKind() == tok::less && "expected <");
627
628 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000629
630 while (1) {
631 if (Tok.getKind() != tok::identifier) {
632 Diag(Tok, diag::err_expected_ident);
633 SkipUntil(tok::greater);
634 return true;
635 }
636 ProtocolRefs.push_back(Tok.getIdentifierInfo());
637 ConsumeToken();
638
639 if (Tok.getKind() != tok::comma)
640 break;
641 ConsumeToken();
642 }
643 // Consume the '>'.
644 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
645}
646
647/// objc-class-instance-variables:
648/// '{' objc-instance-variable-decl-list[opt] '}'
649///
650/// objc-instance-variable-decl-list:
651/// objc-visibility-spec
652/// objc-instance-variable-decl ';'
653/// ';'
654/// objc-instance-variable-decl-list objc-visibility-spec
655/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
656/// objc-instance-variable-decl-list ';'
657///
658/// objc-visibility-spec:
659/// @private
660/// @protected
661/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000662/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000663///
664/// objc-instance-variable-decl:
665/// struct-declaration
666///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000667void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000668 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000669 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000670 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
671 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000672
673 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000674
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000675 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000676 // While we still have something to read, read the instance variables.
677 while (Tok.getKind() != tok::r_brace &&
678 Tok.getKind() != tok::eof) {
679 // Each iteration of this loop reads one objc-instance-variable-decl.
680
681 // Check for extraneous top-level semicolon.
682 if (Tok.getKind() == tok::semi) {
683 Diag(Tok, diag::ext_extra_struct_semi);
684 ConsumeToken();
685 continue;
686 }
687 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000688 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
689 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000690 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000691 case tok::objc_private:
692 case tok::objc_public:
693 case tok::objc_protected:
694 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000695 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000696 ConsumeToken();
697 continue;
698 default:
699 Diag(Tok, diag::err_objc_illegal_visibility_spec);
700 ConsumeToken();
701 continue;
702 }
703 }
704 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000705 for (unsigned i = 0; i < IvarDecls.size(); i++) {
706 AllIvarDecls.push_back(IvarDecls[i]);
707 AllVisibilities.push_back(visibility);
708 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000709 IvarDecls.clear();
710
Steve Naroffc4474992007-08-21 21:17:12 +0000711 if (Tok.getKind() == tok::semi) {
712 ConsumeToken();
713 } else if (Tok.getKind() == tok::r_brace) {
714 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
715 break;
716 } else {
717 Diag(Tok, diag::err_expected_semi_decl_list);
718 // Skip to end of block or statement
719 SkipUntil(tok::r_brace, true, true);
720 }
721 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000722 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000723 Actions.ActOnFields(CurScope, LBraceLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000724 &AllIvarDecls[0], AllIvarDecls.size(),
725 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000726 }
Steve Naroffc4474992007-08-21 21:17:12 +0000727 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
728 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000729}
Steve Narofffb367882007-08-20 21:31:48 +0000730
731/// objc-protocol-declaration:
732/// objc-protocol-definition
733/// objc-protocol-forward-reference
734///
735/// objc-protocol-definition:
736/// @protocol identifier
737/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000738/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000739/// @end
740///
741/// objc-protocol-forward-reference:
742/// @protocol identifier-list ';'
743///
744/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000745/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000746/// semicolon in the first alternative if objc-protocol-refs are omitted.
747
Steve Naroff72f17fb2007-08-22 22:17:26 +0000748Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000749 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000750 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
751 ConsumeToken(); // the "protocol" identifier
752
753 if (Tok.getKind() != tok::identifier) {
754 Diag(Tok, diag::err_expected_ident); // missing protocol name.
755 return 0;
756 }
757 // Save the protocol name, then consume it.
758 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
759 SourceLocation nameLoc = ConsumeToken();
760
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000761 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
762 if (Tok.getKind() == tok::semi) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000763 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000764 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000765 }
766 if (Tok.getKind() == tok::comma) { // list of forward declarations.
767 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000768 ProtocolRefs.push_back(protocolName);
769
770 while (1) {
771 ConsumeToken(); // the ','
772 if (Tok.getKind() != tok::identifier) {
773 Diag(Tok, diag::err_expected_ident);
774 SkipUntil(tok::semi);
775 return 0;
776 }
777 ProtocolRefs.push_back(Tok.getIdentifierInfo());
778 ConsumeToken(); // the identifier
779
780 if (Tok.getKind() != tok::comma)
781 break;
782 }
783 // Consume the ';'.
784 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
785 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000786 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000787 if (ProtocolRefs.size() > 0)
Steve Naroffb4dfe362007-10-02 22:39:18 +0000788 return Actions.ActOnForwardProtocolDeclaration(CurScope, AtLoc,
789 &ProtocolRefs[0],
790 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000791 // Last, and definitely not least, parse a protocol declaration.
792 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000793 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000794 return 0;
795 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000796
Steve Naroff25aace82007-10-03 21:00:46 +0000797 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(CurScope, AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000798 protocolName, nameLoc,
799 &ProtocolRefs[0],
800 ProtocolRefs.size());
801 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000802
803 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000804 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000805 ConsumeToken(); // the "end" identifier
806 return 0;
807 }
808 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000809 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000810}
Steve Narofffb367882007-08-20 21:31:48 +0000811
812/// objc-implementation:
813/// objc-class-implementation-prologue
814/// objc-category-implementation-prologue
815///
816/// objc-class-implementation-prologue:
817/// @implementation identifier objc-superclass[opt]
818/// objc-class-instance-variables[opt]
819///
820/// objc-category-implementation-prologue:
821/// @implementation identifier ( identifier )
822
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000823Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
824 SourceLocation atLoc) {
825 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
826 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
827 ConsumeToken(); // the "implementation" identifier
828
829 if (Tok.getKind() != tok::identifier) {
830 Diag(Tok, diag::err_expected_ident); // missing class or category name.
831 return 0;
832 }
833 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000834 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000835 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
836
837 if (Tok.getKind() == tok::l_paren) {
838 // we have a category implementation.
839 SourceLocation lparenLoc = ConsumeParen();
840 SourceLocation categoryLoc, rparenLoc;
841 IdentifierInfo *categoryId = 0;
842
843 if (Tok.getKind() == tok::identifier) {
844 categoryId = Tok.getIdentifierInfo();
845 categoryLoc = ConsumeToken();
846 } else {
847 Diag(Tok, diag::err_expected_ident); // missing category name.
848 return 0;
849 }
850 if (Tok.getKind() != tok::r_paren) {
851 Diag(Tok, diag::err_expected_rparen);
852 SkipUntil(tok::r_paren, false); // don't stop at ';'
853 return 0;
854 }
855 rparenLoc = ConsumeParen();
Steve Naroff25aace82007-10-03 21:00:46 +0000856 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(CurScope,
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000857 atLoc, nameId, nameLoc, categoryId,
858 categoryLoc);
859 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000860 }
861 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000862 SourceLocation superClassLoc;
863 IdentifierInfo *superClassId = 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000864 if (Tok.getKind() == tok::colon) {
865 // We have a super class
866 ConsumeToken();
867 if (Tok.getKind() != tok::identifier) {
868 Diag(Tok, diag::err_expected_ident); // missing super class name.
869 return 0;
870 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000871 superClassId = Tok.getIdentifierInfo();
872 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000873 }
Steve Naroff25aace82007-10-03 21:00:46 +0000874 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(CurScope,
875 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000876 superClassId, superClassLoc);
877
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000878 if (Tok.getKind() == tok::l_brace)
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000879 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000880
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000881 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000882}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000883Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
884 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
885 "ParseObjCAtEndDeclaration(): Expected @end");
886 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000887 if (ObjcImpDecl) {
888 // Checking is not necessary except that a parse error might have caused
889 // @implementation not to have been parsed to completion and ObjcImpDecl
890 // could be 0.
891 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000892 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
893 &AllImplMethods[0],AllImplMethods.size());
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000894 ObjcImpDecl = 0;
895 AllImplMethods.clear();
896 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000897
Steve Narofffb367882007-08-20 21:31:48 +0000898 return 0;
899}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000900
901/// compatibility-alias-decl:
902/// @compatibility_alias alias-name class-name ';'
903///
904Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
905 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
906 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
907 ConsumeToken(); // consume compatibility_alias
908 if (Tok.getKind() != tok::identifier) {
909 Diag(Tok, diag::err_expected_ident);
910 return 0;
911 }
912 ConsumeToken(); // consume alias-name
913 if (Tok.getKind() != tok::identifier) {
914 Diag(Tok, diag::err_expected_ident);
915 return 0;
916 }
917 ConsumeToken(); // consume class-name;
918 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000919 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000920 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000921}
922
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000923/// property-synthesis:
924/// @synthesize property-ivar-list ';'
925///
926/// property-ivar-list:
927/// property-ivar
928/// property-ivar-list ',' property-ivar
929///
930/// property-ivar:
931/// identifier
932/// identifier '=' identifier
933///
934Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
935 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
936 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
937 SourceLocation loc = ConsumeToken(); // consume dynamic
938 if (Tok.getKind() != tok::identifier) {
939 Diag(Tok, diag::err_expected_ident);
940 return 0;
941 }
942 while (Tok.getKind() == tok::identifier) {
943 ConsumeToken(); // consume property name
944 if (Tok.getKind() == tok::equal) {
945 // property '=' ivar-name
946 ConsumeToken(); // consume '='
947 if (Tok.getKind() != tok::identifier) {
948 Diag(Tok, diag::err_expected_ident);
949 break;
950 }
951 ConsumeToken(); // consume ivar-name
952 }
953 if (Tok.getKind() != tok::comma)
954 break;
955 ConsumeToken(); // consume ','
956 }
957 if (Tok.getKind() != tok::semi)
958 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
959 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000960}
961
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000962/// property-dynamic:
963/// @dynamic property-list
964///
965/// property-list:
966/// identifier
967/// property-list ',' identifier
968///
969Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
970 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
971 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
972 SourceLocation loc = ConsumeToken(); // consume dynamic
973 if (Tok.getKind() != tok::identifier) {
974 Diag(Tok, diag::err_expected_ident);
975 return 0;
976 }
977 while (Tok.getKind() == tok::identifier) {
978 ConsumeToken(); // consume property name
979 if (Tok.getKind() != tok::comma)
980 break;
981 ConsumeToken(); // consume ','
982 }
983 if (Tok.getKind() != tok::semi)
984 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
985 return 0;
986}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000987
988/// objc-throw-statement:
989/// throw expression[opt];
990///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +0000991Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000992 ConsumeToken(); // consume throw
993 if (Tok.getKind() != tok::semi) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +0000994 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000995 if (Res.isInvalid) {
996 SkipUntil(tok::semi);
997 return 0;
998 }
999 }
1000 return 0;
1001}
1002
1003/// objc-try-catch-statement:
1004/// @try compound-statement objc-catch-list[opt]
1005/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1006///
1007/// objc-catch-list:
1008/// @catch ( parameter-declaration ) compound-statement
1009/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1010/// catch-parameter-declaration:
1011/// parameter-declaration
1012/// '...' [OBJC2]
1013///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001014Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001015 bool catch_or_finally_seen = false;
1016 ConsumeToken(); // consume try
1017 if (Tok.getKind() != tok::l_brace) {
1018 Diag (Tok, diag::err_expected_lbrace);
1019 return 0;
1020 }
1021 StmtResult TryBody = ParseCompoundStatementBody();
1022 while (Tok.getKind() == tok::at) {
1023 ConsumeToken();
1024 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1025 SourceLocation catchLoc = ConsumeToken(); // consume catch
1026 if (Tok.getKind() == tok::l_paren) {
1027 ConsumeParen();
1028 if (Tok.getKind() != tok::ellipsis) {
1029 DeclSpec DS;
1030 ParseDeclarationSpecifiers(DS);
1031 // Parse the parameter-declaration.
1032 // FIXME: BlockContext may not be the right context!
1033 Declarator ParmDecl(DS, Declarator::BlockContext);
1034 ParseDeclarator(ParmDecl);
1035 }
1036 else
1037 ConsumeToken(); // consume '...'
1038 ConsumeParen();
1039 StmtResult CatchMody = ParseCompoundStatementBody();
1040 }
1041 else {
1042 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1043 return 0;
1044 }
1045 catch_or_finally_seen = true;
1046 }
1047 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1048 ConsumeToken(); // consume finally
1049 StmtResult FinallyBody = ParseCompoundStatementBody();
1050 catch_or_finally_seen = true;
1051 break;
1052 }
1053 }
1054 if (!catch_or_finally_seen)
1055 Diag(atLoc, diag::err_missing_catch_finally);
1056 return 0;
1057}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001058
Steve Naroff81f1bba2007-09-06 21:24:23 +00001059/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001060///
1061void Parser::ParseObjCInstanceMethodDefinition() {
1062 assert(Tok.getKind() == tok::minus &&
1063 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001064 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001065 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001066 // parse optional ';'
1067 if (Tok.getKind() == tok::semi)
1068 ConsumeToken();
1069
1070 if (Tok.getKind() != tok::l_brace) {
1071 Diag (Tok, diag::err_expected_lbrace);
1072 return;
1073 }
1074
1075 StmtResult FnBody = ParseCompoundStatementBody();
1076}
1077
Steve Naroff81f1bba2007-09-06 21:24:23 +00001078/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001079///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001080void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001081 assert(Tok.getKind() == tok::plus &&
1082 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001083 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001084 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001085 // parse optional ';'
1086 if (Tok.getKind() == tok::semi)
1087 ConsumeToken();
1088 if (Tok.getKind() != tok::l_brace) {
1089 Diag (Tok, diag::err_expected_lbrace);
1090 return;
1091 }
1092
1093 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001094}
Anders Carlssona66cad42007-08-21 17:43:55 +00001095
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001096Parser::ExprResult Parser::ParseObjCExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001097
1098 switch (Tok.getKind()) {
1099 case tok::string_literal: // primary-expression: string-literal
1100 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001101 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001102 default:
1103 break;
1104 }
1105
1106 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001107 case tok::objc_encode:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001108 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001109 case tok::objc_protocol:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001110 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression());
Anders Carlssona66cad42007-08-21 17:43:55 +00001111 default:
1112 Diag(AtLoc, diag::err_unexpected_at);
1113 SkipUntil(tok::semi);
1114 break;
1115 }
1116
1117 return 0;
1118}
1119
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001120/// objc-message-expr:
1121/// '[' objc-receiver objc-message-args ']'
1122///
1123/// objc-receiver:
1124/// expression
1125/// class-name
1126/// type-name
1127///
1128/// objc-message-args:
1129/// objc-selector
1130/// objc-keywordarg-list
1131///
1132/// objc-keywordarg-list:
1133/// objc-keywordarg
1134/// objc-keywordarg-list objc-keywordarg
1135///
1136/// objc-keywordarg:
1137/// selector-name[opt] ':' objc-keywordexpr
1138///
1139/// objc-keywordexpr:
1140/// nonempty-expr-list
1141///
1142/// nonempty-expr-list:
1143/// assignment-expression
1144/// nonempty-expr-list , assignment-expression
1145///
1146Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001147 assert(Tok.getKind() == tok::l_square && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001148 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001149 IdentifierInfo *ReceiverName = 0;
1150 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001151 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001152 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001153 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1154 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001155 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001156 } else {
1157 ExprResult Res = ParseAssignmentExpression();
1158 if (Res.isInvalid) {
1159 SkipUntil(tok::identifier);
1160 return Res;
1161 }
1162 ReceiverExpr = Res.Val;
1163 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001164 // Parse objc-selector
1165 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff4ed9d662007-09-27 14:38:14 +00001166
1167 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1168 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1169
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001170 if (Tok.getKind() == tok::colon) {
1171 while (1) {
1172 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001173 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001174
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001175 if (Tok.getKind() != tok::colon) {
1176 Diag(Tok, diag::err_expected_colon);
1177 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001178 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001179 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001180 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001181 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001182 ExprResult Res = ParseAssignmentExpression();
1183 if (Res.isInvalid) {
1184 SkipUntil(tok::identifier);
1185 return Res;
1186 }
1187 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001188 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001189
1190 // Check for another keyword selector.
1191 selIdent = ParseObjCSelector();
1192 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001193 break;
1194 // We have a selector or a colon, continue parsing.
1195 }
1196 // Parse the, optional, argument list, comma separated.
1197 while (Tok.getKind() == tok::comma) {
1198 ConsumeToken();
1199 /// Parse the expression after ','
1200 ParseAssignmentExpression();
1201 }
1202 } else if (!selIdent) {
1203 Diag(Tok, diag::err_expected_ident); // missing selector name.
1204 SkipUntil(tok::semi);
1205 return 0;
1206 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001207
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001208 if (Tok.getKind() != tok::r_square) {
1209 Diag(Tok, diag::err_expected_rsquare);
1210 SkipUntil(tok::semi);
1211 return 0;
1212 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001213 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001214
Steve Narofff9e80db2007-10-05 18:42:47 +00001215 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001216 if (nKeys == 0)
1217 KeyIdents.push_back(selIdent);
1218 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1219
1220 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001221 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001222 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1223 &KeyExprs[0]);
1224 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1225 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001226}
1227
Anders Carlssona66cad42007-08-21 17:43:55 +00001228Parser::ExprResult Parser::ParseObjCStringLiteral() {
1229 ExprResult Res = ParseStringLiteralExpression();
1230
1231 if (Res.isInvalid) return Res;
1232
1233 return Actions.ParseObjCStringLiteral(Res.Val);
1234}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001235
1236/// objc-encode-expression:
1237/// @encode ( type-name )
1238Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001239 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001240
1241 SourceLocation EncLoc = ConsumeToken();
1242
1243 if (Tok.getKind() != tok::l_paren) {
1244 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1245 return true;
1246 }
1247
1248 SourceLocation LParenLoc = ConsumeParen();
1249
1250 TypeTy *Ty = ParseTypeName();
1251
Anders Carlsson92faeb82007-08-23 15:31:37 +00001252 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001253
1254 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001255 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001256}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001257
1258/// objc-protocol-expression
1259/// @protocol ( protocol-name )
1260
1261Parser::ExprResult Parser::ParseObjCProtocolExpression()
1262{
1263 SourceLocation ProtoLoc = ConsumeToken();
1264
1265 if (Tok.getKind() != tok::l_paren) {
1266 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1267 return true;
1268 }
1269
1270 SourceLocation LParenLoc = ConsumeParen();
1271
1272 if (Tok.getKind() != tok::identifier) {
1273 Diag(Tok, diag::err_expected_ident);
1274 return true;
1275 }
1276
1277 // FIXME: Do something with the protocol name
1278 ConsumeToken();
1279
Anders Carlsson92faeb82007-08-23 15:31:37 +00001280 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001281
1282 // FIXME
1283 return 0;
1284}