blob: bfd6797c964c95db6d279b9aea8cd3dcdd37c25c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroffdac269b2007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff861cf3e2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Naroffdac269b2007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000037 case tok::objc_protocol:
Steve Naroff7ef58fd2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000039 case tok::objc_implementation:
Steve Naroff3536b442007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000041 case tok::objc_end:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000043 case tok::objc_compatibility_alias:
Fariborz Jahaniane992af02007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000052 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Naroffdac269b2007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000065 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000066 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000068 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 }
Reid Spencer5f016e22007-07-11 17:01:13 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
Chris Lattnerdf195262007-10-09 17:51:17 +000073 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000074 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Naroffdac269b2007-08-20 21:31:48 +000081 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000082
Steve Naroffe440eb82007-10-10 17:32:04 +000083 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000084 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
Steve Naroffdac269b2007-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 Naroff861cf3e2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
Chris Lattnerdf195262007-10-09 17:51:17 +0000121 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-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 Naroff7ef58fd2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Chris Lattnerdf195262007-10-09 17:51:17 +0000129 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000133 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffdac269b2007-08-20 21:31:48 +0000134
Steve Naroff527fe232007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-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 Lattnerdf195262007-10-09 17:51:17 +0000150 if (Tok.is(tok::less)) {
Steve Narofff28b2642007-09-05 23:30:30 +0000151 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroffdac269b2007-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 Naroffe440eb82007-10-10 17:32:04 +0000157 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000158 nameId, nameLoc, categoryId, categoryLoc,
159 &ProtocolRefs[0], ProtocolRefs.size());
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000160
161 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Naroffdac269b2007-08-20 21:31:48 +0000162
Steve Naroff294494e2007-08-22 16:35:03 +0000163 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000164 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000165 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000166 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000167 }
Steve Naroff294494e2007-08-22 16:35:03 +0000168 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000169 return 0;
170 }
171 // Parse a class interface.
172 IdentifierInfo *superClassId = 0;
173 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000174
Chris Lattnerdf195262007-10-09 17:51:17 +0000175 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000176 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000177 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-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 Narofff28b2642007-09-05 23:30:30 +0000185 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000186 if (Tok.is(tok::less)) {
Steve Narofff28b2642007-09-05 23:30:30 +0000187 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroffdac269b2007-08-20 21:31:48 +0000188 return 0;
189 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000190 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000191 atLoc, nameId, nameLoc,
Steve Narofff28b2642007-09-05 23:30:30 +0000192 superClassId, superClassLoc, &ProtocolRefs[0],
193 ProtocolRefs.size(), attrList);
194
Chris Lattnerdf195262007-10-09 17:51:17 +0000195 if (Tok.is(tok::l_brace))
Steve Naroff3536b442007-09-06 21:24:23 +0000196 ParseObjCClassInstanceVariables(ClsType);
Steve Naroffdac269b2007-08-20 21:31:48 +0000197
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000198 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff294494e2007-08-22 16:35:03 +0000199
200 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000201 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000202 ConsumeToken(); // the "end" identifier
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000203 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000204 }
Steve Naroff294494e2007-08-22 16:35:03 +0000205 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000206 return 0;
207}
208
209/// objc-interface-decl-list:
210/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000211/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000212/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000213/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000214/// objc-interface-decl-list declaration
215/// objc-interface-decl-list ';'
216///
Steve Naroff294494e2007-08-22 16:35:03 +0000217/// objc-method-requirement: [OBJC2]
218/// @required
219/// @optional
220///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000221void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
222 tok::ObjCKeywordKind contextKey) {
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000223 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000224 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff294494e2007-08-22 16:35:03 +0000225 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000226 if (Tok.is(tok::at)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000227 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff861cf3e2007-08-23 18:16:40 +0000228 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff294494e2007-08-22 16:35:03 +0000229
230 if (ocKind == tok::objc_end) { // terminate list
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000231 break;
Steve Naroff294494e2007-08-22 16:35:03 +0000232 } else if (ocKind == tok::objc_required) { // protocols only
233 ConsumeToken();
Fariborz Jahanian00933592007-09-18 00:25:23 +0000234 MethodImplKind = ocKind;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000235 if (contextKey != tok::objc_protocol)
236 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff294494e2007-08-22 16:35:03 +0000237 } else if (ocKind == tok::objc_optional) { // protocols only
238 ConsumeToken();
Fariborz Jahanian00933592007-09-18 00:25:23 +0000239 MethodImplKind = ocKind;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000240 if (contextKey != tok::objc_protocol)
241 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff294494e2007-08-22 16:35:03 +0000242 } else if (ocKind == tok::objc_property) {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000243 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff294494e2007-08-22 16:35:03 +0000244 continue;
245 } else {
246 Diag(Tok, diag::err_objc_illegal_interface_qual);
247 ConsumeToken();
248 }
249 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000250 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
251 DeclTy *methodPrototype =
252 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000253 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000254 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
255 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000256 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000257 continue;
258 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000259 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000260 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000261 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000262 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000263 else {
Steve Naroff4985ace2007-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 Naroff3536b442007-09-06 21:24:23 +0000266 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000267 }
Steve Naroff294494e2007-08-22 16:35:03 +0000268 }
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000269 /// Insert collected methods declarations into the @interface object.
Steve Naroff3a165b02007-10-03 21:00:46 +0000270 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl,
271 &allMethods[0], allMethods.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000272}
273
Fariborz Jahaniand0f97d12007-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();
Chris Lattnerdf195262007-10-09 17:51:17 +0000299 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000300 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000301 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000302 if (II == ObjcPropertyAttrs[objc_setter]) {
303 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000304 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000305 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
Chris Lattnerdf195262007-10-09 17:51:17 +0000324 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000325 loc = ConsumeToken();
326 continue;
327 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000328 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000329 break;
330 Diag(loc, diag::err_expected_rparen);
331 SkipUntil(tok::semi);
332 return;
333 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000334 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000335 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.
Chris Lattnerdf195262007-10-09 17:51:17 +0000351 if (Tok.is(tok::l_paren)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000352 // 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);
Chris Lattnerdf195262007-10-09 17:51:17 +0000358 if (Tok.is(tok::semi))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000359 ConsumeToken();
360 else {
361 Diag(Tok, diag::err_expected_semi_decl_list);
362 SkipUntil(tok::r_brace, true, true);
363 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
Steve Naroffdac269b2007-08-20 21:31:48 +0000365
Steve Naroff3536b442007-09-06 21:24:23 +0000366/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000367/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000368/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000369///
370/// objc-instance-method: '-'
371/// objc-class-method: '+'
372///
Steve Naroff4985ace2007-08-22 18:35:33 +0000373/// objc-method-attributes: [OBJC2]
374/// __attribute__((deprecated))
375///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000376Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
377 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000378 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000379
380 tok::TokenKind methodType = Tok.getKind();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000381 ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000382
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000383 DeclTy *MDecl = ParseObjCMethodDecl(methodType, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000384 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000385 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000386 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000387}
388
389/// objc-selector:
390/// identifier
391/// one of
392/// enum struct union if else while do for switch case default
393/// break continue return goto asm sizeof typeof __alignof
394/// unsigned long const short volatile signed restrict _Complex
395/// in out inout bycopy byref oneway int char float double void _Bool
396///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000397IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000398 switch (Tok.getKind()) {
399 default:
400 return 0;
401 case tok::identifier:
402 case tok::kw_typeof:
403 case tok::kw___alignof:
404 case tok::kw_auto:
405 case tok::kw_break:
406 case tok::kw_case:
407 case tok::kw_char:
408 case tok::kw_const:
409 case tok::kw_continue:
410 case tok::kw_default:
411 case tok::kw_do:
412 case tok::kw_double:
413 case tok::kw_else:
414 case tok::kw_enum:
415 case tok::kw_extern:
416 case tok::kw_float:
417 case tok::kw_for:
418 case tok::kw_goto:
419 case tok::kw_if:
420 case tok::kw_inline:
421 case tok::kw_int:
422 case tok::kw_long:
423 case tok::kw_register:
424 case tok::kw_restrict:
425 case tok::kw_return:
426 case tok::kw_short:
427 case tok::kw_signed:
428 case tok::kw_sizeof:
429 case tok::kw_static:
430 case tok::kw_struct:
431 case tok::kw_switch:
432 case tok::kw_typedef:
433 case tok::kw_union:
434 case tok::kw_unsigned:
435 case tok::kw_void:
436 case tok::kw_volatile:
437 case tok::kw_while:
438 case tok::kw__Bool:
439 case tok::kw__Complex:
440 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000441 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000442 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000443 }
Steve Naroff294494e2007-08-22 16:35:03 +0000444}
445
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000446/// objc-type-qualifier: one of
447/// in out inout bycopy byref oneway
448///
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000449bool Parser::isObjCTypeQualifier() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000450 if (Tok.is(tok::identifier)) {
Chris Lattner34870da2007-08-29 22:54:08 +0000451 const IdentifierInfo *II = Tok.getIdentifierInfo();
452 for (unsigned i = 0; i < objc_NumQuals; ++i)
453 if (II == ObjcTypeQuals[i]) return true;
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000454 }
455 return false;
456}
457
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000458/// property-attrlist: one of
459/// readonly getter setter assign retain copy nonatomic
460///
461bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000462 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000463 const IdentifierInfo *II = Tok.getIdentifierInfo();
464 for (unsigned i = 0; i < objc_NumAttrs; ++i)
465 if (II == ObjcPropertyAttrs[i]) return true;
466 }
467 return false;
468}
469
Steve Naroff294494e2007-08-22 16:35:03 +0000470/// objc-type-name:
471/// '(' objc-type-qualifiers[opt] type-name ')'
472/// '(' objc-type-qualifiers[opt] ')'
473///
474/// objc-type-qualifiers:
475/// objc-type-qualifier
476/// objc-type-qualifiers objc-type-qualifier
477///
Steve Narofff28b2642007-09-05 23:30:30 +0000478Parser::TypeTy *Parser::ParseObjCTypeName() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000479 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000480
481 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000482 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000483
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000484 while (isObjCTypeQualifier())
485 ConsumeToken();
486
Steve Naroff294494e2007-08-22 16:35:03 +0000487 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000488 Ty = ParseTypeName();
489 // FIXME: back when Sema support is in place...
490 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000491 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000492 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000493 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000494 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000495 }
496 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000497 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000498}
499
500/// objc-method-decl:
501/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000502/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000503/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000504/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000505///
506/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000507/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000508/// objc-keyword-selector objc-keyword-decl
509///
510/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000511/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
512/// objc-selector ':' objc-keyword-attributes[opt] identifier
513/// ':' objc-type-name objc-keyword-attributes[opt] identifier
514/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000515///
Steve Naroff4985ace2007-08-22 18:35:33 +0000516/// objc-parmlist:
517/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000518///
Steve Naroff4985ace2007-08-22 18:35:33 +0000519/// objc-parms:
520/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000521///
Steve Naroff4985ace2007-08-22 18:35:33 +0000522/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000523/// , ...
524///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000525/// objc-keyword-attributes: [OBJC2]
526/// __attribute__((unused))
527///
Steve Naroff68d331a2007-09-27 14:38:14 +0000528Parser::DeclTy *Parser::ParseObjCMethodDecl(tok::TokenKind mType,
Steve Naroff68d331a2007-09-27 14:38:14 +0000529 tok::ObjCKeywordKind MethodImplKind)
530{
Steve Naroff4985ace2007-08-22 18:35:33 +0000531 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000532 TypeTy *ReturnType = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000533 if (Tok.is(tok::l_paren))
Steve Narofff28b2642007-09-05 23:30:30 +0000534 ReturnType = ParseObjCTypeName();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000535 SourceLocation mLoc;
536 IdentifierInfo *SelIdent = ParseObjCSelector(mLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000537 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000538 if (!SelIdent) {
539 Diag(Tok, diag::err_expected_ident); // missing selector name.
540 // FIXME: this creates a unary selector with a null identifier, is this
541 // ok?? Maybe we should skip to the next semicolon or something.
542 }
543
544 // If attributes exist after the method, parse them.
545 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000546 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000547 MethodAttrs = ParseAttributes();
548
549 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
550 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
551 0, 0, MethodAttrs, MethodImplKind);
552 }
Steve Narofff28b2642007-09-05 23:30:30 +0000553
Steve Naroff68d331a2007-09-27 14:38:14 +0000554 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
555 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
556 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000557
558 Action::TypeTy *TypeInfo;
559 while (1) {
560 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000561
Chris Lattnerff384912007-10-07 02:00:24 +0000562 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000563 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000564 Diag(Tok, diag::err_expected_colon);
565 break;
566 }
567 ConsumeToken(); // Eat the ':'.
Chris Lattnerdf195262007-10-09 17:51:17 +0000568 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerff384912007-10-07 02:00:24 +0000569 TypeInfo = ParseObjCTypeName();
570 else
571 TypeInfo = 0;
572 KeyTypes.push_back(TypeInfo);
Steve Narofff28b2642007-09-05 23:30:30 +0000573
Chris Lattnerff384912007-10-07 02:00:24 +0000574 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000575 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000576 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000577
Chris Lattnerdf195262007-10-09 17:51:17 +0000578 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000579 Diag(Tok, diag::err_expected_ident); // missing argument name.
580 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000581 }
Chris Lattnerff384912007-10-07 02:00:24 +0000582 ArgNames.push_back(Tok.getIdentifierInfo());
583 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000584
Chris Lattnerff384912007-10-07 02:00:24 +0000585 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000586 SourceLocation Loc;
587 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000588 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000589 break;
590 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000591 }
Chris Lattnerff384912007-10-07 02:00:24 +0000592
593 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000594 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000595 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000596 if (Tok.is(tok::ellipsis)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000597 ConsumeToken();
598 break;
599 }
600 // Parse the c-style argument declaration-specifier.
601 DeclSpec DS;
602 ParseDeclarationSpecifiers(DS);
603 // Parse the declarator.
604 Declarator ParmDecl(DS, Declarator::PrototypeContext);
605 ParseDeclarator(ParmDecl);
606 }
607
608 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000609 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000610 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000611 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000612 MethodAttrs = ParseAttributes();
613
614 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
615 &KeyIdents[0]);
Steve Naroff37e58d12007-10-02 22:39:18 +0000616 return Actions.ActOnMethodDeclaration(mLoc, mType, ReturnType, Sel,
Chris Lattnerff384912007-10-07 02:00:24 +0000617 &KeyTypes[0], &ArgNames[0],
618 MethodAttrs, MethodImplKind);
Steve Naroff294494e2007-08-22 16:35:03 +0000619}
620
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000621/// CmpProtocolVals - Comparison predicate for sorting protocols.
622static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
623 const IdentifierInfo* const& rhs) {
624 return strcmp(lhs->getName(), rhs->getName()) < 0;
625}
626
Steve Naroffdac269b2007-08-20 21:31:48 +0000627/// objc-protocol-refs:
628/// '<' identifier-list '>'
629///
Steve Narofff28b2642007-09-05 23:30:30 +0000630bool Parser::ParseObjCProtocolReferences(
631 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000632 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000633
634 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000635
636 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000637 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000638 Diag(Tok, diag::err_expected_ident);
639 SkipUntil(tok::greater);
640 return true;
641 }
642 ProtocolRefs.push_back(Tok.getIdentifierInfo());
643 ConsumeToken();
644
Chris Lattnerdf195262007-10-09 17:51:17 +0000645 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000646 break;
647 ConsumeToken();
648 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000649
650 // Sort protocols, keyed by name.
651 // Later on, we remove duplicates.
652 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
653
654 // Make protocol names unique.
655 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
656 ProtocolRefs.end());
657
Steve Naroffdac269b2007-08-20 21:31:48 +0000658 // Consume the '>'.
659 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
660}
661
662/// objc-class-instance-variables:
663/// '{' objc-instance-variable-decl-list[opt] '}'
664///
665/// objc-instance-variable-decl-list:
666/// objc-visibility-spec
667/// objc-instance-variable-decl ';'
668/// ';'
669/// objc-instance-variable-decl-list objc-visibility-spec
670/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
671/// objc-instance-variable-decl-list ';'
672///
673/// objc-visibility-spec:
674/// @private
675/// @protected
676/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000677/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000678///
679/// objc-instance-variable-decl:
680/// struct-declaration
681///
Steve Naroff3536b442007-09-06 21:24:23 +0000682void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000683 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff3536b442007-09-06 21:24:23 +0000684 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000685 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
686 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffddbff782007-08-21 21:17:12 +0000687
688 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000689
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000690 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffddbff782007-08-21 21:17:12 +0000691 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000692 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000693 // Each iteration of this loop reads one objc-instance-variable-decl.
694
695 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000696 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000697 Diag(Tok, diag::ext_extra_struct_semi);
698 ConsumeToken();
699 continue;
700 }
701 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000702 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000703 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000704 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000705 case tok::objc_private:
706 case tok::objc_public:
707 case tok::objc_protected:
708 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000709 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000710 ConsumeToken();
711 continue;
712 default:
713 Diag(Tok, diag::err_objc_illegal_visibility_spec);
714 ConsumeToken();
715 continue;
716 }
717 }
718 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000719 for (unsigned i = 0; i < IvarDecls.size(); i++) {
720 AllIvarDecls.push_back(IvarDecls[i]);
721 AllVisibilities.push_back(visibility);
722 }
Steve Naroff3536b442007-09-06 21:24:23 +0000723 IvarDecls.clear();
724
Chris Lattnerdf195262007-10-09 17:51:17 +0000725 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000726 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000727 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000728 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
729 break;
730 } else {
731 Diag(Tok, diag::err_expected_semi_decl_list);
732 // Skip to end of block or statement
733 SkipUntil(tok::r_brace, true, true);
734 }
735 }
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000736 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Fariborz Jahanian9d048ff2007-09-29 00:54:24 +0000737 Actions.ActOnFields(CurScope, LBraceLoc, interfaceDecl,
Steve Naroff08d92e42007-09-15 18:49:24 +0000738 &AllIvarDecls[0], AllIvarDecls.size(),
739 &AllVisibilities[0]);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000740 }
Steve Naroffddbff782007-08-21 21:17:12 +0000741 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
742 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000743}
Steve Naroffdac269b2007-08-20 21:31:48 +0000744
745/// objc-protocol-declaration:
746/// objc-protocol-definition
747/// objc-protocol-forward-reference
748///
749/// objc-protocol-definition:
750/// @protocol identifier
751/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000752/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000753/// @end
754///
755/// objc-protocol-forward-reference:
756/// @protocol identifier-list ';'
757///
758/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000759/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000760/// semicolon in the first alternative if objc-protocol-refs are omitted.
761
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000762Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000763 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000764 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
765 ConsumeToken(); // the "protocol" identifier
766
Chris Lattnerdf195262007-10-09 17:51:17 +0000767 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000768 Diag(Tok, diag::err_expected_ident); // missing protocol name.
769 return 0;
770 }
771 // Save the protocol name, then consume it.
772 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
773 SourceLocation nameLoc = ConsumeToken();
774
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000775 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000776 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000777 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000778 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000779 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000780 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000781 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000782 ProtocolRefs.push_back(protocolName);
783
784 while (1) {
785 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000786 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000787 Diag(Tok, diag::err_expected_ident);
788 SkipUntil(tok::semi);
789 return 0;
790 }
791 ProtocolRefs.push_back(Tok.getIdentifierInfo());
792 ConsumeToken(); // the identifier
793
Chris Lattnerdf195262007-10-09 17:51:17 +0000794 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000795 break;
796 }
797 // Consume the ';'.
798 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
799 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000800 }
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000801 if (ProtocolRefs.size() > 0)
Steve Naroffe440eb82007-10-10 17:32:04 +0000802 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000803 &ProtocolRefs[0],
804 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000805 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnerdf195262007-10-09 17:51:17 +0000806 if (Tok.is(tok::less)) {
Steve Narofff28b2642007-09-05 23:30:30 +0000807 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000808 return 0;
809 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000810
Steve Naroffe440eb82007-10-10 17:32:04 +0000811 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000812 protocolName, nameLoc,
813 &ProtocolRefs[0],
814 ProtocolRefs.size());
815 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000816
817 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000818 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000819 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000820 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000821 }
822 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000823 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000824}
Steve Naroffdac269b2007-08-20 21:31:48 +0000825
826/// objc-implementation:
827/// objc-class-implementation-prologue
828/// objc-category-implementation-prologue
829///
830/// objc-class-implementation-prologue:
831/// @implementation identifier objc-superclass[opt]
832/// objc-class-instance-variables[opt]
833///
834/// objc-category-implementation-prologue:
835/// @implementation identifier ( identifier )
836
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000837Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
838 SourceLocation atLoc) {
839 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
840 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
841 ConsumeToken(); // the "implementation" identifier
842
Chris Lattnerdf195262007-10-09 17:51:17 +0000843 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000844 Diag(Tok, diag::err_expected_ident); // missing class or category name.
845 return 0;
846 }
847 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000848 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000849 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
850
Chris Lattnerdf195262007-10-09 17:51:17 +0000851 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000852 // we have a category implementation.
853 SourceLocation lparenLoc = ConsumeParen();
854 SourceLocation categoryLoc, rparenLoc;
855 IdentifierInfo *categoryId = 0;
856
Chris Lattnerdf195262007-10-09 17:51:17 +0000857 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000858 categoryId = Tok.getIdentifierInfo();
859 categoryLoc = ConsumeToken();
860 } else {
861 Diag(Tok, diag::err_expected_ident); // missing category name.
862 return 0;
863 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000864 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000865 Diag(Tok, diag::err_expected_rparen);
866 SkipUntil(tok::r_paren, false); // don't stop at ';'
867 return 0;
868 }
869 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000870 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000871 atLoc, nameId, nameLoc, categoryId,
872 categoryLoc);
873 return ImplCatType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000874 }
875 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000876 SourceLocation superClassLoc;
877 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000878 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000879 // We have a super class
880 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000881 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000882 Diag(Tok, diag::err_expected_ident); // missing super class name.
883 return 0;
884 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000885 superClassId = Tok.getIdentifierInfo();
886 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000887 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000888 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff3a165b02007-10-03 21:00:46 +0000889 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000890 superClassId, superClassLoc);
891
Chris Lattnerdf195262007-10-09 17:51:17 +0000892 if (Tok.is(tok::l_brace))
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000893 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000894
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000895 return ImplClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000896}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000897Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
898 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
899 "ParseObjCAtEndDeclaration(): Expected @end");
900 ConsumeToken(); // the "end" identifier
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000901 if (ObjcImpDecl) {
902 // Checking is not necessary except that a parse error might have caused
903 // @implementation not to have been parsed to completion and ObjcImpDecl
904 // could be 0.
905 /// Insert collected methods declarations into the @interface object.
Steve Naroff3a165b02007-10-03 21:00:46 +0000906 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
907 &AllImplMethods[0],AllImplMethods.size());
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000908 ObjcImpDecl = 0;
909 AllImplMethods.clear();
910 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000911
Steve Naroffdac269b2007-08-20 21:31:48 +0000912 return 0;
913}
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000914
915/// compatibility-alias-decl:
916/// @compatibility_alias alias-name class-name ';'
917///
918Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
919 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
920 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
921 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +0000922 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000923 Diag(Tok, diag::err_expected_ident);
924 return 0;
925 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000926 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
927 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +0000928 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000929 Diag(Tok, diag::err_expected_ident);
930 return 0;
931 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000932 IdentifierInfo *classId = Tok.getIdentifierInfo();
933 SourceLocation classLoc = ConsumeToken(); // consume class-name;
934 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +0000935 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000936 return 0;
937 }
938 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
939 aliasId, aliasLoc,
940 classId, classLoc);
941 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000942}
943
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000944/// property-synthesis:
945/// @synthesize property-ivar-list ';'
946///
947/// property-ivar-list:
948/// property-ivar
949/// property-ivar-list ',' property-ivar
950///
951/// property-ivar:
952/// identifier
953/// identifier '=' identifier
954///
955Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
956 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
957 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
958 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000960 Diag(Tok, diag::err_expected_ident);
961 return 0;
962 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000963 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000964 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +0000965 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000966 // property '=' ivar-name
967 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +0000968 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000969 Diag(Tok, diag::err_expected_ident);
970 break;
971 }
972 ConsumeToken(); // consume ivar-name
973 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000974 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000975 break;
976 ConsumeToken(); // consume ','
977 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000978 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000979 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
980 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000981}
982
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000983/// property-dynamic:
984/// @dynamic property-list
985///
986/// property-list:
987/// identifier
988/// property-list ',' identifier
989///
990Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
991 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
992 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
993 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +0000994 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000995 Diag(Tok, diag::err_expected_ident);
996 return 0;
997 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000998 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000999 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001000 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001001 break;
1002 ConsumeToken(); // consume ','
1003 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001004 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001005 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1006 return 0;
1007}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001008
1009/// objc-throw-statement:
1010/// throw expression[opt];
1011///
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001012Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001013 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001014 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001015 ExprResult Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001016 if (Res.isInvalid) {
1017 SkipUntil(tok::semi);
1018 return 0;
1019 }
1020 }
1021 return 0;
1022}
1023
1024/// objc-try-catch-statement:
1025/// @try compound-statement objc-catch-list[opt]
1026/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1027///
1028/// objc-catch-list:
1029/// @catch ( parameter-declaration ) compound-statement
1030/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1031/// catch-parameter-declaration:
1032/// parameter-declaration
1033/// '...' [OBJC2]
1034///
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001035Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001036 bool catch_or_finally_seen = false;
1037 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001038 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001039 Diag (Tok, diag::err_expected_lbrace);
1040 return 0;
1041 }
1042 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnerdf195262007-10-09 17:51:17 +00001043 while (Tok.is(tok::at)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001044 ConsumeToken();
1045 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1046 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001047 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001048 ConsumeParen();
Chris Lattnerdf195262007-10-09 17:51:17 +00001049 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001050 DeclSpec DS;
1051 ParseDeclarationSpecifiers(DS);
1052 // Parse the parameter-declaration.
1053 // FIXME: BlockContext may not be the right context!
1054 Declarator ParmDecl(DS, Declarator::BlockContext);
1055 ParseDeclarator(ParmDecl);
1056 }
1057 else
1058 ConsumeToken(); // consume '...'
1059 ConsumeParen();
1060 StmtResult CatchMody = ParseCompoundStatementBody();
1061 }
1062 else {
1063 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1064 return 0;
1065 }
1066 catch_or_finally_seen = true;
1067 }
1068 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1069 ConsumeToken(); // consume finally
1070 StmtResult FinallyBody = ParseCompoundStatementBody();
1071 catch_or_finally_seen = true;
1072 break;
1073 }
1074 }
1075 if (!catch_or_finally_seen)
1076 Diag(atLoc, diag::err_missing_catch_finally);
1077 return 0;
1078}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001079
Steve Naroff3536b442007-09-06 21:24:23 +00001080/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001081///
1082void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnerdf195262007-10-09 17:51:17 +00001083 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1084
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001085 // FIXME: @optional/@protocol??
Fariborz Jahaniand0b01542007-09-27 18:57:03 +00001086 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001087 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001088 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001089 ConsumeToken();
1090
Chris Lattnerdf195262007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001092 Diag (Tok, diag::err_expected_lbrace);
1093 return;
1094 }
1095
1096 StmtResult FnBody = ParseCompoundStatementBody();
1097}
1098
Steve Naroff3536b442007-09-06 21:24:23 +00001099/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001100///
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001101void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnerdf195262007-10-09 17:51:17 +00001102 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001103 // FIXME: @optional/@protocol??
Fariborz Jahaniand0b01542007-09-27 18:57:03 +00001104 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001105 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001106 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001107 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001108 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001109 Diag (Tok, diag::err_expected_lbrace);
1110 return;
1111 }
1112
1113 StmtResult FnBody = ParseCompoundStatementBody();
Reid Spencer5f016e22007-07-11 17:01:13 +00001114}
Anders Carlsson55085182007-08-21 17:43:55 +00001115
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001116Parser::ExprResult Parser::ParseObjCExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001117
1118 switch (Tok.getKind()) {
1119 case tok::string_literal: // primary-expression: string-literal
1120 case tok::wide_string_literal:
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001121 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001122 default:
1123 break;
1124 }
1125
1126 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001127 case tok::objc_encode:
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001128 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression());
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001129 case tok::objc_protocol:
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001130 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression());
Anders Carlsson55085182007-08-21 17:43:55 +00001131 default:
1132 Diag(AtLoc, diag::err_unexpected_at);
1133 SkipUntil(tok::semi);
1134 break;
1135 }
1136
1137 return 0;
1138}
1139
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001140/// objc-message-expr:
1141/// '[' objc-receiver objc-message-args ']'
1142///
1143/// objc-receiver:
1144/// expression
1145/// class-name
1146/// type-name
1147///
1148/// objc-message-args:
1149/// objc-selector
1150/// objc-keywordarg-list
1151///
1152/// objc-keywordarg-list:
1153/// objc-keywordarg
1154/// objc-keywordarg-list objc-keywordarg
1155///
1156/// objc-keywordarg:
1157/// selector-name[opt] ':' objc-keywordexpr
1158///
1159/// objc-keywordexpr:
1160/// nonempty-expr-list
1161///
1162/// nonempty-expr-list:
1163/// assignment-expression
1164/// nonempty-expr-list , assignment-expression
1165///
1166Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnerdf195262007-10-09 17:51:17 +00001167 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroff563477d2007-09-18 23:55:05 +00001168 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff37387c92007-09-17 20:25:27 +00001169 IdentifierInfo *ReceiverName = 0;
1170 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001171 // Parse receiver
Chris Lattnerdf195262007-10-09 17:51:17 +00001172 if (Tok.is(tok::identifier) &&
Steve Naroff37387c92007-09-17 20:25:27 +00001173 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1174 ReceiverName = Tok.getIdentifierInfo();
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001175 ConsumeToken();
Steve Naroff37387c92007-09-17 20:25:27 +00001176 } else {
1177 ExprResult Res = ParseAssignmentExpression();
1178 if (Res.isInvalid) {
1179 SkipUntil(tok::identifier);
1180 return Res;
1181 }
1182 ReceiverExpr = Res.Val;
1183 }
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001184 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001185 SourceLocation Loc;
1186 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001187
1188 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1189 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1190
Chris Lattnerdf195262007-10-09 17:51:17 +00001191 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001192 while (1) {
1193 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001194 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001195
Chris Lattnerdf195262007-10-09 17:51:17 +00001196 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001197 Diag(Tok, diag::err_expected_colon);
1198 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001199 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001200 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001201 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001202 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001203 ExprResult Res = ParseAssignmentExpression();
1204 if (Res.isInvalid) {
1205 SkipUntil(tok::identifier);
1206 return Res;
1207 }
1208 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001209 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001210
1211 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001212 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001213 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001214 break;
1215 // We have a selector or a colon, continue parsing.
1216 }
1217 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001218 while (Tok.is(tok::comma)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001219 ConsumeToken();
1220 /// Parse the expression after ','
1221 ParseAssignmentExpression();
1222 }
1223 } else if (!selIdent) {
1224 Diag(Tok, diag::err_expected_ident); // missing selector name.
1225 SkipUntil(tok::semi);
1226 return 0;
1227 }
Chris Lattnerff384912007-10-07 02:00:24 +00001228
Chris Lattnerdf195262007-10-09 17:51:17 +00001229 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001230 Diag(Tok, diag::err_expected_rsquare);
1231 SkipUntil(tok::semi);
1232 return 0;
1233 }
Steve Naroff563477d2007-09-18 23:55:05 +00001234 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001235
Steve Naroff29238a02007-10-05 18:42:47 +00001236 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001237 if (nKeys == 0)
1238 KeyIdents.push_back(selIdent);
1239 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1240
1241 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001242 if (ReceiverName)
Chris Lattnerff384912007-10-07 02:00:24 +00001243 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1244 &KeyExprs[0]);
1245 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1246 &KeyExprs[0]);
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001247}
1248
Anders Carlsson55085182007-08-21 17:43:55 +00001249Parser::ExprResult Parser::ParseObjCStringLiteral() {
1250 ExprResult Res = ParseStringLiteralExpression();
1251
1252 if (Res.isInvalid) return Res;
1253
1254 return Actions.ParseObjCStringLiteral(Res.Val);
1255}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001256
1257/// objc-encode-expression:
1258/// @encode ( type-name )
1259Parser::ExprResult Parser::ParseObjCEncodeExpression() {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001260 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001261
1262 SourceLocation EncLoc = ConsumeToken();
1263
Chris Lattnerdf195262007-10-09 17:51:17 +00001264 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001265 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1266 return true;
1267 }
1268
1269 SourceLocation LParenLoc = ConsumeParen();
1270
1271 TypeTy *Ty = ParseTypeName();
1272
Anders Carlsson4988ae32007-08-23 15:31:37 +00001273 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001274
1275 return Actions.ParseObjCEncodeExpression(EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001276 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001277}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001278
1279/// objc-protocol-expression
1280/// @protocol ( protocol-name )
1281
1282Parser::ExprResult Parser::ParseObjCProtocolExpression()
1283{
1284 SourceLocation ProtoLoc = ConsumeToken();
1285
Chris Lattnerdf195262007-10-09 17:51:17 +00001286 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001287 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1288 return true;
1289 }
1290
1291 SourceLocation LParenLoc = ConsumeParen();
1292
Chris Lattnerdf195262007-10-09 17:51:17 +00001293 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001294 Diag(Tok, diag::err_expected_ident);
1295 return true;
1296 }
1297
1298 // FIXME: Do something with the protocol name
1299 ConsumeToken();
1300
Anders Carlsson4988ae32007-08-23 15:31:37 +00001301 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001302
1303 // FIXME
1304 return 0;
1305}