blob: eddbb4a2541893ed8b1e37bf78adeb0eecd0e356 [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
466/// objc-method-decl:
467/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000468/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000469/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000470/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000471///
472/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000473/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000474/// objc-keyword-selector objc-keyword-decl
475///
476/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000477/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
478/// objc-selector ':' objc-keyword-attributes[opt] identifier
479/// ':' objc-type-name objc-keyword-attributes[opt] identifier
480/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000481///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000482/// objc-parmlist:
483/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000484///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000485/// objc-parms:
486/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000487///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000488/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000489/// , ...
490///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000491/// objc-keyword-attributes: [OBJC2]
492/// __attribute__((unused))
493///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000494Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc,
495 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000496
Steve Naroff304ed392007-09-05 23:30:30 +0000497 TypeTy *ReturnType = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000498 AttributeList *methodAttrs = 0;
Steve Naroff304ed392007-09-05 23:30:30 +0000499
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000500 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000501 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000502 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000503 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000504
Steve Naroff253118b2007-09-17 20:25:27 +0000505 llvm::SmallVector<ObjcKeywordDecl, 12> KeyInfo;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000506
507 if (Tok.getKind() == tok::colon) {
Steve Naroff304ed392007-09-05 23:30:30 +0000508
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000509 while (1) {
Steve Naroff253118b2007-09-17 20:25:27 +0000510 ObjcKeywordDecl KeyInfoDecl;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000511 KeyInfoDecl.SelectorName = selIdent;
Steve Naroff304ed392007-09-05 23:30:30 +0000512
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000513 // Each iteration parses a single keyword argument.
514 if (Tok.getKind() != tok::colon) {
515 Diag(Tok, diag::err_expected_colon);
516 break;
517 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000518 KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000519 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000520 KeyInfoDecl.TypeInfo = ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000521
522 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000523 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000524 KeyInfoDecl.AttrList = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000525
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000526 if (Tok.getKind() != tok::identifier) {
527 Diag(Tok, diag::err_expected_ident); // missing argument name.
528 break;
529 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000530 KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000531 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000532
Steve Naroff253118b2007-09-17 20:25:27 +0000533 // Rather than call out to the actions, package up the info locally,
534 // like we do for Declarator.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000535 KeyInfo.push_back(KeyInfoDecl);
Steve Naroff253118b2007-09-17 20:25:27 +0000536
537 // Check for another keyword selector.
Steve Naroff304ed392007-09-05 23:30:30 +0000538 selIdent = ParseObjCSelector();
539 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000540 break;
541 // We have a selector or a colon, continue parsing.
542 }
543 // Parse the (optional) parameter list.
544 while (Tok.getKind() == tok::comma) {
545 ConsumeToken();
546 if (Tok.getKind() == tok::ellipsis) {
547 ConsumeToken();
548 break;
549 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000550 // Parse the c-style argument declaration-specifier.
551 DeclSpec DS;
552 ParseDeclarationSpecifiers(DS);
553 // Parse the declarator.
554 Declarator ParmDecl(DS, Declarator::PrototypeContext);
555 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000556 }
Steve Naroff304ed392007-09-05 23:30:30 +0000557 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000558 // If attributes exist after the method, parse them.
559 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
560 methodAttrs = ParseAttributes();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000561 return Actions.ObjcBuildMethodDeclaration(mLoc, mType,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000562 ReturnType,
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000563 &KeyInfo[0], KeyInfo.size(),
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000564 methodAttrs, MethodImplKind);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000565 } else if (!selIdent) {
566 Diag(Tok, diag::err_expected_ident); // missing selector name.
567 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000568 // If attributes exist after the method, parse them.
569 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
570 methodAttrs = ParseAttributes();
571
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000572 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
573 selIdent, methodAttrs,
574 MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000575}
576
Steve Narofffb367882007-08-20 21:31:48 +0000577/// objc-protocol-refs:
578/// '<' identifier-list '>'
579///
Steve Naroff304ed392007-09-05 23:30:30 +0000580bool Parser::ParseObjCProtocolReferences(
581 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000582 assert(Tok.getKind() == tok::less && "expected <");
583
584 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000585
586 while (1) {
587 if (Tok.getKind() != tok::identifier) {
588 Diag(Tok, diag::err_expected_ident);
589 SkipUntil(tok::greater);
590 return true;
591 }
592 ProtocolRefs.push_back(Tok.getIdentifierInfo());
593 ConsumeToken();
594
595 if (Tok.getKind() != tok::comma)
596 break;
597 ConsumeToken();
598 }
599 // Consume the '>'.
600 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
601}
602
603/// objc-class-instance-variables:
604/// '{' objc-instance-variable-decl-list[opt] '}'
605///
606/// objc-instance-variable-decl-list:
607/// objc-visibility-spec
608/// objc-instance-variable-decl ';'
609/// ';'
610/// objc-instance-variable-decl-list objc-visibility-spec
611/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
612/// objc-instance-variable-decl-list ';'
613///
614/// objc-visibility-spec:
615/// @private
616/// @protected
617/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000618/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000619///
620/// objc-instance-variable-decl:
621/// struct-declaration
622///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000623void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000624 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000625 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000626 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
627 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000628
629 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000630
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000631 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000632 // While we still have something to read, read the instance variables.
633 while (Tok.getKind() != tok::r_brace &&
634 Tok.getKind() != tok::eof) {
635 // Each iteration of this loop reads one objc-instance-variable-decl.
636
637 // Check for extraneous top-level semicolon.
638 if (Tok.getKind() == tok::semi) {
639 Diag(Tok, diag::ext_extra_struct_semi);
640 ConsumeToken();
641 continue;
642 }
643 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000644 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
645 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000646 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000647 case tok::objc_private:
648 case tok::objc_public:
649 case tok::objc_protected:
650 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000651 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000652 ConsumeToken();
653 continue;
654 default:
655 Diag(Tok, diag::err_objc_illegal_visibility_spec);
656 ConsumeToken();
657 continue;
658 }
659 }
660 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000661 for (unsigned i = 0; i < IvarDecls.size(); i++) {
662 AllIvarDecls.push_back(IvarDecls[i]);
663 AllVisibilities.push_back(visibility);
664 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000665 IvarDecls.clear();
666
Steve Naroffc4474992007-08-21 21:17:12 +0000667 if (Tok.getKind() == tok::semi) {
668 ConsumeToken();
669 } else if (Tok.getKind() == tok::r_brace) {
670 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
671 break;
672 } else {
673 Diag(Tok, diag::err_expected_semi_decl_list);
674 // Skip to end of block or statement
675 SkipUntil(tok::r_brace, true, true);
676 }
677 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000678 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff0acc9c92007-09-15 18:49:24 +0000679 Actions.ActOnFields(LBraceLoc, interfaceDecl,
680 &AllIvarDecls[0], AllIvarDecls.size(),
681 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000682 }
Steve Naroffc4474992007-08-21 21:17:12 +0000683 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
684 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000685}
Steve Narofffb367882007-08-20 21:31:48 +0000686
687/// objc-protocol-declaration:
688/// objc-protocol-definition
689/// objc-protocol-forward-reference
690///
691/// objc-protocol-definition:
692/// @protocol identifier
693/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000694/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000695/// @end
696///
697/// objc-protocol-forward-reference:
698/// @protocol identifier-list ';'
699///
700/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000701/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000702/// semicolon in the first alternative if objc-protocol-refs are omitted.
703
Steve Naroff72f17fb2007-08-22 22:17:26 +0000704Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000705 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000706 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
707 ConsumeToken(); // the "protocol" identifier
708
709 if (Tok.getKind() != tok::identifier) {
710 Diag(Tok, diag::err_expected_ident); // missing protocol name.
711 return 0;
712 }
713 // Save the protocol name, then consume it.
714 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
715 SourceLocation nameLoc = ConsumeToken();
716
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000717 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
718 if (Tok.getKind() == tok::semi) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000719 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000720 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000721 }
722 if (Tok.getKind() == tok::comma) { // list of forward declarations.
723 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000724 ProtocolRefs.push_back(protocolName);
725
726 while (1) {
727 ConsumeToken(); // the ','
728 if (Tok.getKind() != tok::identifier) {
729 Diag(Tok, diag::err_expected_ident);
730 SkipUntil(tok::semi);
731 return 0;
732 }
733 ProtocolRefs.push_back(Tok.getIdentifierInfo());
734 ConsumeToken(); // the identifier
735
736 if (Tok.getKind() != tok::comma)
737 break;
738 }
739 // Consume the ';'.
740 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
741 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000742 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000743 if (ProtocolRefs.size() > 0)
744 return Actions.ObjcForwardProtocolDeclaration(CurScope, AtLoc,
745 &ProtocolRefs[0],
746 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000747 // Last, and definitely not least, parse a protocol declaration.
748 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000749 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000750 return 0;
751 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000752
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000753 DeclTy *ProtoType = Actions.ObjcStartProtoInterface(CurScope, AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000754 protocolName, nameLoc,
755 &ProtocolRefs[0],
756 ProtocolRefs.size());
757 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000758
759 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000760 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000761 ConsumeToken(); // the "end" identifier
762 return 0;
763 }
764 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000765 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000766}
Steve Narofffb367882007-08-20 21:31:48 +0000767
768/// objc-implementation:
769/// objc-class-implementation-prologue
770/// objc-category-implementation-prologue
771///
772/// objc-class-implementation-prologue:
773/// @implementation identifier objc-superclass[opt]
774/// objc-class-instance-variables[opt]
775///
776/// objc-category-implementation-prologue:
777/// @implementation identifier ( identifier )
778
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000779Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
780 SourceLocation atLoc) {
781 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
782 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
783 ConsumeToken(); // the "implementation" identifier
784
785 if (Tok.getKind() != tok::identifier) {
786 Diag(Tok, diag::err_expected_ident); // missing class or category name.
787 return 0;
788 }
789 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000790 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000791 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
792
793 if (Tok.getKind() == tok::l_paren) {
794 // we have a category implementation.
795 SourceLocation lparenLoc = ConsumeParen();
796 SourceLocation categoryLoc, rparenLoc;
797 IdentifierInfo *categoryId = 0;
798
799 if (Tok.getKind() == tok::identifier) {
800 categoryId = Tok.getIdentifierInfo();
801 categoryLoc = ConsumeToken();
802 } else {
803 Diag(Tok, diag::err_expected_ident); // missing category name.
804 return 0;
805 }
806 if (Tok.getKind() != tok::r_paren) {
807 Diag(Tok, diag::err_expected_rparen);
808 SkipUntil(tok::r_paren, false); // don't stop at ';'
809 return 0;
810 }
811 rparenLoc = ConsumeParen();
812 return 0;
813 }
814 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000815 SourceLocation superClassLoc;
816 IdentifierInfo *superClassId = 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000817 if (Tok.getKind() == tok::colon) {
818 // We have a super class
819 ConsumeToken();
820 if (Tok.getKind() != tok::identifier) {
821 Diag(Tok, diag::err_expected_ident); // missing super class name.
822 return 0;
823 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000824 superClassId = Tok.getIdentifierInfo();
825 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000826 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000827 DeclTy *ImplClsType = Actions.ObjcStartClassImplementation(CurScope,
828 atLoc,
829 nameId, nameLoc,
830 superClassId, superClassLoc);
831
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000832 if (Tok.getKind() == tok::l_brace)
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000833 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000834
Steve Narofffb367882007-08-20 21:31:48 +0000835 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000836}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000837Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
838 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
839 "ParseObjCAtEndDeclaration(): Expected @end");
840 ConsumeToken(); // the "end" identifier
841
Steve Narofffb367882007-08-20 21:31:48 +0000842 return 0;
843}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000844
845/// compatibility-alias-decl:
846/// @compatibility_alias alias-name class-name ';'
847///
848Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
849 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
850 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
851 ConsumeToken(); // consume compatibility_alias
852 if (Tok.getKind() != tok::identifier) {
853 Diag(Tok, diag::err_expected_ident);
854 return 0;
855 }
856 ConsumeToken(); // consume alias-name
857 if (Tok.getKind() != tok::identifier) {
858 Diag(Tok, diag::err_expected_ident);
859 return 0;
860 }
861 ConsumeToken(); // consume class-name;
862 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000863 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000864 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000865}
866
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000867/// property-synthesis:
868/// @synthesize property-ivar-list ';'
869///
870/// property-ivar-list:
871/// property-ivar
872/// property-ivar-list ',' property-ivar
873///
874/// property-ivar:
875/// identifier
876/// identifier '=' identifier
877///
878Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
879 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
880 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
881 SourceLocation loc = ConsumeToken(); // consume dynamic
882 if (Tok.getKind() != tok::identifier) {
883 Diag(Tok, diag::err_expected_ident);
884 return 0;
885 }
886 while (Tok.getKind() == tok::identifier) {
887 ConsumeToken(); // consume property name
888 if (Tok.getKind() == tok::equal) {
889 // property '=' ivar-name
890 ConsumeToken(); // consume '='
891 if (Tok.getKind() != tok::identifier) {
892 Diag(Tok, diag::err_expected_ident);
893 break;
894 }
895 ConsumeToken(); // consume ivar-name
896 }
897 if (Tok.getKind() != tok::comma)
898 break;
899 ConsumeToken(); // consume ','
900 }
901 if (Tok.getKind() != tok::semi)
902 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
903 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000904}
905
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000906/// property-dynamic:
907/// @dynamic property-list
908///
909/// property-list:
910/// identifier
911/// property-list ',' identifier
912///
913Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
914 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
915 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
916 SourceLocation loc = ConsumeToken(); // consume dynamic
917 if (Tok.getKind() != tok::identifier) {
918 Diag(Tok, diag::err_expected_ident);
919 return 0;
920 }
921 while (Tok.getKind() == tok::identifier) {
922 ConsumeToken(); // consume property name
923 if (Tok.getKind() != tok::comma)
924 break;
925 ConsumeToken(); // consume ','
926 }
927 if (Tok.getKind() != tok::semi)
928 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
929 return 0;
930}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000931
932/// objc-throw-statement:
933/// throw expression[opt];
934///
935Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation &atLoc) {
936 ConsumeToken(); // consume throw
937 if (Tok.getKind() != tok::semi) {
938 ExprResult Res = ParseAssignmentExpression();
939 if (Res.isInvalid) {
940 SkipUntil(tok::semi);
941 return 0;
942 }
943 }
944 return 0;
945}
946
947/// objc-try-catch-statement:
948/// @try compound-statement objc-catch-list[opt]
949/// @try compound-statement objc-catch-list[opt] @finally compound-statement
950///
951/// objc-catch-list:
952/// @catch ( parameter-declaration ) compound-statement
953/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
954/// catch-parameter-declaration:
955/// parameter-declaration
956/// '...' [OBJC2]
957///
958Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation &atLoc) {
959 bool catch_or_finally_seen = false;
960 ConsumeToken(); // consume try
961 if (Tok.getKind() != tok::l_brace) {
962 Diag (Tok, diag::err_expected_lbrace);
963 return 0;
964 }
965 StmtResult TryBody = ParseCompoundStatementBody();
966 while (Tok.getKind() == tok::at) {
967 ConsumeToken();
968 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
969 SourceLocation catchLoc = ConsumeToken(); // consume catch
970 if (Tok.getKind() == tok::l_paren) {
971 ConsumeParen();
972 if (Tok.getKind() != tok::ellipsis) {
973 DeclSpec DS;
974 ParseDeclarationSpecifiers(DS);
975 // Parse the parameter-declaration.
976 // FIXME: BlockContext may not be the right context!
977 Declarator ParmDecl(DS, Declarator::BlockContext);
978 ParseDeclarator(ParmDecl);
979 }
980 else
981 ConsumeToken(); // consume '...'
982 ConsumeParen();
983 StmtResult CatchMody = ParseCompoundStatementBody();
984 }
985 else {
986 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
987 return 0;
988 }
989 catch_or_finally_seen = true;
990 }
991 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
992 ConsumeToken(); // consume finally
993 StmtResult FinallyBody = ParseCompoundStatementBody();
994 catch_or_finally_seen = true;
995 break;
996 }
997 }
998 if (!catch_or_finally_seen)
999 Diag(atLoc, diag::err_missing_catch_finally);
1000 return 0;
1001}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001002
Steve Naroff81f1bba2007-09-06 21:24:23 +00001003/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001004///
1005void Parser::ParseObjCInstanceMethodDefinition() {
1006 assert(Tok.getKind() == tok::minus &&
1007 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001008 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +00001009 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001010 // parse optional ';'
1011 if (Tok.getKind() == tok::semi)
1012 ConsumeToken();
1013
1014 if (Tok.getKind() != tok::l_brace) {
1015 Diag (Tok, diag::err_expected_lbrace);
1016 return;
1017 }
1018
1019 StmtResult FnBody = ParseCompoundStatementBody();
1020}
1021
Steve Naroff81f1bba2007-09-06 21:24:23 +00001022/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001023///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001024void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001025 assert(Tok.getKind() == tok::plus &&
1026 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001027 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +00001028 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001029 // parse optional ';'
1030 if (Tok.getKind() == tok::semi)
1031 ConsumeToken();
1032 if (Tok.getKind() != tok::l_brace) {
1033 Diag (Tok, diag::err_expected_lbrace);
1034 return;
1035 }
1036
1037 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001038}
Anders Carlssona66cad42007-08-21 17:43:55 +00001039
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001040Parser::ExprResult Parser::ParseObjCExpression(SourceLocation &AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001041
1042 switch (Tok.getKind()) {
1043 case tok::string_literal: // primary-expression: string-literal
1044 case tok::wide_string_literal:
1045 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001046 default:
1047 break;
1048 }
1049
1050 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001051 case tok::objc_encode:
1052 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001053 case tok::objc_protocol:
1054 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001055 default:
1056 Diag(AtLoc, diag::err_unexpected_at);
1057 SkipUntil(tok::semi);
1058 break;
1059 }
1060
1061 return 0;
1062}
1063
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001064/// objc-message-expr:
1065/// '[' objc-receiver objc-message-args ']'
1066///
1067/// objc-receiver:
1068/// expression
1069/// class-name
1070/// type-name
1071///
1072/// objc-message-args:
1073/// objc-selector
1074/// objc-keywordarg-list
1075///
1076/// objc-keywordarg-list:
1077/// objc-keywordarg
1078/// objc-keywordarg-list objc-keywordarg
1079///
1080/// objc-keywordarg:
1081/// selector-name[opt] ':' objc-keywordexpr
1082///
1083/// objc-keywordexpr:
1084/// nonempty-expr-list
1085///
1086/// nonempty-expr-list:
1087/// assignment-expression
1088/// nonempty-expr-list , assignment-expression
1089///
1090Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001091 assert(Tok.getKind() == tok::l_square && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001092 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001093 IdentifierInfo *ReceiverName = 0;
1094 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001095 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001096 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001097 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1098 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001099 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001100 } else {
1101 ExprResult Res = ParseAssignmentExpression();
1102 if (Res.isInvalid) {
1103 SkipUntil(tok::identifier);
1104 return Res;
1105 }
1106 ReceiverExpr = Res.Val;
1107 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001108 // Parse objc-selector
1109 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff253118b2007-09-17 20:25:27 +00001110 llvm::SmallVector<ObjcKeywordMessage, 12> KeyInfo;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001111 if (Tok.getKind() == tok::colon) {
1112 while (1) {
1113 // Each iteration parses a single keyword argument.
Steve Naroff253118b2007-09-17 20:25:27 +00001114 ObjcKeywordMessage KeyInfoMess;
1115 KeyInfoMess.SelectorName = selIdent;
1116
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001117 if (Tok.getKind() != tok::colon) {
1118 Diag(Tok, diag::err_expected_colon);
1119 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001120 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001121 }
Steve Naroff253118b2007-09-17 20:25:27 +00001122 KeyInfoMess.ColonLoc = ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001123 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001124 ExprResult Res = ParseAssignmentExpression();
1125 if (Res.isInvalid) {
1126 SkipUntil(tok::identifier);
1127 return Res;
1128 }
1129 // We have a valid expression.
1130 KeyInfoMess.KeywordExpr = Res.Val;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001131
Steve Naroff253118b2007-09-17 20:25:27 +00001132 // Rather than call out to the actions, package up the info locally,
1133 // like we do for Declarator.
1134 KeyInfo.push_back(KeyInfoMess);
1135
1136 // Check for another keyword selector.
1137 selIdent = ParseObjCSelector();
1138 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001139 break;
1140 // We have a selector or a colon, continue parsing.
1141 }
1142 // Parse the, optional, argument list, comma separated.
1143 while (Tok.getKind() == tok::comma) {
1144 ConsumeToken();
1145 /// Parse the expression after ','
1146 ParseAssignmentExpression();
1147 }
1148 } else if (!selIdent) {
1149 Diag(Tok, diag::err_expected_ident); // missing selector name.
1150 SkipUntil(tok::semi);
1151 return 0;
1152 }
1153 if (Tok.getKind() != tok::r_square) {
1154 Diag(Tok, diag::err_expected_rsquare);
1155 SkipUntil(tok::semi);
1156 return 0;
1157 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001158 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001159
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001160 if (KeyInfo.size()) {
1161 // We've just parsed a keyword message.
1162 if (ReceiverName)
1163 return Actions.ActOnKeywordMessage(ReceiverName,
Steve Naroffc39ca262007-09-18 23:55:05 +00001164 &KeyInfo[0], KeyInfo.size(),
1165 LBracloc, RBracloc);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001166 return Actions.ActOnKeywordMessage(ReceiverExpr,
Steve Naroffc39ca262007-09-18 23:55:05 +00001167 &KeyInfo[0], KeyInfo.size(),
1168 LBracloc, RBracloc);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001169 }
1170 // We've just parsed a unary message (a message with no arguments).
Steve Naroff253118b2007-09-17 20:25:27 +00001171 if (ReceiverName)
Steve Naroffc39ca262007-09-18 23:55:05 +00001172 return Actions.ActOnUnaryMessage(ReceiverName, selIdent, LBracloc,RBracloc);
1173 return Actions.ActOnUnaryMessage(ReceiverExpr, selIdent, LBracloc,RBracloc);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001174}
1175
Anders Carlssona66cad42007-08-21 17:43:55 +00001176Parser::ExprResult Parser::ParseObjCStringLiteral() {
1177 ExprResult Res = ParseStringLiteralExpression();
1178
1179 if (Res.isInvalid) return Res;
1180
1181 return Actions.ParseObjCStringLiteral(Res.Val);
1182}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001183
1184/// objc-encode-expression:
1185/// @encode ( type-name )
1186Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001187 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001188
1189 SourceLocation EncLoc = ConsumeToken();
1190
1191 if (Tok.getKind() != tok::l_paren) {
1192 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1193 return true;
1194 }
1195
1196 SourceLocation LParenLoc = ConsumeParen();
1197
1198 TypeTy *Ty = ParseTypeName();
1199
Anders Carlsson92faeb82007-08-23 15:31:37 +00001200 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001201
1202 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001203 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001204}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001205
1206/// objc-protocol-expression
1207/// @protocol ( protocol-name )
1208
1209Parser::ExprResult Parser::ParseObjCProtocolExpression()
1210{
1211 SourceLocation ProtoLoc = ConsumeToken();
1212
1213 if (Tok.getKind() != tok::l_paren) {
1214 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1215 return true;
1216 }
1217
1218 SourceLocation LParenLoc = ConsumeParen();
1219
1220 if (Tok.getKind() != tok::identifier) {
1221 Diag(Tok, diag::err_expected_ident);
1222 return true;
1223 }
1224
1225 // FIXME: Do something with the protocol name
1226 ConsumeToken();
1227
Anders Carlsson92faeb82007-08-23 15:31:37 +00001228 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001229
1230 // FIXME
1231 return 0;
1232}