blob: 4abf8fd43dd5136f253ea6728315752234a570eb [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();
Steve Naroff3774dd92007-10-26 20:53:56 +0000382 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000383
Steve Naroff3774dd92007-10-26 20:53:56 +0000384 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, 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 Naroff3774dd92007-10-26 20:53:56 +0000529Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
530 tok::TokenKind mType,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000531 tok::ObjCKeywordKind MethodImplKind)
532{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000533 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000534 TypeTy *ReturnType = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000535 if (Tok.is(tok::l_paren))
Steve Naroff304ed392007-09-05 23:30:30 +0000536 ReturnType = ParseObjCTypeName();
Steve Naroff3774dd92007-10-26 20:53:56 +0000537 SourceLocation selLoc;
538 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000539 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000540 if (!SelIdent) {
541 Diag(Tok, diag::err_expected_ident); // missing selector name.
542 // FIXME: this creates a unary selector with a null identifier, is this
543 // ok?? Maybe we should skip to the next semicolon or something.
544 }
545
546 // If attributes exist after the method, parse them.
547 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000548 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000549 MethodAttrs = ParseAttributes();
550
551 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000552 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
553 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000554 0, 0, MethodAttrs, MethodImplKind);
555 }
Steve Naroff304ed392007-09-05 23:30:30 +0000556
Steve Naroff4ed9d662007-09-27 14:38:14 +0000557 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
558 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
559 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000560
561 Action::TypeTy *TypeInfo;
562 while (1) {
563 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000564
Chris Lattnerd031a452007-10-07 02:00:24 +0000565 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000566 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000567 Diag(Tok, diag::err_expected_colon);
568 break;
569 }
570 ConsumeToken(); // Eat the ':'.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000571 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000572 TypeInfo = ParseObjCTypeName();
573 else
574 TypeInfo = 0;
575 KeyTypes.push_back(TypeInfo);
Steve Naroff304ed392007-09-05 23:30:30 +0000576
Chris Lattnerd031a452007-10-07 02:00:24 +0000577 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000578 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000579 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000580
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000581 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000582 Diag(Tok, diag::err_expected_ident); // missing argument name.
583 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000584 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000585 ArgNames.push_back(Tok.getIdentifierInfo());
586 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000587
Chris Lattnerd031a452007-10-07 02:00:24 +0000588 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000589 SourceLocation Loc;
590 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000591 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000592 break;
593 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000594 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000595
596 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000597 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000598 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000599 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000600 ConsumeToken();
601 break;
602 }
603 // Parse the c-style argument declaration-specifier.
604 DeclSpec DS;
605 ParseDeclarationSpecifiers(DS);
606 // Parse the declarator.
607 Declarator ParmDecl(DS, Declarator::PrototypeContext);
608 ParseDeclarator(ParmDecl);
609 }
610
611 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000612 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000613 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000614 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000615 MethodAttrs = ParseAttributes();
616
617 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
618 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000619 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
620 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000621 &KeyTypes[0], &ArgNames[0],
622 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000623}
624
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000625/// CmpProtocolVals - Comparison predicate for sorting protocols.
626static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
627 const IdentifierInfo* const& rhs) {
628 return strcmp(lhs->getName(), rhs->getName()) < 0;
629}
630
Steve Narofffb367882007-08-20 21:31:48 +0000631/// objc-protocol-refs:
632/// '<' identifier-list '>'
633///
Steve Naroff304ed392007-09-05 23:30:30 +0000634bool Parser::ParseObjCProtocolReferences(
635 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000636 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000637
638 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000639
640 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000641 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000642 Diag(Tok, diag::err_expected_ident);
643 SkipUntil(tok::greater);
644 return true;
645 }
646 ProtocolRefs.push_back(Tok.getIdentifierInfo());
647 ConsumeToken();
648
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000649 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000650 break;
651 ConsumeToken();
652 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000653
654 // Sort protocols, keyed by name.
655 // Later on, we remove duplicates.
656 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
657
658 // Make protocol names unique.
659 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
660 ProtocolRefs.end());
661
Steve Narofffb367882007-08-20 21:31:48 +0000662 // Consume the '>'.
663 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
664}
665
666/// objc-class-instance-variables:
667/// '{' objc-instance-variable-decl-list[opt] '}'
668///
669/// objc-instance-variable-decl-list:
670/// objc-visibility-spec
671/// objc-instance-variable-decl ';'
672/// ';'
673/// objc-instance-variable-decl-list objc-visibility-spec
674/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
675/// objc-instance-variable-decl-list ';'
676///
677/// objc-visibility-spec:
678/// @private
679/// @protected
680/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000681/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000682///
683/// objc-instance-variable-decl:
684/// struct-declaration
685///
Steve Naroff81f1bba2007-09-06 21:24:23 +0000686void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000687 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000688 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000689 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
690 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000691
692 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000693
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000694 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000695 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000696 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000697 // Each iteration of this loop reads one objc-instance-variable-decl.
698
699 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000700 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000701 Diag(Tok, diag::ext_extra_struct_semi);
702 ConsumeToken();
703 continue;
704 }
705 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000706 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000707 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000708 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000709 case tok::objc_private:
710 case tok::objc_public:
711 case tok::objc_protected:
712 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000713 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000714 ConsumeToken();
715 continue;
716 default:
717 Diag(Tok, diag::err_objc_illegal_visibility_spec);
718 ConsumeToken();
719 continue;
720 }
721 }
722 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000723 for (unsigned i = 0; i < IvarDecls.size(); i++) {
724 AllIvarDecls.push_back(IvarDecls[i]);
725 AllVisibilities.push_back(visibility);
726 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000727 IvarDecls.clear();
728
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000729 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000730 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000731 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000732 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
733 break;
734 } else {
735 Diag(Tok, diag::err_expected_semi_decl_list);
736 // Skip to end of block or statement
737 SkipUntil(tok::r_brace, true, true);
738 }
739 }
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000740 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Fariborz Jahanian0c5affb2007-09-29 00:54:24 +0000741 Actions.ActOnFields(CurScope, LBraceLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000742 &AllIvarDecls[0], AllIvarDecls.size(),
743 &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000744 }
Steve Naroffc4474992007-08-21 21:17:12 +0000745 MatchRHSPunctuation(tok::r_brace, LBraceLoc);
746 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000747}
Steve Narofffb367882007-08-20 21:31:48 +0000748
749/// objc-protocol-declaration:
750/// objc-protocol-definition
751/// objc-protocol-forward-reference
752///
753/// objc-protocol-definition:
754/// @protocol identifier
755/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000756/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000757/// @end
758///
759/// objc-protocol-forward-reference:
760/// @protocol identifier-list ';'
761///
762/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000763/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000764/// semicolon in the first alternative if objc-protocol-refs are omitted.
765
Steve Naroff72f17fb2007-08-22 22:17:26 +0000766Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000767 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000768 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
769 ConsumeToken(); // the "protocol" identifier
770
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000771 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000772 Diag(Tok, diag::err_expected_ident); // missing protocol name.
773 return 0;
774 }
775 // Save the protocol name, then consume it.
776 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
777 SourceLocation nameLoc = ConsumeToken();
778
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000779 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000780 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000781 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000782 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000783 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000784 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000785 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000786 ProtocolRefs.push_back(protocolName);
787
788 while (1) {
789 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000790 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000791 Diag(Tok, diag::err_expected_ident);
792 SkipUntil(tok::semi);
793 return 0;
794 }
795 ProtocolRefs.push_back(Tok.getIdentifierInfo());
796 ConsumeToken(); // the identifier
797
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000798 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000799 break;
800 }
801 // Consume the ';'.
802 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
803 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000804 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000805 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000806 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000807 &ProtocolRefs[0],
808 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000809 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000810 if (Tok.is(tok::less)) {
Steve Naroff304ed392007-09-05 23:30:30 +0000811 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000812 return 0;
813 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000814
Steve Naroff415c1832007-10-10 17:32:04 +0000815 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000816 protocolName, nameLoc,
817 &ProtocolRefs[0],
818 ProtocolRefs.size());
819 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000820
821 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000822 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000823 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000824 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000825 }
826 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000827 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000828}
Steve Narofffb367882007-08-20 21:31:48 +0000829
830/// objc-implementation:
831/// objc-class-implementation-prologue
832/// objc-category-implementation-prologue
833///
834/// objc-class-implementation-prologue:
835/// @implementation identifier objc-superclass[opt]
836/// objc-class-instance-variables[opt]
837///
838/// objc-category-implementation-prologue:
839/// @implementation identifier ( identifier )
840
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000841Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
842 SourceLocation atLoc) {
843 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
844 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
845 ConsumeToken(); // the "implementation" identifier
846
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000847 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000848 Diag(Tok, diag::err_expected_ident); // missing class or category name.
849 return 0;
850 }
851 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000852 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000853 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
854
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000855 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000856 // we have a category implementation.
857 SourceLocation lparenLoc = ConsumeParen();
858 SourceLocation categoryLoc, rparenLoc;
859 IdentifierInfo *categoryId = 0;
860
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000861 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000862 categoryId = Tok.getIdentifierInfo();
863 categoryLoc = ConsumeToken();
864 } else {
865 Diag(Tok, diag::err_expected_ident); // missing category name.
866 return 0;
867 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000868 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000869 Diag(Tok, diag::err_expected_rparen);
870 SkipUntil(tok::r_paren, false); // don't stop at ';'
871 return 0;
872 }
873 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000874 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000875 atLoc, nameId, nameLoc, categoryId,
876 categoryLoc);
877 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000878 }
879 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000880 SourceLocation superClassLoc;
881 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000882 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000883 // We have a super class
884 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000885 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000886 Diag(Tok, diag::err_expected_ident); // missing super class name.
887 return 0;
888 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000889 superClassId = Tok.getIdentifierInfo();
890 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000891 }
Steve Naroff415c1832007-10-10 17:32:04 +0000892 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000893 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000894 superClassId, superClassLoc);
895
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000896 if (Tok.is(tok::l_brace))
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000897 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/); // we have ivars
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000898
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000899 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000900}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000901Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
902 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
903 "ParseObjCAtEndDeclaration(): Expected @end");
904 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000905 if (ObjcImpDecl) {
906 // Checking is not necessary except that a parse error might have caused
907 // @implementation not to have been parsed to completion and ObjcImpDecl
908 // could be 0.
909 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000910 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
911 &AllImplMethods[0],AllImplMethods.size());
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000912 ObjcImpDecl = 0;
913 AllImplMethods.clear();
914 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000915
Steve Narofffb367882007-08-20 21:31:48 +0000916 return 0;
917}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000918
919/// compatibility-alias-decl:
920/// @compatibility_alias alias-name class-name ';'
921///
922Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
923 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
924 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
925 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000926 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000927 Diag(Tok, diag::err_expected_ident);
928 return 0;
929 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000930 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
931 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000932 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000933 Diag(Tok, diag::err_expected_ident);
934 return 0;
935 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000936 IdentifierInfo *classId = Tok.getIdentifierInfo();
937 SourceLocation classLoc = ConsumeToken(); // consume class-name;
938 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000939 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000940 return 0;
941 }
942 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
943 aliasId, aliasLoc,
944 classId, classLoc);
945 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000946}
947
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000948/// property-synthesis:
949/// @synthesize property-ivar-list ';'
950///
951/// property-ivar-list:
952/// property-ivar
953/// property-ivar-list ',' property-ivar
954///
955/// property-ivar:
956/// identifier
957/// identifier '=' identifier
958///
959Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
960 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
961 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
962 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000963 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000964 Diag(Tok, diag::err_expected_ident);
965 return 0;
966 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000967 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000968 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000969 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000970 // property '=' ivar-name
971 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000972 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000973 Diag(Tok, diag::err_expected_ident);
974 break;
975 }
976 ConsumeToken(); // consume ivar-name
977 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000978 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000979 break;
980 ConsumeToken(); // consume ','
981 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000982 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000983 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
984 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000985}
986
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000987/// property-dynamic:
988/// @dynamic property-list
989///
990/// property-list:
991/// identifier
992/// property-list ',' identifier
993///
994Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
995 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
996 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
997 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000998 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000999 Diag(Tok, diag::err_expected_ident);
1000 return 0;
1001 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001002 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001003 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001004 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001005 break;
1006 ConsumeToken(); // consume ','
1007 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001008 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001009 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1010 return 0;
1011}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001012
1013/// objc-throw-statement:
1014/// throw expression[opt];
1015///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001016Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001017 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001018 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001019 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001020 if (Res.isInvalid) {
1021 SkipUntil(tok::semi);
1022 return 0;
1023 }
1024 }
1025 return 0;
1026}
1027
1028/// objc-try-catch-statement:
1029/// @try compound-statement objc-catch-list[opt]
1030/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1031///
1032/// objc-catch-list:
1033/// @catch ( parameter-declaration ) compound-statement
1034/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1035/// catch-parameter-declaration:
1036/// parameter-declaration
1037/// '...' [OBJC2]
1038///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001039Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001040 bool catch_or_finally_seen = false;
1041 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001042 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001043 Diag (Tok, diag::err_expected_lbrace);
1044 return 0;
1045 }
1046 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001047 while (Tok.is(tok::at)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001048 ConsumeToken();
1049 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1050 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001051 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001052 ConsumeParen();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001053 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001054 DeclSpec DS;
1055 ParseDeclarationSpecifiers(DS);
1056 // Parse the parameter-declaration.
1057 // FIXME: BlockContext may not be the right context!
1058 Declarator ParmDecl(DS, Declarator::BlockContext);
1059 ParseDeclarator(ParmDecl);
1060 }
1061 else
1062 ConsumeToken(); // consume '...'
1063 ConsumeParen();
1064 StmtResult CatchMody = ParseCompoundStatementBody();
1065 }
1066 else {
1067 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1068 return 0;
1069 }
1070 catch_or_finally_seen = true;
1071 }
1072 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1073 ConsumeToken(); // consume finally
1074 StmtResult FinallyBody = ParseCompoundStatementBody();
1075 catch_or_finally_seen = true;
1076 break;
1077 }
1078 }
1079 if (!catch_or_finally_seen)
1080 Diag(atLoc, diag::err_missing_catch_finally);
1081 return 0;
1082}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001083
Steve Naroff81f1bba2007-09-06 21:24:23 +00001084/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001085///
1086void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001087 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1088
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001089 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001090 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001091 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001092 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001093 ConsumeToken();
1094
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001095 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001096 Diag (Tok, diag::err_expected_lbrace);
1097 return;
1098 }
1099
1100 StmtResult FnBody = ParseCompoundStatementBody();
1101}
1102
Steve Naroff81f1bba2007-09-06 21:24:23 +00001103/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001104///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001105void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001106 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001107 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001108 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001109 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001110 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001111 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001112 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001113 Diag (Tok, diag::err_expected_lbrace);
1114 return;
1115 }
1116
1117 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001118}
Anders Carlssona66cad42007-08-21 17:43:55 +00001119
Steve Narofffb9dd752007-10-15 20:55:58 +00001120Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001121
1122 switch (Tok.getKind()) {
1123 case tok::string_literal: // primary-expression: string-literal
1124 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001125 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001126 default:
1127 break;
1128 }
1129
1130 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001131 case tok::objc_encode:
1132 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1133 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001134 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001135 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001136 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001137 default:
1138 Diag(AtLoc, diag::err_unexpected_at);
1139 SkipUntil(tok::semi);
1140 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001141 }
1142
1143 return 0;
1144}
1145
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001146/// objc-message-expr:
1147/// '[' objc-receiver objc-message-args ']'
1148///
1149/// objc-receiver:
1150/// expression
1151/// class-name
1152/// type-name
1153///
1154/// objc-message-args:
1155/// objc-selector
1156/// objc-keywordarg-list
1157///
1158/// objc-keywordarg-list:
1159/// objc-keywordarg
1160/// objc-keywordarg-list objc-keywordarg
1161///
1162/// objc-keywordarg:
1163/// selector-name[opt] ':' objc-keywordexpr
1164///
1165/// objc-keywordexpr:
1166/// nonempty-expr-list
1167///
1168/// nonempty-expr-list:
1169/// assignment-expression
1170/// nonempty-expr-list , assignment-expression
1171///
1172Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001173 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001174 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001175 IdentifierInfo *ReceiverName = 0;
1176 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001177 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001178 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001179 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1180 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001181 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001182 } else {
1183 ExprResult Res = ParseAssignmentExpression();
1184 if (Res.isInvalid) {
1185 SkipUntil(tok::identifier);
1186 return Res;
1187 }
1188 ReceiverExpr = Res.Val;
1189 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001190 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001191 SourceLocation Loc;
1192 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001193
1194 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1195 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1196
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001197 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001198 while (1) {
1199 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001200 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001201
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001202 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001203 Diag(Tok, diag::err_expected_colon);
1204 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001205 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001206 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001207 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001208 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001209 ExprResult Res = ParseAssignmentExpression();
1210 if (Res.isInvalid) {
1211 SkipUntil(tok::identifier);
1212 return Res;
1213 }
1214 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001215 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001216
1217 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001218 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001219 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001220 break;
1221 // We have a selector or a colon, continue parsing.
1222 }
1223 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001224 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001225 ConsumeToken();
1226 /// Parse the expression after ','
1227 ParseAssignmentExpression();
1228 }
1229 } else if (!selIdent) {
1230 Diag(Tok, diag::err_expected_ident); // missing selector name.
1231 SkipUntil(tok::semi);
1232 return 0;
1233 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001234
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001235 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001236 Diag(Tok, diag::err_expected_rsquare);
1237 SkipUntil(tok::semi);
1238 return 0;
1239 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001240 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001241
Steve Narofff9e80db2007-10-05 18:42:47 +00001242 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001243 if (nKeys == 0)
1244 KeyIdents.push_back(selIdent);
1245 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1246
1247 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001248 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001249 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1250 &KeyExprs[0]);
1251 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1252 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001253}
1254
Anders Carlssona66cad42007-08-21 17:43:55 +00001255Parser::ExprResult Parser::ParseObjCStringLiteral() {
1256 ExprResult Res = ParseStringLiteralExpression();
1257
1258 if (Res.isInvalid) return Res;
1259
1260 return Actions.ParseObjCStringLiteral(Res.Val);
1261}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001262
1263/// objc-encode-expression:
1264/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001265Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001266 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001267
1268 SourceLocation EncLoc = ConsumeToken();
1269
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001270 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001271 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1272 return true;
1273 }
1274
1275 SourceLocation LParenLoc = ConsumeParen();
1276
1277 TypeTy *Ty = ParseTypeName();
1278
Anders Carlsson92faeb82007-08-23 15:31:37 +00001279 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001280
Chris Lattnercfd61c82007-10-16 22:51:17 +00001281 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001282 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001283}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001284
1285/// objc-protocol-expression
1286/// @protocol ( protocol-name )
1287
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001288Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001289{
1290 SourceLocation ProtoLoc = ConsumeToken();
1291
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001292 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001293 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1294 return true;
1295 }
1296
1297 SourceLocation LParenLoc = ConsumeParen();
1298
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001299 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001300 Diag(Tok, diag::err_expected_ident);
1301 return true;
1302 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001303 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001304 ConsumeToken();
1305
Anders Carlsson92faeb82007-08-23 15:31:37 +00001306 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001307
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001308 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1309 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001310}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001311
1312/// objc-selector-expression
1313/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001314Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001315{
1316 SourceLocation SelectorLoc = ConsumeToken();
1317
1318 if (Tok.isNot(tok::l_paren)) {
1319 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1320 return 0;
1321 }
1322
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001323 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001324 SourceLocation LParenLoc = ConsumeParen();
1325 SourceLocation sLoc;
1326 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1327 if (!SelIdent && Tok.isNot(tok::colon)) {
1328
1329 Diag(Tok, diag::err_expected_ident); // missing selector name.
1330 return 0;
1331 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001332 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001333 if (Tok.isNot(tok::r_paren))
1334 while (1) {
1335 if (Tok.isNot(tok::colon)) {
1336 Diag(Tok, diag::err_expected_colon);
1337 break;
1338 }
1339 ConsumeToken(); // Eat the ':'.
1340 if (Tok.is(tok::r_paren))
1341 break;
1342 // Check for another keyword selector.
1343 SourceLocation Loc;
1344 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001345 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001346 if (!SelIdent && Tok.isNot(tok::colon))
1347 break;
1348 }
1349 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001350 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1351 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001352 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001353 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001354 }