blob: 31caa58ef3d3d8b9eda6275a84023a94c131b984 [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
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000166 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000167 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000168 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000169 return 0;
170 }
171 // Parse a class interface.
172 IdentifierInfo *superClassId = 0;
173 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000174
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
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000621/// CmpProtocolVals - Comparison predicate for sorting protocols.
622static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
623 const IdentifierInfo* const& rhs) {
624 return strcmp(lhs->getName(), rhs->getName()) < 0;
625}
626
Steve Narofffb367882007-08-20 21:31:48 +0000627/// objc-protocol-refs:
628/// '<' identifier-list '>'
629///
Steve Naroff304ed392007-09-05 23:30:30 +0000630bool Parser::ParseObjCProtocolReferences(
631 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000632 assert(Tok.getKind() == tok::less && "expected <");
633
634 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000635
636 while (1) {
637 if (Tok.getKind() != tok::identifier) {
638 Diag(Tok, diag::err_expected_ident);
639 SkipUntil(tok::greater);
640 return true;
641 }
642 ProtocolRefs.push_back(Tok.getIdentifierInfo());
643 ConsumeToken();
644
645 if (Tok.getKind() != tok::comma)
646 break;
647 ConsumeToken();
648 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000649
650 // Sort protocols, keyed by name.
651 // Later on, we remove duplicates.
652 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
653
654 // Make protocol names unique.
655 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
656 ProtocolRefs.end());
657
Steve Narofffb367882007-08-20 21:31:48 +0000658 // Consume the '>'.
659 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
660}
661
662/// objc-class-instance-variables:
663/// '{' objc-instance-variable-decl-list[opt] '}'
664///
665/// objc-instance-variable-decl-list:
666/// objc-visibility-spec
667/// objc-instance-variable-decl ';'
668/// ';'
669/// objc-instance-variable-decl-list objc-visibility-spec
670/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
671/// objc-instance-variable-decl-list ';'
672///
673/// objc-visibility-spec:
674/// @private
675/// @protected
676/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000677/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000678///
679/// objc-instance-variable-decl:
680/// struct-declaration
681///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000682void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000683 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000684 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000685 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
686 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000687
688 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000689
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000690 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000691 // While we still have something to read, read the instance variables.
692 while (Tok.getKind() != tok::r_brace &&
693 Tok.getKind() != tok::eof) {
694 // Each iteration of this loop reads one objc-instance-variable-decl.
695
696 // Check for extraneous top-level semicolon.
697 if (Tok.getKind() == tok::semi) {
698 Diag(Tok, diag::ext_extra_struct_semi);
699 ConsumeToken();
700 continue;
701 }
702 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000703 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
704 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000705 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000706 case tok::objc_private:
707 case tok::objc_public:
708 case tok::objc_protected:
709 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000710 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000711 ConsumeToken();
712 continue;
713 default:
714 Diag(Tok, diag::err_objc_illegal_visibility_spec);
715 ConsumeToken();
716 continue;
717 }
718 }
719 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000720 for (unsigned i = 0; i < IvarDecls.size(); i++) {
721 AllIvarDecls.push_back(IvarDecls[i]);
722 AllVisibilities.push_back(visibility);
723 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000724 IvarDecls.clear();
725
Steve Naroffc4474992007-08-21 21:17:12 +0000726 if (Tok.getKind() == tok::semi) {
727 ConsumeToken();
728 } else if (Tok.getKind() == tok::r_brace) {
729 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
730 break;
731 } else {
732 Diag(Tok, diag::err_expected_semi_decl_list);
733 // Skip to end of block or statement
734 SkipUntil(tok::r_brace, true, true);
735 }
736 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000737 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000738 Actions.ActOnFields(CurScope, LBraceLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000739 &AllIvarDecls[0], AllIvarDecls.size(),
740 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000741 }
Steve Naroffc4474992007-08-21 21:17:12 +0000742 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
743 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000744}
Steve Narofffb367882007-08-20 21:31:48 +0000745
746/// objc-protocol-declaration:
747/// objc-protocol-definition
748/// objc-protocol-forward-reference
749///
750/// objc-protocol-definition:
751/// @protocol identifier
752/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000753/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000754/// @end
755///
756/// objc-protocol-forward-reference:
757/// @protocol identifier-list ';'
758///
759/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000760/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000761/// semicolon in the first alternative if objc-protocol-refs are omitted.
762
Steve Naroff72f17fb2007-08-22 22:17:26 +0000763Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000764 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000765 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
766 ConsumeToken(); // the "protocol" identifier
767
768 if (Tok.getKind() != tok::identifier) {
769 Diag(Tok, diag::err_expected_ident); // missing protocol name.
770 return 0;
771 }
772 // Save the protocol name, then consume it.
773 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
774 SourceLocation nameLoc = ConsumeToken();
775
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000776 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
777 if (Tok.getKind() == tok::semi) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000778 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000779 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000780 }
781 if (Tok.getKind() == tok::comma) { // list of forward declarations.
782 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000783 ProtocolRefs.push_back(protocolName);
784
785 while (1) {
786 ConsumeToken(); // the ','
787 if (Tok.getKind() != tok::identifier) {
788 Diag(Tok, diag::err_expected_ident);
789 SkipUntil(tok::semi);
790 return 0;
791 }
792 ProtocolRefs.push_back(Tok.getIdentifierInfo());
793 ConsumeToken(); // the identifier
794
795 if (Tok.getKind() != tok::comma)
796 break;
797 }
798 // Consume the ';'.
799 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
800 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000801 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000802 if (ProtocolRefs.size() > 0)
Steve Naroffb4dfe362007-10-02 22:39:18 +0000803 return Actions.ActOnForwardProtocolDeclaration(CurScope, AtLoc,
804 &ProtocolRefs[0],
805 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000806 // Last, and definitely not least, parse a protocol declaration.
807 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000808 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000809 return 0;
810 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000811
Steve Naroff25aace82007-10-03 21:00:46 +0000812 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(CurScope, AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000813 protocolName, nameLoc,
814 &ProtocolRefs[0],
815 ProtocolRefs.size());
816 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000817
818 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000819 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000820 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000821 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000822 }
823 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000824 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000825}
Steve Narofffb367882007-08-20 21:31:48 +0000826
827/// objc-implementation:
828/// objc-class-implementation-prologue
829/// objc-category-implementation-prologue
830///
831/// objc-class-implementation-prologue:
832/// @implementation identifier objc-superclass[opt]
833/// objc-class-instance-variables[opt]
834///
835/// objc-category-implementation-prologue:
836/// @implementation identifier ( identifier )
837
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000838Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
839 SourceLocation atLoc) {
840 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
841 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
842 ConsumeToken(); // the "implementation" identifier
843
844 if (Tok.getKind() != tok::identifier) {
845 Diag(Tok, diag::err_expected_ident); // missing class or category name.
846 return 0;
847 }
848 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000849 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000850 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
851
852 if (Tok.getKind() == tok::l_paren) {
853 // we have a category implementation.
854 SourceLocation lparenLoc = ConsumeParen();
855 SourceLocation categoryLoc, rparenLoc;
856 IdentifierInfo *categoryId = 0;
857
858 if (Tok.getKind() == tok::identifier) {
859 categoryId = Tok.getIdentifierInfo();
860 categoryLoc = ConsumeToken();
861 } else {
862 Diag(Tok, diag::err_expected_ident); // missing category name.
863 return 0;
864 }
865 if (Tok.getKind() != tok::r_paren) {
866 Diag(Tok, diag::err_expected_rparen);
867 SkipUntil(tok::r_paren, false); // don't stop at ';'
868 return 0;
869 }
870 rparenLoc = ConsumeParen();
Steve Naroff25aace82007-10-03 21:00:46 +0000871 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(CurScope,
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000872 atLoc, nameId, nameLoc, categoryId,
873 categoryLoc);
874 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000875 }
876 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000877 SourceLocation superClassLoc;
878 IdentifierInfo *superClassId = 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000879 if (Tok.getKind() == tok::colon) {
880 // We have a super class
881 ConsumeToken();
882 if (Tok.getKind() != tok::identifier) {
883 Diag(Tok, diag::err_expected_ident); // missing super class name.
884 return 0;
885 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000886 superClassId = Tok.getIdentifierInfo();
887 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000888 }
Steve Naroff25aace82007-10-03 21:00:46 +0000889 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(CurScope,
890 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000891 superClassId, superClassLoc);
892
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000893 if (Tok.getKind() == tok::l_brace)
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000894 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000895
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000896 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000897}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000898Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
899 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
900 "ParseObjCAtEndDeclaration(): Expected @end");
901 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000902 if (ObjcImpDecl) {
903 // Checking is not necessary except that a parse error might have caused
904 // @implementation not to have been parsed to completion and ObjcImpDecl
905 // could be 0.
906 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000907 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
908 &AllImplMethods[0],AllImplMethods.size());
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000909 ObjcImpDecl = 0;
910 AllImplMethods.clear();
911 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000912
Steve Narofffb367882007-08-20 21:31:48 +0000913 return 0;
914}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000915
916/// compatibility-alias-decl:
917/// @compatibility_alias alias-name class-name ';'
918///
919Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
920 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
921 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
922 ConsumeToken(); // consume compatibility_alias
923 if (Tok.getKind() != tok::identifier) {
924 Diag(Tok, diag::err_expected_ident);
925 return 0;
926 }
927 ConsumeToken(); // consume alias-name
928 if (Tok.getKind() != tok::identifier) {
929 Diag(Tok, diag::err_expected_ident);
930 return 0;
931 }
932 ConsumeToken(); // consume class-name;
933 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000934 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000935 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000936}
937
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000938/// property-synthesis:
939/// @synthesize property-ivar-list ';'
940///
941/// property-ivar-list:
942/// property-ivar
943/// property-ivar-list ',' property-ivar
944///
945/// property-ivar:
946/// identifier
947/// identifier '=' identifier
948///
949Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
950 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
951 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
952 SourceLocation loc = ConsumeToken(); // consume dynamic
953 if (Tok.getKind() != tok::identifier) {
954 Diag(Tok, diag::err_expected_ident);
955 return 0;
956 }
957 while (Tok.getKind() == tok::identifier) {
958 ConsumeToken(); // consume property name
959 if (Tok.getKind() == tok::equal) {
960 // property '=' ivar-name
961 ConsumeToken(); // consume '='
962 if (Tok.getKind() != tok::identifier) {
963 Diag(Tok, diag::err_expected_ident);
964 break;
965 }
966 ConsumeToken(); // consume ivar-name
967 }
968 if (Tok.getKind() != tok::comma)
969 break;
970 ConsumeToken(); // consume ','
971 }
972 if (Tok.getKind() != tok::semi)
973 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
974 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000975}
976
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000977/// property-dynamic:
978/// @dynamic property-list
979///
980/// property-list:
981/// identifier
982/// property-list ',' identifier
983///
984Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
985 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
986 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
987 SourceLocation loc = ConsumeToken(); // consume dynamic
988 if (Tok.getKind() != tok::identifier) {
989 Diag(Tok, diag::err_expected_ident);
990 return 0;
991 }
992 while (Tok.getKind() == tok::identifier) {
993 ConsumeToken(); // consume property name
994 if (Tok.getKind() != tok::comma)
995 break;
996 ConsumeToken(); // consume ','
997 }
998 if (Tok.getKind() != tok::semi)
999 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1000 return 0;
1001}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001002
1003/// objc-throw-statement:
1004/// throw expression[opt];
1005///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001006Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001007 ConsumeToken(); // consume throw
1008 if (Tok.getKind() != tok::semi) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001009 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001010 if (Res.isInvalid) {
1011 SkipUntil(tok::semi);
1012 return 0;
1013 }
1014 }
1015 return 0;
1016}
1017
1018/// objc-try-catch-statement:
1019/// @try compound-statement objc-catch-list[opt]
1020/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1021///
1022/// objc-catch-list:
1023/// @catch ( parameter-declaration ) compound-statement
1024/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1025/// catch-parameter-declaration:
1026/// parameter-declaration
1027/// '...' [OBJC2]
1028///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001029Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001030 bool catch_or_finally_seen = false;
1031 ConsumeToken(); // consume try
1032 if (Tok.getKind() != tok::l_brace) {
1033 Diag (Tok, diag::err_expected_lbrace);
1034 return 0;
1035 }
1036 StmtResult TryBody = ParseCompoundStatementBody();
1037 while (Tok.getKind() == tok::at) {
1038 ConsumeToken();
1039 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1040 SourceLocation catchLoc = ConsumeToken(); // consume catch
1041 if (Tok.getKind() == tok::l_paren) {
1042 ConsumeParen();
1043 if (Tok.getKind() != tok::ellipsis) {
1044 DeclSpec DS;
1045 ParseDeclarationSpecifiers(DS);
1046 // Parse the parameter-declaration.
1047 // FIXME: BlockContext may not be the right context!
1048 Declarator ParmDecl(DS, Declarator::BlockContext);
1049 ParseDeclarator(ParmDecl);
1050 }
1051 else
1052 ConsumeToken(); // consume '...'
1053 ConsumeParen();
1054 StmtResult CatchMody = ParseCompoundStatementBody();
1055 }
1056 else {
1057 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1058 return 0;
1059 }
1060 catch_or_finally_seen = true;
1061 }
1062 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1063 ConsumeToken(); // consume finally
1064 StmtResult FinallyBody = ParseCompoundStatementBody();
1065 catch_or_finally_seen = true;
1066 break;
1067 }
1068 }
1069 if (!catch_or_finally_seen)
1070 Diag(atLoc, diag::err_missing_catch_finally);
1071 return 0;
1072}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001073
Steve Naroff81f1bba2007-09-06 21:24:23 +00001074/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001075///
1076void Parser::ParseObjCInstanceMethodDefinition() {
1077 assert(Tok.getKind() == tok::minus &&
1078 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001079 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001080 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001081 // parse optional ';'
1082 if (Tok.getKind() == tok::semi)
1083 ConsumeToken();
1084
1085 if (Tok.getKind() != tok::l_brace) {
1086 Diag (Tok, diag::err_expected_lbrace);
1087 return;
1088 }
1089
1090 StmtResult FnBody = ParseCompoundStatementBody();
1091}
1092
Steve Naroff81f1bba2007-09-06 21:24:23 +00001093/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001094///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001095void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001096 assert(Tok.getKind() == tok::plus &&
1097 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001098 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001099 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001100 // parse optional ';'
1101 if (Tok.getKind() == tok::semi)
1102 ConsumeToken();
1103 if (Tok.getKind() != tok::l_brace) {
1104 Diag (Tok, diag::err_expected_lbrace);
1105 return;
1106 }
1107
1108 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001109}
Anders Carlssona66cad42007-08-21 17:43:55 +00001110
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001111Parser::ExprResult Parser::ParseObjCExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001112
1113 switch (Tok.getKind()) {
1114 case tok::string_literal: // primary-expression: string-literal
1115 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001116 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001117 default:
1118 break;
1119 }
1120
1121 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001122 case tok::objc_encode:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001123 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001124 case tok::objc_protocol:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001125 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression());
Anders Carlssona66cad42007-08-21 17:43:55 +00001126 default:
1127 Diag(AtLoc, diag::err_unexpected_at);
1128 SkipUntil(tok::semi);
1129 break;
1130 }
1131
1132 return 0;
1133}
1134
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001135/// objc-message-expr:
1136/// '[' objc-receiver objc-message-args ']'
1137///
1138/// objc-receiver:
1139/// expression
1140/// class-name
1141/// type-name
1142///
1143/// objc-message-args:
1144/// objc-selector
1145/// objc-keywordarg-list
1146///
1147/// objc-keywordarg-list:
1148/// objc-keywordarg
1149/// objc-keywordarg-list objc-keywordarg
1150///
1151/// objc-keywordarg:
1152/// selector-name[opt] ':' objc-keywordexpr
1153///
1154/// objc-keywordexpr:
1155/// nonempty-expr-list
1156///
1157/// nonempty-expr-list:
1158/// assignment-expression
1159/// nonempty-expr-list , assignment-expression
1160///
1161Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001162 assert(Tok.getKind() == tok::l_square && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001163 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001164 IdentifierInfo *ReceiverName = 0;
1165 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001166 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001167 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001168 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1169 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001170 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001171 } else {
1172 ExprResult Res = ParseAssignmentExpression();
1173 if (Res.isInvalid) {
1174 SkipUntil(tok::identifier);
1175 return Res;
1176 }
1177 ReceiverExpr = Res.Val;
1178 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001179 // Parse objc-selector
1180 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff4ed9d662007-09-27 14:38:14 +00001181
1182 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1183 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1184
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001185 if (Tok.getKind() == tok::colon) {
1186 while (1) {
1187 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001188 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001189
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001190 if (Tok.getKind() != tok::colon) {
1191 Diag(Tok, diag::err_expected_colon);
1192 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001193 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001194 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001195 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001196 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001197 ExprResult Res = ParseAssignmentExpression();
1198 if (Res.isInvalid) {
1199 SkipUntil(tok::identifier);
1200 return Res;
1201 }
1202 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001203 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001204
1205 // Check for another keyword selector.
1206 selIdent = ParseObjCSelector();
1207 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001208 break;
1209 // We have a selector or a colon, continue parsing.
1210 }
1211 // Parse the, optional, argument list, comma separated.
1212 while (Tok.getKind() == tok::comma) {
1213 ConsumeToken();
1214 /// Parse the expression after ','
1215 ParseAssignmentExpression();
1216 }
1217 } else if (!selIdent) {
1218 Diag(Tok, diag::err_expected_ident); // missing selector name.
1219 SkipUntil(tok::semi);
1220 return 0;
1221 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001222
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001223 if (Tok.getKind() != tok::r_square) {
1224 Diag(Tok, diag::err_expected_rsquare);
1225 SkipUntil(tok::semi);
1226 return 0;
1227 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001228 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001229
Steve Narofff9e80db2007-10-05 18:42:47 +00001230 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001231 if (nKeys == 0)
1232 KeyIdents.push_back(selIdent);
1233 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1234
1235 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001236 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001237 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1238 &KeyExprs[0]);
1239 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1240 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001241}
1242
Anders Carlssona66cad42007-08-21 17:43:55 +00001243Parser::ExprResult Parser::ParseObjCStringLiteral() {
1244 ExprResult Res = ParseStringLiteralExpression();
1245
1246 if (Res.isInvalid) return Res;
1247
1248 return Actions.ParseObjCStringLiteral(Res.Val);
1249}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001250
1251/// objc-encode-expression:
1252/// @encode ( type-name )
1253Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001254 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001255
1256 SourceLocation EncLoc = ConsumeToken();
1257
1258 if (Tok.getKind() != tok::l_paren) {
1259 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1260 return true;
1261 }
1262
1263 SourceLocation LParenLoc = ConsumeParen();
1264
1265 TypeTy *Ty = ParseTypeName();
1266
Anders Carlsson92faeb82007-08-23 15:31:37 +00001267 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001268
1269 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001270 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001271}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001272
1273/// objc-protocol-expression
1274/// @protocol ( protocol-name )
1275
1276Parser::ExprResult Parser::ParseObjCProtocolExpression()
1277{
1278 SourceLocation ProtoLoc = ConsumeToken();
1279
1280 if (Tok.getKind() != tok::l_paren) {
1281 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1282 return true;
1283 }
1284
1285 SourceLocation LParenLoc = ConsumeParen();
1286
1287 if (Tok.getKind() != tok::identifier) {
1288 Diag(Tok, diag::err_expected_ident);
1289 return true;
1290 }
1291
1292 // FIXME: Do something with the protocol name
1293 ConsumeToken();
1294
Anders Carlsson92faeb82007-08-23 15:31:37 +00001295 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001296
1297 // FIXME
1298 return 0;
1299}