blob: 2fd4446ae6d14689de1c9f1ea1739a96595eeb45 [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) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000065 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000066 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
Chris Lattnera1d2bb72007-10-09 17:51:17 +000073 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000074 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 Naroff415c1832007-10-10 17:32:04 +000083 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000084 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Steve Narofffb367882007-08-20 21:31:48 +000087///
88/// objc-interface:
89/// objc-class-interface-attributes[opt] objc-class-interface
90/// objc-category-interface
91///
92/// objc-class-interface:
93/// '@' 'interface' identifier objc-superclass[opt]
94/// objc-protocol-refs[opt]
95/// objc-class-instance-variables[opt]
96/// objc-interface-decl-list
97/// @end
98///
99/// objc-category-interface:
100/// '@' 'interface' identifier '(' identifier[opt] ')'
101/// objc-protocol-refs[opt]
102/// objc-interface-decl-list
103/// @end
104///
105/// objc-superclass:
106/// ':' identifier
107///
108/// objc-class-interface-attributes:
109/// __attribute__((visibility("default")))
110/// __attribute__((visibility("hidden")))
111/// __attribute__((deprecated))
112/// __attribute__((unavailable))
113/// __attribute__((objc_exception)) - used by NSException on 64-bit
114///
115Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
116 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000121 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000122 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
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000129 if (Tok.is(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).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000137 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 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000144 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.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000150 if (Tok.is(tok::less)) {
Steve Naroff304ed392007-09-05 23:30:30 +0000151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000152 return 0;
153 }
154 if (attrList) // categories don't support attributes.
155 Diag(Tok, diag::err_objc_no_attributes_on_category);
156
Steve Naroff415c1832007-10-10 17:32:04 +0000157 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000158 nameId, nameLoc, categoryId, categoryLoc,
159 &ProtocolRefs[0], ProtocolRefs.size());
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000160
161 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000162
Steve Naroff0bbffd82007-08-22 16:35:03 +0000163 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000164 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000165 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000166 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000167 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000168 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000169 return 0;
170 }
171 // Parse a class interface.
172 IdentifierInfo *superClassId = 0;
173 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000174
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000175 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000176 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000177 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000178 Diag(Tok, diag::err_expected_ident); // missing super class name.
179 return 0;
180 }
181 superClassId = Tok.getIdentifierInfo();
182 superClassLoc = ConsumeToken();
183 }
184 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000185 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000186 if (Tok.is(tok::less)) {
Steve Naroff304ed392007-09-05 23:30:30 +0000187 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Narofffb367882007-08-20 21:31:48 +0000188 return 0;
189 }
Steve Naroff415c1832007-10-10 17:32:04 +0000190 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000191 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000192 superClassId, superClassLoc, &ProtocolRefs[0],
193 ProtocolRefs.size(), attrList);
194
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff81f1bba2007-09-06 21:24:23 +0000196 ParseObjCClassInstanceVariables(ClsType);
Steve Narofffb367882007-08-20 21:31:48 +0000197
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000199
200 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000201 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000202 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000203 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000204 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000206 return 0;
207}
208
209/// objc-interface-decl-list:
210/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000211/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000212/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000213/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000214/// objc-interface-decl-list declaration
215/// objc-interface-decl-list ';'
216///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000217/// objc-method-requirement: [OBJC2]
218/// @required
219/// @optional
220///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000221void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
222 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000223 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000224 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000225 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000226 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000227 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000228 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000229
230 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000231 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 } else if (ocKind == tok::objc_required) { // protocols only
233 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000234 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000235 if (contextKey != tok::objc_protocol)
236 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000237 } else if (ocKind == tok::objc_optional) { // protocols only
238 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000239 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000240 if (contextKey != tok::objc_protocol)
241 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000242 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000243 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000244 continue;
245 } else {
246 Diag(Tok, diag::err_objc_illegal_interface_qual);
247 ConsumeToken();
248 }
249 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000250 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
251 DeclTy *methodPrototype =
252 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 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000259 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000260 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000261 else if (Tok.is(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 }
Steve Naroff8255b042007-10-14 00:58:41 +0000269 if (allMethods.size())
270 /// Insert collected methods declarations into the @interface object.
271 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl,
272 &allMethods[0], allMethods.size());
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();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000300 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000301 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000302 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000303 if (II == ObjcPropertyAttrs[objc_setter]) {
304 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000305 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000306 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
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000325 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000326 loc = ConsumeToken();
327 continue;
328 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000329 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000330 break;
331 Diag(loc, diag::err_expected_rparen);
332 SkipUntil(tok::semi);
333 return;
334 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000335 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000336 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.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000352 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000353 // 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);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000359 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000360 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) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000379 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000380
381 tok::TokenKind methodType = Tok.getKind();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000382 ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000383
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000384 DeclTy *MDecl = ParseObjCMethodDecl(methodType, 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///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000398IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000399 switch (Tok.getKind()) {
400 default:
401 return 0;
402 case tok::identifier:
403 case tok::kw_typeof:
404 case tok::kw___alignof:
405 case tok::kw_auto:
406 case tok::kw_break:
407 case tok::kw_case:
408 case tok::kw_char:
409 case tok::kw_const:
410 case tok::kw_continue:
411 case tok::kw_default:
412 case tok::kw_do:
413 case tok::kw_double:
414 case tok::kw_else:
415 case tok::kw_enum:
416 case tok::kw_extern:
417 case tok::kw_float:
418 case tok::kw_for:
419 case tok::kw_goto:
420 case tok::kw_if:
421 case tok::kw_inline:
422 case tok::kw_int:
423 case tok::kw_long:
424 case tok::kw_register:
425 case tok::kw_restrict:
426 case tok::kw_return:
427 case tok::kw_short:
428 case tok::kw_signed:
429 case tok::kw_sizeof:
430 case tok::kw_static:
431 case tok::kw_struct:
432 case tok::kw_switch:
433 case tok::kw_typedef:
434 case tok::kw_union:
435 case tok::kw_unsigned:
436 case tok::kw_void:
437 case tok::kw_volatile:
438 case tok::kw_while:
439 case tok::kw__Bool:
440 case tok::kw__Complex:
441 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000442 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000443 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000444 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000445}
446
Steve Naroffa8ee2262007-08-22 23:18:22 +0000447/// objc-type-qualifier: one of
448/// in out inout bycopy byref oneway
449///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000450bool Parser::isObjCTypeQualifier() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000451 if (Tok.is(tok::identifier)) {
Chris Lattner32352462007-08-29 22:54:08 +0000452 const IdentifierInfo *II = Tok.getIdentifierInfo();
453 for (unsigned i = 0; i < objc_NumQuals; ++i)
454 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000455 }
456 return false;
457}
458
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000459/// property-attrlist: one of
460/// readonly getter setter assign retain copy nonatomic
461///
462bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000463 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000464 const IdentifierInfo *II = Tok.getIdentifierInfo();
465 for (unsigned i = 0; i < objc_NumAttrs; ++i)
466 if (II == ObjcPropertyAttrs[i]) return true;
467 }
468 return false;
469}
470
Steve Naroff0bbffd82007-08-22 16:35:03 +0000471/// objc-type-name:
472/// '(' objc-type-qualifiers[opt] type-name ')'
473/// '(' objc-type-qualifiers[opt] ')'
474///
475/// objc-type-qualifiers:
476/// objc-type-qualifier
477/// objc-type-qualifiers objc-type-qualifier
478///
Steve Naroff304ed392007-09-05 23:30:30 +0000479Parser::TypeTy *Parser::ParseObjCTypeName() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000480 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000481
482 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000483 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000484
Steve Naroffa8ee2262007-08-22 23:18:22 +0000485 while (isObjCTypeQualifier())
486 ConsumeToken();
487
Steve Naroff0bbffd82007-08-22 16:35:03 +0000488 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000489 Ty = ParseTypeName();
490 // FIXME: back when Sema support is in place...
491 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000492 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000493 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000494 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000495 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000496 }
497 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000498 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000499}
500
501/// objc-method-decl:
502/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000503/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000504/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000505/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000506///
507/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000508/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000509/// objc-keyword-selector objc-keyword-decl
510///
511/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000512/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
513/// objc-selector ':' objc-keyword-attributes[opt] identifier
514/// ':' objc-type-name objc-keyword-attributes[opt] identifier
515/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000516///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000517/// objc-parmlist:
518/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000519///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000520/// objc-parms:
521/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000522///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000523/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000524/// , ...
525///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000526/// objc-keyword-attributes: [OBJC2]
527/// __attribute__((unused))
528///
Steve Naroff4ed9d662007-09-27 14:38:14 +0000529Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000530 tok::ObjCKeywordKind MethodImplKind)
531{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000532 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000533 TypeTy *ReturnType = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000534 if (Tok.is(tok::l_paren))
Steve Naroff304ed392007-09-05 23:30:30 +0000535 ReturnType = ParseObjCTypeName();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000536 SourceLocation mLoc;
537 IdentifierInfo *SelIdent = ParseObjCSelector(mLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000538 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000539 if (!SelIdent) {
540 Diag(Tok, diag::err_expected_ident); // missing selector name.
541 // FIXME: this creates a unary selector with a null identifier, is this
542 // ok?? Maybe we should skip to the next semicolon or something.
543 }
544
545 // If attributes exist after the method, parse them.
546 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000547 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000548 MethodAttrs = ParseAttributes();
549
550 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
551 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
552 0, 0, MethodAttrs, MethodImplKind);
553 }
Steve Naroff304ed392007-09-05 23:30:30 +0000554
Steve Naroff4ed9d662007-09-27 14:38:14 +0000555 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
556 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
557 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000558
559 Action::TypeTy *TypeInfo;
560 while (1) {
561 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000562
Chris Lattnerd031a452007-10-07 02:00:24 +0000563 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000564 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000565 Diag(Tok, diag::err_expected_colon);
566 break;
567 }
568 ConsumeToken(); // Eat the ':'.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000569 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000570 TypeInfo = ParseObjCTypeName();
571 else
572 TypeInfo = 0;
573 KeyTypes.push_back(TypeInfo);
Steve Naroff304ed392007-09-05 23:30:30 +0000574
Chris Lattnerd031a452007-10-07 02:00:24 +0000575 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000576 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000577 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000578
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000579 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000580 Diag(Tok, diag::err_expected_ident); // missing argument name.
581 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000582 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000583 ArgNames.push_back(Tok.getIdentifierInfo());
584 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000585
Chris Lattnerd031a452007-10-07 02:00:24 +0000586 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000587 SourceLocation Loc;
588 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000589 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000590 break;
591 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000592 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000593
594 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000595 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000596 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000597 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000598 ConsumeToken();
599 break;
600 }
601 // Parse the c-style argument declaration-specifier.
602 DeclSpec DS;
603 ParseDeclarationSpecifiers(DS);
604 // Parse the declarator.
605 Declarator ParmDecl(DS, Declarator::PrototypeContext);
606 ParseDeclarator(ParmDecl);
607 }
608
609 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000610 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000611 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000612 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000613 MethodAttrs = ParseAttributes();
614
615 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
616 &KeyIdents[0]);
Steve Naroffb4dfe362007-10-02 22:39:18 +0000617 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000618 &KeyTypes[0], &ArgNames[0],
619 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000620}
621
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000622/// CmpProtocolVals - Comparison predicate for sorting protocols.
623static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
624 const IdentifierInfo* const& rhs) {
625 return strcmp(lhs->getName(), rhs->getName()) < 0;
626}
627
Steve Narofffb367882007-08-20 21:31:48 +0000628/// objc-protocol-refs:
629/// '<' identifier-list '>'
630///
Steve Naroff304ed392007-09-05 23:30:30 +0000631bool Parser::ParseObjCProtocolReferences(
632 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000633 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000634
635 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000636
637 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000638 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000639 Diag(Tok, diag::err_expected_ident);
640 SkipUntil(tok::greater);
641 return true;
642 }
643 ProtocolRefs.push_back(Tok.getIdentifierInfo());
644 ConsumeToken();
645
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000646 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000647 break;
648 ConsumeToken();
649 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000650
651 // Sort protocols, keyed by name.
652 // Later on, we remove duplicates.
653 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
654
655 // Make protocol names unique.
656 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
657 ProtocolRefs.end());
658
Steve Narofffb367882007-08-20 21:31:48 +0000659 // Consume the '>'.
660 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
661}
662
663/// objc-class-instance-variables:
664/// '{' objc-instance-variable-decl-list[opt] '}'
665///
666/// objc-instance-variable-decl-list:
667/// objc-visibility-spec
668/// objc-instance-variable-decl ';'
669/// ';'
670/// objc-instance-variable-decl-list objc-visibility-spec
671/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
672/// objc-instance-variable-decl-list ';'
673///
674/// objc-visibility-spec:
675/// @private
676/// @protected
677/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000678/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000679///
680/// objc-instance-variable-decl:
681/// struct-declaration
682///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000683void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000684 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000685 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000686 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
687 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000688
689 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000690
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000691 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000692 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000693 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000694 // Each iteration of this loop reads one objc-instance-variable-decl.
695
696 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000697 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000698 Diag(Tok, diag::ext_extra_struct_semi);
699 ConsumeToken();
700 continue;
701 }
702 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000703 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000704 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000705 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000706 case tok::objc_private:
707 case tok::objc_public:
708 case tok::objc_protected:
709 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000710 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000711 ConsumeToken();
712 continue;
713 default:
714 Diag(Tok, diag::err_objc_illegal_visibility_spec);
715 ConsumeToken();
716 continue;
717 }
718 }
719 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000720 for (unsigned i = 0; i < IvarDecls.size(); i++) {
721 AllIvarDecls.push_back(IvarDecls[i]);
722 AllVisibilities.push_back(visibility);
723 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000724 IvarDecls.clear();
725
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000726 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000727 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000728 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000729 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
730 break;
731 } else {
732 Diag(Tok, diag::err_expected_semi_decl_list);
733 // Skip to end of block or statement
734 SkipUntil(tok::r_brace, true, true);
735 }
736 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000737 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000738 Actions.ActOnFields(CurScope, LBraceLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000739 &AllIvarDecls[0], AllIvarDecls.size(),
740 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000741 }
Steve Naroffc4474992007-08-21 21:17:12 +0000742 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
743 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000744}
Steve Narofffb367882007-08-20 21:31:48 +0000745
746/// objc-protocol-declaration:
747/// objc-protocol-definition
748/// objc-protocol-forward-reference
749///
750/// objc-protocol-definition:
751/// @protocol identifier
752/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000753/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000754/// @end
755///
756/// objc-protocol-forward-reference:
757/// @protocol identifier-list ';'
758///
759/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000760/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000761/// semicolon in the first alternative if objc-protocol-refs are omitted.
762
Steve Naroff72f17fb2007-08-22 22:17:26 +0000763Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000764 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000765 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
766 ConsumeToken(); // the "protocol" identifier
767
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000768 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000769 Diag(Tok, diag::err_expected_ident); // missing protocol name.
770 return 0;
771 }
772 // Save the protocol name, then consume it.
773 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
774 SourceLocation nameLoc = ConsumeToken();
775
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000776 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000777 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000778 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000779 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000780 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000781 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000782 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000783 ProtocolRefs.push_back(protocolName);
784
785 while (1) {
786 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000787 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000788 Diag(Tok, diag::err_expected_ident);
789 SkipUntil(tok::semi);
790 return 0;
791 }
792 ProtocolRefs.push_back(Tok.getIdentifierInfo());
793 ConsumeToken(); // the identifier
794
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000795 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000796 break;
797 }
798 // Consume the ';'.
799 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
800 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000801 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000802 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000803 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000804 &ProtocolRefs[0],
805 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000806 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000807 if (Tok.is(tok::less)) {
Steve Naroff304ed392007-09-05 23:30:30 +0000808 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000809 return 0;
810 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000811
Steve Naroff415c1832007-10-10 17:32:04 +0000812 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000813 protocolName, nameLoc,
814 &ProtocolRefs[0],
815 ProtocolRefs.size());
816 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000817
818 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000819 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000820 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000821 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000822 }
823 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000824 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000825}
Steve Narofffb367882007-08-20 21:31:48 +0000826
827/// objc-implementation:
828/// objc-class-implementation-prologue
829/// objc-category-implementation-prologue
830///
831/// objc-class-implementation-prologue:
832/// @implementation identifier objc-superclass[opt]
833/// objc-class-instance-variables[opt]
834///
835/// objc-category-implementation-prologue:
836/// @implementation identifier ( identifier )
837
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000838Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
839 SourceLocation atLoc) {
840 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
841 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
842 ConsumeToken(); // the "implementation" identifier
843
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000844 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000845 Diag(Tok, diag::err_expected_ident); // missing class or category name.
846 return 0;
847 }
848 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000849 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000850 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
851
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000852 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000853 // we have a category implementation.
854 SourceLocation lparenLoc = ConsumeParen();
855 SourceLocation categoryLoc, rparenLoc;
856 IdentifierInfo *categoryId = 0;
857
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000858 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000859 categoryId = Tok.getIdentifierInfo();
860 categoryLoc = ConsumeToken();
861 } else {
862 Diag(Tok, diag::err_expected_ident); // missing category name.
863 return 0;
864 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000865 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000866 Diag(Tok, diag::err_expected_rparen);
867 SkipUntil(tok::r_paren, false); // don't stop at ';'
868 return 0;
869 }
870 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000871 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000872 atLoc, nameId, nameLoc, categoryId,
873 categoryLoc);
874 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000875 }
876 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000877 SourceLocation superClassLoc;
878 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000879 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000880 // We have a super class
881 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000882 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000883 Diag(Tok, diag::err_expected_ident); // missing super class name.
884 return 0;
885 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000886 superClassId = Tok.getIdentifierInfo();
887 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000888 }
Steve Naroff415c1832007-10-10 17:32:04 +0000889 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000890 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000891 superClassId, superClassLoc);
892
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000893 if (Tok.is(tok::l_brace))
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000894 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000895
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000896 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000897}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000898Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
899 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
900 "ParseObjCAtEndDeclaration(): Expected @end");
901 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000902 if (ObjcImpDecl) {
903 // Checking is not necessary except that a parse error might have caused
904 // @implementation not to have been parsed to completion and ObjcImpDecl
905 // could be 0.
906 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000907 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
908 &AllImplMethods[0],AllImplMethods.size());
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000909 ObjcImpDecl = 0;
910 AllImplMethods.clear();
911 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000912
Steve Narofffb367882007-08-20 21:31:48 +0000913 return 0;
914}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000915
916/// compatibility-alias-decl:
917/// @compatibility_alias alias-name class-name ';'
918///
919Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
920 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
921 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
922 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000923 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000924 Diag(Tok, diag::err_expected_ident);
925 return 0;
926 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000927 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
928 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000929 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000930 Diag(Tok, diag::err_expected_ident);
931 return 0;
932 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000933 IdentifierInfo *classId = Tok.getIdentifierInfo();
934 SourceLocation classLoc = ConsumeToken(); // consume class-name;
935 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000936 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000937 return 0;
938 }
939 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
940 aliasId, aliasLoc,
941 classId, classLoc);
942 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000943}
944
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000945/// property-synthesis:
946/// @synthesize property-ivar-list ';'
947///
948/// property-ivar-list:
949/// property-ivar
950/// property-ivar-list ',' property-ivar
951///
952/// property-ivar:
953/// identifier
954/// identifier '=' identifier
955///
956Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
957 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
958 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
959 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000960 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000961 Diag(Tok, diag::err_expected_ident);
962 return 0;
963 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000964 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000965 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000966 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000967 // property '=' ivar-name
968 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000969 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000970 Diag(Tok, diag::err_expected_ident);
971 break;
972 }
973 ConsumeToken(); // consume ivar-name
974 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000975 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000976 break;
977 ConsumeToken(); // consume ','
978 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000979 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000980 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
981 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000982}
983
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000984/// property-dynamic:
985/// @dynamic property-list
986///
987/// property-list:
988/// identifier
989/// property-list ',' identifier
990///
991Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
992 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
993 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
994 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000995 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000996 Diag(Tok, diag::err_expected_ident);
997 return 0;
998 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000999 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001000 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001001 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001002 break;
1003 ConsumeToken(); // consume ','
1004 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001005 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001006 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1007 return 0;
1008}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001009
1010/// objc-throw-statement:
1011/// throw expression[opt];
1012///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001013Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001014 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001015 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001016 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001017 if (Res.isInvalid) {
1018 SkipUntil(tok::semi);
1019 return 0;
1020 }
1021 }
1022 return 0;
1023}
1024
1025/// objc-try-catch-statement:
1026/// @try compound-statement objc-catch-list[opt]
1027/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1028///
1029/// objc-catch-list:
1030/// @catch ( parameter-declaration ) compound-statement
1031/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1032/// catch-parameter-declaration:
1033/// parameter-declaration
1034/// '...' [OBJC2]
1035///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001036Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001037 bool catch_or_finally_seen = false;
1038 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001039 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001040 Diag (Tok, diag::err_expected_lbrace);
1041 return 0;
1042 }
1043 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001044 while (Tok.is(tok::at)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001045 ConsumeToken();
1046 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1047 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001048 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001049 ConsumeParen();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001050 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001051 DeclSpec DS;
1052 ParseDeclarationSpecifiers(DS);
1053 // Parse the parameter-declaration.
1054 // FIXME: BlockContext may not be the right context!
1055 Declarator ParmDecl(DS, Declarator::BlockContext);
1056 ParseDeclarator(ParmDecl);
1057 }
1058 else
1059 ConsumeToken(); // consume '...'
1060 ConsumeParen();
1061 StmtResult CatchMody = ParseCompoundStatementBody();
1062 }
1063 else {
1064 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1065 return 0;
1066 }
1067 catch_or_finally_seen = true;
1068 }
1069 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1070 ConsumeToken(); // consume finally
1071 StmtResult FinallyBody = ParseCompoundStatementBody();
1072 catch_or_finally_seen = true;
1073 break;
1074 }
1075 }
1076 if (!catch_or_finally_seen)
1077 Diag(atLoc, diag::err_missing_catch_finally);
1078 return 0;
1079}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001080
Steve Naroff81f1bba2007-09-06 21:24:23 +00001081/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001082///
1083void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001084 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1085
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001086 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001087 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001088 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001089 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001090 ConsumeToken();
1091
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001092 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001093 Diag (Tok, diag::err_expected_lbrace);
1094 return;
1095 }
1096
1097 StmtResult FnBody = ParseCompoundStatementBody();
1098}
1099
Steve Naroff81f1bba2007-09-06 21:24:23 +00001100/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001101///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001102void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001103 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001104 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001105 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001106 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001107 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001108 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001109 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001110 Diag (Tok, diag::err_expected_lbrace);
1111 return;
1112 }
1113
1114 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001115}
Anders Carlssona66cad42007-08-21 17:43:55 +00001116
Steve Narofffb9dd752007-10-15 20:55:58 +00001117Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001118
1119 switch (Tok.getKind()) {
1120 case tok::string_literal: // primary-expression: string-literal
1121 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001122 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001123 default:
1124 break;
1125 }
1126
1127 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001128 case tok::objc_encode:
1129 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1130 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001131 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001132 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001133 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001134 default:
1135 Diag(AtLoc, diag::err_unexpected_at);
1136 SkipUntil(tok::semi);
1137 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001138 }
1139
1140 return 0;
1141}
1142
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001143/// objc-message-expr:
1144/// '[' objc-receiver objc-message-args ']'
1145///
1146/// objc-receiver:
1147/// expression
1148/// class-name
1149/// type-name
1150///
1151/// objc-message-args:
1152/// objc-selector
1153/// objc-keywordarg-list
1154///
1155/// objc-keywordarg-list:
1156/// objc-keywordarg
1157/// objc-keywordarg-list objc-keywordarg
1158///
1159/// objc-keywordarg:
1160/// selector-name[opt] ':' objc-keywordexpr
1161///
1162/// objc-keywordexpr:
1163/// nonempty-expr-list
1164///
1165/// nonempty-expr-list:
1166/// assignment-expression
1167/// nonempty-expr-list , assignment-expression
1168///
1169Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001170 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001171 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001172 IdentifierInfo *ReceiverName = 0;
1173 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001174 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001175 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001176 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1177 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001178 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001179 } else {
1180 ExprResult Res = ParseAssignmentExpression();
1181 if (Res.isInvalid) {
1182 SkipUntil(tok::identifier);
1183 return Res;
1184 }
1185 ReceiverExpr = Res.Val;
1186 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001187 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001188 SourceLocation Loc;
1189 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001190
1191 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1192 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1193
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001194 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001195 while (1) {
1196 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001197 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001198
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001199 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001200 Diag(Tok, diag::err_expected_colon);
1201 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001202 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001203 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001204 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001205 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001206 ExprResult Res = ParseAssignmentExpression();
1207 if (Res.isInvalid) {
1208 SkipUntil(tok::identifier);
1209 return Res;
1210 }
1211 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001212 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001213
1214 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001215 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001216 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001217 break;
1218 // We have a selector or a colon, continue parsing.
1219 }
1220 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001221 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001222 ConsumeToken();
1223 /// Parse the expression after ','
1224 ParseAssignmentExpression();
1225 }
1226 } else if (!selIdent) {
1227 Diag(Tok, diag::err_expected_ident); // missing selector name.
1228 SkipUntil(tok::semi);
1229 return 0;
1230 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001231
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001232 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001233 Diag(Tok, diag::err_expected_rsquare);
1234 SkipUntil(tok::semi);
1235 return 0;
1236 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001237 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001238
Steve Narofff9e80db2007-10-05 18:42:47 +00001239 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001240 if (nKeys == 0)
1241 KeyIdents.push_back(selIdent);
1242 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1243
1244 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001245 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001246 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1247 &KeyExprs[0]);
1248 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1249 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001250}
1251
Anders Carlssona66cad42007-08-21 17:43:55 +00001252Parser::ExprResult Parser::ParseObjCStringLiteral() {
1253 ExprResult Res = ParseStringLiteralExpression();
1254
1255 if (Res.isInvalid) return Res;
1256
1257 return Actions.ParseObjCStringLiteral(Res.Val);
1258}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001259
1260/// objc-encode-expression:
1261/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001262Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001263 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001264
1265 SourceLocation EncLoc = ConsumeToken();
1266
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001267 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001268 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1269 return true;
1270 }
1271
1272 SourceLocation LParenLoc = ConsumeParen();
1273
1274 TypeTy *Ty = ParseTypeName();
1275
Anders Carlsson92faeb82007-08-23 15:31:37 +00001276 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001277
Chris Lattnercfd61c82007-10-16 22:51:17 +00001278 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001279 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001280}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001281
1282/// objc-protocol-expression
1283/// @protocol ( protocol-name )
1284
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001285Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001286{
1287 SourceLocation ProtoLoc = ConsumeToken();
1288
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001289 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001290 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1291 return true;
1292 }
1293
1294 SourceLocation LParenLoc = ConsumeParen();
1295
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001296 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001297 Diag(Tok, diag::err_expected_ident);
1298 return true;
1299 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001300 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001301 ConsumeToken();
1302
Anders Carlsson92faeb82007-08-23 15:31:37 +00001303 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001304
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001305 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1306 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001307}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001308
1309/// objc-selector-expression
1310/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001311Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001312{
1313 SourceLocation SelectorLoc = ConsumeToken();
1314
1315 if (Tok.isNot(tok::l_paren)) {
1316 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1317 return 0;
1318 }
1319
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001320 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001321 SourceLocation LParenLoc = ConsumeParen();
1322 SourceLocation sLoc;
1323 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1324 if (!SelIdent && Tok.isNot(tok::colon)) {
1325
1326 Diag(Tok, diag::err_expected_ident); // missing selector name.
1327 return 0;
1328 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001329 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001330 if (Tok.isNot(tok::r_paren))
1331 while (1) {
1332 if (Tok.isNot(tok::colon)) {
1333 Diag(Tok, diag::err_expected_colon);
1334 break;
1335 }
1336 ConsumeToken(); // Eat the ':'.
1337 if (Tok.is(tok::r_paren))
1338 break;
1339 // Check for another keyword selector.
1340 SourceLocation Loc;
1341 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001342 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001343 if (!SelIdent && Tok.isNot(tok::colon))
1344 break;
1345 }
1346 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001347 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1348 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001349 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001350 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001351 }