blob: 5d691a63ae5404fac419a4139461f08f73e30f4a [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"
Fariborz Jahanian06798362007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
22/// ParseExternalDeclaration:
23/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff5b96e2e2007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Steve Narofffb367882007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff87c329f2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000037 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000038 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000039 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000040 case tok::objc_implementation:
Steve Naroff81f1bba2007-09-06 21:24:23 +000041 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000042 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000043 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000044 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000045 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000046 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000050 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000053 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Narofffb367882007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000069 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000070 }
Chris Lattner4b009652007-07-25 00:24:17 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnera1d2bb72007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000082 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000083
Steve Naroff415c1832007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Steve Narofffb367882007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
94/// '@' 'interface' identifier objc-superclass[opt]
95/// objc-protocol-refs[opt]
96/// objc-class-instance-variables[opt]
97/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
101/// '@' 'interface' identifier '(' identifier[opt] ')'
102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
116Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
124 return 0;
125 }
126 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000134 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000135
Steve Naroffa7f62782007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000143 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
147 return 0;
148 }
149 rparenLoc = ConsumeParen();
Steve Naroffef20ed32007-10-30 02:23:23 +0000150 SourceLocation endProtoLoc;
Steve Narofffb367882007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000152 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000153 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000154 return 0;
155 }
156 if (attrList) // categories don't support attributes.
157 Diag(Tok, diag::err_objc_no_attributes_on_category);
158
Steve Naroff415c1832007-10-10 17:32:04 +0000159 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000160 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000161 &ProtocolRefs[0], ProtocolRefs.size(),
162 endProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000163
164 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000165
Steve Naroff0bbffd82007-08-22 16:35:03 +0000166 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000167 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000168 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000169 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000170 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000171 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000172 return 0;
173 }
174 // Parse a class interface.
175 IdentifierInfo *superClassId = 0;
176 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000177
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000178 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000179 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000181 Diag(Tok, diag::err_expected_ident); // missing super class name.
182 return 0;
183 }
184 superClassId = Tok.getIdentifierInfo();
185 superClassLoc = ConsumeToken();
186 }
187 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000188 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000189 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000190 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000191 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000192 return 0;
193 }
Steve Naroff415c1832007-10-10 17:32:04 +0000194 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000195 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000196 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000197 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000198
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000199 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000200 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000201
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000202 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000203
204 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000205 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000206 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000207 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000208 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000209 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000210 return 0;
211}
212
213/// objc-interface-decl-list:
214/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000225void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
226 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000227 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000228 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000229 SourceLocation AtEndLoc;
230
Steve Naroff0bbffd82007-08-22 16:35:03 +0000231 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000232 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000233 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000234 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000235
236 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000237 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000238 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000239 } else if (ocKind == tok::objc_required) { // protocols only
240 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000241 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000242 if (contextKey != tok::objc_protocol)
243 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000244 } else if (ocKind == tok::objc_optional) { // protocols only
245 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000246 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000247 if (contextKey != tok::objc_protocol)
248 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000249 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000250 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000251 continue;
252 } else {
253 Diag(Tok, diag::err_objc_illegal_interface_qual);
254 ConsumeToken();
255 }
256 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000257 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
258 DeclTy *methodPrototype =
259 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000260 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000261 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
262 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000263 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000264 continue;
265 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000266 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000267 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000268 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000269 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000270 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000271 // FIXME: as the name implies, this rule allows function definitions.
272 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000273 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000274 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000275 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000276 /// Insert collected methods declarations into the @interface object.
277 /// This action is executed even if we don't have any methods (so the @end
278 /// can be recorded properly).
279 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl, &allMethods[0],
280 allMethods.size(), AtEndLoc);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000281}
282
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000283/// Parse property attribute declarations.
284///
285/// property-attr-decl: '(' property-attrlist ')'
286/// property-attrlist:
287/// property-attribute
288/// property-attrlist ',' property-attribute
289/// property-attribute:
290/// getter '=' identifier
291/// setter '=' identifier ':'
292/// readonly
293/// readwrite
294/// assign
295/// retain
296/// copy
297/// nonatomic
298///
299void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
300 SourceLocation loc = ConsumeParen(); // consume '('
301 while (isObjCPropertyAttribute()) {
302 const IdentifierInfo *II = Tok.getIdentifierInfo();
303 // getter/setter require extra treatment.
304 if (II == ObjcPropertyAttrs[objc_getter] ||
305 II == ObjcPropertyAttrs[objc_setter]) {
306 // skip getter/setter part.
307 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000308 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000309 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000310 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000311 if (II == ObjcPropertyAttrs[objc_setter]) {
312 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000313 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000314 Diag(loc, diag::err_expected_colon);
315 SkipUntil(tok::r_paren,true,true);
316 break;
317 }
318 }
319 }
320 else {
321 Diag(loc, diag::err_expected_ident);
322 SkipUntil(tok::r_paren,true,true);
323 break;
324 }
325 }
326 else {
327 Diag(loc, diag::err_objc_expected_equal);
328 SkipUntil(tok::r_paren,true,true);
329 break;
330 }
331 }
332 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000333 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000334 loc = ConsumeToken();
335 continue;
336 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000337 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000338 break;
339 Diag(loc, diag::err_expected_rparen);
340 SkipUntil(tok::semi);
341 return;
342 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000343 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000344 ConsumeParen();
345 else {
346 Diag(loc, diag::err_objc_expected_property_attr);
347 SkipUntil(tok::r_paren); // recover from error inside attribute list
348 }
349}
350
351/// Main routine to parse property declaration.
352///
353/// @property property-attr-decl[opt] property-component-decl ';'
354///
355void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
356 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
357 "ParseObjCPropertyDecl(): Expected @property");
358 ConsumeToken(); // the "property" identifier
359 // Parse property attribute list, if any.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000360 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000361 // property has attribute list.
362 ParseObjCPropertyAttribute(0/*FIXME*/);
363 }
364 // Parse declaration portion of @property.
365 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
366 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000367 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000368 ConsumeToken();
369 else {
370 Diag(Tok, diag::err_expected_semi_decl_list);
371 SkipUntil(tok::r_brace, true, true);
372 }
Chris Lattner4b009652007-07-25 00:24:17 +0000373}
Steve Narofffb367882007-08-20 21:31:48 +0000374
Steve Naroff81f1bba2007-09-06 21:24:23 +0000375/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000376/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000377/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000378///
379/// objc-instance-method: '-'
380/// objc-class-method: '+'
381///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000382/// objc-method-attributes: [OBJC2]
383/// __attribute__((deprecated))
384///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000385Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
386 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000387 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000388
389 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000390 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000391
Steve Naroff3774dd92007-10-26 20:53:56 +0000392 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000393 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000394 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000395 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000396}
397
398/// objc-selector:
399/// identifier
400/// one of
401/// enum struct union if else while do for switch case default
402/// break continue return goto asm sizeof typeof __alignof
403/// unsigned long const short volatile signed restrict _Complex
404/// in out inout bycopy byref oneway int char float double void _Bool
405///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000406IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000407 switch (Tok.getKind()) {
408 default:
409 return 0;
410 case tok::identifier:
411 case tok::kw_typeof:
412 case tok::kw___alignof:
413 case tok::kw_auto:
414 case tok::kw_break:
415 case tok::kw_case:
416 case tok::kw_char:
417 case tok::kw_const:
418 case tok::kw_continue:
419 case tok::kw_default:
420 case tok::kw_do:
421 case tok::kw_double:
422 case tok::kw_else:
423 case tok::kw_enum:
424 case tok::kw_extern:
425 case tok::kw_float:
426 case tok::kw_for:
427 case tok::kw_goto:
428 case tok::kw_if:
429 case tok::kw_inline:
430 case tok::kw_int:
431 case tok::kw_long:
432 case tok::kw_register:
433 case tok::kw_restrict:
434 case tok::kw_return:
435 case tok::kw_short:
436 case tok::kw_signed:
437 case tok::kw_sizeof:
438 case tok::kw_static:
439 case tok::kw_struct:
440 case tok::kw_switch:
441 case tok::kw_typedef:
442 case tok::kw_union:
443 case tok::kw_unsigned:
444 case tok::kw_void:
445 case tok::kw_volatile:
446 case tok::kw_while:
447 case tok::kw__Bool:
448 case tok::kw__Complex:
449 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000450 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000451 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000452 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000453}
454
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000455/// property-attrlist: one of
456/// readonly getter setter assign retain copy nonatomic
457///
458bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000459 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000460 const IdentifierInfo *II = Tok.getIdentifierInfo();
461 for (unsigned i = 0; i < objc_NumAttrs; ++i)
462 if (II == ObjcPropertyAttrs[i]) return true;
463 }
464 return false;
465}
466
Steve Naroff0bbffd82007-08-22 16:35:03 +0000467/// objc-type-name:
468/// '(' objc-type-qualifiers[opt] type-name ')'
469/// '(' objc-type-qualifiers[opt] ')'
470///
471/// objc-type-qualifiers:
472/// objc-type-qualifier
473/// objc-type-qualifiers objc-type-qualifier
474///
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000475Parser::TypeTy *Parser::ParseObjCTypeName(ObjcDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000476 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000477
478 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000479 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000480
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000481 // Parse type qualifiers, in, inout, etc.
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000482 ParseObjcTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000483
Steve Naroff0bbffd82007-08-22 16:35:03 +0000484 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000485 Ty = ParseTypeName();
486 // FIXME: back when Sema support is in place...
487 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000488 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000489 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000490 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000491 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000492 }
493 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000494 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000495}
496
497/// objc-method-decl:
498/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000499/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000500/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000501/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000502///
503/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000504/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000505/// objc-keyword-selector objc-keyword-decl
506///
507/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000508/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
509/// objc-selector ':' objc-keyword-attributes[opt] identifier
510/// ':' objc-type-name objc-keyword-attributes[opt] identifier
511/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000512///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000513/// objc-parmlist:
514/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000515///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000516/// objc-parms:
517/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000518///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000519/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000520/// , ...
521///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000522/// objc-keyword-attributes: [OBJC2]
523/// __attribute__((unused))
524///
Steve Naroff3774dd92007-10-26 20:53:56 +0000525Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
526 tok::TokenKind mType,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000527 tok::ObjCKeywordKind MethodImplKind)
528{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000529 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000530 TypeTy *ReturnType = 0;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000531 ObjcDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000532 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000533 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000534 SourceLocation selLoc;
535 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000536 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000537 if (!SelIdent) {
538 Diag(Tok, diag::err_expected_ident); // missing selector name.
539 // FIXME: this creates a unary selector with a null identifier, is this
540 // ok?? Maybe we should skip to the next semicolon or something.
541 }
542
543 // If attributes exist after the method, parse them.
544 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000545 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000546 MethodAttrs = ParseAttributes();
547
548 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000549 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000550 mType, DSRet, ReturnType, Sel,
551 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000552 }
Steve Naroff304ed392007-09-05 23:30:30 +0000553
Steve Naroff4ed9d662007-09-27 14:38:14 +0000554 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
555 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000556 llvm::SmallVector<ObjcDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000557 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000558
559 Action::TypeTy *TypeInfo;
560 while (1) {
561 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000562
Chris Lattnerd031a452007-10-07 02:00:24 +0000563 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000564 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000565 Diag(Tok, diag::err_expected_colon);
566 break;
567 }
568 ConsumeToken(); // Eat the ':'.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000569 ObjcDeclSpec DSType;
570 if (Tok.is(tok::l_paren)) { // Parse the argument type.
571 TypeInfo = ParseObjCTypeName(DSType);
572 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000573 else
574 TypeInfo = 0;
575 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000576 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000577
Chris Lattnerd031a452007-10-07 02:00:24 +0000578 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000579 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000580 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000581
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000582 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000583 Diag(Tok, diag::err_expected_ident); // missing argument name.
584 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000585 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000586 ArgNames.push_back(Tok.getIdentifierInfo());
587 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000588
Chris Lattnerd031a452007-10-07 02:00:24 +0000589 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000590 SourceLocation Loc;
591 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000592 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000593 break;
594 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000595 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000596
597 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000598 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000599 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000600 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000601 ConsumeToken();
602 break;
603 }
604 // Parse the c-style argument declaration-specifier.
605 DeclSpec DS;
606 ParseDeclarationSpecifiers(DS);
607 // Parse the declarator.
608 Declarator ParmDecl(DS, Declarator::PrototypeContext);
609 ParseDeclarator(ParmDecl);
610 }
611
612 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000613 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000614 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000615 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000616 MethodAttrs = ParseAttributes();
617
618 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
619 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000620 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000621 mType, DSRet, ReturnType, Sel,
622 &ArgTypeQuals[0], &KeyTypes[0],
623 &ArgNames[0],
Chris Lattnerd031a452007-10-07 02:00:24 +0000624 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000625}
626
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000627/// CmpProtocolVals - Comparison predicate for sorting protocols.
628static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
629 const IdentifierInfo* const& rhs) {
630 return strcmp(lhs->getName(), rhs->getName()) < 0;
631}
632
Steve Narofffb367882007-08-20 21:31:48 +0000633/// objc-protocol-refs:
634/// '<' identifier-list '>'
635///
Steve Naroff304ed392007-09-05 23:30:30 +0000636bool Parser::ParseObjCProtocolReferences(
Steve Naroffef20ed32007-10-30 02:23:23 +0000637 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
638{
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());
Steve Narofffb367882007-08-20 21:31:48 +0000664 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000665 if (Tok.is(tok::greater)) {
666 endLoc = ConsumeAnyToken();
667 return false;
668 }
669 Diag(Tok, diag::err_expected_greater);
670 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000671}
672
673/// objc-class-instance-variables:
674/// '{' objc-instance-variable-decl-list[opt] '}'
675///
676/// objc-instance-variable-decl-list:
677/// objc-visibility-spec
678/// objc-instance-variable-decl ';'
679/// ';'
680/// objc-instance-variable-decl-list objc-visibility-spec
681/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
682/// objc-instance-variable-decl-list ';'
683///
684/// objc-visibility-spec:
685/// @private
686/// @protected
687/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000688/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000689///
690/// objc-instance-variable-decl:
691/// struct-declaration
692///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000693void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
694 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000695 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000696 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000697 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
698 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000699
700 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000701
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000702 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000703 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000704 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000705 // Each iteration of this loop reads one objc-instance-variable-decl.
706
707 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000708 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000709 Diag(Tok, diag::ext_extra_struct_semi);
710 ConsumeToken();
711 continue;
712 }
713 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000714 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000715 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000716 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000717 case tok::objc_private:
718 case tok::objc_public:
719 case tok::objc_protected:
720 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000721 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000722 ConsumeToken();
723 continue;
724 default:
725 Diag(Tok, diag::err_objc_illegal_visibility_spec);
726 ConsumeToken();
727 continue;
728 }
729 }
730 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000731 for (unsigned i = 0; i < IvarDecls.size(); i++) {
732 AllIvarDecls.push_back(IvarDecls[i]);
733 AllVisibilities.push_back(visibility);
734 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000735 IvarDecls.clear();
736
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000737 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000738 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000739 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000740 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
741 break;
742 } else {
743 Diag(Tok, diag::err_expected_semi_decl_list);
744 // Skip to end of block or statement
745 SkipUntil(tok::r_brace, true, true);
746 }
747 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000748 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000749 // Call ActOnFields() even if we don't have any decls. This is useful
750 // for code rewriting tools that need to be aware of the empty list.
751 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
752 &AllIvarDecls[0], AllIvarDecls.size(),
753 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Steve Naroffc4474992007-08-21 21:17:12 +0000754 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000755}
Steve Narofffb367882007-08-20 21:31:48 +0000756
757/// objc-protocol-declaration:
758/// objc-protocol-definition
759/// objc-protocol-forward-reference
760///
761/// objc-protocol-definition:
762/// @protocol identifier
763/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000764/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000765/// @end
766///
767/// objc-protocol-forward-reference:
768/// @protocol identifier-list ';'
769///
770/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000771/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000772/// semicolon in the first alternative if objc-protocol-refs are omitted.
773
Steve Naroff72f17fb2007-08-22 22:17:26 +0000774Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000775 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000776 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
777 ConsumeToken(); // the "protocol" identifier
778
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000779 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000780 Diag(Tok, diag::err_expected_ident); // missing protocol name.
781 return 0;
782 }
783 // Save the protocol name, then consume it.
784 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
785 SourceLocation nameLoc = ConsumeToken();
786
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000787 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000788 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000789 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000790 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000791 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000792 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000793 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000794 ProtocolRefs.push_back(protocolName);
795
796 while (1) {
797 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000798 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000799 Diag(Tok, diag::err_expected_ident);
800 SkipUntil(tok::semi);
801 return 0;
802 }
803 ProtocolRefs.push_back(Tok.getIdentifierInfo());
804 ConsumeToken(); // the identifier
805
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000806 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000807 break;
808 }
809 // Consume the ';'.
810 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
811 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000812 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000813 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000814 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000815 &ProtocolRefs[0],
816 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000817 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000818 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000819 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000820 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000821 return 0;
822 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000823
Steve Naroff415c1832007-10-10 17:32:04 +0000824 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000825 protocolName, nameLoc,
826 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000827 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000828 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000829
830 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000831 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000832 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000833 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000834 }
835 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000836 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000837}
Steve Narofffb367882007-08-20 21:31:48 +0000838
839/// objc-implementation:
840/// objc-class-implementation-prologue
841/// objc-category-implementation-prologue
842///
843/// objc-class-implementation-prologue:
844/// @implementation identifier objc-superclass[opt]
845/// objc-class-instance-variables[opt]
846///
847/// objc-category-implementation-prologue:
848/// @implementation identifier ( identifier )
849
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000850Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
851 SourceLocation atLoc) {
852 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
853 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
854 ConsumeToken(); // the "implementation" identifier
855
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000856 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000857 Diag(Tok, diag::err_expected_ident); // missing class or category name.
858 return 0;
859 }
860 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000861 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000862 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
863
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000864 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000865 // we have a category implementation.
866 SourceLocation lparenLoc = ConsumeParen();
867 SourceLocation categoryLoc, rparenLoc;
868 IdentifierInfo *categoryId = 0;
869
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000870 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000871 categoryId = Tok.getIdentifierInfo();
872 categoryLoc = ConsumeToken();
873 } else {
874 Diag(Tok, diag::err_expected_ident); // missing category name.
875 return 0;
876 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000877 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000878 Diag(Tok, diag::err_expected_rparen);
879 SkipUntil(tok::r_paren, false); // don't stop at ';'
880 return 0;
881 }
882 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000883 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000884 atLoc, nameId, nameLoc, categoryId,
885 categoryLoc);
886 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000887 }
888 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000889 SourceLocation superClassLoc;
890 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000891 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000892 // We have a super class
893 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000894 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000895 Diag(Tok, diag::err_expected_ident); // missing super class name.
896 return 0;
897 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000898 superClassId = Tok.getIdentifierInfo();
899 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000900 }
Steve Naroff415c1832007-10-10 17:32:04 +0000901 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000902 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000903 superClassId, superClassLoc);
904
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000905 if (Tok.is(tok::l_brace)) // we have ivars
906 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000907
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000908 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000909}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000910
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000911Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
912 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
913 "ParseObjCAtEndDeclaration(): Expected @end");
914 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000915 if (ObjcImpDecl) {
916 // Checking is not necessary except that a parse error might have caused
917 // @implementation not to have been parsed to completion and ObjcImpDecl
918 // could be 0.
919 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000920 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000921 &AllImplMethods[0], AllImplMethods.size(),
922 atLoc);
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000923 ObjcImpDecl = 0;
924 AllImplMethods.clear();
925 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000926
Steve Narofffb367882007-08-20 21:31:48 +0000927 return 0;
928}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000929
930/// compatibility-alias-decl:
931/// @compatibility_alias alias-name class-name ';'
932///
933Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
934 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
935 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
936 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000937 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000938 Diag(Tok, diag::err_expected_ident);
939 return 0;
940 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000941 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
942 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000943 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000944 Diag(Tok, diag::err_expected_ident);
945 return 0;
946 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000947 IdentifierInfo *classId = Tok.getIdentifierInfo();
948 SourceLocation classLoc = ConsumeToken(); // consume class-name;
949 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000950 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000951 return 0;
952 }
953 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
954 aliasId, aliasLoc,
955 classId, classLoc);
956 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000957}
958
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000959/// property-synthesis:
960/// @synthesize property-ivar-list ';'
961///
962/// property-ivar-list:
963/// property-ivar
964/// property-ivar-list ',' property-ivar
965///
966/// property-ivar:
967/// identifier
968/// identifier '=' identifier
969///
970Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
971 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
972 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
973 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000974 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000975 Diag(Tok, diag::err_expected_ident);
976 return 0;
977 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000978 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000979 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000980 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000981 // property '=' ivar-name
982 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000983 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000984 Diag(Tok, diag::err_expected_ident);
985 break;
986 }
987 ConsumeToken(); // consume ivar-name
988 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000989 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000990 break;
991 ConsumeToken(); // consume ','
992 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000993 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000994 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
995 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000996}
997
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000998/// property-dynamic:
999/// @dynamic property-list
1000///
1001/// property-list:
1002/// identifier
1003/// property-list ',' identifier
1004///
1005Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1006 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1007 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1008 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001009 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001010 Diag(Tok, diag::err_expected_ident);
1011 return 0;
1012 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001013 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001014 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001015 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001016 break;
1017 ConsumeToken(); // consume ','
1018 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001019 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001020 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1021 return 0;
1022}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001023
1024/// objc-throw-statement:
1025/// throw expression[opt];
1026///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001027Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001028 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001029 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001030 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001031 if (Res.isInvalid) {
1032 SkipUntil(tok::semi);
1033 return 0;
1034 }
1035 }
1036 return 0;
1037}
1038
1039/// objc-try-catch-statement:
1040/// @try compound-statement objc-catch-list[opt]
1041/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1042///
1043/// objc-catch-list:
1044/// @catch ( parameter-declaration ) compound-statement
1045/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1046/// catch-parameter-declaration:
1047/// parameter-declaration
1048/// '...' [OBJC2]
1049///
Fariborz Jahanian70952482007-11-01 21:12:44 +00001050Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001051 bool catch_or_finally_seen = false;
1052 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001053 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001054 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001055 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001056 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001057 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001058 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001059 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001060 while (Tok.is(tok::at)) {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001061 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001062 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001063 StmtTy *FirstPart = 0;
1064 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001065 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001066 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001067 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001068 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001069 DeclSpec DS;
1070 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001071 // FIXME: Is BlockContext right?
1072 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1073 ParseDeclarator(DeclaratorInfo);
1074 StmtResult stmtResult = Actions.ActOnDeclarator(CurScope,
1075 DeclaratorInfo, 0);
1076 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001077 }
1078 else
1079 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001080 SourceLocation RParenLoc = ConsumeParen();
Fariborz Jahanian70952482007-11-01 21:12:44 +00001081 StmtResult CatchBody = ParseCompoundStatementBody();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001082 if (CatchBody.isInvalid)
1083 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001084 CatchStmts = Actions.ActOnObjcAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001085 FirstPart, CatchBody.Val, CatchStmts.Val);
1086 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001087 }
1088 else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001089 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1090 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001091 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001092 }
1093 catch_or_finally_seen = true;
1094 }
1095 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1096 ConsumeToken(); // consume finally
1097 StmtResult FinallyBody = ParseCompoundStatementBody();
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001098 if (FinallyBody.isInvalid)
1099 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
1100 FinallyStmt = Actions.ActOnObjcAtFinallyStmt(AtCatchFinallyLoc,
1101 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001102 catch_or_finally_seen = true;
1103 break;
1104 }
1105 }
1106 if (!catch_or_finally_seen)
1107 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001108 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001109}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001110
Steve Naroff81f1bba2007-09-06 21:24:23 +00001111/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001112///
1113void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001114 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1115
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001116 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001117 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001118 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001119 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001120 ConsumeToken();
1121
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001122 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001123 Diag (Tok, diag::err_expected_lbrace);
1124 return;
1125 }
1126
1127 StmtResult FnBody = ParseCompoundStatementBody();
1128}
1129
Steve Naroff81f1bba2007-09-06 21:24:23 +00001130/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001131///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001132void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001133 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001134 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001135 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001136 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001137 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001138 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001139 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001140 Diag (Tok, diag::err_expected_lbrace);
1141 return;
1142 }
1143
1144 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001145}
Anders Carlssona66cad42007-08-21 17:43:55 +00001146
Steve Narofffb9dd752007-10-15 20:55:58 +00001147Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001148
1149 switch (Tok.getKind()) {
1150 case tok::string_literal: // primary-expression: string-literal
1151 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001152 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001153 default:
1154 break;
1155 }
1156
1157 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001158 case tok::objc_encode:
1159 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1160 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001161 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001162 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001163 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001164 default:
1165 Diag(AtLoc, diag::err_unexpected_at);
1166 SkipUntil(tok::semi);
1167 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001168 }
1169
1170 return 0;
1171}
1172
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001173/// objc-message-expr:
1174/// '[' objc-receiver objc-message-args ']'
1175///
1176/// objc-receiver:
1177/// expression
1178/// class-name
1179/// type-name
1180///
1181/// objc-message-args:
1182/// objc-selector
1183/// objc-keywordarg-list
1184///
1185/// objc-keywordarg-list:
1186/// objc-keywordarg
1187/// objc-keywordarg-list objc-keywordarg
1188///
1189/// objc-keywordarg:
1190/// selector-name[opt] ':' objc-keywordexpr
1191///
1192/// objc-keywordexpr:
1193/// nonempty-expr-list
1194///
1195/// nonempty-expr-list:
1196/// assignment-expression
1197/// nonempty-expr-list , assignment-expression
1198///
1199Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001200 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001201 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001202 IdentifierInfo *ReceiverName = 0;
1203 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001204 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001205 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001206 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1207 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001208 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001209 } else {
1210 ExprResult Res = ParseAssignmentExpression();
1211 if (Res.isInvalid) {
1212 SkipUntil(tok::identifier);
1213 return Res;
1214 }
1215 ReceiverExpr = Res.Val;
1216 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001217 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001218 SourceLocation Loc;
1219 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001220
1221 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1222 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1223
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001224 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001225 while (1) {
1226 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001227 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001228
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001229 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001230 Diag(Tok, diag::err_expected_colon);
1231 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001232 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001233 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001234 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001235 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001236 ExprResult Res = ParseAssignmentExpression();
1237 if (Res.isInvalid) {
1238 SkipUntil(tok::identifier);
1239 return Res;
1240 }
1241 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001242 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001243
1244 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001245 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001246 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001247 break;
1248 // We have a selector or a colon, continue parsing.
1249 }
1250 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001251 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001252 ConsumeToken();
1253 /// Parse the expression after ','
1254 ParseAssignmentExpression();
1255 }
1256 } else if (!selIdent) {
1257 Diag(Tok, diag::err_expected_ident); // missing selector name.
1258 SkipUntil(tok::semi);
1259 return 0;
1260 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001261
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001262 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001263 Diag(Tok, diag::err_expected_rsquare);
1264 SkipUntil(tok::semi);
1265 return 0;
1266 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001267 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001268
Steve Narofff9e80db2007-10-05 18:42:47 +00001269 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001270 if (nKeys == 0)
1271 KeyIdents.push_back(selIdent);
1272 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1273
1274 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001275 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001276 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1277 &KeyExprs[0]);
1278 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1279 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001280}
1281
Anders Carlssona66cad42007-08-21 17:43:55 +00001282Parser::ExprResult Parser::ParseObjCStringLiteral() {
1283 ExprResult Res = ParseStringLiteralExpression();
1284
1285 if (Res.isInvalid) return Res;
1286
1287 return Actions.ParseObjCStringLiteral(Res.Val);
1288}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001289
1290/// objc-encode-expression:
1291/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001292Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001293 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001294
1295 SourceLocation EncLoc = ConsumeToken();
1296
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001297 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001298 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1299 return true;
1300 }
1301
1302 SourceLocation LParenLoc = ConsumeParen();
1303
1304 TypeTy *Ty = ParseTypeName();
1305
Anders Carlsson92faeb82007-08-23 15:31:37 +00001306 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001307
Chris Lattnercfd61c82007-10-16 22:51:17 +00001308 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001309 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001310}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001311
1312/// objc-protocol-expression
1313/// @protocol ( protocol-name )
1314
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001315Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001316{
1317 SourceLocation ProtoLoc = ConsumeToken();
1318
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001319 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001320 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1321 return true;
1322 }
1323
1324 SourceLocation LParenLoc = ConsumeParen();
1325
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001326 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001327 Diag(Tok, diag::err_expected_ident);
1328 return true;
1329 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001330 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001331 ConsumeToken();
1332
Anders Carlsson92faeb82007-08-23 15:31:37 +00001333 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001334
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001335 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1336 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001337}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001338
1339/// objc-selector-expression
1340/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001341Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001342{
1343 SourceLocation SelectorLoc = ConsumeToken();
1344
1345 if (Tok.isNot(tok::l_paren)) {
1346 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1347 return 0;
1348 }
1349
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001350 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001351 SourceLocation LParenLoc = ConsumeParen();
1352 SourceLocation sLoc;
1353 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1354 if (!SelIdent && Tok.isNot(tok::colon)) {
1355
1356 Diag(Tok, diag::err_expected_ident); // missing selector name.
1357 return 0;
1358 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001359 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001360 if (Tok.isNot(tok::r_paren))
1361 while (1) {
1362 if (Tok.isNot(tok::colon)) {
1363 Diag(Tok, diag::err_expected_colon);
1364 break;
1365 }
1366 ConsumeToken(); // Eat the ':'.
1367 if (Tok.is(tok::r_paren))
1368 break;
1369 // Check for another keyword selector.
1370 SourceLocation Loc;
1371 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001372 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001373 if (!SelIdent && Tok.isNot(tok::colon))
1374 break;
1375 }
1376 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001377 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1378 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001379 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001380 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001381 }