blob: 0533660974c7a3f7780f933ebb0e5d5e524ade40 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallVector.h"
18using namespace clang;
19
20
21/// ParseExternalDeclaration:
22/// external-declaration: [C99 6.9]
23/// [OBJC] objc-class-definition
24/// [OBJC] objc-class-declaration [TODO]
25/// [OBJC] objc-alias-declaration [TODO]
26/// [OBJC] objc-protocol-definition [TODO]
27/// [OBJC] objc-method-definition [TODO]
28/// [OBJC] '@' 'end' [TODO]
Steve Narofffb367882007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff87c329f2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000037 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000039 case tok::objc_implementation:
Steve Naroff81f1bba2007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000041 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000043 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000052 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Narofffb367882007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
65 if (Tok.getKind() != tok::identifier) {
66 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000068 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
Chris Lattner4b009652007-07-25 00:24:17 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
73 if (Tok.getKind() != tok::comma)
74 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000081 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000082
Steve Naroff81f1bba2007-09-06 21:24:23 +000083 return Actions.ObjcClassDeclaration(CurScope, atLoc,
84 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Steve Narofffb367882007-08-20 21:31:48 +000087///
88/// objc-interface:
89/// objc-class-interface-attributes[opt] objc-class-interface
90/// objc-category-interface
91///
92/// objc-class-interface:
93/// '@' 'interface' identifier objc-superclass[opt]
94/// objc-protocol-refs[opt]
95/// objc-class-instance-variables[opt]
96/// objc-interface-decl-list
97/// @end
98///
99/// objc-category-interface:
100/// '@' 'interface' identifier '(' identifier[opt] ')'
101/// objc-protocol-refs[opt]
102/// objc-interface-decl-list
103/// @end
104///
105/// objc-superclass:
106/// ':' identifier
107///
108/// objc-class-interface-attributes:
109/// __attribute__((visibility("default")))
110/// __attribute__((visibility("hidden")))
111/// __attribute__((deprecated))
112/// __attribute__((unavailable))
113/// __attribute__((objc_exception)) - used by NSException on 64-bit
114///
115Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
116 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
121 if (Tok.getKind() != tok::identifier) {
122 Diag(Tok, diag::err_expected_ident); // missing class or category name.
123 return 0;
124 }
125 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Steve Naroffa7f62782007-08-23 19:56:30 +0000129 if (Tok.getKind() == tok::l_paren) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000133 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Steve Narofffb367882007-08-20 21:31:48 +0000136 if (Tok.getKind() == tok::identifier) {
137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
143 if (Tok.getKind() != tok::r_paren) {
144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
149 // Next, we need to check for any protocol references.
150 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000152 return 0;
153 }
154 if (attrList) // categories don't support attributes.
155 Diag(Tok, diag::err_objc_no_attributes_on_category);
156
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000157 DeclTy *CategoryType = Actions.ObjcStartCatInterface(atLoc,
158 nameId, nameLoc,
159 categoryId, categoryLoc,
160 &ProtocolRefs[0],
161 ProtocolRefs.size());
162
163 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000164
Steve Naroff0bbffd82007-08-22 16:35:03 +0000165 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000166 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000167 ConsumeToken(); // the "end" identifier
Steve Narofffb367882007-08-20 21:31:48 +0000168 return 0;
169 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000170 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000171 return 0;
172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000176
Steve Narofffb367882007-08-20 21:31:48 +0000177 if (Tok.getKind() == tok::colon) { // a super class is specified.
178 ConsumeToken();
179 if (Tok.getKind() != tok::identifier) {
180 Diag(Tok, diag::err_expected_ident); // missing super class name.
181 return 0;
182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000187 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000188 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000189 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000190 return 0;
191 }
Steve Naroff304ed392007-09-05 23:30:30 +0000192 DeclTy *ClsType = Actions.ObjcStartClassInterface(atLoc, nameId, nameLoc,
193 superClassId, superClassLoc, &ProtocolRefs[0],
194 ProtocolRefs.size(), attrList);
195
Steve Narofffb367882007-08-20 21:31:48 +0000196 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000197 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000198
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000199 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000200
201 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000202 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000203 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000204 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000205 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000206 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000207 return 0;
208}
209
210/// objc-interface-decl-list:
211/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000212/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000213/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000214/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000215/// objc-interface-decl-list declaration
216/// objc-interface-decl-list ';'
217///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000218/// objc-method-requirement: [OBJC2]
219/// @required
220/// @optional
221///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000222void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
223 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000224 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000225 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000226 while (1) {
227 if (Tok.getKind() == tok::at) {
228 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000229 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000230
231 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000232 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000233 } else if (ocKind == tok::objc_required) { // protocols only
234 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000235 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000236 if (contextKey != tok::objc_protocol)
237 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000238 } else if (ocKind == tok::objc_optional) { // protocols only
239 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000240 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000241 if (contextKey != tok::objc_protocol)
242 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000243 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000244 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000245 continue;
246 } else {
247 Diag(Tok, diag::err_objc_illegal_interface_qual);
248 ConsumeToken();
249 }
250 }
251 if (Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) {
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000252 DeclTy *methodPrototype = ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000253 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000254 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
255 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000256 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000257 continue;
258 }
259 if (Tok.getKind() == tok::semi)
260 ConsumeToken();
261 else if (Tok.getKind() == tok::eof)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000262 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000263 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000264 // FIXME: as the name implies, this rule allows function definitions.
265 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000266 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000267 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000268 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000269 /// Insert collected methods declarations into the @interface object.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000270 Actions.ObjcAddMethodsToClass(interfaceDecl,&allMethods[0],allMethods.size());
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000271 return;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000272}
273
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000274/// Parse property attribute declarations.
275///
276/// property-attr-decl: '(' property-attrlist ')'
277/// property-attrlist:
278/// property-attribute
279/// property-attrlist ',' property-attribute
280/// property-attribute:
281/// getter '=' identifier
282/// setter '=' identifier ':'
283/// readonly
284/// readwrite
285/// assign
286/// retain
287/// copy
288/// nonatomic
289///
290void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
291 SourceLocation loc = ConsumeParen(); // consume '('
292 while (isObjCPropertyAttribute()) {
293 const IdentifierInfo *II = Tok.getIdentifierInfo();
294 // getter/setter require extra treatment.
295 if (II == ObjcPropertyAttrs[objc_getter] ||
296 II == ObjcPropertyAttrs[objc_setter]) {
297 // skip getter/setter part.
298 SourceLocation loc = ConsumeToken();
299 if (Tok.getKind() == tok::equal) {
300 loc = ConsumeToken();
301 if (Tok.getKind() == tok::identifier) {
302 if (II == ObjcPropertyAttrs[objc_setter]) {
303 loc = ConsumeToken(); // consume method name
304 if (Tok.getKind() != tok::colon) {
305 Diag(loc, diag::err_expected_colon);
306 SkipUntil(tok::r_paren,true,true);
307 break;
308 }
309 }
310 }
311 else {
312 Diag(loc, diag::err_expected_ident);
313 SkipUntil(tok::r_paren,true,true);
314 break;
315 }
316 }
317 else {
318 Diag(loc, diag::err_objc_expected_equal);
319 SkipUntil(tok::r_paren,true,true);
320 break;
321 }
322 }
323 ConsumeToken(); // consume last attribute token
324 if (Tok.getKind() == tok::comma) {
325 loc = ConsumeToken();
326 continue;
327 }
328 if (Tok.getKind() == tok::r_paren)
329 break;
330 Diag(loc, diag::err_expected_rparen);
331 SkipUntil(tok::semi);
332 return;
333 }
334 if (Tok.getKind() == tok::r_paren)
335 ConsumeParen();
336 else {
337 Diag(loc, diag::err_objc_expected_property_attr);
338 SkipUntil(tok::r_paren); // recover from error inside attribute list
339 }
340}
341
342/// Main routine to parse property declaration.
343///
344/// @property property-attr-decl[opt] property-component-decl ';'
345///
346void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
347 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
348 "ParseObjCPropertyDecl(): Expected @property");
349 ConsumeToken(); // the "property" identifier
350 // Parse property attribute list, if any.
351 if (Tok.getKind() == tok::l_paren) {
352 // property has attribute list.
353 ParseObjCPropertyAttribute(0/*FIXME*/);
354 }
355 // Parse declaration portion of @property.
356 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
357 ParseStructDeclaration(interfaceDecl, PropertyDecls);
358 if (Tok.getKind() == tok::semi)
359 ConsumeToken();
360 else {
361 Diag(Tok, diag::err_expected_semi_decl_list);
362 SkipUntil(tok::r_brace, true, true);
363 }
Chris Lattner4b009652007-07-25 00:24:17 +0000364}
Steve Narofffb367882007-08-20 21:31:48 +0000365
Steve Naroff81f1bba2007-09-06 21:24:23 +0000366/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000367/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000368/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000369///
370/// objc-instance-method: '-'
371/// objc-class-method: '+'
372///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000373/// objc-method-attributes: [OBJC2]
374/// __attribute__((deprecated))
375///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000376Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
377 tok::ObjCKeywordKind MethodImplKind) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000378 assert((Tok.getKind() == tok::minus || Tok.getKind() == tok::plus) &&
379 "expected +/-");
380
381 tok::TokenKind methodType = Tok.getKind();
382 SourceLocation methodLoc = ConsumeToken();
383
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000384 DeclTy *MDecl = ParseObjCMethodDecl(methodType, methodLoc, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000385 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000386 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000387 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000388}
389
390/// objc-selector:
391/// identifier
392/// one of
393/// enum struct union if else while do for switch case default
394/// break continue return goto asm sizeof typeof __alignof
395/// unsigned long const short volatile signed restrict _Complex
396/// in out inout bycopy byref oneway int char float double void _Bool
397///
398IdentifierInfo *Parser::ParseObjCSelector() {
399 tok::TokenKind tKind = Tok.getKind();
400 IdentifierInfo *II = 0;
401
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000402 if (tKind == tok::identifier || tKind == tok::kw_typeof ||
403 tKind == tok::kw___alignof ||
Steve Naroff0bbffd82007-08-22 16:35:03 +0000404 (tKind >= tok::kw_auto && tKind <= tok::kw__Complex)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000405 II = Tok.getIdentifierInfo();
406 ConsumeToken();
407 }
408 return II;
409}
410
Steve Naroffa8ee2262007-08-22 23:18:22 +0000411/// objc-type-qualifier: one of
412/// in out inout bycopy byref oneway
413///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000414bool Parser::isObjCTypeQualifier() {
415 if (Tok.getKind() == tok::identifier) {
Chris Lattner32352462007-08-29 22:54:08 +0000416 const IdentifierInfo *II = Tok.getIdentifierInfo();
417 for (unsigned i = 0; i < objc_NumQuals; ++i)
418 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000419 }
420 return false;
421}
422
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000423/// property-attrlist: one of
424/// readonly getter setter assign retain copy nonatomic
425///
426bool Parser::isObjCPropertyAttribute() {
427 if (Tok.getKind() == tok::identifier) {
428 const IdentifierInfo *II = Tok.getIdentifierInfo();
429 for (unsigned i = 0; i < objc_NumAttrs; ++i)
430 if (II == ObjcPropertyAttrs[i]) return true;
431 }
432 return false;
433}
434
Steve Naroff0bbffd82007-08-22 16:35:03 +0000435/// objc-type-name:
436/// '(' objc-type-qualifiers[opt] type-name ')'
437/// '(' objc-type-qualifiers[opt] ')'
438///
439/// objc-type-qualifiers:
440/// objc-type-qualifier
441/// objc-type-qualifiers objc-type-qualifier
442///
Steve Naroff304ed392007-09-05 23:30:30 +0000443Parser::TypeTy *Parser::ParseObjCTypeName() {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000444 assert(Tok.getKind() == tok::l_paren && "expected (");
445
446 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Steve Naroff304ed392007-09-05 23:30:30 +0000447 TypeTy *Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000448
Steve Naroffa8ee2262007-08-22 23:18:22 +0000449 while (isObjCTypeQualifier())
450 ConsumeToken();
451
Steve Naroff0bbffd82007-08-22 16:35:03 +0000452 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000453 Ty = ParseTypeName();
454 // FIXME: back when Sema support is in place...
455 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000456 }
457 if (Tok.getKind() != tok::r_paren) {
458 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000459 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000460 }
461 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000462 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000463}
464
465/// objc-method-decl:
466/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000467/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000468/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000469/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000470///
471/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000472/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000473/// objc-keyword-selector objc-keyword-decl
474///
475/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000476/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
477/// objc-selector ':' objc-keyword-attributes[opt] identifier
478/// ':' objc-type-name objc-keyword-attributes[opt] identifier
479/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000480///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000481/// objc-parmlist:
482/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000483///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000484/// objc-parms:
485/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000486///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000487/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000488/// , ...
489///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000490/// objc-keyword-attributes: [OBJC2]
491/// __attribute__((unused))
492///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000493Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType, SourceLocation mLoc,
494 tok::ObjCKeywordKind MethodImplKind) {
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000495
Steve Naroff304ed392007-09-05 23:30:30 +0000496 TypeTy *ReturnType = 0;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000497 AttributeList *methodAttrs = 0;
Steve Naroff304ed392007-09-05 23:30:30 +0000498
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000499 // Parse the return type.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000500 if (Tok.getKind() == tok::l_paren)
Steve Naroff304ed392007-09-05 23:30:30 +0000501 ReturnType = ParseObjCTypeName();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000502 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff304ed392007-09-05 23:30:30 +0000503
Steve Naroff253118b2007-09-17 20:25:27 +0000504 llvm::SmallVector<ObjcKeywordDecl, 12> KeyInfo;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000505
506 if (Tok.getKind() == tok::colon) {
Steve Naroff304ed392007-09-05 23:30:30 +0000507
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000508 while (1) {
Steve Naroff253118b2007-09-17 20:25:27 +0000509 ObjcKeywordDecl KeyInfoDecl;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000510 KeyInfoDecl.SelectorName = selIdent;
Steve Naroff304ed392007-09-05 23:30:30 +0000511
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000512 // Each iteration parses a single keyword argument.
513 if (Tok.getKind() != tok::colon) {
514 Diag(Tok, diag::err_expected_colon);
515 break;
516 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000517 KeyInfoDecl.ColonLoc = ConsumeToken(); // Eat the ':'.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000518 if (Tok.getKind() == tok::l_paren) // Parse the argument type.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000519 KeyInfoDecl.TypeInfo = ParseObjCTypeName();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000520
521 // If attributes exist before the argument name, parse them.
Steve Naroffa7f62782007-08-23 19:56:30 +0000522 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000523 KeyInfoDecl.AttrList = ParseAttributes();
Steve Naroff72f17fb2007-08-22 22:17:26 +0000524
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000525 if (Tok.getKind() != tok::identifier) {
526 Diag(Tok, diag::err_expected_ident); // missing argument name.
527 break;
528 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000529 KeyInfoDecl.ArgumentName = Tok.getIdentifierInfo();
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000530 ConsumeToken(); // Eat the identifier.
Steve Naroff304ed392007-09-05 23:30:30 +0000531
Steve Naroff253118b2007-09-17 20:25:27 +0000532 // Rather than call out to the actions, package up the info locally,
533 // like we do for Declarator.
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000534 KeyInfo.push_back(KeyInfoDecl);
Steve Naroff253118b2007-09-17 20:25:27 +0000535
536 // Check for another keyword selector.
Steve Naroff304ed392007-09-05 23:30:30 +0000537 selIdent = ParseObjCSelector();
538 if (!selIdent && Tok.getKind() != tok::colon)
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000539 break;
540 // We have a selector or a colon, continue parsing.
541 }
542 // Parse the (optional) parameter list.
543 while (Tok.getKind() == tok::comma) {
544 ConsumeToken();
545 if (Tok.getKind() == tok::ellipsis) {
546 ConsumeToken();
547 break;
548 }
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +0000549 // Parse the c-style argument declaration-specifier.
550 DeclSpec DS;
551 ParseDeclarationSpecifiers(DS);
552 // Parse the declarator.
553 Declarator ParmDecl(DS, Declarator::PrototypeContext);
554 ParseDeclarator(ParmDecl);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000555 }
Steve Naroff304ed392007-09-05 23:30:30 +0000556 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000557 // If attributes exist after the method, parse them.
558 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
559 methodAttrs = ParseAttributes();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000560 return Actions.ObjcBuildMethodDeclaration(mLoc, mType,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000561 ReturnType,
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000562 &KeyInfo[0], KeyInfo.size(),
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000563 methodAttrs, MethodImplKind);
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000564 } else if (!selIdent) {
565 Diag(Tok, diag::err_expected_ident); // missing selector name.
566 }
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000567 // If attributes exist after the method, parse them.
568 if (getLang().ObjC2 && Tok.getKind() == tok::kw___attribute)
569 methodAttrs = ParseAttributes();
570
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000571 return Actions.ObjcBuildMethodDeclaration(mLoc, mType, ReturnType,
572 selIdent, methodAttrs,
573 MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000574}
575
Steve Narofffb367882007-08-20 21:31:48 +0000576/// objc-protocol-refs:
577/// '<' identifier-list '>'
578///
Steve Naroff304ed392007-09-05 23:30:30 +0000579bool Parser::ParseObjCProtocolReferences(
580 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Steve Narofffb367882007-08-20 21:31:48 +0000581 assert(Tok.getKind() == tok::less && "expected <");
582
583 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000584
585 while (1) {
586 if (Tok.getKind() != tok::identifier) {
587 Diag(Tok, diag::err_expected_ident);
588 SkipUntil(tok::greater);
589 return true;
590 }
591 ProtocolRefs.push_back(Tok.getIdentifierInfo());
592 ConsumeToken();
593
594 if (Tok.getKind() != tok::comma)
595 break;
596 ConsumeToken();
597 }
598 // Consume the '>'.
599 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
600}
601
602/// objc-class-instance-variables:
603/// '{' objc-instance-variable-decl-list[opt] '}'
604///
605/// objc-instance-variable-decl-list:
606/// objc-visibility-spec
607/// objc-instance-variable-decl ';'
608/// ';'
609/// objc-instance-variable-decl-list objc-visibility-spec
610/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
611/// objc-instance-variable-decl-list ';'
612///
613/// objc-visibility-spec:
614/// @private
615/// @protected
616/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000617/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000618///
619/// objc-instance-variable-decl:
620/// struct-declaration
621///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000622void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Steve Naroffc4474992007-08-21 21:17:12 +0000623 assert(Tok.getKind() == tok::l_brace && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000624 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000625 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
626 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000627
628 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000629
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000630 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000631 // While we still have something to read, read the instance variables.
632 while (Tok.getKind() != tok::r_brace &&
633 Tok.getKind() != tok::eof) {
634 // Each iteration of this loop reads one objc-instance-variable-decl.
635
636 // Check for extraneous top-level semicolon.
637 if (Tok.getKind() == tok::semi) {
638 Diag(Tok, diag::ext_extra_struct_semi);
639 ConsumeToken();
640 continue;
641 }
642 // Set the default visibility to private.
Steve Naroffc4474992007-08-21 21:17:12 +0000643 if (Tok.getKind() == tok::at) { // parse objc-visibility-spec
644 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000645 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000646 case tok::objc_private:
647 case tok::objc_public:
648 case tok::objc_protected:
649 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000650 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000651 ConsumeToken();
652 continue;
653 default:
654 Diag(Tok, diag::err_objc_illegal_visibility_spec);
655 ConsumeToken();
656 continue;
657 }
658 }
659 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000660 for (unsigned i = 0; i < IvarDecls.size(); i++) {
661 AllIvarDecls.push_back(IvarDecls[i]);
662 AllVisibilities.push_back(visibility);
663 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000664 IvarDecls.clear();
665
Steve Naroffc4474992007-08-21 21:17:12 +0000666 if (Tok.getKind() == tok::semi) {
667 ConsumeToken();
668 } else if (Tok.getKind() == tok::r_brace) {
669 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
670 break;
671 } else {
672 Diag(Tok, diag::err_expected_semi_decl_list);
673 // Skip to end of block or statement
674 SkipUntil(tok::r_brace, true, true);
675 }
676 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000677 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff0acc9c92007-09-15 18:49:24 +0000678 Actions.ActOnFields(LBraceLoc, interfaceDecl,
679 &AllIvarDecls[0], AllIvarDecls.size(),
680 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000681 }
Steve Naroffc4474992007-08-21 21:17:12 +0000682 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
683 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000684}
Steve Narofffb367882007-08-20 21:31:48 +0000685
686/// objc-protocol-declaration:
687/// objc-protocol-definition
688/// objc-protocol-forward-reference
689///
690/// objc-protocol-definition:
691/// @protocol identifier
692/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000693/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000694/// @end
695///
696/// objc-protocol-forward-reference:
697/// @protocol identifier-list ';'
698///
699/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000700/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000701/// semicolon in the first alternative if objc-protocol-refs are omitted.
702
Steve Naroff72f17fb2007-08-22 22:17:26 +0000703Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000704 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000705 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
706 ConsumeToken(); // the "protocol" identifier
707
708 if (Tok.getKind() != tok::identifier) {
709 Diag(Tok, diag::err_expected_ident); // missing protocol name.
710 return 0;
711 }
712 // Save the protocol name, then consume it.
713 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
714 SourceLocation nameLoc = ConsumeToken();
715
716 if (Tok.getKind() == tok::semi) { // forward declaration.
717 ConsumeToken();
718 return 0; // FIXME: add protocolName
719 }
720 if (Tok.getKind() == tok::comma) { // list of forward declarations.
721 // Parse the list of forward declarations.
722 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
723 ProtocolRefs.push_back(protocolName);
724
725 while (1) {
726 ConsumeToken(); // the ','
727 if (Tok.getKind() != tok::identifier) {
728 Diag(Tok, diag::err_expected_ident);
729 SkipUntil(tok::semi);
730 return 0;
731 }
732 ProtocolRefs.push_back(Tok.getIdentifierInfo());
733 ConsumeToken(); // the identifier
734
735 if (Tok.getKind() != tok::comma)
736 break;
737 }
738 // Consume the ';'.
739 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
740 return 0;
741 return 0; // FIXME
742 }
743 // Last, and definitely not least, parse a protocol declaration.
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000744 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000745 if (Tok.getKind() == tok::less) {
Steve Naroff304ed392007-09-05 23:30:30 +0000746 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000747 return 0;
748 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000749
750 DeclTy *ProtoType = Actions.ObjcStartProtoInterface(AtLoc,
751 protocolName, nameLoc,
752 &ProtocolRefs[0],
753 ProtocolRefs.size());
754 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000755
756 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000757 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000758 ConsumeToken(); // the "end" identifier
759 return 0;
760 }
761 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000762 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000763}
Steve Narofffb367882007-08-20 21:31:48 +0000764
765/// objc-implementation:
766/// objc-class-implementation-prologue
767/// objc-category-implementation-prologue
768///
769/// objc-class-implementation-prologue:
770/// @implementation identifier objc-superclass[opt]
771/// objc-class-instance-variables[opt]
772///
773/// objc-category-implementation-prologue:
774/// @implementation identifier ( identifier )
775
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000776Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
777 SourceLocation atLoc) {
778 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
779 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
780 ConsumeToken(); // the "implementation" identifier
781
782 if (Tok.getKind() != tok::identifier) {
783 Diag(Tok, diag::err_expected_ident); // missing class or category name.
784 return 0;
785 }
786 // We have a class or category name - consume it.
787 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
788
789 if (Tok.getKind() == tok::l_paren) {
790 // we have a category implementation.
791 SourceLocation lparenLoc = ConsumeParen();
792 SourceLocation categoryLoc, rparenLoc;
793 IdentifierInfo *categoryId = 0;
794
795 if (Tok.getKind() == tok::identifier) {
796 categoryId = Tok.getIdentifierInfo();
797 categoryLoc = ConsumeToken();
798 } else {
799 Diag(Tok, diag::err_expected_ident); // missing category name.
800 return 0;
801 }
802 if (Tok.getKind() != tok::r_paren) {
803 Diag(Tok, diag::err_expected_rparen);
804 SkipUntil(tok::r_paren, false); // don't stop at ';'
805 return 0;
806 }
807 rparenLoc = ConsumeParen();
808 return 0;
809 }
810 // We have a class implementation
811 if (Tok.getKind() == tok::colon) {
812 // We have a super class
813 ConsumeToken();
814 if (Tok.getKind() != tok::identifier) {
815 Diag(Tok, diag::err_expected_ident); // missing super class name.
816 return 0;
817 }
818 ConsumeToken(); // Consume super class name
819 }
820 if (Tok.getKind() == tok::l_brace)
Steve Naroff81f1bba2007-09-06 21:24:23 +0000821 ParseObjCClassInstanceVariables(0/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000822
Steve Narofffb367882007-08-20 21:31:48 +0000823 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000824}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000825Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
826 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
827 "ParseObjCAtEndDeclaration(): Expected @end");
828 ConsumeToken(); // the "end" identifier
829
Steve Narofffb367882007-08-20 21:31:48 +0000830 return 0;
831}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000832
833/// compatibility-alias-decl:
834/// @compatibility_alias alias-name class-name ';'
835///
836Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
837 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
838 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
839 ConsumeToken(); // consume compatibility_alias
840 if (Tok.getKind() != tok::identifier) {
841 Diag(Tok, diag::err_expected_ident);
842 return 0;
843 }
844 ConsumeToken(); // consume alias-name
845 if (Tok.getKind() != tok::identifier) {
846 Diag(Tok, diag::err_expected_ident);
847 return 0;
848 }
849 ConsumeToken(); // consume class-name;
850 if (Tok.getKind() != tok::semi)
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000851 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Steve Narofffb367882007-08-20 21:31:48 +0000852 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000853}
854
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000855/// property-synthesis:
856/// @synthesize property-ivar-list ';'
857///
858/// property-ivar-list:
859/// property-ivar
860/// property-ivar-list ',' property-ivar
861///
862/// property-ivar:
863/// identifier
864/// identifier '=' identifier
865///
866Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
867 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
868 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
869 SourceLocation loc = ConsumeToken(); // consume dynamic
870 if (Tok.getKind() != tok::identifier) {
871 Diag(Tok, diag::err_expected_ident);
872 return 0;
873 }
874 while (Tok.getKind() == tok::identifier) {
875 ConsumeToken(); // consume property name
876 if (Tok.getKind() == tok::equal) {
877 // property '=' ivar-name
878 ConsumeToken(); // consume '='
879 if (Tok.getKind() != tok::identifier) {
880 Diag(Tok, diag::err_expected_ident);
881 break;
882 }
883 ConsumeToken(); // consume ivar-name
884 }
885 if (Tok.getKind() != tok::comma)
886 break;
887 ConsumeToken(); // consume ','
888 }
889 if (Tok.getKind() != tok::semi)
890 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
891 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000892}
893
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000894/// property-dynamic:
895/// @dynamic property-list
896///
897/// property-list:
898/// identifier
899/// property-list ',' identifier
900///
901Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
902 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
903 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
904 SourceLocation loc = ConsumeToken(); // consume dynamic
905 if (Tok.getKind() != tok::identifier) {
906 Diag(Tok, diag::err_expected_ident);
907 return 0;
908 }
909 while (Tok.getKind() == tok::identifier) {
910 ConsumeToken(); // consume property name
911 if (Tok.getKind() != tok::comma)
912 break;
913 ConsumeToken(); // consume ','
914 }
915 if (Tok.getKind() != tok::semi)
916 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
917 return 0;
918}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +0000919
920/// objc-throw-statement:
921/// throw expression[opt];
922///
923Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation &atLoc) {
924 ConsumeToken(); // consume throw
925 if (Tok.getKind() != tok::semi) {
926 ExprResult Res = ParseAssignmentExpression();
927 if (Res.isInvalid) {
928 SkipUntil(tok::semi);
929 return 0;
930 }
931 }
932 return 0;
933}
934
935/// objc-try-catch-statement:
936/// @try compound-statement objc-catch-list[opt]
937/// @try compound-statement objc-catch-list[opt] @finally compound-statement
938///
939/// objc-catch-list:
940/// @catch ( parameter-declaration ) compound-statement
941/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
942/// catch-parameter-declaration:
943/// parameter-declaration
944/// '...' [OBJC2]
945///
946Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation &atLoc) {
947 bool catch_or_finally_seen = false;
948 ConsumeToken(); // consume try
949 if (Tok.getKind() != tok::l_brace) {
950 Diag (Tok, diag::err_expected_lbrace);
951 return 0;
952 }
953 StmtResult TryBody = ParseCompoundStatementBody();
954 while (Tok.getKind() == tok::at) {
955 ConsumeToken();
956 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
957 SourceLocation catchLoc = ConsumeToken(); // consume catch
958 if (Tok.getKind() == tok::l_paren) {
959 ConsumeParen();
960 if (Tok.getKind() != tok::ellipsis) {
961 DeclSpec DS;
962 ParseDeclarationSpecifiers(DS);
963 // Parse the parameter-declaration.
964 // FIXME: BlockContext may not be the right context!
965 Declarator ParmDecl(DS, Declarator::BlockContext);
966 ParseDeclarator(ParmDecl);
967 }
968 else
969 ConsumeToken(); // consume '...'
970 ConsumeParen();
971 StmtResult CatchMody = ParseCompoundStatementBody();
972 }
973 else {
974 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
975 return 0;
976 }
977 catch_or_finally_seen = true;
978 }
979 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
980 ConsumeToken(); // consume finally
981 StmtResult FinallyBody = ParseCompoundStatementBody();
982 catch_or_finally_seen = true;
983 break;
984 }
985 }
986 if (!catch_or_finally_seen)
987 Diag(atLoc, diag::err_missing_catch_finally);
988 return 0;
989}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000990
Steve Naroff81f1bba2007-09-06 21:24:23 +0000991/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000992///
993void Parser::ParseObjCInstanceMethodDefinition() {
994 assert(Tok.getKind() == tok::minus &&
995 "ParseObjCInstanceMethodDefinition(): Expected '-'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000996 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000997 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000998 // parse optional ';'
999 if (Tok.getKind() == tok::semi)
1000 ConsumeToken();
1001
1002 if (Tok.getKind() != tok::l_brace) {
1003 Diag (Tok, diag::err_expected_lbrace);
1004 return;
1005 }
1006
1007 StmtResult FnBody = ParseCompoundStatementBody();
1008}
1009
Steve Naroff81f1bba2007-09-06 21:24:23 +00001010/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001011///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001012void Parser::ParseObjCClassMethodDefinition() {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001013 assert(Tok.getKind() == tok::plus &&
1014 "ParseObjCClassMethodDefinition(): Expected '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001015 // FIXME: @optional/@protocol??
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +00001016 ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001017 // parse optional ';'
1018 if (Tok.getKind() == tok::semi)
1019 ConsumeToken();
1020 if (Tok.getKind() != tok::l_brace) {
1021 Diag (Tok, diag::err_expected_lbrace);
1022 return;
1023 }
1024
1025 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001026}
Anders Carlssona66cad42007-08-21 17:43:55 +00001027
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001028Parser::ExprResult Parser::ParseObjCExpression(SourceLocation &AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001029
1030 switch (Tok.getKind()) {
1031 case tok::string_literal: // primary-expression: string-literal
1032 case tok::wide_string_literal:
1033 return ParseObjCStringLiteral();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001034 default:
1035 break;
1036 }
1037
1038 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001039 case tok::objc_encode:
1040 return ParseObjCEncodeExpression();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001041 case tok::objc_protocol:
1042 return ParseObjCProtocolExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001043 default:
1044 Diag(AtLoc, diag::err_unexpected_at);
1045 SkipUntil(tok::semi);
1046 break;
1047 }
1048
1049 return 0;
1050}
1051
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001052/// objc-message-expr:
1053/// '[' objc-receiver objc-message-args ']'
1054///
1055/// objc-receiver:
1056/// expression
1057/// class-name
1058/// type-name
1059///
1060/// objc-message-args:
1061/// objc-selector
1062/// objc-keywordarg-list
1063///
1064/// objc-keywordarg-list:
1065/// objc-keywordarg
1066/// objc-keywordarg-list objc-keywordarg
1067///
1068/// objc-keywordarg:
1069/// selector-name[opt] ':' objc-keywordexpr
1070///
1071/// objc-keywordexpr:
1072/// nonempty-expr-list
1073///
1074/// nonempty-expr-list:
1075/// assignment-expression
1076/// nonempty-expr-list , assignment-expression
1077///
1078Parser::ExprResult Parser::ParseObjCMessageExpression() {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001079 assert(Tok.getKind() == tok::l_square && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001080 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001081 IdentifierInfo *ReceiverName = 0;
1082 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001083 // Parse receiver
Steve Narofff0c31dd2007-09-16 16:16:00 +00001084 if (Tok.getKind() == tok::identifier &&
Steve Naroff253118b2007-09-17 20:25:27 +00001085 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1086 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001087 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001088 } else {
1089 ExprResult Res = ParseAssignmentExpression();
1090 if (Res.isInvalid) {
1091 SkipUntil(tok::identifier);
1092 return Res;
1093 }
1094 ReceiverExpr = Res.Val;
1095 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001096 // Parse objc-selector
1097 IdentifierInfo *selIdent = ParseObjCSelector();
Steve Naroff253118b2007-09-17 20:25:27 +00001098 llvm::SmallVector<ObjcKeywordMessage, 12> KeyInfo;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001099 if (Tok.getKind() == tok::colon) {
1100 while (1) {
1101 // Each iteration parses a single keyword argument.
Steve Naroff253118b2007-09-17 20:25:27 +00001102 ObjcKeywordMessage KeyInfoMess;
1103 KeyInfoMess.SelectorName = selIdent;
1104
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001105 if (Tok.getKind() != tok::colon) {
1106 Diag(Tok, diag::err_expected_colon);
1107 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001108 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001109 }
Steve Naroff253118b2007-09-17 20:25:27 +00001110 KeyInfoMess.ColonLoc = ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001111 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001112 ExprResult Res = ParseAssignmentExpression();
1113 if (Res.isInvalid) {
1114 SkipUntil(tok::identifier);
1115 return Res;
1116 }
1117 // We have a valid expression.
1118 KeyInfoMess.KeywordExpr = Res.Val;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001119
Steve Naroff253118b2007-09-17 20:25:27 +00001120 // Rather than call out to the actions, package up the info locally,
1121 // like we do for Declarator.
1122 KeyInfo.push_back(KeyInfoMess);
1123
1124 // Check for another keyword selector.
1125 selIdent = ParseObjCSelector();
1126 if (!selIdent && Tok.getKind() != tok::colon)
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001127 break;
1128 // We have a selector or a colon, continue parsing.
1129 }
1130 // Parse the, optional, argument list, comma separated.
1131 while (Tok.getKind() == tok::comma) {
1132 ConsumeToken();
1133 /// Parse the expression after ','
1134 ParseAssignmentExpression();
1135 }
1136 } else if (!selIdent) {
1137 Diag(Tok, diag::err_expected_ident); // missing selector name.
1138 SkipUntil(tok::semi);
1139 return 0;
1140 }
1141 if (Tok.getKind() != tok::r_square) {
1142 Diag(Tok, diag::err_expected_rsquare);
1143 SkipUntil(tok::semi);
1144 return 0;
1145 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001146 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001147
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001148 if (KeyInfo.size()) {
1149 // We've just parsed a keyword message.
1150 if (ReceiverName)
1151 return Actions.ActOnKeywordMessage(ReceiverName,
Steve Naroffc39ca262007-09-18 23:55:05 +00001152 &KeyInfo[0], KeyInfo.size(),
1153 LBracloc, RBracloc);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001154 return Actions.ActOnKeywordMessage(ReceiverExpr,
Steve Naroffc39ca262007-09-18 23:55:05 +00001155 &KeyInfo[0], KeyInfo.size(),
1156 LBracloc, RBracloc);
Steve Naroffd3f5ee42007-09-17 21:01:15 +00001157 }
1158 // We've just parsed a unary message (a message with no arguments).
Steve Naroff253118b2007-09-17 20:25:27 +00001159 if (ReceiverName)
Steve Naroffc39ca262007-09-18 23:55:05 +00001160 return Actions.ActOnUnaryMessage(ReceiverName, selIdent, LBracloc,RBracloc);
1161 return Actions.ActOnUnaryMessage(ReceiverExpr, selIdent, LBracloc,RBracloc);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001162}
1163
Anders Carlssona66cad42007-08-21 17:43:55 +00001164Parser::ExprResult Parser::ParseObjCStringLiteral() {
1165 ExprResult Res = ParseStringLiteralExpression();
1166
1167 if (Res.isInvalid) return Res;
1168
1169 return Actions.ParseObjCStringLiteral(Res.Val);
1170}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001171
1172/// objc-encode-expression:
1173/// @encode ( type-name )
1174Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff87c329f2007-08-23 18:16:40 +00001175 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001176
1177 SourceLocation EncLoc = ConsumeToken();
1178
1179 if (Tok.getKind() != tok::l_paren) {
1180 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1181 return true;
1182 }
1183
1184 SourceLocation LParenLoc = ConsumeParen();
1185
1186 TypeTy *Ty = ParseTypeName();
1187
Anders Carlsson92faeb82007-08-23 15:31:37 +00001188 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001189
1190 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001191 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001192}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001193
1194/// objc-protocol-expression
1195/// @protocol ( protocol-name )
1196
1197Parser::ExprResult Parser::ParseObjCProtocolExpression()
1198{
1199 SourceLocation ProtoLoc = ConsumeToken();
1200
1201 if (Tok.getKind() != tok::l_paren) {
1202 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1203 return true;
1204 }
1205
1206 SourceLocation LParenLoc = ConsumeParen();
1207
1208 if (Tok.getKind() != tok::identifier) {
1209 Diag(Tok, diag::err_expected_ident);
1210 return true;
1211 }
1212
1213 // FIXME: Do something with the protocol name
1214 ConsumeToken();
1215
Anders Carlsson92faeb82007-08-23 15:31:37 +00001216 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001217
1218 // FIXME
1219 return 0;
1220}