blob: 0c0c165c2a3b281be8340f65432aebc899b2e112 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ParseObjc.cpp - Objective C Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Steve Naroff and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Chris Lattner4b009652007-07-25 00:24:17 +000016#include "clang/Basic/Diagnostic.h"
17#include "llvm/ADT/SmallVector.h"
18using namespace clang;
19
20
21/// ParseExternalDeclaration:
22/// external-declaration: [C99 6.9]
23/// [OBJC] objc-class-definition
Steve Naroff5b96e2e2007-10-29 21:39:29 +000024/// [OBJC] objc-class-declaration
25/// [OBJC] objc-alias-declaration
26/// [OBJC] objc-protocol-definition
27/// [OBJC] objc-method-definition
28/// [OBJC] '@' 'end'
Steve Narofffb367882007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff87c329f2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Chris Lattner4b009652007-07-25 00:24:17 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Narofffb367882007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000037 case tok::objc_protocol:
Steve Naroff72f17fb2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000039 case tok::objc_implementation:
Steve Naroff81f1bba2007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000041 case tok::objc_end:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000043 case tok::objc_compatibility_alias:
Fariborz Jahanianb62aff32007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Chris Lattner4b009652007-07-25 00:24:17 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000052 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Narofffb367882007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000065 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000066 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000068 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000069 }
Chris Lattner4b009652007-07-25 00:24:17 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
Chris Lattnera1d2bb72007-10-09 17:51:17 +000073 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000074 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000081 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000082
Steve Naroff415c1832007-10-10 17:32:04 +000083 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000084 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000085}
86
Steve Narofffb367882007-08-20 21:31:48 +000087///
88/// objc-interface:
89/// objc-class-interface-attributes[opt] objc-class-interface
90/// objc-category-interface
91///
92/// objc-class-interface:
93/// '@' 'interface' identifier objc-superclass[opt]
94/// objc-protocol-refs[opt]
95/// objc-class-instance-variables[opt]
96/// objc-interface-decl-list
97/// @end
98///
99/// objc-category-interface:
100/// '@' 'interface' identifier '(' identifier[opt] ')'
101/// objc-protocol-refs[opt]
102/// objc-interface-decl-list
103/// @end
104///
105/// objc-superclass:
106/// ':' identifier
107///
108/// objc-class-interface-attributes:
109/// __attribute__((visibility("default")))
110/// __attribute__((visibility("hidden")))
111/// __attribute__((deprecated))
112/// __attribute__((unavailable))
113/// __attribute__((objc_exception)) - used by NSException on 64-bit
114///
115Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
116 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000121 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000122 Diag(Tok, diag::err_expected_ident); // missing class or category name.
123 return 0;
124 }
125 // We have a class or category name - consume it.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000129 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000133 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofffb367882007-08-20 21:31:48 +0000134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
Steve Naroffef20ed32007-10-30 02:23:23 +0000149 SourceLocation endProtoLoc;
Steve Narofffb367882007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000151 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000152 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000153 return 0;
154 }
155 if (attrList) // categories don't support attributes.
156 Diag(Tok, diag::err_objc_no_attributes_on_category);
157
Steve Naroff415c1832007-10-10 17:32:04 +0000158 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000159 nameId, nameLoc, categoryId, categoryLoc,
160 &ProtocolRefs[0], ProtocolRefs.size());
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000161
162 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000163
Steve Naroff0bbffd82007-08-22 16:35:03 +0000164 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000165 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000166 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000167 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000168 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000169 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000170 return 0;
171 }
172 // Parse a class interface.
173 IdentifierInfo *superClassId = 0;
174 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000175
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000176 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000177 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000178 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000179 Diag(Tok, diag::err_expected_ident); // missing super class name.
180 return 0;
181 }
182 superClassId = Tok.getIdentifierInfo();
183 superClassLoc = ConsumeToken();
184 }
185 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000186 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000187 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000188 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000189 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000190 return 0;
191 }
Steve Naroff415c1832007-10-10 17:32:04 +0000192 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000193 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000194 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000195 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000196
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000197 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000198 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000199
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000200 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000201
202 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000203 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000204 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000205 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000206 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000207 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000208 return 0;
209}
210
211/// objc-interface-decl-list:
212/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000213/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000214/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000215/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000216/// objc-interface-decl-list declaration
217/// objc-interface-decl-list ';'
218///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000219/// objc-method-requirement: [OBJC2]
220/// @required
221/// @optional
222///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000223void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
224 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000225 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000226 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000227 SourceLocation AtEndLoc;
228
Steve Naroff0bbffd82007-08-22 16:35:03 +0000229 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000230 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000231 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000232 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000233
234 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000235 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000236 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000237 } else if (ocKind == tok::objc_required) { // protocols only
238 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000239 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000240 if (contextKey != tok::objc_protocol)
241 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000242 } else if (ocKind == tok::objc_optional) { // protocols only
243 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000244 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000245 if (contextKey != tok::objc_protocol)
246 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000247 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000248 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000249 continue;
250 } else {
251 Diag(Tok, diag::err_objc_illegal_interface_qual);
252 ConsumeToken();
253 }
254 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000255 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
256 DeclTy *methodPrototype =
257 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000258 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000259 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
260 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000261 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000262 continue;
263 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000264 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000265 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000266 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000267 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000268 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000269 // FIXME: as the name implies, this rule allows function definitions.
270 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000271 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000272 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000273 }
Steve Naroff8255b042007-10-14 00:58:41 +0000274 if (allMethods.size())
275 /// Insert collected methods declarations into the @interface object.
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000276 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl, &allMethods[0],
277 allMethods.size(), AtEndLoc);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000278}
279
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000280/// Parse property attribute declarations.
281///
282/// property-attr-decl: '(' property-attrlist ')'
283/// property-attrlist:
284/// property-attribute
285/// property-attrlist ',' property-attribute
286/// property-attribute:
287/// getter '=' identifier
288/// setter '=' identifier ':'
289/// readonly
290/// readwrite
291/// assign
292/// retain
293/// copy
294/// nonatomic
295///
296void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
297 SourceLocation loc = ConsumeParen(); // consume '('
298 while (isObjCPropertyAttribute()) {
299 const IdentifierInfo *II = Tok.getIdentifierInfo();
300 // getter/setter require extra treatment.
301 if (II == ObjcPropertyAttrs[objc_getter] ||
302 II == ObjcPropertyAttrs[objc_setter]) {
303 // skip getter/setter part.
304 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000305 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000306 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000307 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000308 if (II == ObjcPropertyAttrs[objc_setter]) {
309 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000310 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000311 Diag(loc, diag::err_expected_colon);
312 SkipUntil(tok::r_paren,true,true);
313 break;
314 }
315 }
316 }
317 else {
318 Diag(loc, diag::err_expected_ident);
319 SkipUntil(tok::r_paren,true,true);
320 break;
321 }
322 }
323 else {
324 Diag(loc, diag::err_objc_expected_equal);
325 SkipUntil(tok::r_paren,true,true);
326 break;
327 }
328 }
329 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000330 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000331 loc = ConsumeToken();
332 continue;
333 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000334 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000335 break;
336 Diag(loc, diag::err_expected_rparen);
337 SkipUntil(tok::semi);
338 return;
339 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000340 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000341 ConsumeParen();
342 else {
343 Diag(loc, diag::err_objc_expected_property_attr);
344 SkipUntil(tok::r_paren); // recover from error inside attribute list
345 }
346}
347
348/// Main routine to parse property declaration.
349///
350/// @property property-attr-decl[opt] property-component-decl ';'
351///
352void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
353 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
354 "ParseObjCPropertyDecl(): Expected @property");
355 ConsumeToken(); // the "property" identifier
356 // Parse property attribute list, if any.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000357 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000358 // property has attribute list.
359 ParseObjCPropertyAttribute(0/*FIXME*/);
360 }
361 // Parse declaration portion of @property.
362 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
363 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000364 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000365 ConsumeToken();
366 else {
367 Diag(Tok, diag::err_expected_semi_decl_list);
368 SkipUntil(tok::r_brace, true, true);
369 }
Chris Lattner4b009652007-07-25 00:24:17 +0000370}
Steve Narofffb367882007-08-20 21:31:48 +0000371
Steve Naroff81f1bba2007-09-06 21:24:23 +0000372/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000373/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000374/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000375///
376/// objc-instance-method: '-'
377/// objc-class-method: '+'
378///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000379/// objc-method-attributes: [OBJC2]
380/// __attribute__((deprecated))
381///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000382Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
383 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000384 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000385
386 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000387 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000388
Steve Naroff3774dd92007-10-26 20:53:56 +0000389 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000390 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000391 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000392 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000393}
394
395/// objc-selector:
396/// identifier
397/// one of
398/// enum struct union if else while do for switch case default
399/// break continue return goto asm sizeof typeof __alignof
400/// unsigned long const short volatile signed restrict _Complex
401/// in out inout bycopy byref oneway int char float double void _Bool
402///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000403IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000404 switch (Tok.getKind()) {
405 default:
406 return 0;
407 case tok::identifier:
408 case tok::kw_typeof:
409 case tok::kw___alignof:
410 case tok::kw_auto:
411 case tok::kw_break:
412 case tok::kw_case:
413 case tok::kw_char:
414 case tok::kw_const:
415 case tok::kw_continue:
416 case tok::kw_default:
417 case tok::kw_do:
418 case tok::kw_double:
419 case tok::kw_else:
420 case tok::kw_enum:
421 case tok::kw_extern:
422 case tok::kw_float:
423 case tok::kw_for:
424 case tok::kw_goto:
425 case tok::kw_if:
426 case tok::kw_inline:
427 case tok::kw_int:
428 case tok::kw_long:
429 case tok::kw_register:
430 case tok::kw_restrict:
431 case tok::kw_return:
432 case tok::kw_short:
433 case tok::kw_signed:
434 case tok::kw_sizeof:
435 case tok::kw_static:
436 case tok::kw_struct:
437 case tok::kw_switch:
438 case tok::kw_typedef:
439 case tok::kw_union:
440 case tok::kw_unsigned:
441 case tok::kw_void:
442 case tok::kw_volatile:
443 case tok::kw_while:
444 case tok::kw__Bool:
445 case tok::kw__Complex:
446 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000447 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000448 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000449 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000450}
451
Steve Naroffa8ee2262007-08-22 23:18:22 +0000452/// objc-type-qualifier: one of
453/// in out inout bycopy byref oneway
454///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000455bool Parser::isObjCTypeQualifier() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000456 if (Tok.is(tok::identifier)) {
Chris Lattner32352462007-08-29 22:54:08 +0000457 const IdentifierInfo *II = Tok.getIdentifierInfo();
458 for (unsigned i = 0; i < objc_NumQuals; ++i)
459 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000460 }
461 return false;
462}
463
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000464/// property-attrlist: one of
465/// readonly getter setter assign retain copy nonatomic
466///
467bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000468 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000469 const IdentifierInfo *II = Tok.getIdentifierInfo();
470 for (unsigned i = 0; i < objc_NumAttrs; ++i)
471 if (II == ObjcPropertyAttrs[i]) return true;
472 }
473 return false;
474}
475
Steve Naroff0bbffd82007-08-22 16:35:03 +0000476/// objc-type-name:
477/// '(' objc-type-qualifiers[opt] type-name ')'
478/// '(' objc-type-qualifiers[opt] ')'
479///
480/// objc-type-qualifiers:
481/// objc-type-qualifier
482/// objc-type-qualifiers objc-type-qualifier
483///
Steve Naroff304ed392007-09-05 23:30:30 +0000484Parser::TypeTy *Parser::ParseObjCTypeName() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000485 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000486
487 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000488 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000489
Steve Naroffa8ee2262007-08-22 23:18:22 +0000490 while (isObjCTypeQualifier())
491 ConsumeToken();
492
Steve Naroff0bbffd82007-08-22 16:35:03 +0000493 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000494 Ty = ParseTypeName();
495 // FIXME: back when Sema support is in place...
496 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000497 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000498 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000499 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000500 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000501 }
502 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000503 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000504}
505
506/// objc-method-decl:
507/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000508/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000509/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000510/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000511///
512/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000513/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000514/// objc-keyword-selector objc-keyword-decl
515///
516/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000517/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
518/// objc-selector ':' objc-keyword-attributes[opt] identifier
519/// ':' objc-type-name objc-keyword-attributes[opt] identifier
520/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000521///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000522/// objc-parmlist:
523/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000524///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000525/// objc-parms:
526/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000527///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000528/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000529/// , ...
530///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000531/// objc-keyword-attributes: [OBJC2]
532/// __attribute__((unused))
533///
Steve Naroff3774dd92007-10-26 20:53:56 +0000534Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
535 tok::TokenKind mType,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000536 tok::ObjCKeywordKind MethodImplKind)
537{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000538 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000539 TypeTy *ReturnType = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000540 if (Tok.is(tok::l_paren))
Steve Naroff304ed392007-09-05 23:30:30 +0000541 ReturnType = ParseObjCTypeName();
Steve Naroff3774dd92007-10-26 20:53:56 +0000542 SourceLocation selLoc;
543 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000544 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000545 if (!SelIdent) {
546 Diag(Tok, diag::err_expected_ident); // missing selector name.
547 // FIXME: this creates a unary selector with a null identifier, is this
548 // ok?? Maybe we should skip to the next semicolon or something.
549 }
550
551 // If attributes exist after the method, parse them.
552 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000553 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000554 MethodAttrs = ParseAttributes();
555
556 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000557 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
558 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000559 0, 0, MethodAttrs, MethodImplKind);
560 }
Steve Naroff304ed392007-09-05 23:30:30 +0000561
Steve Naroff4ed9d662007-09-27 14:38:14 +0000562 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
563 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
564 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000565
566 Action::TypeTy *TypeInfo;
567 while (1) {
568 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000569
Chris Lattnerd031a452007-10-07 02:00:24 +0000570 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000571 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000572 Diag(Tok, diag::err_expected_colon);
573 break;
574 }
575 ConsumeToken(); // Eat the ':'.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000576 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000577 TypeInfo = ParseObjCTypeName();
578 else
579 TypeInfo = 0;
580 KeyTypes.push_back(TypeInfo);
Steve Naroff304ed392007-09-05 23:30:30 +0000581
Chris Lattnerd031a452007-10-07 02:00:24 +0000582 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000583 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000584 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000585
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000586 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000587 Diag(Tok, diag::err_expected_ident); // missing argument name.
588 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000589 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000590 ArgNames.push_back(Tok.getIdentifierInfo());
591 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000592
Chris Lattnerd031a452007-10-07 02:00:24 +0000593 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000594 SourceLocation Loc;
595 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000596 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000597 break;
598 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000599 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000600
601 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000602 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000603 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000604 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000605 ConsumeToken();
606 break;
607 }
608 // Parse the c-style argument declaration-specifier.
609 DeclSpec DS;
610 ParseDeclarationSpecifiers(DS);
611 // Parse the declarator.
612 Declarator ParmDecl(DS, Declarator::PrototypeContext);
613 ParseDeclarator(ParmDecl);
614 }
615
616 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000617 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000618 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000619 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000620 MethodAttrs = ParseAttributes();
621
622 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
623 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000624 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
625 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000626 &KeyTypes[0], &ArgNames[0],
627 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000628}
629
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000630/// CmpProtocolVals - Comparison predicate for sorting protocols.
631static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
632 const IdentifierInfo* const& rhs) {
633 return strcmp(lhs->getName(), rhs->getName()) < 0;
634}
635
Steve Narofffb367882007-08-20 21:31:48 +0000636/// objc-protocol-refs:
637/// '<' identifier-list '>'
638///
Steve Naroff304ed392007-09-05 23:30:30 +0000639bool Parser::ParseObjCProtocolReferences(
Steve Naroffef20ed32007-10-30 02:23:23 +0000640 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
641{
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000642 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000643
644 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000645
646 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000647 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000648 Diag(Tok, diag::err_expected_ident);
649 SkipUntil(tok::greater);
650 return true;
651 }
652 ProtocolRefs.push_back(Tok.getIdentifierInfo());
653 ConsumeToken();
654
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000655 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000656 break;
657 ConsumeToken();
658 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000659
660 // Sort protocols, keyed by name.
661 // Later on, we remove duplicates.
662 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
663
664 // Make protocol names unique.
665 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
666 ProtocolRefs.end());
Steve Narofffb367882007-08-20 21:31:48 +0000667 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000668 if (Tok.is(tok::greater)) {
669 endLoc = ConsumeAnyToken();
670 return false;
671 }
672 Diag(Tok, diag::err_expected_greater);
673 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000674}
675
676/// objc-class-instance-variables:
677/// '{' objc-instance-variable-decl-list[opt] '}'
678///
679/// objc-instance-variable-decl-list:
680/// objc-visibility-spec
681/// objc-instance-variable-decl ';'
682/// ';'
683/// objc-instance-variable-decl-list objc-visibility-spec
684/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
685/// objc-instance-variable-decl-list ';'
686///
687/// objc-visibility-spec:
688/// @private
689/// @protected
690/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000691/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000692///
693/// objc-instance-variable-decl:
694/// struct-declaration
695///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000696void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
697 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000698 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000699 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000700 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
701 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000702
703 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000704
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000705 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000706 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000707 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000708 // Each iteration of this loop reads one objc-instance-variable-decl.
709
710 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000711 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000712 Diag(Tok, diag::ext_extra_struct_semi);
713 ConsumeToken();
714 continue;
715 }
716 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000717 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000718 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000719 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000720 case tok::objc_private:
721 case tok::objc_public:
722 case tok::objc_protected:
723 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000724 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000725 ConsumeToken();
726 continue;
727 default:
728 Diag(Tok, diag::err_objc_illegal_visibility_spec);
729 ConsumeToken();
730 continue;
731 }
732 }
733 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000734 for (unsigned i = 0; i < IvarDecls.size(); i++) {
735 AllIvarDecls.push_back(IvarDecls[i]);
736 AllVisibilities.push_back(visibility);
737 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000738 IvarDecls.clear();
739
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000740 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000741 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000742 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000743 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
744 break;
745 } else {
746 Diag(Tok, diag::err_expected_semi_decl_list);
747 // Skip to end of block or statement
748 SkipUntil(tok::r_brace, true, true);
749 }
750 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000751 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000752 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000753 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000754 &AllIvarDecls[0], AllIvarDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000755 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000756 }
Steve Naroffc4474992007-08-21 21:17:12 +0000757 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000758}
Steve Narofffb367882007-08-20 21:31:48 +0000759
760/// objc-protocol-declaration:
761/// objc-protocol-definition
762/// objc-protocol-forward-reference
763///
764/// objc-protocol-definition:
765/// @protocol identifier
766/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000767/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000768/// @end
769///
770/// objc-protocol-forward-reference:
771/// @protocol identifier-list ';'
772///
773/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000774/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000775/// semicolon in the first alternative if objc-protocol-refs are omitted.
776
Steve Naroff72f17fb2007-08-22 22:17:26 +0000777Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000778 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000779 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
780 ConsumeToken(); // the "protocol" identifier
781
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000782 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000783 Diag(Tok, diag::err_expected_ident); // missing protocol name.
784 return 0;
785 }
786 // Save the protocol name, then consume it.
787 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
788 SourceLocation nameLoc = ConsumeToken();
789
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000790 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000791 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000792 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000793 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000794 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000795 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000796 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000797 ProtocolRefs.push_back(protocolName);
798
799 while (1) {
800 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000801 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000802 Diag(Tok, diag::err_expected_ident);
803 SkipUntil(tok::semi);
804 return 0;
805 }
806 ProtocolRefs.push_back(Tok.getIdentifierInfo());
807 ConsumeToken(); // the identifier
808
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000809 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000810 break;
811 }
812 // Consume the ';'.
813 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
814 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000815 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000816 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000817 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000818 &ProtocolRefs[0],
819 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000820 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000821 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000822 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000823 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000824 return 0;
825 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000826
Steve Naroff415c1832007-10-10 17:32:04 +0000827 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000828 protocolName, nameLoc,
829 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000830 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000831 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000832
833 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000834 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000835 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000836 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000837 }
838 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000839 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000840}
Steve Narofffb367882007-08-20 21:31:48 +0000841
842/// objc-implementation:
843/// objc-class-implementation-prologue
844/// objc-category-implementation-prologue
845///
846/// objc-class-implementation-prologue:
847/// @implementation identifier objc-superclass[opt]
848/// objc-class-instance-variables[opt]
849///
850/// objc-category-implementation-prologue:
851/// @implementation identifier ( identifier )
852
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000853Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
854 SourceLocation atLoc) {
855 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
856 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
857 ConsumeToken(); // the "implementation" identifier
858
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000859 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000860 Diag(Tok, diag::err_expected_ident); // missing class or category name.
861 return 0;
862 }
863 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000864 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000865 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
866
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000867 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000868 // we have a category implementation.
869 SourceLocation lparenLoc = ConsumeParen();
870 SourceLocation categoryLoc, rparenLoc;
871 IdentifierInfo *categoryId = 0;
872
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000873 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000874 categoryId = Tok.getIdentifierInfo();
875 categoryLoc = ConsumeToken();
876 } else {
877 Diag(Tok, diag::err_expected_ident); // missing category name.
878 return 0;
879 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000880 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000881 Diag(Tok, diag::err_expected_rparen);
882 SkipUntil(tok::r_paren, false); // don't stop at ';'
883 return 0;
884 }
885 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000886 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000887 atLoc, nameId, nameLoc, categoryId,
888 categoryLoc);
889 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000890 }
891 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000892 SourceLocation superClassLoc;
893 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000894 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000895 // We have a super class
896 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000897 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000898 Diag(Tok, diag::err_expected_ident); // missing super class name.
899 return 0;
900 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000901 superClassId = Tok.getIdentifierInfo();
902 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000903 }
Steve Naroff415c1832007-10-10 17:32:04 +0000904 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000905 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000906 superClassId, superClassLoc);
907
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000908 if (Tok.is(tok::l_brace)) // we have ivars
909 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000910
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000911 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000912}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000913
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000914Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
915 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
916 "ParseObjCAtEndDeclaration(): Expected @end");
917 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000918 if (ObjcImpDecl) {
919 // Checking is not necessary except that a parse error might have caused
920 // @implementation not to have been parsed to completion and ObjcImpDecl
921 // could be 0.
922 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000923 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000924 &AllImplMethods[0], AllImplMethods.size(),
925 atLoc);
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000926 ObjcImpDecl = 0;
927 AllImplMethods.clear();
928 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000929
Steve Narofffb367882007-08-20 21:31:48 +0000930 return 0;
931}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000932
933/// compatibility-alias-decl:
934/// @compatibility_alias alias-name class-name ';'
935///
936Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
937 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
938 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
939 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000940 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000941 Diag(Tok, diag::err_expected_ident);
942 return 0;
943 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000944 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
945 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000946 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000947 Diag(Tok, diag::err_expected_ident);
948 return 0;
949 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000950 IdentifierInfo *classId = Tok.getIdentifierInfo();
951 SourceLocation classLoc = ConsumeToken(); // consume class-name;
952 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000953 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000954 return 0;
955 }
956 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
957 aliasId, aliasLoc,
958 classId, classLoc);
959 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000960}
961
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000962/// property-synthesis:
963/// @synthesize property-ivar-list ';'
964///
965/// property-ivar-list:
966/// property-ivar
967/// property-ivar-list ',' property-ivar
968///
969/// property-ivar:
970/// identifier
971/// identifier '=' identifier
972///
973Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
974 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
975 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
976 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000977 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000978 Diag(Tok, diag::err_expected_ident);
979 return 0;
980 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000981 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000982 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000983 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000984 // property '=' ivar-name
985 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000986 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000987 Diag(Tok, diag::err_expected_ident);
988 break;
989 }
990 ConsumeToken(); // consume ivar-name
991 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000992 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000993 break;
994 ConsumeToken(); // consume ','
995 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000996 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000997 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
998 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000999}
1000
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001001/// property-dynamic:
1002/// @dynamic property-list
1003///
1004/// property-list:
1005/// identifier
1006/// property-list ',' identifier
1007///
1008Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1009 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1010 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1011 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001012 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001013 Diag(Tok, diag::err_expected_ident);
1014 return 0;
1015 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001016 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001017 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001018 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001019 break;
1020 ConsumeToken(); // consume ','
1021 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001022 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001023 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1024 return 0;
1025}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001026
1027/// objc-throw-statement:
1028/// throw expression[opt];
1029///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001030Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001031 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001032 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001033 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001034 if (Res.isInvalid) {
1035 SkipUntil(tok::semi);
1036 return 0;
1037 }
1038 }
1039 return 0;
1040}
1041
1042/// objc-try-catch-statement:
1043/// @try compound-statement objc-catch-list[opt]
1044/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1045///
1046/// objc-catch-list:
1047/// @catch ( parameter-declaration ) compound-statement
1048/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1049/// catch-parameter-declaration:
1050/// parameter-declaration
1051/// '...' [OBJC2]
1052///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001053Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001054 bool catch_or_finally_seen = false;
1055 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001056 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001057 Diag (Tok, diag::err_expected_lbrace);
1058 return 0;
1059 }
1060 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001061 while (Tok.is(tok::at)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001062 ConsumeToken();
1063 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1064 SourceLocation catchLoc = 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();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001067 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001068 DeclSpec DS;
1069 ParseDeclarationSpecifiers(DS);
1070 // Parse the parameter-declaration.
1071 // FIXME: BlockContext may not be the right context!
1072 Declarator ParmDecl(DS, Declarator::BlockContext);
1073 ParseDeclarator(ParmDecl);
1074 }
1075 else
1076 ConsumeToken(); // consume '...'
1077 ConsumeParen();
1078 StmtResult CatchMody = ParseCompoundStatementBody();
1079 }
1080 else {
1081 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1082 return 0;
1083 }
1084 catch_or_finally_seen = true;
1085 }
1086 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1087 ConsumeToken(); // consume finally
1088 StmtResult FinallyBody = ParseCompoundStatementBody();
1089 catch_or_finally_seen = true;
1090 break;
1091 }
1092 }
1093 if (!catch_or_finally_seen)
1094 Diag(atLoc, diag::err_missing_catch_finally);
1095 return 0;
1096}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001097
Steve Naroff81f1bba2007-09-06 21:24:23 +00001098/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001099///
1100void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001101 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1102
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001103 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001104 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001105 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001106 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001107 ConsumeToken();
1108
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001109 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001110 Diag (Tok, diag::err_expected_lbrace);
1111 return;
1112 }
1113
1114 StmtResult FnBody = ParseCompoundStatementBody();
1115}
1116
Steve Naroff81f1bba2007-09-06 21:24:23 +00001117/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001118///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001119void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001120 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001121 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001122 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001123 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001124 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001125 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001126 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001127 Diag (Tok, diag::err_expected_lbrace);
1128 return;
1129 }
1130
1131 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001132}
Anders Carlssona66cad42007-08-21 17:43:55 +00001133
Steve Narofffb9dd752007-10-15 20:55:58 +00001134Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001135
1136 switch (Tok.getKind()) {
1137 case tok::string_literal: // primary-expression: string-literal
1138 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001139 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001140 default:
1141 break;
1142 }
1143
1144 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001145 case tok::objc_encode:
1146 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1147 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001148 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001149 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001150 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001151 default:
1152 Diag(AtLoc, diag::err_unexpected_at);
1153 SkipUntil(tok::semi);
1154 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001155 }
1156
1157 return 0;
1158}
1159
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001160/// objc-message-expr:
1161/// '[' objc-receiver objc-message-args ']'
1162///
1163/// objc-receiver:
1164/// expression
1165/// class-name
1166/// type-name
1167///
1168/// objc-message-args:
1169/// objc-selector
1170/// objc-keywordarg-list
1171///
1172/// objc-keywordarg-list:
1173/// objc-keywordarg
1174/// objc-keywordarg-list objc-keywordarg
1175///
1176/// objc-keywordarg:
1177/// selector-name[opt] ':' objc-keywordexpr
1178///
1179/// objc-keywordexpr:
1180/// nonempty-expr-list
1181///
1182/// nonempty-expr-list:
1183/// assignment-expression
1184/// nonempty-expr-list , assignment-expression
1185///
1186Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001187 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001188 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001189 IdentifierInfo *ReceiverName = 0;
1190 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001191 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001192 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001193 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1194 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001195 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001196 } else {
1197 ExprResult Res = ParseAssignmentExpression();
1198 if (Res.isInvalid) {
1199 SkipUntil(tok::identifier);
1200 return Res;
1201 }
1202 ReceiverExpr = Res.Val;
1203 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001204 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001205 SourceLocation Loc;
1206 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001207
1208 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1209 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1210
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001211 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001212 while (1) {
1213 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001214 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001215
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001216 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001217 Diag(Tok, diag::err_expected_colon);
1218 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001219 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001220 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001221 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001222 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001223 ExprResult Res = ParseAssignmentExpression();
1224 if (Res.isInvalid) {
1225 SkipUntil(tok::identifier);
1226 return Res;
1227 }
1228 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001229 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001230
1231 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001232 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001233 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001234 break;
1235 // We have a selector or a colon, continue parsing.
1236 }
1237 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001238 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001239 ConsumeToken();
1240 /// Parse the expression after ','
1241 ParseAssignmentExpression();
1242 }
1243 } else if (!selIdent) {
1244 Diag(Tok, diag::err_expected_ident); // missing selector name.
1245 SkipUntil(tok::semi);
1246 return 0;
1247 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001248
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001249 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001250 Diag(Tok, diag::err_expected_rsquare);
1251 SkipUntil(tok::semi);
1252 return 0;
1253 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001254 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001255
Steve Narofff9e80db2007-10-05 18:42:47 +00001256 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001257 if (nKeys == 0)
1258 KeyIdents.push_back(selIdent);
1259 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1260
1261 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001262 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001263 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1264 &KeyExprs[0]);
1265 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1266 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001267}
1268
Anders Carlssona66cad42007-08-21 17:43:55 +00001269Parser::ExprResult Parser::ParseObjCStringLiteral() {
1270 ExprResult Res = ParseStringLiteralExpression();
1271
1272 if (Res.isInvalid) return Res;
1273
1274 return Actions.ParseObjCStringLiteral(Res.Val);
1275}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001276
1277/// objc-encode-expression:
1278/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001279Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001280 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001281
1282 SourceLocation EncLoc = ConsumeToken();
1283
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001284 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001285 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1286 return true;
1287 }
1288
1289 SourceLocation LParenLoc = ConsumeParen();
1290
1291 TypeTy *Ty = ParseTypeName();
1292
Anders Carlsson92faeb82007-08-23 15:31:37 +00001293 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001294
Chris Lattnercfd61c82007-10-16 22:51:17 +00001295 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001296 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001297}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001298
1299/// objc-protocol-expression
1300/// @protocol ( protocol-name )
1301
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001302Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001303{
1304 SourceLocation ProtoLoc = ConsumeToken();
1305
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001306 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001307 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1308 return true;
1309 }
1310
1311 SourceLocation LParenLoc = ConsumeParen();
1312
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001313 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001314 Diag(Tok, diag::err_expected_ident);
1315 return true;
1316 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001317 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001318 ConsumeToken();
1319
Anders Carlsson92faeb82007-08-23 15:31:37 +00001320 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001321
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001322 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1323 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001324}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001325
1326/// objc-selector-expression
1327/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001328Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001329{
1330 SourceLocation SelectorLoc = ConsumeToken();
1331
1332 if (Tok.isNot(tok::l_paren)) {
1333 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1334 return 0;
1335 }
1336
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001337 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001338 SourceLocation LParenLoc = ConsumeParen();
1339 SourceLocation sLoc;
1340 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1341 if (!SelIdent && Tok.isNot(tok::colon)) {
1342
1343 Diag(Tok, diag::err_expected_ident); // missing selector name.
1344 return 0;
1345 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001346 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001347 if (Tok.isNot(tok::r_paren))
1348 while (1) {
1349 if (Tok.isNot(tok::colon)) {
1350 Diag(Tok, diag::err_expected_colon);
1351 break;
1352 }
1353 ConsumeToken(); // Eat the ':'.
1354 if (Tok.is(tok::r_paren))
1355 break;
1356 // Check for another keyword selector.
1357 SourceLocation Loc;
1358 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001359 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001360 if (!SelIdent && Tok.isNot(tok::colon))
1361 break;
1362 }
1363 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001364 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1365 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001366 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001367 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001368 }