blob: 76a9a969b26159e05577919b16f7bb5abd4102ae [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:
Fariborz Jahanian83ddf822007-11-10 20:59:13 +000041 return 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 Jahaniand8df6d82007-11-06 22:01:00 +0000228 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000229 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000230 SourceLocation AtEndLoc;
231
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000233 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000234 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000235 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000236
237 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000238 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000239 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000240 } else if (ocKind == tok::objc_required) { // 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_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000245 } else if (ocKind == tok::objc_optional) { // protocols only
246 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000247 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000248 if (contextKey != tok::objc_protocol)
249 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000250 } else if (ocKind == tok::objc_property) {
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000251 allProperties.push_back(ParseObjCPropertyDecl(interfaceDecl, AtLoc));
Steve Naroff0bbffd82007-08-22 16:35:03 +0000252 continue;
253 } else {
254 Diag(Tok, diag::err_objc_illegal_interface_qual);
255 ConsumeToken();
256 }
257 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000258 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
259 DeclTy *methodPrototype =
260 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000261 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000262 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
263 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000264 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000265 continue;
266 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000267 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000268 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000269 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000270 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000271 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000272 // FIXME: as the name implies, this rule allows function definitions.
273 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000274 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000275 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000276 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000277 /// Insert collected methods declarations into the @interface object.
Steve Naroffb82c50f2007-11-11 17:19:15 +0000278 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
279 &allProperties[0], allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000280}
281
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000282/// Parse property attribute declarations.
283///
284/// property-attr-decl: '(' property-attrlist ')'
285/// property-attrlist:
286/// property-attribute
287/// property-attrlist ',' property-attribute
288/// property-attribute:
289/// getter '=' identifier
290/// setter '=' identifier ':'
291/// readonly
292/// readwrite
293/// assign
294/// retain
295/// copy
296/// nonatomic
297///
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000298void Parser::ParseObjCPropertyAttribute (ObjcDeclSpec &DS) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000299 SourceLocation loc = ConsumeParen(); // consume '('
300 while (isObjCPropertyAttribute()) {
301 const IdentifierInfo *II = Tok.getIdentifierInfo();
302 // getter/setter require extra treatment.
303 if (II == ObjcPropertyAttrs[objc_getter] ||
304 II == ObjcPropertyAttrs[objc_setter]) {
305 // skip getter/setter part.
306 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000307 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000308 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000309 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000310 if (II == ObjcPropertyAttrs[objc_setter]) {
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000311 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_setter);
312 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000313 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000314 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000315 Diag(loc, diag::err_expected_colon);
316 SkipUntil(tok::r_paren,true,true);
317 break;
318 }
319 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000320 else {
321 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_getter);
322 DS.setGetterName(Tok.getIdentifierInfo());
323 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000324 }
325 else {
326 Diag(loc, diag::err_expected_ident);
327 SkipUntil(tok::r_paren,true,true);
328 break;
329 }
330 }
331 else {
332 Diag(loc, diag::err_objc_expected_equal);
333 SkipUntil(tok::r_paren,true,true);
334 break;
335 }
336 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000337
338 else if (II == ObjcPropertyAttrs[objc_readonly])
339 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_readonly);
340 else if (II == ObjcPropertyAttrs[objc_assign])
341 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_assign);
342 else if (II == ObjcPropertyAttrs[objc_readwrite])
343 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_readwrite);
344 else if (II == ObjcPropertyAttrs[objc_retain])
345 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_retain);
346 else if (II == ObjcPropertyAttrs[objc_copy])
347 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_copy);
348 else if (II == ObjcPropertyAttrs[objc_nonatomic])
349 DS.setPropertyAttributes(ObjcDeclSpec::DQ_PR_nonatomic);
350
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000351 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000352 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000353 loc = ConsumeToken();
354 continue;
355 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000356 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000357 break;
358 Diag(loc, diag::err_expected_rparen);
359 SkipUntil(tok::semi);
360 return;
361 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000362 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000363 ConsumeParen();
364 else {
365 Diag(loc, diag::err_objc_expected_property_attr);
366 SkipUntil(tok::r_paren); // recover from error inside attribute list
367 }
368}
369
370/// Main routine to parse property declaration.
371///
372/// @property property-attr-decl[opt] property-component-decl ';'
373///
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000374Parser::DeclTy *Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl,
375 SourceLocation AtLoc) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000376 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
377 "ParseObjCPropertyDecl(): Expected @property");
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000378 ObjcDeclSpec DS;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000379 ConsumeToken(); // the "property" identifier
380 // Parse property attribute list, if any.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000381 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000382 // property has attribute list.
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000383 ParseObjCPropertyAttribute(DS);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000384 }
385 // Parse declaration portion of @property.
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000386 llvm::SmallVector<DeclTy*, 8> PropertyDecls;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000387 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000388 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000389 ConsumeToken();
390 else {
391 Diag(Tok, diag::err_expected_semi_decl_list);
392 SkipUntil(tok::r_brace, true, true);
393 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000394 return Actions.ActOnAddObjcProperties(AtLoc,
395 &PropertyDecls[0], PropertyDecls.size(), DS);
Chris Lattner4b009652007-07-25 00:24:17 +0000396}
Steve Narofffb367882007-08-20 21:31:48 +0000397
Steve Naroff81f1bba2007-09-06 21:24:23 +0000398/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000399/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000400/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000401///
402/// objc-instance-method: '-'
403/// objc-class-method: '+'
404///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000405/// objc-method-attributes: [OBJC2]
406/// __attribute__((deprecated))
407///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000408Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
409 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000410 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000411
412 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000413 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000414
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000415 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000416 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000417 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000418 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000419}
420
421/// objc-selector:
422/// identifier
423/// one of
424/// enum struct union if else while do for switch case default
425/// break continue return goto asm sizeof typeof __alignof
426/// unsigned long const short volatile signed restrict _Complex
427/// in out inout bycopy byref oneway int char float double void _Bool
428///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000429IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000430 switch (Tok.getKind()) {
431 default:
432 return 0;
433 case tok::identifier:
434 case tok::kw_typeof:
435 case tok::kw___alignof:
436 case tok::kw_auto:
437 case tok::kw_break:
438 case tok::kw_case:
439 case tok::kw_char:
440 case tok::kw_const:
441 case tok::kw_continue:
442 case tok::kw_default:
443 case tok::kw_do:
444 case tok::kw_double:
445 case tok::kw_else:
446 case tok::kw_enum:
447 case tok::kw_extern:
448 case tok::kw_float:
449 case tok::kw_for:
450 case tok::kw_goto:
451 case tok::kw_if:
452 case tok::kw_inline:
453 case tok::kw_int:
454 case tok::kw_long:
455 case tok::kw_register:
456 case tok::kw_restrict:
457 case tok::kw_return:
458 case tok::kw_short:
459 case tok::kw_signed:
460 case tok::kw_sizeof:
461 case tok::kw_static:
462 case tok::kw_struct:
463 case tok::kw_switch:
464 case tok::kw_typedef:
465 case tok::kw_union:
466 case tok::kw_unsigned:
467 case tok::kw_void:
468 case tok::kw_volatile:
469 case tok::kw_while:
470 case tok::kw__Bool:
471 case tok::kw__Complex:
472 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000473 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000474 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000475 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000476}
477
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000478/// property-attrlist: one of
479/// readonly getter setter assign retain copy nonatomic
480///
481bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000482 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000483 const IdentifierInfo *II = Tok.getIdentifierInfo();
484 for (unsigned i = 0; i < objc_NumAttrs; ++i)
485 if (II == ObjcPropertyAttrs[i]) return true;
486 }
487 return false;
488}
489
Steve Naroff0bbffd82007-08-22 16:35:03 +0000490/// objc-type-name:
491/// '(' objc-type-qualifiers[opt] type-name ')'
492/// '(' objc-type-qualifiers[opt] ')'
493///
494/// objc-type-qualifiers:
495/// objc-type-qualifier
496/// objc-type-qualifiers objc-type-qualifier
497///
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000498Parser::TypeTy *Parser::ParseObjCTypeName(ObjcDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000499 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000500
501 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000502 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000503
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000504 // Parse type qualifiers, in, inout, etc.
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000505 ParseObjcTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000506
Steve Naroff0bbffd82007-08-22 16:35:03 +0000507 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000508 Ty = ParseTypeName();
509 // FIXME: back when Sema support is in place...
510 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000511 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000512 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000513 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000514 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000515 }
516 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000517 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000518}
519
520/// objc-method-decl:
521/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000522/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000523/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000524/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000525///
526/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000527/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000528/// objc-keyword-selector objc-keyword-decl
529///
530/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000531/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
532/// objc-selector ':' objc-keyword-attributes[opt] identifier
533/// ':' objc-type-name objc-keyword-attributes[opt] identifier
534/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000535///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000536/// objc-parmlist:
537/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000538///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000539/// objc-parms:
540/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000541///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000542/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000543/// , ...
544///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000545/// objc-keyword-attributes: [OBJC2]
546/// __attribute__((unused))
547///
Steve Naroff3774dd92007-10-26 20:53:56 +0000548Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000549 tok::TokenKind mType,
550 DeclTy *IDecl,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000551 tok::ObjCKeywordKind MethodImplKind)
552{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000553 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000554 TypeTy *ReturnType = 0;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000555 ObjcDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000556 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000557 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000558 SourceLocation selLoc;
559 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000560 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000561 if (!SelIdent) {
562 Diag(Tok, diag::err_expected_ident); // missing selector name.
563 // FIXME: this creates a unary selector with a null identifier, is this
564 // ok?? Maybe we should skip to the next semicolon or something.
565 }
566
567 // If attributes exist after the method, parse them.
568 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000569 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000570 MethodAttrs = ParseAttributes();
571
572 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000573 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000574 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000575 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000576 }
Steve Naroff304ed392007-09-05 23:30:30 +0000577
Steve Naroff4ed9d662007-09-27 14:38:14 +0000578 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
579 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000580 llvm::SmallVector<ObjcDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000581 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000582
583 Action::TypeTy *TypeInfo;
584 while (1) {
585 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000586
Chris Lattnerd031a452007-10-07 02:00:24 +0000587 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000588 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000589 Diag(Tok, diag::err_expected_colon);
590 break;
591 }
592 ConsumeToken(); // Eat the ':'.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000593 ObjcDeclSpec DSType;
594 if (Tok.is(tok::l_paren)) { // Parse the argument type.
595 TypeInfo = ParseObjCTypeName(DSType);
596 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000597 else
598 TypeInfo = 0;
599 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000600 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000601
Chris Lattnerd031a452007-10-07 02:00:24 +0000602 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000603 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000604 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000605
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000606 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000607 Diag(Tok, diag::err_expected_ident); // missing argument name.
608 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000609 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000610 ArgNames.push_back(Tok.getIdentifierInfo());
611 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000612
Chris Lattnerd031a452007-10-07 02:00:24 +0000613 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000614 SourceLocation Loc;
615 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000616 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000617 break;
618 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000619 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000620
621 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000622 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000623 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000624 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000625 ConsumeToken();
626 break;
627 }
628 // Parse the c-style argument declaration-specifier.
629 DeclSpec DS;
630 ParseDeclarationSpecifiers(DS);
631 // Parse the declarator.
632 Declarator ParmDecl(DS, Declarator::PrototypeContext);
633 ParseDeclarator(ParmDecl);
634 }
635
636 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000637 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000638 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000639 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000640 MethodAttrs = ParseAttributes();
641
642 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
643 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000644 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000645 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000646 &ArgTypeQuals[0], &KeyTypes[0],
647 &ArgNames[0],
Chris Lattnerd031a452007-10-07 02:00:24 +0000648 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000649}
650
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000651/// CmpProtocolVals - Comparison predicate for sorting protocols.
652static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
653 const IdentifierInfo* const& rhs) {
654 return strcmp(lhs->getName(), rhs->getName()) < 0;
655}
656
Steve Narofffb367882007-08-20 21:31:48 +0000657/// objc-protocol-refs:
658/// '<' identifier-list '>'
659///
Steve Naroff304ed392007-09-05 23:30:30 +0000660bool Parser::ParseObjCProtocolReferences(
Steve Naroffef20ed32007-10-30 02:23:23 +0000661 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
662{
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000663 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000664
665 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000666
667 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000668 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000669 Diag(Tok, diag::err_expected_ident);
670 SkipUntil(tok::greater);
671 return true;
672 }
673 ProtocolRefs.push_back(Tok.getIdentifierInfo());
674 ConsumeToken();
675
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000676 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000677 break;
678 ConsumeToken();
679 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000680
681 // Sort protocols, keyed by name.
682 // Later on, we remove duplicates.
683 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
684
685 // Make protocol names unique.
686 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
687 ProtocolRefs.end());
Steve Narofffb367882007-08-20 21:31:48 +0000688 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000689 if (Tok.is(tok::greater)) {
690 endLoc = ConsumeAnyToken();
691 return false;
692 }
693 Diag(Tok, diag::err_expected_greater);
694 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000695}
696
697/// objc-class-instance-variables:
698/// '{' objc-instance-variable-decl-list[opt] '}'
699///
700/// objc-instance-variable-decl-list:
701/// objc-visibility-spec
702/// objc-instance-variable-decl ';'
703/// ';'
704/// objc-instance-variable-decl-list objc-visibility-spec
705/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
706/// objc-instance-variable-decl-list ';'
707///
708/// objc-visibility-spec:
709/// @private
710/// @protected
711/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000712/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000713///
714/// objc-instance-variable-decl:
715/// struct-declaration
716///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000717void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
718 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000719 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000720 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000721 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
722 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000723
724 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000725
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000726 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000727 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000728 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000729 // Each iteration of this loop reads one objc-instance-variable-decl.
730
731 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000732 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000733 Diag(Tok, diag::ext_extra_struct_semi);
734 ConsumeToken();
735 continue;
736 }
737 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000738 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000739 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000740 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000741 case tok::objc_private:
742 case tok::objc_public:
743 case tok::objc_protected:
744 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000745 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000746 ConsumeToken();
747 continue;
748 default:
749 Diag(Tok, diag::err_objc_illegal_visibility_spec);
750 ConsumeToken();
751 continue;
752 }
753 }
754 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000755 for (unsigned i = 0; i < IvarDecls.size(); i++) {
756 AllIvarDecls.push_back(IvarDecls[i]);
757 AllVisibilities.push_back(visibility);
758 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000759 IvarDecls.clear();
760
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000761 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000762 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000763 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000764 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
765 break;
766 } else {
767 Diag(Tok, diag::err_expected_semi_decl_list);
768 // Skip to end of block or statement
769 SkipUntil(tok::r_brace, true, true);
770 }
771 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000772 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000773 // Call ActOnFields() even if we don't have any decls. This is useful
774 // for code rewriting tools that need to be aware of the empty list.
775 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
776 &AllIvarDecls[0], AllIvarDecls.size(),
777 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Steve Naroffc4474992007-08-21 21:17:12 +0000778 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000779}
Steve Narofffb367882007-08-20 21:31:48 +0000780
781/// objc-protocol-declaration:
782/// objc-protocol-definition
783/// objc-protocol-forward-reference
784///
785/// objc-protocol-definition:
786/// @protocol identifier
787/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000788/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000789/// @end
790///
791/// objc-protocol-forward-reference:
792/// @protocol identifier-list ';'
793///
794/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000795/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000796/// semicolon in the first alternative if objc-protocol-refs are omitted.
797
Steve Naroff72f17fb2007-08-22 22:17:26 +0000798Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000799 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000800 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
801 ConsumeToken(); // the "protocol" identifier
802
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000803 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000804 Diag(Tok, diag::err_expected_ident); // missing protocol name.
805 return 0;
806 }
807 // Save the protocol name, then consume it.
808 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
809 SourceLocation nameLoc = ConsumeToken();
810
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000811 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000812 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000813 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000814 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000815 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000816 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000817 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000818 ProtocolRefs.push_back(protocolName);
819
820 while (1) {
821 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000822 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000823 Diag(Tok, diag::err_expected_ident);
824 SkipUntil(tok::semi);
825 return 0;
826 }
827 ProtocolRefs.push_back(Tok.getIdentifierInfo());
828 ConsumeToken(); // the identifier
829
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000830 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000831 break;
832 }
833 // Consume the ';'.
834 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
835 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000836 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000837 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000838 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000839 &ProtocolRefs[0],
840 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000841 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000842 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000843 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000844 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000845 return 0;
846 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000847
Steve Naroff415c1832007-10-10 17:32:04 +0000848 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000849 protocolName, nameLoc,
850 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000851 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000852 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000853
854 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000855 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000856 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000857 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000858 }
859 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000860 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000861}
Steve Narofffb367882007-08-20 21:31:48 +0000862
863/// objc-implementation:
864/// objc-class-implementation-prologue
865/// objc-category-implementation-prologue
866///
867/// objc-class-implementation-prologue:
868/// @implementation identifier objc-superclass[opt]
869/// objc-class-instance-variables[opt]
870///
871/// objc-category-implementation-prologue:
872/// @implementation identifier ( identifier )
873
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000874Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
875 SourceLocation atLoc) {
876 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
877 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
878 ConsumeToken(); // the "implementation" identifier
879
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000880 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000881 Diag(Tok, diag::err_expected_ident); // missing class or category name.
882 return 0;
883 }
884 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000885 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000886 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
887
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000888 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000889 // we have a category implementation.
890 SourceLocation lparenLoc = ConsumeParen();
891 SourceLocation categoryLoc, rparenLoc;
892 IdentifierInfo *categoryId = 0;
893
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000894 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000895 categoryId = Tok.getIdentifierInfo();
896 categoryLoc = ConsumeToken();
897 } else {
898 Diag(Tok, diag::err_expected_ident); // missing category name.
899 return 0;
900 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000901 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000902 Diag(Tok, diag::err_expected_rparen);
903 SkipUntil(tok::r_paren, false); // don't stop at ';'
904 return 0;
905 }
906 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000907 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000908 atLoc, nameId, nameLoc, categoryId,
909 categoryLoc);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000910 ObjcImpDecl = ImplCatType;
911 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000912 }
913 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000914 SourceLocation superClassLoc;
915 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000916 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000917 // We have a super class
918 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000919 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000920 Diag(Tok, diag::err_expected_ident); // missing super class name.
921 return 0;
922 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000923 superClassId = Tok.getIdentifierInfo();
924 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000925 }
Steve Naroff415c1832007-10-10 17:32:04 +0000926 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000927 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000928 superClassId, superClassLoc);
929
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000930 if (Tok.is(tok::l_brace)) // we have ivars
931 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000932 ObjcImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000933
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000934 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000935}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000936
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000937Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
938 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
939 "ParseObjCAtEndDeclaration(): Expected @end");
940 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000941 if (ObjcImpDecl) {
942 // Checking is not necessary except that a parse error might have caused
943 // @implementation not to have been parsed to completion and ObjcImpDecl
944 // could be 0.
Steve Naroffb82c50f2007-11-11 17:19:15 +0000945 Actions.ActOnAtEnd(atLoc, ObjcImpDecl);
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000946 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000947
Fariborz Jahanian83ddf822007-11-10 20:59:13 +0000948 return ObjcImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +0000949}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000950
951/// compatibility-alias-decl:
952/// @compatibility_alias alias-name class-name ';'
953///
954Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
955 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
956 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
957 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000958 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000959 Diag(Tok, diag::err_expected_ident);
960 return 0;
961 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000962 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
963 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000964 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000965 Diag(Tok, diag::err_expected_ident);
966 return 0;
967 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000968 IdentifierInfo *classId = Tok.getIdentifierInfo();
969 SourceLocation classLoc = ConsumeToken(); // consume class-name;
970 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000971 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000972 return 0;
973 }
974 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
975 aliasId, aliasLoc,
976 classId, classLoc);
977 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000978}
979
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000980/// property-synthesis:
981/// @synthesize property-ivar-list ';'
982///
983/// property-ivar-list:
984/// property-ivar
985/// property-ivar-list ',' property-ivar
986///
987/// property-ivar:
988/// identifier
989/// identifier '=' identifier
990///
991Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
992 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
993 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
994 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000995 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000996 Diag(Tok, diag::err_expected_ident);
997 return 0;
998 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000999 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001000 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001001 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001002 // property '=' ivar-name
1003 ConsumeToken(); // consume '='
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 break;
1007 }
1008 ConsumeToken(); // consume ivar-name
1009 }
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, "@synthesize");
1016 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001017}
1018
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001019/// property-dynamic:
1020/// @dynamic property-list
1021///
1022/// property-list:
1023/// identifier
1024/// property-list ',' identifier
1025///
1026Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1027 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1028 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1029 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001030 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001031 Diag(Tok, diag::err_expected_ident);
1032 return 0;
1033 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001034 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001035 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001036 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001037 break;
1038 ConsumeToken(); // consume ','
1039 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001040 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001041 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1042 return 0;
1043}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001044
1045/// objc-throw-statement:
1046/// throw expression[opt];
1047///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001048Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1049 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001050 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001051 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001052 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001053 if (Res.isInvalid) {
1054 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001055 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001056 }
1057 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001058 ConsumeToken(); // consume ';'
1059 return Actions.ActOnObjcAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001060}
1061
1062/// objc-try-catch-statement:
1063/// @try compound-statement objc-catch-list[opt]
1064/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1065///
1066/// objc-catch-list:
1067/// @catch ( parameter-declaration ) compound-statement
1068/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1069/// catch-parameter-declaration:
1070/// parameter-declaration
1071/// '...' [OBJC2]
1072///
Fariborz Jahanian70952482007-11-01 21:12:44 +00001073Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001074 bool catch_or_finally_seen = false;
1075 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001076 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001077 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001078 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001079 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001080 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001081 StmtResult FinallyStmt;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001082 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001083 if (TryBody.isInvalid)
1084 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001085 while (Tok.is(tok::at)) {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001086 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001087 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001088 StmtTy *FirstPart = 0;
1089 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001090 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001091 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001092 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001093 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001094 DeclSpec DS;
1095 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001096 // FIXME: Is BlockContext right?
1097 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1098 ParseDeclarator(DeclaratorInfo);
Fariborz Jahanian59e9e042007-11-02 18:16:07 +00001099 DeclTy * aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1100 DeclaratorInfo, 0);
1101 StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001102 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001103 }
1104 else
1105 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001106 SourceLocation RParenLoc = ConsumeParen();
Fariborz Jahanian70952482007-11-01 21:12:44 +00001107 StmtResult CatchBody = ParseCompoundStatementBody();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001108 if (CatchBody.isInvalid)
1109 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001110 CatchStmts = Actions.ActOnObjcAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001111 FirstPart, CatchBody.Val, CatchStmts.Val);
1112 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001113 }
1114 else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001115 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1116 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001117 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001118 }
1119 catch_or_finally_seen = true;
1120 }
1121 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1122 ConsumeToken(); // consume finally
1123 StmtResult FinallyBody = ParseCompoundStatementBody();
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001124 if (FinallyBody.isInvalid)
1125 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
1126 FinallyStmt = Actions.ActOnObjcAtFinallyStmt(AtCatchFinallyLoc,
1127 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001128 catch_or_finally_seen = true;
1129 break;
1130 }
1131 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001132 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001133 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001134 return true;
1135 }
1136 return Actions.ActOnObjcAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
1137 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001138}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001139
Steve Naroff81f1bba2007-09-06 21:24:23 +00001140/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001141///
Steve Naroff9191a9e82007-11-11 19:54:21 +00001142void Parser::ParseObjCMethodDefinition() {
Fariborz Jahaniandfb1c372007-11-08 23:49:49 +00001143 DeclTy *MDecl = ParseObjCMethodPrototype(ObjcImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001144 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001145 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001146 ConsumeToken();
1147
Steve Naroff9191a9e82007-11-11 19:54:21 +00001148 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001149 if (Tok.isNot(tok::l_brace)) {
Steve Naroff9191a9e82007-11-11 19:54:21 +00001150 Diag(Tok, diag::err_expected_fn_body);
1151
1152 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1153 SkipUntil(tok::l_brace, true, true);
1154
1155 // If we didn't find the '{', bail out.
1156 if (Tok.isNot(tok::l_brace))
1157 return;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001158 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001159 SourceLocation BraceLoc = Tok.getLocation();
1160
1161 // Enter a scope for the method body.
1162 EnterScope(Scope::FnScope|Scope::DeclScope);
1163
1164 // Tell the actions module that we have entered a method definition with the
1165 // specified Declarator for the method.
1166 Actions.ObjcActOnStartOfMethodDef(CurScope, MDecl);
1167
1168 StmtResult FnBody = ParseCompoundStatementBody();
1169
1170 // If the function body could not be parsed, make a bogus compoundstmt.
1171 if (FnBody.isInvalid)
1172 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1173
1174 // Leave the function body scope.
1175 ExitScope();
1176
1177 // TODO: Pass argument information.
1178 Actions.ActOnMethodDefBody(MDecl, FnBody.Val);
Chris Lattner4b009652007-07-25 00:24:17 +00001179}
Anders Carlssona66cad42007-08-21 17:43:55 +00001180
Steve Narofffb9dd752007-10-15 20:55:58 +00001181Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001182
1183 switch (Tok.getKind()) {
1184 case tok::string_literal: // primary-expression: string-literal
1185 case tok::wide_string_literal:
Steve Naroff0add5d22007-11-03 11:27:19 +00001186 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001187 default:
1188 break;
1189 }
1190
1191 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001192 case tok::objc_encode:
1193 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1194 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001195 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001196 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001197 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001198 default:
1199 Diag(AtLoc, diag::err_unexpected_at);
1200 SkipUntil(tok::semi);
1201 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001202 }
1203
1204 return 0;
1205}
1206
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001207/// objc-message-expr:
1208/// '[' objc-receiver objc-message-args ']'
1209///
1210/// objc-receiver:
1211/// expression
1212/// class-name
1213/// type-name
1214///
1215/// objc-message-args:
1216/// objc-selector
1217/// objc-keywordarg-list
1218///
1219/// objc-keywordarg-list:
1220/// objc-keywordarg
1221/// objc-keywordarg-list objc-keywordarg
1222///
1223/// objc-keywordarg:
1224/// selector-name[opt] ':' objc-keywordexpr
1225///
1226/// objc-keywordexpr:
1227/// nonempty-expr-list
1228///
1229/// nonempty-expr-list:
1230/// assignment-expression
1231/// nonempty-expr-list , assignment-expression
1232///
1233Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001234 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001235 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001236 IdentifierInfo *ReceiverName = 0;
1237 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001238 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001239 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001240 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1241 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001242 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001243 } else {
1244 ExprResult Res = ParseAssignmentExpression();
1245 if (Res.isInvalid) {
1246 SkipUntil(tok::identifier);
1247 return Res;
1248 }
1249 ReceiverExpr = Res.Val;
1250 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001251 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001252 SourceLocation Loc;
1253 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001254
1255 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1256 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1257
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001258 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001259 while (1) {
1260 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001261 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001262
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001263 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001264 Diag(Tok, diag::err_expected_colon);
1265 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001266 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001267 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001268 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001269 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001270 ExprResult Res = ParseAssignmentExpression();
1271 if (Res.isInvalid) {
1272 SkipUntil(tok::identifier);
1273 return Res;
1274 }
1275 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001276 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001277
1278 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001279 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001280 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001281 break;
1282 // We have a selector or a colon, continue parsing.
1283 }
1284 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001285 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001286 ConsumeToken();
1287 /// Parse the expression after ','
1288 ParseAssignmentExpression();
1289 }
1290 } else if (!selIdent) {
1291 Diag(Tok, diag::err_expected_ident); // missing selector name.
1292 SkipUntil(tok::semi);
1293 return 0;
1294 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001295
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001296 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001297 Diag(Tok, diag::err_expected_rsquare);
1298 SkipUntil(tok::semi);
1299 return 0;
1300 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001301 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001302
Steve Narofff9e80db2007-10-05 18:42:47 +00001303 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001304 if (nKeys == 0)
1305 KeyIdents.push_back(selIdent);
1306 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1307
1308 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001309 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001310 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1311 &KeyExprs[0]);
1312 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1313 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001314}
1315
Steve Naroff0add5d22007-11-03 11:27:19 +00001316Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001317 ExprResult Res = ParseStringLiteralExpression();
1318
1319 if (Res.isInvalid) return Res;
1320
Steve Naroff0add5d22007-11-03 11:27:19 +00001321 return Actions.ParseObjCStringLiteral(AtLoc, Res.Val);
Anders Carlssona66cad42007-08-21 17:43:55 +00001322}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001323
1324/// objc-encode-expression:
1325/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001326Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001327 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001328
1329 SourceLocation EncLoc = ConsumeToken();
1330
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001331 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001332 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1333 return true;
1334 }
1335
1336 SourceLocation LParenLoc = ConsumeParen();
1337
1338 TypeTy *Ty = ParseTypeName();
1339
Anders Carlsson92faeb82007-08-23 15:31:37 +00001340 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001341
Chris Lattnercfd61c82007-10-16 22:51:17 +00001342 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001343 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001344}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001345
1346/// objc-protocol-expression
1347/// @protocol ( protocol-name )
1348
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001349Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001350{
1351 SourceLocation ProtoLoc = ConsumeToken();
1352
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001353 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001354 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1355 return true;
1356 }
1357
1358 SourceLocation LParenLoc = ConsumeParen();
1359
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001360 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001361 Diag(Tok, diag::err_expected_ident);
1362 return true;
1363 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001364 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001365 ConsumeToken();
1366
Anders Carlsson92faeb82007-08-23 15:31:37 +00001367 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001368
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001369 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1370 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001371}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001372
1373/// objc-selector-expression
1374/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001375Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001376{
1377 SourceLocation SelectorLoc = ConsumeToken();
1378
1379 if (Tok.isNot(tok::l_paren)) {
1380 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1381 return 0;
1382 }
1383
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001384 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001385 SourceLocation LParenLoc = ConsumeParen();
1386 SourceLocation sLoc;
1387 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1388 if (!SelIdent && Tok.isNot(tok::colon)) {
1389
1390 Diag(Tok, diag::err_expected_ident); // missing selector name.
1391 return 0;
1392 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001393 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001394 if (Tok.isNot(tok::r_paren))
1395 while (1) {
1396 if (Tok.isNot(tok::colon)) {
1397 Diag(Tok, diag::err_expected_colon);
1398 break;
1399 }
1400 ConsumeToken(); // Eat the ':'.
1401 if (Tok.is(tok::r_paren))
1402 break;
1403 // Check for another keyword selector.
1404 SourceLocation Loc;
1405 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001406 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001407 if (!SelIdent && Tok.isNot(tok::colon))
1408 break;
1409 }
1410 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001411 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1412 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001413 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001414 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001415 }