blob: 0d44dcdd3a86ff52fa74040fba358c1c8e243f80 [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
Steve Naroff5b96e2e2007-10-29 21:39:29 +000024/// [OBJC] objc-class-declaration
25/// [OBJC] objc-alias-declaration
26/// [OBJC] objc-protocol-definition
27/// [OBJC] objc-method-definition
28/// [OBJC] '@' 'end'
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 Naroff1a7fa7b2007-10-29 21:38:07 +0000196 ParseObjCClassInstanceVariables(ClsType, atLoc);
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 Naroff1a7fa7b2007-10-29 21:38:07 +0000225 SourceLocation AtEndLoc;
226
Steve Naroff0bbffd82007-08-22 16:35:03 +0000227 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000228 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000229 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000230 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000231
232 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000233 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000234 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000235 } else if (ocKind == tok::objc_required) { // protocols only
236 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000237 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000238 if (contextKey != tok::objc_protocol)
239 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000240 } else if (ocKind == tok::objc_optional) { // protocols only
241 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000242 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000243 if (contextKey != tok::objc_protocol)
244 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000245 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000246 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000247 continue;
248 } else {
249 Diag(Tok, diag::err_objc_illegal_interface_qual);
250 ConsumeToken();
251 }
252 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000253 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
254 DeclTy *methodPrototype =
255 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000256 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000257 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
258 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000259 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000260 continue;
261 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000262 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000263 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000264 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000265 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000266 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000267 // FIXME: as the name implies, this rule allows function definitions.
268 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000269 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000270 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000271 }
Steve Naroff8255b042007-10-14 00:58:41 +0000272 if (allMethods.size())
273 /// Insert collected methods declarations into the @interface object.
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000274 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl, &allMethods[0],
275 allMethods.size(), AtEndLoc);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000276}
277
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000278/// Parse property attribute declarations.
279///
280/// property-attr-decl: '(' property-attrlist ')'
281/// property-attrlist:
282/// property-attribute
283/// property-attrlist ',' property-attribute
284/// property-attribute:
285/// getter '=' identifier
286/// setter '=' identifier ':'
287/// readonly
288/// readwrite
289/// assign
290/// retain
291/// copy
292/// nonatomic
293///
294void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
295 SourceLocation loc = ConsumeParen(); // consume '('
296 while (isObjCPropertyAttribute()) {
297 const IdentifierInfo *II = Tok.getIdentifierInfo();
298 // getter/setter require extra treatment.
299 if (II == ObjcPropertyAttrs[objc_getter] ||
300 II == ObjcPropertyAttrs[objc_setter]) {
301 // skip getter/setter part.
302 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000303 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000304 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000305 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000306 if (II == ObjcPropertyAttrs[objc_setter]) {
307 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000308 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000309 Diag(loc, diag::err_expected_colon);
310 SkipUntil(tok::r_paren,true,true);
311 break;
312 }
313 }
314 }
315 else {
316 Diag(loc, diag::err_expected_ident);
317 SkipUntil(tok::r_paren,true,true);
318 break;
319 }
320 }
321 else {
322 Diag(loc, diag::err_objc_expected_equal);
323 SkipUntil(tok::r_paren,true,true);
324 break;
325 }
326 }
327 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000328 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000329 loc = ConsumeToken();
330 continue;
331 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000332 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000333 break;
334 Diag(loc, diag::err_expected_rparen);
335 SkipUntil(tok::semi);
336 return;
337 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000338 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000339 ConsumeParen();
340 else {
341 Diag(loc, diag::err_objc_expected_property_attr);
342 SkipUntil(tok::r_paren); // recover from error inside attribute list
343 }
344}
345
346/// Main routine to parse property declaration.
347///
348/// @property property-attr-decl[opt] property-component-decl ';'
349///
350void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
351 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
352 "ParseObjCPropertyDecl(): Expected @property");
353 ConsumeToken(); // the "property" identifier
354 // Parse property attribute list, if any.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000355 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000356 // property has attribute list.
357 ParseObjCPropertyAttribute(0/*FIXME*/);
358 }
359 // Parse declaration portion of @property.
360 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
361 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000362 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000363 ConsumeToken();
364 else {
365 Diag(Tok, diag::err_expected_semi_decl_list);
366 SkipUntil(tok::r_brace, true, true);
367 }
Chris Lattner4b009652007-07-25 00:24:17 +0000368}
Steve Narofffb367882007-08-20 21:31:48 +0000369
Steve Naroff81f1bba2007-09-06 21:24:23 +0000370/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000371/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000372/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000373///
374/// objc-instance-method: '-'
375/// objc-class-method: '+'
376///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000377/// objc-method-attributes: [OBJC2]
378/// __attribute__((deprecated))
379///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000380Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
381 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000382 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000383
384 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000385 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000386
Steve Naroff3774dd92007-10-26 20:53:56 +0000387 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000388 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000389 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000390 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000391}
392
393/// objc-selector:
394/// identifier
395/// one of
396/// enum struct union if else while do for switch case default
397/// break continue return goto asm sizeof typeof __alignof
398/// unsigned long const short volatile signed restrict _Complex
399/// in out inout bycopy byref oneway int char float double void _Bool
400///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000401IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000402 switch (Tok.getKind()) {
403 default:
404 return 0;
405 case tok::identifier:
406 case tok::kw_typeof:
407 case tok::kw___alignof:
408 case tok::kw_auto:
409 case tok::kw_break:
410 case tok::kw_case:
411 case tok::kw_char:
412 case tok::kw_const:
413 case tok::kw_continue:
414 case tok::kw_default:
415 case tok::kw_do:
416 case tok::kw_double:
417 case tok::kw_else:
418 case tok::kw_enum:
419 case tok::kw_extern:
420 case tok::kw_float:
421 case tok::kw_for:
422 case tok::kw_goto:
423 case tok::kw_if:
424 case tok::kw_inline:
425 case tok::kw_int:
426 case tok::kw_long:
427 case tok::kw_register:
428 case tok::kw_restrict:
429 case tok::kw_return:
430 case tok::kw_short:
431 case tok::kw_signed:
432 case tok::kw_sizeof:
433 case tok::kw_static:
434 case tok::kw_struct:
435 case tok::kw_switch:
436 case tok::kw_typedef:
437 case tok::kw_union:
438 case tok::kw_unsigned:
439 case tok::kw_void:
440 case tok::kw_volatile:
441 case tok::kw_while:
442 case tok::kw__Bool:
443 case tok::kw__Complex:
444 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000445 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000446 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000447 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000448}
449
Steve Naroffa8ee2262007-08-22 23:18:22 +0000450/// objc-type-qualifier: one of
451/// in out inout bycopy byref oneway
452///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000453bool Parser::isObjCTypeQualifier() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000454 if (Tok.is(tok::identifier)) {
Chris Lattner32352462007-08-29 22:54:08 +0000455 const IdentifierInfo *II = Tok.getIdentifierInfo();
456 for (unsigned i = 0; i < objc_NumQuals; ++i)
457 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000458 }
459 return false;
460}
461
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000462/// property-attrlist: one of
463/// readonly getter setter assign retain copy nonatomic
464///
465bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000466 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000467 const IdentifierInfo *II = Tok.getIdentifierInfo();
468 for (unsigned i = 0; i < objc_NumAttrs; ++i)
469 if (II == ObjcPropertyAttrs[i]) return true;
470 }
471 return false;
472}
473
Steve Naroff0bbffd82007-08-22 16:35:03 +0000474/// objc-type-name:
475/// '(' objc-type-qualifiers[opt] type-name ')'
476/// '(' objc-type-qualifiers[opt] ')'
477///
478/// objc-type-qualifiers:
479/// objc-type-qualifier
480/// objc-type-qualifiers objc-type-qualifier
481///
Steve Naroff304ed392007-09-05 23:30:30 +0000482Parser::TypeTy *Parser::ParseObjCTypeName() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000483 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000484
485 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000486 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000487
Steve Naroffa8ee2262007-08-22 23:18:22 +0000488 while (isObjCTypeQualifier())
489 ConsumeToken();
490
Steve Naroff0bbffd82007-08-22 16:35:03 +0000491 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000492 Ty = ParseTypeName();
493 // FIXME: back when Sema support is in place...
494 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000495 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000496 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000497 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000498 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000499 }
500 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000501 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000502}
503
504/// objc-method-decl:
505/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000506/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000507/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000508/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000509///
510/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000511/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000512/// objc-keyword-selector objc-keyword-decl
513///
514/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000515/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
516/// objc-selector ':' objc-keyword-attributes[opt] identifier
517/// ':' objc-type-name objc-keyword-attributes[opt] identifier
518/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000519///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000520/// objc-parmlist:
521/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000522///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000523/// objc-parms:
524/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000525///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000526/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000527/// , ...
528///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000529/// objc-keyword-attributes: [OBJC2]
530/// __attribute__((unused))
531///
Steve Naroff3774dd92007-10-26 20:53:56 +0000532Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
533 tok::TokenKind mType,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000534 tok::ObjCKeywordKind MethodImplKind)
535{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000536 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000537 TypeTy *ReturnType = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000538 if (Tok.is(tok::l_paren))
Steve Naroff304ed392007-09-05 23:30:30 +0000539 ReturnType = ParseObjCTypeName();
Steve Naroff3774dd92007-10-26 20:53:56 +0000540 SourceLocation selLoc;
541 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000542 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000543 if (!SelIdent) {
544 Diag(Tok, diag::err_expected_ident); // missing selector name.
545 // FIXME: this creates a unary selector with a null identifier, is this
546 // ok?? Maybe we should skip to the next semicolon or something.
547 }
548
549 // If attributes exist after the method, parse them.
550 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000551 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000552 MethodAttrs = ParseAttributes();
553
554 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000555 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
556 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000557 0, 0, MethodAttrs, MethodImplKind);
558 }
Steve Naroff304ed392007-09-05 23:30:30 +0000559
Steve Naroff4ed9d662007-09-27 14:38:14 +0000560 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
561 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
562 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000563
564 Action::TypeTy *TypeInfo;
565 while (1) {
566 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000567
Chris Lattnerd031a452007-10-07 02:00:24 +0000568 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000569 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000570 Diag(Tok, diag::err_expected_colon);
571 break;
572 }
573 ConsumeToken(); // Eat the ':'.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000574 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000575 TypeInfo = ParseObjCTypeName();
576 else
577 TypeInfo = 0;
578 KeyTypes.push_back(TypeInfo);
Steve Naroff304ed392007-09-05 23:30:30 +0000579
Chris Lattnerd031a452007-10-07 02:00:24 +0000580 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000581 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000582 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000583
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000584 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000585 Diag(Tok, diag::err_expected_ident); // missing argument name.
586 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000587 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000588 ArgNames.push_back(Tok.getIdentifierInfo());
589 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000590
Chris Lattnerd031a452007-10-07 02:00:24 +0000591 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000592 SourceLocation Loc;
593 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000594 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000595 break;
596 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000597 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000598
599 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000600 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000601 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000602 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000603 ConsumeToken();
604 break;
605 }
606 // Parse the c-style argument declaration-specifier.
607 DeclSpec DS;
608 ParseDeclarationSpecifiers(DS);
609 // Parse the declarator.
610 Declarator ParmDecl(DS, Declarator::PrototypeContext);
611 ParseDeclarator(ParmDecl);
612 }
613
614 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000615 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000616 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000617 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000618 MethodAttrs = ParseAttributes();
619
620 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
621 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000622 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
623 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000624 &KeyTypes[0], &ArgNames[0],
625 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000626}
627
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000628/// CmpProtocolVals - Comparison predicate for sorting protocols.
629static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
630 const IdentifierInfo* const& rhs) {
631 return strcmp(lhs->getName(), rhs->getName()) < 0;
632}
633
Steve Narofffb367882007-08-20 21:31:48 +0000634/// objc-protocol-refs:
635/// '<' identifier-list '>'
636///
Steve Naroff304ed392007-09-05 23:30:30 +0000637bool Parser::ParseObjCProtocolReferences(
638 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000639 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000640
641 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000642
643 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000644 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000645 Diag(Tok, diag::err_expected_ident);
646 SkipUntil(tok::greater);
647 return true;
648 }
649 ProtocolRefs.push_back(Tok.getIdentifierInfo());
650 ConsumeToken();
651
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000652 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000653 break;
654 ConsumeToken();
655 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000656
657 // Sort protocols, keyed by name.
658 // Later on, we remove duplicates.
659 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
660
661 // Make protocol names unique.
662 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
663 ProtocolRefs.end());
664
Steve Narofffb367882007-08-20 21:31:48 +0000665 // Consume the '>'.
666 return ExpectAndConsume(tok::greater, diag::err_expected_greater);
667}
668
669/// objc-class-instance-variables:
670/// '{' objc-instance-variable-decl-list[opt] '}'
671///
672/// objc-instance-variable-decl-list:
673/// objc-visibility-spec
674/// objc-instance-variable-decl ';'
675/// ';'
676/// objc-instance-variable-decl-list objc-visibility-spec
677/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
678/// objc-instance-variable-decl-list ';'
679///
680/// objc-visibility-spec:
681/// @private
682/// @protected
683/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000684/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000685///
686/// objc-instance-variable-decl:
687/// struct-declaration
688///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000689void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
690 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000691 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000692 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000693 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
694 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000695
696 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000697
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000698 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000699 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000700 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000701 // Each iteration of this loop reads one objc-instance-variable-decl.
702
703 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000704 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000705 Diag(Tok, diag::ext_extra_struct_semi);
706 ConsumeToken();
707 continue;
708 }
709 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000710 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000711 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000712 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000713 case tok::objc_private:
714 case tok::objc_public:
715 case tok::objc_protected:
716 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000717 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000718 ConsumeToken();
719 continue;
720 default:
721 Diag(Tok, diag::err_objc_illegal_visibility_spec);
722 ConsumeToken();
723 continue;
724 }
725 }
726 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000727 for (unsigned i = 0; i < IvarDecls.size(); i++) {
728 AllIvarDecls.push_back(IvarDecls[i]);
729 AllVisibilities.push_back(visibility);
730 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000731 IvarDecls.clear();
732
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000733 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000734 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000735 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000736 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
737 break;
738 } else {
739 Diag(Tok, diag::err_expected_semi_decl_list);
740 // Skip to end of block or statement
741 SkipUntil(tok::r_brace, true, true);
742 }
743 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000744 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000745 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000746 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000747 &AllIvarDecls[0], AllIvarDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000748 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000749 }
Steve Naroffc4474992007-08-21 21:17:12 +0000750 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000751}
Steve Narofffb367882007-08-20 21:31:48 +0000752
753/// objc-protocol-declaration:
754/// objc-protocol-definition
755/// objc-protocol-forward-reference
756///
757/// objc-protocol-definition:
758/// @protocol identifier
759/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000760/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000761/// @end
762///
763/// objc-protocol-forward-reference:
764/// @protocol identifier-list ';'
765///
766/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000767/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000768/// semicolon in the first alternative if objc-protocol-refs are omitted.
769
Steve Naroff72f17fb2007-08-22 22:17:26 +0000770Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000771 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000772 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
773 ConsumeToken(); // the "protocol" identifier
774
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000775 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000776 Diag(Tok, diag::err_expected_ident); // missing protocol name.
777 return 0;
778 }
779 // Save the protocol name, then consume it.
780 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
781 SourceLocation nameLoc = ConsumeToken();
782
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000783 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000784 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000785 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000786 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000787 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000788 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000789 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000790 ProtocolRefs.push_back(protocolName);
791
792 while (1) {
793 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000794 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000795 Diag(Tok, diag::err_expected_ident);
796 SkipUntil(tok::semi);
797 return 0;
798 }
799 ProtocolRefs.push_back(Tok.getIdentifierInfo());
800 ConsumeToken(); // the identifier
801
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000802 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000803 break;
804 }
805 // Consume the ';'.
806 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
807 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000808 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000809 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000810 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000811 &ProtocolRefs[0],
812 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000813 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000814 if (Tok.is(tok::less)) {
Steve Naroff304ed392007-09-05 23:30:30 +0000815 if (ParseObjCProtocolReferences(ProtocolRefs))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000816 return 0;
817 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000818
Steve Naroff415c1832007-10-10 17:32:04 +0000819 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000820 protocolName, nameLoc,
821 &ProtocolRefs[0],
822 ProtocolRefs.size());
823 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000824
825 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000826 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000827 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000828 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000829 }
830 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000831 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000832}
Steve Narofffb367882007-08-20 21:31:48 +0000833
834/// objc-implementation:
835/// objc-class-implementation-prologue
836/// objc-category-implementation-prologue
837///
838/// objc-class-implementation-prologue:
839/// @implementation identifier objc-superclass[opt]
840/// objc-class-instance-variables[opt]
841///
842/// objc-category-implementation-prologue:
843/// @implementation identifier ( identifier )
844
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000845Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
846 SourceLocation atLoc) {
847 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
848 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
849 ConsumeToken(); // the "implementation" identifier
850
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000851 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000852 Diag(Tok, diag::err_expected_ident); // missing class or category name.
853 return 0;
854 }
855 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000856 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000857 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
858
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000859 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000860 // we have a category implementation.
861 SourceLocation lparenLoc = ConsumeParen();
862 SourceLocation categoryLoc, rparenLoc;
863 IdentifierInfo *categoryId = 0;
864
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000865 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000866 categoryId = Tok.getIdentifierInfo();
867 categoryLoc = ConsumeToken();
868 } else {
869 Diag(Tok, diag::err_expected_ident); // missing category name.
870 return 0;
871 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000872 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000873 Diag(Tok, diag::err_expected_rparen);
874 SkipUntil(tok::r_paren, false); // don't stop at ';'
875 return 0;
876 }
877 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000878 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000879 atLoc, nameId, nameLoc, categoryId,
880 categoryLoc);
881 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000882 }
883 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000884 SourceLocation superClassLoc;
885 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000886 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000887 // We have a super class
888 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000889 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000890 Diag(Tok, diag::err_expected_ident); // missing super class name.
891 return 0;
892 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000893 superClassId = Tok.getIdentifierInfo();
894 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000895 }
Steve Naroff415c1832007-10-10 17:32:04 +0000896 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000897 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000898 superClassId, superClassLoc);
899
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000900 if (Tok.is(tok::l_brace)) // we have ivars
901 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000902
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000903 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000904}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000905
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000906Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
907 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
908 "ParseObjCAtEndDeclaration(): Expected @end");
909 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000910 if (ObjcImpDecl) {
911 // Checking is not necessary except that a parse error might have caused
912 // @implementation not to have been parsed to completion and ObjcImpDecl
913 // could be 0.
914 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000915 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000916 &AllImplMethods[0], AllImplMethods.size(),
917 atLoc);
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000918 ObjcImpDecl = 0;
919 AllImplMethods.clear();
920 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000921
Steve Narofffb367882007-08-20 21:31:48 +0000922 return 0;
923}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000924
925/// compatibility-alias-decl:
926/// @compatibility_alias alias-name class-name ';'
927///
928Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
929 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
930 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
931 ConsumeToken(); // consume compatibility_alias
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 *aliasId = Tok.getIdentifierInfo();
937 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000938 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000939 Diag(Tok, diag::err_expected_ident);
940 return 0;
941 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000942 IdentifierInfo *classId = Tok.getIdentifierInfo();
943 SourceLocation classLoc = ConsumeToken(); // consume class-name;
944 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000945 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000946 return 0;
947 }
948 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
949 aliasId, aliasLoc,
950 classId, classLoc);
951 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000952}
953
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000954/// property-synthesis:
955/// @synthesize property-ivar-list ';'
956///
957/// property-ivar-list:
958/// property-ivar
959/// property-ivar-list ',' property-ivar
960///
961/// property-ivar:
962/// identifier
963/// identifier '=' identifier
964///
965Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
966 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
967 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
968 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000969 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000970 Diag(Tok, diag::err_expected_ident);
971 return 0;
972 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000973 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000974 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000975 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000976 // property '=' ivar-name
977 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000978 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000979 Diag(Tok, diag::err_expected_ident);
980 break;
981 }
982 ConsumeToken(); // consume ivar-name
983 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000984 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000985 break;
986 ConsumeToken(); // consume ','
987 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000988 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000989 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
990 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000991}
992
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000993/// property-dynamic:
994/// @dynamic property-list
995///
996/// property-list:
997/// identifier
998/// property-list ',' identifier
999///
1000Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1001 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1002 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1003 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001004 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001005 Diag(Tok, diag::err_expected_ident);
1006 return 0;
1007 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001008 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001009 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001010 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001011 break;
1012 ConsumeToken(); // consume ','
1013 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001014 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001015 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1016 return 0;
1017}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001018
1019/// objc-throw-statement:
1020/// throw expression[opt];
1021///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001022Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001023 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001024 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001025 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001026 if (Res.isInvalid) {
1027 SkipUntil(tok::semi);
1028 return 0;
1029 }
1030 }
1031 return 0;
1032}
1033
1034/// objc-try-catch-statement:
1035/// @try compound-statement objc-catch-list[opt]
1036/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1037///
1038/// objc-catch-list:
1039/// @catch ( parameter-declaration ) compound-statement
1040/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1041/// catch-parameter-declaration:
1042/// parameter-declaration
1043/// '...' [OBJC2]
1044///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001045Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001046 bool catch_or_finally_seen = false;
1047 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001048 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001049 Diag (Tok, diag::err_expected_lbrace);
1050 return 0;
1051 }
1052 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001053 while (Tok.is(tok::at)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001054 ConsumeToken();
1055 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1056 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001057 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001058 ConsumeParen();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001059 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001060 DeclSpec DS;
1061 ParseDeclarationSpecifiers(DS);
1062 // Parse the parameter-declaration.
1063 // FIXME: BlockContext may not be the right context!
1064 Declarator ParmDecl(DS, Declarator::BlockContext);
1065 ParseDeclarator(ParmDecl);
1066 }
1067 else
1068 ConsumeToken(); // consume '...'
1069 ConsumeParen();
1070 StmtResult CatchMody = ParseCompoundStatementBody();
1071 }
1072 else {
1073 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1074 return 0;
1075 }
1076 catch_or_finally_seen = true;
1077 }
1078 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1079 ConsumeToken(); // consume finally
1080 StmtResult FinallyBody = ParseCompoundStatementBody();
1081 catch_or_finally_seen = true;
1082 break;
1083 }
1084 }
1085 if (!catch_or_finally_seen)
1086 Diag(atLoc, diag::err_missing_catch_finally);
1087 return 0;
1088}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001089
Steve Naroff81f1bba2007-09-06 21:24:23 +00001090/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001091///
1092void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001093 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1094
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001095 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001096 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001097 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001098 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001099 ConsumeToken();
1100
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001101 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001102 Diag (Tok, diag::err_expected_lbrace);
1103 return;
1104 }
1105
1106 StmtResult FnBody = ParseCompoundStatementBody();
1107}
1108
Steve Naroff81f1bba2007-09-06 21:24:23 +00001109/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001110///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001111void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001112 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001113 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001114 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001115 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001116 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001117 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001118 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001119 Diag (Tok, diag::err_expected_lbrace);
1120 return;
1121 }
1122
1123 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001124}
Anders Carlssona66cad42007-08-21 17:43:55 +00001125
Steve Narofffb9dd752007-10-15 20:55:58 +00001126Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001127
1128 switch (Tok.getKind()) {
1129 case tok::string_literal: // primary-expression: string-literal
1130 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001131 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001132 default:
1133 break;
1134 }
1135
1136 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001137 case tok::objc_encode:
1138 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1139 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001140 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001141 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001142 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001143 default:
1144 Diag(AtLoc, diag::err_unexpected_at);
1145 SkipUntil(tok::semi);
1146 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001147 }
1148
1149 return 0;
1150}
1151
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001152/// objc-message-expr:
1153/// '[' objc-receiver objc-message-args ']'
1154///
1155/// objc-receiver:
1156/// expression
1157/// class-name
1158/// type-name
1159///
1160/// objc-message-args:
1161/// objc-selector
1162/// objc-keywordarg-list
1163///
1164/// objc-keywordarg-list:
1165/// objc-keywordarg
1166/// objc-keywordarg-list objc-keywordarg
1167///
1168/// objc-keywordarg:
1169/// selector-name[opt] ':' objc-keywordexpr
1170///
1171/// objc-keywordexpr:
1172/// nonempty-expr-list
1173///
1174/// nonempty-expr-list:
1175/// assignment-expression
1176/// nonempty-expr-list , assignment-expression
1177///
1178Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001179 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001180 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001181 IdentifierInfo *ReceiverName = 0;
1182 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001183 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001184 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001185 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1186 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001187 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001188 } else {
1189 ExprResult Res = ParseAssignmentExpression();
1190 if (Res.isInvalid) {
1191 SkipUntil(tok::identifier);
1192 return Res;
1193 }
1194 ReceiverExpr = Res.Val;
1195 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001196 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001197 SourceLocation Loc;
1198 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001199
1200 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1201 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1202
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001203 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001204 while (1) {
1205 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001206 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001207
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001208 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001209 Diag(Tok, diag::err_expected_colon);
1210 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001211 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001212 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001213 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001214 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001215 ExprResult Res = ParseAssignmentExpression();
1216 if (Res.isInvalid) {
1217 SkipUntil(tok::identifier);
1218 return Res;
1219 }
1220 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001221 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001222
1223 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001224 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001225 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001226 break;
1227 // We have a selector or a colon, continue parsing.
1228 }
1229 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001230 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001231 ConsumeToken();
1232 /// Parse the expression after ','
1233 ParseAssignmentExpression();
1234 }
1235 } else if (!selIdent) {
1236 Diag(Tok, diag::err_expected_ident); // missing selector name.
1237 SkipUntil(tok::semi);
1238 return 0;
1239 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001240
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001241 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001242 Diag(Tok, diag::err_expected_rsquare);
1243 SkipUntil(tok::semi);
1244 return 0;
1245 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001246 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001247
Steve Narofff9e80db2007-10-05 18:42:47 +00001248 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001249 if (nKeys == 0)
1250 KeyIdents.push_back(selIdent);
1251 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1252
1253 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001254 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001255 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1256 &KeyExprs[0]);
1257 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1258 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001259}
1260
Anders Carlssona66cad42007-08-21 17:43:55 +00001261Parser::ExprResult Parser::ParseObjCStringLiteral() {
1262 ExprResult Res = ParseStringLiteralExpression();
1263
1264 if (Res.isInvalid) return Res;
1265
1266 return Actions.ParseObjCStringLiteral(Res.Val);
1267}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001268
1269/// objc-encode-expression:
1270/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001271Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001272 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001273
1274 SourceLocation EncLoc = ConsumeToken();
1275
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001276 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001277 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1278 return true;
1279 }
1280
1281 SourceLocation LParenLoc = ConsumeParen();
1282
1283 TypeTy *Ty = ParseTypeName();
1284
Anders Carlsson92faeb82007-08-23 15:31:37 +00001285 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001286
Chris Lattnercfd61c82007-10-16 22:51:17 +00001287 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001288 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001289}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001290
1291/// objc-protocol-expression
1292/// @protocol ( protocol-name )
1293
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001294Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001295{
1296 SourceLocation ProtoLoc = ConsumeToken();
1297
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001298 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001299 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1300 return true;
1301 }
1302
1303 SourceLocation LParenLoc = ConsumeParen();
1304
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001305 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001306 Diag(Tok, diag::err_expected_ident);
1307 return true;
1308 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001309 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001310 ConsumeToken();
1311
Anders Carlsson92faeb82007-08-23 15:31:37 +00001312 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001313
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001314 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1315 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001316}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001317
1318/// objc-selector-expression
1319/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001320Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001321{
1322 SourceLocation SelectorLoc = ConsumeToken();
1323
1324 if (Tok.isNot(tok::l_paren)) {
1325 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1326 return 0;
1327 }
1328
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001329 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001330 SourceLocation LParenLoc = ConsumeParen();
1331 SourceLocation sLoc;
1332 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1333 if (!SelIdent && Tok.isNot(tok::colon)) {
1334
1335 Diag(Tok, diag::err_expected_ident); // missing selector name.
1336 return 0;
1337 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001338 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001339 if (Tok.isNot(tok::r_paren))
1340 while (1) {
1341 if (Tok.isNot(tok::colon)) {
1342 Diag(Tok, diag::err_expected_colon);
1343 break;
1344 }
1345 ConsumeToken(); // Eat the ':'.
1346 if (Tok.is(tok::r_paren))
1347 break;
1348 // Check for another keyword selector.
1349 SourceLocation Loc;
1350 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001351 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001352 if (!SelIdent && Tok.isNot(tok::colon))
1353 break;
1354 }
1355 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001356 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1357 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001358 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001359 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001360 }