blob: 16434cdcddbfc909d3dcc025377239f2134e08b8 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallVector.h"
18using namespace clang;
19
20
21/// ParseExternalDeclaration:
22/// external-declaration: [C99 6.9]
23/// [OBJC] objc-class-definition
24/// [OBJC] objc-class-declaration [TODO]
25/// [OBJC] objc-alias-declaration [TODO]
26/// [OBJC] objc-protocol-definition [TODO]
27/// [OBJC] objc-method-definition [TODO]
28/// [OBJC] '@' 'end' [TODO]
Steve Narofffb367882007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff87c329f2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000037 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000039 case tok::objc_implementation:
Steve Naroff81f1bba2007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000041 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000043 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000052 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Narofffb367882007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
65 if (Tok.getKind() != tok::identifier) {
66 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000068 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
Chris Lattner4b009652007-07-25 00:24:17 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
73 if (Tok.getKind() != tok::comma)
74 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000081 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000082
Steve Naroff81f1bba2007-09-06 21:24:23 +000083 return Actions.ObjcClassDeclaration(CurScope, atLoc,
84 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Steve Narofffb367882007-08-20 21:31:48 +000087///
88/// objc-interface:
89/// objc-class-interface-attributes[opt] objc-class-interface
90/// objc-category-interface
91///
92/// objc-class-interface:
93/// '@' 'interface' identifier objc-superclass[opt]
94/// objc-protocol-refs[opt]
95/// objc-class-instance-variables[opt]
96/// objc-interface-decl-list
97/// @end
98///
99/// objc-category-interface:
100/// '@' 'interface' identifier '(' identifier[opt] ')'
101/// objc-protocol-refs[opt]
102/// objc-interface-decl-list
103/// @end
104///
105/// objc-superclass:
106/// ':' identifier
107///
108/// objc-class-interface-attributes:
109/// __attribute__((visibility("default")))
110/// __attribute__((visibility("hidden")))
111/// __attribute__((deprecated))
112/// __attribute__((unavailable))
113/// __attribute__((objc_exception)) - used by NSException on 64-bit
114///
115Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
116 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
121 if (Tok.getKind() != tok::identifier) {
122 Diag(Tok, diag::err_expected_ident); // missing class or category name.
123 return 0;
124 }
125 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Steve Naroffa7f62782007-08-23 19:56:30 +0000129 if (Tok.getKind() == tok::l_paren) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000133 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Steve Narofffb367882007-08-20 21:31:48 +0000136 if (Tok.getKind() == tok::identifier) {
137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
143 if (Tok.getKind() != tok::r_paren) {
144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
149 // Next, we need to check for any protocol references.
150 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000152 return 0;
153 }
154 if (attrList) // categories don't support attributes.
155 Diag(Tok, diag::err_objc_no_attributes_on_category);
156
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000157 DeclTy *CategoryType = Actions.ObjcStartCatInterface(atLoc,
158 nameId, nameLoc,
159 categoryId, categoryLoc,
160 &ProtocolRefs[0],
161 ProtocolRefs.size());
162
163 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000164
Steve Naroff0bbffd82007-08-22 16:35:03 +0000165 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000166 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000167 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000168 return 0;
169 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000170 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000171 return 0;
172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000176
Steve Narofffb367882007-08-20 21:31:48 +0000177 if (Tok.getKind() == tok::colon) { // a super class is specified.
178 ConsumeToken();
179 if (Tok.getKind() != tok::identifier) {
180 Diag(Tok, diag::err_expected_ident); // missing super class name.
181 return 0;
182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000187 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000188 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000189 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000190 return 0;
191 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000192 DeclTy *ClsType = Actions.ObjcStartClassInterface(CurScope,
193 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000194 superClassId, superClassLoc, &ProtocolRefs[0],
195 ProtocolRefs.size(), attrList);
196
Steve Narofffb367882007-08-20 21:31:48 +0000197 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000198 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000199
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000200 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000201
202 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000203 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000204 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000205 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000206 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000207 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000208 return 0;
209}
210
211/// objc-interface-decl-list:
212/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000213/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000214/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000215/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000216/// objc-interface-decl-list declaration
217/// objc-interface-decl-list ';'
218///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000219/// objc-method-requirement: [OBJC2]
220/// @required
221/// @optional
222///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000223void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
224 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000225 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000226 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000227 while (1) {
228 if (Tok.getKind() == tok::at) {
229 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000230 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000231
232 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000233 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000234 } else if (ocKind == tok::objc_required) { // protocols only
235 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000236 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000237 if (contextKey != tok::objc_protocol)
238 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000239 } else if (ocKind == tok::objc_optional) { // protocols only
240 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000241 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000242 if (contextKey != tok::objc_protocol)
243 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000244 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000245 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000246 continue;
247 } else {
248 Diag(Tok, diag::err_objc_illegal_interface_qual);
249 ConsumeToken();
250 }
251 }
252 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000253 DeclTy *methodPrototype = ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000254 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000255 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
256 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000257 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000258 continue;
259 }
260 if (Tok.getKind() == tok::semi)
261 ConsumeToken();
262 else if (Tok.getKind() == tok::eof)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000263 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000264 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000265 // FIXME: as the name implies, this rule allows function definitions.
266 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000267 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000268 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000269 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000270 /// Insert collected methods declarations into the @interface object.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000271 Actions.ObjcAddMethodsToClass(interfaceDecl,&allMethods[0],allMethods.size());
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000272 return;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000273}
274
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000275/// Parse property attribute declarations.
276///
277/// property-attr-decl: '(' property-attrlist ')'
278/// property-attrlist:
279/// property-attribute
280/// property-attrlist ',' property-attribute
281/// property-attribute:
282/// getter '=' identifier
283/// setter '=' identifier ':'
284/// readonly
285/// readwrite
286/// assign
287/// retain
288/// copy
289/// nonatomic
290///
291void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
292 SourceLocation loc = ConsumeParen(); // consume '('
293 while (isObjCPropertyAttribute()) {
294 const IdentifierInfo *II = Tok.getIdentifierInfo();
295 // getter/setter require extra treatment.
296 if (II == ObjcPropertyAttrs[objc_getter] ||
297 II == ObjcPropertyAttrs[objc_setter]) {
298 // skip getter/setter part.
299 SourceLocation loc = ConsumeToken();
300 if (Tok.getKind() == tok::equal) {
301 loc = ConsumeToken();
302 if (Tok.getKind() == tok::identifier) {
303 if (II == ObjcPropertyAttrs[objc_setter]) {
304 loc = ConsumeToken(); // consume method name
305 if (Tok.getKind() != tok::colon) {
306 Diag(loc, diag::err_expected_colon);
307 SkipUntil(tok::r_paren,true,true);
308 break;
309 }
310 }
311 }
312 else {
313 Diag(loc, diag::err_expected_ident);
314 SkipUntil(tok::r_paren,true,true);
315 break;
316 }
317 }
318 else {
319 Diag(loc, diag::err_objc_expected_equal);
320 SkipUntil(tok::r_paren,true,true);
321 break;
322 }
323 }
324 ConsumeToken(); // consume last attribute token
325 if (Tok.getKind() == tok::comma) {
326 loc = ConsumeToken();
327 continue;
328 }
329 if (Tok.getKind() == tok::r_paren)
330 break;
331 Diag(loc, diag::err_expected_rparen);
332 SkipUntil(tok::semi);
333 return;
334 }
335 if (Tok.getKind() == tok::r_paren)
336 ConsumeParen();
337 else {
338 Diag(loc, diag::err_objc_expected_property_attr);
339 SkipUntil(tok::r_paren); // recover from error inside attribute list
340 }
341}
342
343/// Main routine to parse property declaration.
344///
345/// @property property-attr-decl[opt] property-component-decl ';'
346///
347void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
348 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
349 "ParseObjCPropertyDecl(): Expected @property");
350 ConsumeToken(); // the "property" identifier
351 // Parse property attribute list, if any.
352 if (Tok.getKind() == tok::l_paren) {
353 // property has attribute list.
354 ParseObjCPropertyAttribute(0/*FIXME*/);
355 }
356 // Parse declaration portion of @property.
357 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
358 ParseStructDeclaration(interfaceDecl, PropertyDecls);
359 if (Tok.getKind() == tok::semi)
360 ConsumeToken();
361 else {
362 Diag(Tok, diag::err_expected_semi_decl_list);
363 SkipUntil(tok::r_brace, true, true);
364 }
Chris Lattner4b009652007-07-25 00:24:17 +0000365}
Steve Narofffb367882007-08-20 21:31:48 +0000366
Steve Naroff81f1bba2007-09-06 21:24:23 +0000367/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000368/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000369/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000370///
371/// objc-instance-method: '-'
372/// objc-class-method: '+'
373///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000374/// objc-method-attributes: [OBJC2]
375/// __attribute__((deprecated))
376///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000377Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
378 tok::ObjCKeywordKind MethodImplKind) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000379 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
380 "expected +/-");
381
382 tok::TokenKind methodType = Tok.getKind();
383 SourceLocation methodLoc = ConsumeToken();
384
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000385 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000386 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000387 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000388 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000389}
390
391/// objc-selector:
392/// identifier
393/// one of
394/// enum struct union if else while do for switch case default
395/// break continue return goto asm sizeof typeof __alignof
396/// unsigned long const short volatile signed restrict _Complex
397/// in out inout bycopy byref oneway int char float double void _Bool
398///
399IdentifierInfo *Parser::ParseObjCSelector() {
400 tok::TokenKind tKind = Tok.getKind();
401 IdentifierInfo *II = 0;
402
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000403 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
404 tKind == tok::kw___alignof ||
Steve Naroff0bbffd82007-08-22 16:35:03 +0000405 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000406 II = Tok.getIdentifierInfo();
407 ConsumeToken();
408 }
409 return II;
410}
411
Steve Naroffa8ee2262007-08-22 23:18:22 +0000412/// objc-type-qualifier: one of
413/// in out inout bycopy byref oneway
414///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000415bool Parser::isObjCTypeQualifier() {
416 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000417 const IdentifierInfo *II = Tok.getIdentifierInfo();
418 for (unsigned i = 0; i < objc_NumQuals; ++i)
419 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000420 }
421 return false;
422}
423
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000424/// property-attrlist: one of
425/// readonly getter setter assign retain copy nonatomic
426///
427bool Parser::isObjCPropertyAttribute() {
428 if (Tok.getKind() == tok::identifier) {
429 const IdentifierInfo *II = Tok.getIdentifierInfo();
430 for (unsigned i = 0; i < objc_NumAttrs; ++i)
431 if (II == ObjcPropertyAttrs[i]) return true;
432 }
433 return false;
434}
435
Steve Naroff0bbffd82007-08-22 16:35:03 +0000436/// objc-type-name:
437/// '(' objc-type-qualifiers[opt] type-name ')'
438/// '(' objc-type-qualifiers[opt] ')'
439///
440/// objc-type-qualifiers:
441/// objc-type-qualifier
442/// objc-type-qualifiers objc-type-qualifier
443///
Steve Naroff304ed392007-09-05 23:30:30 +0000444Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000445 assert(Tok.getKind() == tok::l_paren && "expected (");
446
447 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Naroff304ed392007-09-05 23:30:30 +0000448 TypeTy *Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000449
Steve Naroffa8ee2262007-08-22 23:18:22 +0000450 while (isObjCTypeQualifier())
451 ConsumeToken();
452
Steve Naroff0bbffd82007-08-22 16:35:03 +0000453 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000454 Ty = ParseTypeName();
455 // FIXME: back when Sema support is in place...
456 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000457 }
458 if (Tok.getKind() != tok::r_paren) {
459 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000460 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000461 }
462 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000463 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000464}
465
Steve Naroff4ed9d662007-09-27 14:38:14 +0000466static SelectorInfo *ObjcGetUnarySelectorInfo(
467 IdentifierInfo *unarySel,
468 llvm::FoldingSet<SelectorInfo> &SelTab)
469{
470 // Unique selector, to guarantee there is one per name.
471 llvm::SmallVector<IdentifierInfo *, 1> IIV;
472 llvm::FoldingSetNodeID ID;
473
474 IIV.push_back(unarySel);
475 SelectorInfo::Profile(ID, &IIV[0], 0);
476
477 void *InsertPos = 0;
478 if (SelectorInfo *SI = SelTab.FindNodeOrInsertPos(ID, InsertPos))
479 return SI;
480
481 // SelectorInfo objects are not allocated with new because they have a
482 // variable size array (for parameter types) at the end of them.
483 SelectorInfo *SI =
484 (SelectorInfo*)malloc(sizeof(SelectorInfo) + sizeof(IdentifierInfo *));
485 new (SI) SelectorInfo(IIV[0]);
486 SelTab.InsertNode(SI, InsertPos);
487 return SI;
488}
489
490static SelectorInfo *ObjcGetKeywordSelectorInfo(
491 llvm::SmallVectorImpl<IdentifierInfo *> &IIV,
492 llvm::FoldingSet<SelectorInfo> &SelTab)
493{
494 // Unique selector, to guarantee there is one per name.
495 llvm::FoldingSetNodeID ID;
496 SelectorInfo::Profile(ID, &IIV[0], IIV.size());
497
498 void *InsertPos = 0;
499 if (SelectorInfo *SI = SelTab.FindNodeOrInsertPos(ID, InsertPos))
500 return SI;
501
502 // SelectorInfo objects are not allocated with new because they have a
503 // variable size array (for parameter types) at the end of them.
504 SelectorInfo *SI =
505 (SelectorInfo*)malloc(sizeof(SelectorInfo) +
506 IIV.size()*sizeof(IdentifierInfo *));
507 new (SI) SelectorInfo(IIV.size(), &IIV[0]);
508 SelTab.InsertNode(SI, InsertPos);
509 return SI;
510}
511
Steve Naroff0bbffd82007-08-22 16:35:03 +0000512/// objc-method-decl:
513/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000514/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000515/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000516/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000517///
518/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000519/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000520/// objc-keyword-selector objc-keyword-decl
521///
522/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000523/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
524/// objc-selector ':' objc-keyword-attributes[opt] identifier
525/// ':' objc-type-name objc-keyword-attributes[opt] identifier
526/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000527///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000528/// objc-parmlist:
529/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000530///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000531/// objc-parms:
532/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000533///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000534/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000535/// , ...
536///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000537/// objc-keyword-attributes: [OBJC2]
538/// __attribute__((unused))
539///
Steve Naroff4ed9d662007-09-27 14:38:14 +0000540Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType,
541 SourceLocation mLoc,
542 tok::ObjCKeywordKind MethodImplKind)
543{
Steve Naroff304ed392007-09-05 23:30:30 +0000544 TypeTy *ReturnType = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000545 AttributeList *methodAttrs = 0;
Steve Naroff304ed392007-09-05 23:30:30 +0000546
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000547 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000548 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000549 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000550 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000551
Steve Naroff4ed9d662007-09-27 14:38:14 +0000552 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
553 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
554 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
555
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000556 if (Tok.getKind() == tok::colon) {
Steve Naroff4ed9d662007-09-27 14:38:14 +0000557 Action::TypeTy *TypeInfo;
Steve Naroff304ed392007-09-05 23:30:30 +0000558
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000559 while (1) {
Steve Naroff4ed9d662007-09-27 14:38:14 +0000560 KeyIdents.push_back(selIdent);
Steve Naroff304ed392007-09-05 23:30:30 +0000561
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000562 // Each iteration parses a single keyword argument.
563 if (Tok.getKind() != tok::colon) {
564 Diag(Tok, diag::err_expected_colon);
565 break;
566 }
Steve Naroff4ed9d662007-09-27 14:38:14 +0000567 ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000568 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Steve Naroff4ed9d662007-09-27 14:38:14 +0000569 TypeInfo = ParseObjCTypeName();
570 else
571 TypeInfo = 0;
572 KeyTypes.push_back(TypeInfo);
573
Steve Naroff72f17fb2007-08-22 22:17:26 +0000574 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000575 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000576 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000577
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000578 if (Tok.getKind() != tok::identifier) {
579 Diag(Tok, diag::err_expected_ident); // missing argument name.
580 break;
581 }
Steve Naroff4ed9d662007-09-27 14:38:14 +0000582 ArgNames.push_back(Tok.getIdentifierInfo());
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000583 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000584
Steve Naroff253118b2007-09-17 20:25:27 +0000585 // Check for another keyword selector.
Steve Naroff304ed392007-09-05 23:30:30 +0000586 selIdent = ParseObjCSelector();
587 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000588 break;
589 // We have a selector or a colon, continue parsing.
590 }
591 // Parse the (optional) parameter list.
592 while (Tok.getKind() == tok::comma) {
593 ConsumeToken();
594 if (Tok.getKind() == tok::ellipsis) {
595 ConsumeToken();
596 break;
597 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000598 // Parse the c-style argument declaration-specifier.
599 DeclSpec DS;
600 ParseDeclarationSpecifiers(DS);
601 // Parse the declarator.
602 Declarator ParmDecl(DS, Declarator::PrototypeContext);
603 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000604 }
Steve Naroff304ed392007-09-05 23:30:30 +0000605 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000606 // If attributes exist after the method, parse them.
607 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
608 methodAttrs = ParseAttributes();
Steve Naroff4ed9d662007-09-27 14:38:14 +0000609
610 SelectorInfo *SI = ObjcGetKeywordSelectorInfo(KeyIdents,
611 PP.getSelectorTable());
612 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType, SI,
613 &KeyTypes[0], &ArgNames[0],
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000614 methodAttrs, MethodImplKind);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000615 } else if (!selIdent) {
616 Diag(Tok, diag::err_expected_ident); // missing selector name.
617 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000618 // If attributes exist after the method, parse them.
619 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
620 methodAttrs = ParseAttributes();
621
Steve Naroff4ed9d662007-09-27 14:38:14 +0000622 SelectorInfo *SI = ObjcGetUnarySelectorInfo(selIdent, PP.getSelectorTable());
623 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType, SI,
624 0, 0, methodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000625}
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 }
649 // Consume the '>'.
650 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
651}
652
653/// objc-class-instance-variables:
654/// '{' objc-instance-variable-decl-list[opt] '}'
655///
656/// objc-instance-variable-decl-list:
657/// objc-visibility-spec
658/// objc-instance-variable-decl ';'
659/// ';'
660/// objc-instance-variable-decl-list objc-visibility-spec
661/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
662/// objc-instance-variable-decl-list ';'
663///
664/// objc-visibility-spec:
665/// @private
666/// @protected
667/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000668/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000669///
670/// objc-instance-variable-decl:
671/// struct-declaration
672///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000673void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000674 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000675 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000676 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
677 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000678
679 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000680
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000681 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000682 // While we still have something to read, read the instance variables.
683 while (Tok.getKind() != tok::r_brace &&
684 Tok.getKind() != tok::eof) {
685 // Each iteration of this loop reads one objc-instance-variable-decl.
686
687 // Check for extraneous top-level semicolon.
688 if (Tok.getKind() == tok::semi) {
689 Diag(Tok, diag::ext_extra_struct_semi);
690 ConsumeToken();
691 continue;
692 }
693 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000694 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
695 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000696 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000697 case tok::objc_private:
698 case tok::objc_public:
699 case tok::objc_protected:
700 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000701 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000702 ConsumeToken();
703 continue;
704 default:
705 Diag(Tok, diag::err_objc_illegal_visibility_spec);
706 ConsumeToken();
707 continue;
708 }
709 }
710 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000711 for (unsigned i = 0; i < IvarDecls.size(); i++) {
712 AllIvarDecls.push_back(IvarDecls[i]);
713 AllVisibilities.push_back(visibility);
714 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000715 IvarDecls.clear();
716
Steve Naroffc4474992007-08-21 21:17:12 +0000717 if (Tok.getKind() == tok::semi) {
718 ConsumeToken();
719 } else if (Tok.getKind() == tok::r_brace) {
720 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
721 break;
722 } else {
723 Diag(Tok, diag::err_expected_semi_decl_list);
724 // Skip to end of block or statement
725 SkipUntil(tok::r_brace, true, true);
726 }
727 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000728 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff0acc9c92007-09-15 18:49:24 +0000729 Actions.ActOnFields(LBraceLoc, interfaceDecl,
730 &AllIvarDecls[0], AllIvarDecls.size(),
731 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000732 }
Steve Naroffc4474992007-08-21 21:17:12 +0000733 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
734 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000735}
Steve Narofffb367882007-08-20 21:31:48 +0000736
737/// objc-protocol-declaration:
738/// objc-protocol-definition
739/// objc-protocol-forward-reference
740///
741/// objc-protocol-definition:
742/// @protocol identifier
743/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000744/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000745/// @end
746///
747/// objc-protocol-forward-reference:
748/// @protocol identifier-list ';'
749///
750/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000751/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000752/// semicolon in the first alternative if objc-protocol-refs are omitted.
753
Steve Naroff72f17fb2007-08-22 22:17:26 +0000754Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000755 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000756 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
757 ConsumeToken(); // the "protocol" identifier
758
759 if (Tok.getKind() != tok::identifier) {
760 Diag(Tok, diag::err_expected_ident); // missing protocol name.
761 return 0;
762 }
763 // Save the protocol name, then consume it.
764 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
765 SourceLocation nameLoc = ConsumeToken();
766
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000767 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
768 if (Tok.getKind() == tok::semi) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000769 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000770 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000771 }
772 if (Tok.getKind() == tok::comma) { // list of forward declarations.
773 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000774 ProtocolRefs.push_back(protocolName);
775
776 while (1) {
777 ConsumeToken(); // the ','
778 if (Tok.getKind() != tok::identifier) {
779 Diag(Tok, diag::err_expected_ident);
780 SkipUntil(tok::semi);
781 return 0;
782 }
783 ProtocolRefs.push_back(Tok.getIdentifierInfo());
784 ConsumeToken(); // the identifier
785
786 if (Tok.getKind() != tok::comma)
787 break;
788 }
789 // Consume the ';'.
790 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
791 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000792 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000793 if (ProtocolRefs.size() > 0)
794 return Actions.ObjcForwardProtocolDeclaration(CurScope, AtLoc,
795 &ProtocolRefs[0],
796 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000797 // Last, and definitely not least, parse a protocol declaration.
798 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000799 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000800 return 0;
801 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000802
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000803 DeclTy *ProtoType = Actions.ObjcStartProtoInterface(CurScope, AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000804 protocolName, nameLoc,
805 &ProtocolRefs[0],
806 ProtocolRefs.size());
807 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000808
809 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000810 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000811 ConsumeToken(); // the "end" identifier
812 return 0;
813 }
814 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000815 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000816}
Steve Narofffb367882007-08-20 21:31:48 +0000817
818/// objc-implementation:
819/// objc-class-implementation-prologue
820/// objc-category-implementation-prologue
821///
822/// objc-class-implementation-prologue:
823/// @implementation identifier objc-superclass[opt]
824/// objc-class-instance-variables[opt]
825///
826/// objc-category-implementation-prologue:
827/// @implementation identifier ( identifier )
828
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000829Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
830 SourceLocation atLoc) {
831 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
832 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
833 ConsumeToken(); // the "implementation" identifier
834
835 if (Tok.getKind() != tok::identifier) {
836 Diag(Tok, diag::err_expected_ident); // missing class or category name.
837 return 0;
838 }
839 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000840 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000841 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
842
843 if (Tok.getKind() == tok::l_paren) {
844 // we have a category implementation.
845 SourceLocation lparenLoc = ConsumeParen();
846 SourceLocation categoryLoc, rparenLoc;
847 IdentifierInfo *categoryId = 0;
848
849 if (Tok.getKind() == tok::identifier) {
850 categoryId = Tok.getIdentifierInfo();
851 categoryLoc = ConsumeToken();
852 } else {
853 Diag(Tok, diag::err_expected_ident); // missing category name.
854 return 0;
855 }
856 if (Tok.getKind() != tok::r_paren) {
857 Diag(Tok, diag::err_expected_rparen);
858 SkipUntil(tok::r_paren, false); // don't stop at ';'
859 return 0;
860 }
861 rparenLoc = ConsumeParen();
862 return 0;
863 }
864 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000865 SourceLocation superClassLoc;
866 IdentifierInfo *superClassId = 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000867 if (Tok.getKind() == tok::colon) {
868 // We have a super class
869 ConsumeToken();
870 if (Tok.getKind() != tok::identifier) {
871 Diag(Tok, diag::err_expected_ident); // missing super class name.
872 return 0;
873 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000874 superClassId = Tok.getIdentifierInfo();
875 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000876 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000877 DeclTy *ImplClsType = Actions.ObjcStartClassImplementation(CurScope,
878 atLoc,
879 nameId, nameLoc,
880 superClassId, superClassLoc);
881
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000882 if (Tok.getKind() == tok::l_brace)
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000883 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000884
Steve Narofffb367882007-08-20 21:31:48 +0000885 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000886}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000887Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
888 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
889 "ParseObjCAtEndDeclaration(): Expected @end");
890 ConsumeToken(); // the "end" identifier
891
Steve Narofffb367882007-08-20 21:31:48 +0000892 return 0;
893}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000894
895/// compatibility-alias-decl:
896/// @compatibility_alias alias-name class-name ';'
897///
898Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
899 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
900 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
901 ConsumeToken(); // consume compatibility_alias
902 if (Tok.getKind() != tok::identifier) {
903 Diag(Tok, diag::err_expected_ident);
904 return 0;
905 }
906 ConsumeToken(); // consume alias-name
907 if (Tok.getKind() != tok::identifier) {
908 Diag(Tok, diag::err_expected_ident);
909 return 0;
910 }
911 ConsumeToken(); // consume class-name;
912 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000913 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000914 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000915}
916
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000917/// property-synthesis:
918/// @synthesize property-ivar-list ';'
919///
920/// property-ivar-list:
921/// property-ivar
922/// property-ivar-list ',' property-ivar
923///
924/// property-ivar:
925/// identifier
926/// identifier '=' identifier
927///
928Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
929 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
930 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
931 SourceLocation loc = ConsumeToken(); // consume dynamic
932 if (Tok.getKind() != tok::identifier) {
933 Diag(Tok, diag::err_expected_ident);
934 return 0;
935 }
936 while (Tok.getKind() == tok::identifier) {
937 ConsumeToken(); // consume property name
938 if (Tok.getKind() == tok::equal) {
939 // property '=' ivar-name
940 ConsumeToken(); // consume '='
941 if (Tok.getKind() != tok::identifier) {
942 Diag(Tok, diag::err_expected_ident);
943 break;
944 }
945 ConsumeToken(); // consume ivar-name
946 }
947 if (Tok.getKind() != tok::comma)
948 break;
949 ConsumeToken(); // consume ','
950 }
951 if (Tok.getKind() != tok::semi)
952 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
953 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000954}
955
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000956/// property-dynamic:
957/// @dynamic property-list
958///
959/// property-list:
960/// identifier
961/// property-list ',' identifier
962///
963Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
964 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
965 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
966 SourceLocation loc = ConsumeToken(); // consume dynamic
967 if (Tok.getKind() != tok::identifier) {
968 Diag(Tok, diag::err_expected_ident);
969 return 0;
970 }
971 while (Tok.getKind() == tok::identifier) {
972 ConsumeToken(); // consume property name
973 if (Tok.getKind() != tok::comma)
974 break;
975 ConsumeToken(); // consume ','
976 }
977 if (Tok.getKind() != tok::semi)
978 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
979 return 0;
980}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000981
982/// objc-throw-statement:
983/// throw expression[opt];
984///
985Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation &atLoc) {
986 ConsumeToken(); // consume throw
987 if (Tok.getKind() != tok::semi) {
988 ExprResult Res = ParseAssignmentExpression();
989 if (Res.isInvalid) {
990 SkipUntil(tok::semi);
991 return 0;
992 }
993 }
994 return 0;
995}
996
997/// objc-try-catch-statement:
998/// @try compound-statement objc-catch-list[opt]
999/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1000///
1001/// objc-catch-list:
1002/// @catch ( parameter-declaration ) compound-statement
1003/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1004/// catch-parameter-declaration:
1005/// parameter-declaration
1006/// '...' [OBJC2]
1007///
1008Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation &atLoc) {
1009 bool catch_or_finally_seen = false;
1010 ConsumeToken(); // consume try
1011 if (Tok.getKind() != tok::l_brace) {
1012 Diag (Tok, diag::err_expected_lbrace);
1013 return 0;
1014 }
1015 StmtResult TryBody = ParseCompoundStatementBody();
1016 while (Tok.getKind() == tok::at) {
1017 ConsumeToken();
1018 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1019 SourceLocation catchLoc = ConsumeToken(); // consume catch
1020 if (Tok.getKind() == tok::l_paren) {
1021 ConsumeParen();
1022 if (Tok.getKind() != tok::ellipsis) {
1023 DeclSpec DS;
1024 ParseDeclarationSpecifiers(DS);
1025 // Parse the parameter-declaration.
1026 // FIXME: BlockContext may not be the right context!
1027 Declarator ParmDecl(DS, Declarator::BlockContext);
1028 ParseDeclarator(ParmDecl);
1029 }
1030 else
1031 ConsumeToken(); // consume '...'
1032 ConsumeParen();
1033 StmtResult CatchMody = ParseCompoundStatementBody();
1034 }
1035 else {
1036 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1037 return 0;
1038 }
1039 catch_or_finally_seen = true;
1040 }
1041 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1042 ConsumeToken(); // consume finally
1043 StmtResult FinallyBody = ParseCompoundStatementBody();
1044 catch_or_finally_seen = true;
1045 break;
1046 }
1047 }
1048 if (!catch_or_finally_seen)
1049 Diag(atLoc, diag::err_missing_catch_finally);
1050 return 0;
1051}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001052
Steve Naroff81f1bba2007-09-06 21:24:23 +00001053/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001054///
1055void Parser::ParseObjCInstanceMethodDefinition() {
1056 assert(Tok.getKind() == tok::minus &&
1057 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001058 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +00001059 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001060 // parse optional ';'
1061 if (Tok.getKind() == tok::semi)
1062 ConsumeToken();
1063
1064 if (Tok.getKind() != tok::l_brace) {
1065 Diag (Tok, diag::err_expected_lbrace);
1066 return;
1067 }
1068
1069 StmtResult FnBody = ParseCompoundStatementBody();
1070}
1071
Steve Naroff81f1bba2007-09-06 21:24:23 +00001072/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001073///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001074void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001075 assert(Tok.getKind() == tok::plus &&
1076 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001077 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +00001078 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001079 // parse optional ';'
1080 if (Tok.getKind() == tok::semi)
1081 ConsumeToken();
1082 if (Tok.getKind() != tok::l_brace) {
1083 Diag (Tok, diag::err_expected_lbrace);
1084 return;
1085 }
1086
1087 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001088}
Anders Carlssona66cad42007-08-21 17:43:55 +00001089
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001090Parser::ExprResult Parser::ParseObjCExpression(SourceLocation &AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001091
1092 switch (Tok.getKind()) {
1093 case tok::string_literal: // primary-expression: string-literal
1094 case tok::wide_string_literal:
1095 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001096 default:
1097 break;
1098 }
1099
1100 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001101 case tok::objc_encode:
1102 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001103 case tok::objc_protocol:
1104 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001105 default:
1106 Diag(AtLoc, diag::err_unexpected_at);
1107 SkipUntil(tok::semi);
1108 break;
1109 }
1110
1111 return 0;
1112}
1113
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001114/// objc-message-expr:
1115/// '[' objc-receiver objc-message-args ']'
1116///
1117/// objc-receiver:
1118/// expression
1119/// class-name
1120/// type-name
1121///
1122/// objc-message-args:
1123/// objc-selector
1124/// objc-keywordarg-list
1125///
1126/// objc-keywordarg-list:
1127/// objc-keywordarg
1128/// objc-keywordarg-list objc-keywordarg
1129///
1130/// objc-keywordarg:
1131/// selector-name[opt] ':' objc-keywordexpr
1132///
1133/// objc-keywordexpr:
1134/// nonempty-expr-list
1135///
1136/// nonempty-expr-list:
1137/// assignment-expression
1138/// nonempty-expr-list , assignment-expression
1139///
1140Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001141 assert(Tok.getKind() == tok::l_square && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001142 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001143 IdentifierInfo *ReceiverName = 0;
1144 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001145 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001146 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001147 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1148 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001149 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001150 } else {
1151 ExprResult Res = ParseAssignmentExpression();
1152 if (Res.isInvalid) {
1153 SkipUntil(tok::identifier);
1154 return Res;
1155 }
1156 ReceiverExpr = Res.Val;
1157 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001158 // Parse objc-selector
1159 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff4ed9d662007-09-27 14:38:14 +00001160
1161 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1162 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1163
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001164 if (Tok.getKind() == tok::colon) {
1165 while (1) {
1166 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001167 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001168
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001169 if (Tok.getKind() != tok::colon) {
1170 Diag(Tok, diag::err_expected_colon);
1171 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001172 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001173 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001174 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001175 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001176 ExprResult Res = ParseAssignmentExpression();
1177 if (Res.isInvalid) {
1178 SkipUntil(tok::identifier);
1179 return Res;
1180 }
1181 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001182 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001183
1184 // Check for another keyword selector.
1185 selIdent = ParseObjCSelector();
1186 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001187 break;
1188 // We have a selector or a colon, continue parsing.
1189 }
1190 // Parse the, optional, argument list, comma separated.
1191 while (Tok.getKind() == tok::comma) {
1192 ConsumeToken();
1193 /// Parse the expression after ','
1194 ParseAssignmentExpression();
1195 }
1196 } else if (!selIdent) {
1197 Diag(Tok, diag::err_expected_ident); // missing selector name.
1198 SkipUntil(tok::semi);
1199 return 0;
1200 }
1201 if (Tok.getKind() != tok::r_square) {
1202 Diag(Tok, diag::err_expected_rsquare);
1203 SkipUntil(tok::semi);
1204 return 0;
1205 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001206 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001207
Steve Naroff4ed9d662007-09-27 14:38:14 +00001208 if (KeyIdents.size()) {
1209 SelectorInfo *SI = ObjcGetKeywordSelectorInfo(KeyIdents,
1210 PP.getSelectorTable());
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001211 // We've just parsed a keyword message.
1212 if (ReceiverName)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001213 return Actions.ActOnClassMessage(ReceiverName, SI, LBracloc, RBracloc,
1214 &KeyExprs[0]);
1215 return Actions.ActOnInstanceMessage(ReceiverExpr, SI, LBracloc, RBracloc,
1216 &KeyExprs[0]);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001217 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001218 SelectorInfo *SI = ObjcGetUnarySelectorInfo(selIdent, PP.getSelectorTable());
1219
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001220 // We've just parsed a unary message (a message with no arguments).
Steve Naroff253118b2007-09-17 20:25:27 +00001221 if (ReceiverName)
Steve Naroff4ed9d662007-09-27 14:38:14 +00001222 return Actions.ActOnClassMessage(ReceiverName, SI, LBracloc, RBracloc, 0);
1223 return Actions.ActOnInstanceMessage(ReceiverExpr, SI, LBracloc, RBracloc, 0);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001224}
1225
Anders Carlssona66cad42007-08-21 17:43:55 +00001226Parser::ExprResult Parser::ParseObjCStringLiteral() {
1227 ExprResult Res = ParseStringLiteralExpression();
1228
1229 if (Res.isInvalid) return Res;
1230
1231 return Actions.ParseObjCStringLiteral(Res.Val);
1232}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001233
1234/// objc-encode-expression:
1235/// @encode ( type-name )
1236Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001237 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001238
1239 SourceLocation EncLoc = ConsumeToken();
1240
1241 if (Tok.getKind() != tok::l_paren) {
1242 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1243 return true;
1244 }
1245
1246 SourceLocation LParenLoc = ConsumeParen();
1247
1248 TypeTy *Ty = ParseTypeName();
1249
Anders Carlsson92faeb82007-08-23 15:31:37 +00001250 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001251
1252 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001253 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001254}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001255
1256/// objc-protocol-expression
1257/// @protocol ( protocol-name )
1258
1259Parser::ExprResult Parser::ParseObjCProtocolExpression()
1260{
1261 SourceLocation ProtoLoc = ConsumeToken();
1262
1263 if (Tok.getKind() != tok::l_paren) {
1264 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1265 return true;
1266 }
1267
1268 SourceLocation LParenLoc = ConsumeParen();
1269
1270 if (Tok.getKind() != tok::identifier) {
1271 Diag(Tok, diag::err_expected_ident);
1272 return true;
1273 }
1274
1275 // FIXME: Do something with the protocol name
1276 ConsumeToken();
1277
Anders Carlsson92faeb82007-08-23 15:31:37 +00001278 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001279
1280 // FIXME
1281 return 0;
1282}