blob: b2323a2e104f228a448ca3276287921e49be790b [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 Naroff1ccf4632007-10-30 03:43:13 +0000274 /// Insert collected methods declarations into the @interface object.
275 /// This action is executed even if we don't have any methods (so the @end
276 /// can be recorded properly).
277 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl, &allMethods[0],
278 allMethods.size(), AtEndLoc);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000279}
280
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000281/// Parse property attribute declarations.
282///
283/// property-attr-decl: '(' property-attrlist ')'
284/// property-attrlist:
285/// property-attribute
286/// property-attrlist ',' property-attribute
287/// property-attribute:
288/// getter '=' identifier
289/// setter '=' identifier ':'
290/// readonly
291/// readwrite
292/// assign
293/// retain
294/// copy
295/// nonatomic
296///
297void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
298 SourceLocation loc = ConsumeParen(); // consume '('
299 while (isObjCPropertyAttribute()) {
300 const IdentifierInfo *II = Tok.getIdentifierInfo();
301 // getter/setter require extra treatment.
302 if (II == ObjcPropertyAttrs[objc_getter] ||
303 II == ObjcPropertyAttrs[objc_setter]) {
304 // skip getter/setter part.
305 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000306 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000307 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000308 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000309 if (II == ObjcPropertyAttrs[objc_setter]) {
310 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000311 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000312 Diag(loc, diag::err_expected_colon);
313 SkipUntil(tok::r_paren,true,true);
314 break;
315 }
316 }
317 }
318 else {
319 Diag(loc, diag::err_expected_ident);
320 SkipUntil(tok::r_paren,true,true);
321 break;
322 }
323 }
324 else {
325 Diag(loc, diag::err_objc_expected_equal);
326 SkipUntil(tok::r_paren,true,true);
327 break;
328 }
329 }
330 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000331 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000332 loc = ConsumeToken();
333 continue;
334 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000335 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000336 break;
337 Diag(loc, diag::err_expected_rparen);
338 SkipUntil(tok::semi);
339 return;
340 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000341 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000342 ConsumeParen();
343 else {
344 Diag(loc, diag::err_objc_expected_property_attr);
345 SkipUntil(tok::r_paren); // recover from error inside attribute list
346 }
347}
348
349/// Main routine to parse property declaration.
350///
351/// @property property-attr-decl[opt] property-component-decl ';'
352///
353void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
354 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
355 "ParseObjCPropertyDecl(): Expected @property");
356 ConsumeToken(); // the "property" identifier
357 // Parse property attribute list, if any.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000358 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000359 // property has attribute list.
360 ParseObjCPropertyAttribute(0/*FIXME*/);
361 }
362 // Parse declaration portion of @property.
363 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
364 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000365 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000366 ConsumeToken();
367 else {
368 Diag(Tok, diag::err_expected_semi_decl_list);
369 SkipUntil(tok::r_brace, true, true);
370 }
Chris Lattner4b009652007-07-25 00:24:17 +0000371}
Steve Narofffb367882007-08-20 21:31:48 +0000372
Steve Naroff81f1bba2007-09-06 21:24:23 +0000373/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000374/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000375/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000376///
377/// objc-instance-method: '-'
378/// objc-class-method: '+'
379///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000380/// objc-method-attributes: [OBJC2]
381/// __attribute__((deprecated))
382///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000383Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
384 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000385 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000386
387 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000388 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000389
Steve Naroff3774dd92007-10-26 20:53:56 +0000390 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000391 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000392 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000393 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000394}
395
396/// objc-selector:
397/// identifier
398/// one of
399/// enum struct union if else while do for switch case default
400/// break continue return goto asm sizeof typeof __alignof
401/// unsigned long const short volatile signed restrict _Complex
402/// in out inout bycopy byref oneway int char float double void _Bool
403///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000404IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000405 switch (Tok.getKind()) {
406 default:
407 return 0;
408 case tok::identifier:
409 case tok::kw_typeof:
410 case tok::kw___alignof:
411 case tok::kw_auto:
412 case tok::kw_break:
413 case tok::kw_case:
414 case tok::kw_char:
415 case tok::kw_const:
416 case tok::kw_continue:
417 case tok::kw_default:
418 case tok::kw_do:
419 case tok::kw_double:
420 case tok::kw_else:
421 case tok::kw_enum:
422 case tok::kw_extern:
423 case tok::kw_float:
424 case tok::kw_for:
425 case tok::kw_goto:
426 case tok::kw_if:
427 case tok::kw_inline:
428 case tok::kw_int:
429 case tok::kw_long:
430 case tok::kw_register:
431 case tok::kw_restrict:
432 case tok::kw_return:
433 case tok::kw_short:
434 case tok::kw_signed:
435 case tok::kw_sizeof:
436 case tok::kw_static:
437 case tok::kw_struct:
438 case tok::kw_switch:
439 case tok::kw_typedef:
440 case tok::kw_union:
441 case tok::kw_unsigned:
442 case tok::kw_void:
443 case tok::kw_volatile:
444 case tok::kw_while:
445 case tok::kw__Bool:
446 case tok::kw__Complex:
447 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000448 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000449 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000450 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000451}
452
Steve Naroffa8ee2262007-08-22 23:18:22 +0000453/// objc-type-qualifier: one of
454/// in out inout bycopy byref oneway
455///
Steve Naroffa8ee2262007-08-22 23:18:22 +0000456bool Parser::isObjCTypeQualifier() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000457 if (Tok.is(tok::identifier)) {
Chris Lattner32352462007-08-29 22:54:08 +0000458 const IdentifierInfo *II = Tok.getIdentifierInfo();
459 for (unsigned i = 0; i < objc_NumQuals; ++i)
460 if (II == ObjcTypeQuals[i]) return true;
Steve Naroffa8ee2262007-08-22 23:18:22 +0000461 }
462 return false;
463}
464
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000465/// property-attrlist: one of
466/// readonly getter setter assign retain copy nonatomic
467///
468bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000469 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000470 const IdentifierInfo *II = Tok.getIdentifierInfo();
471 for (unsigned i = 0; i < objc_NumAttrs; ++i)
472 if (II == ObjcPropertyAttrs[i]) return true;
473 }
474 return false;
475}
476
Steve Naroff0bbffd82007-08-22 16:35:03 +0000477/// objc-type-name:
478/// '(' objc-type-qualifiers[opt] type-name ')'
479/// '(' objc-type-qualifiers[opt] ')'
480///
481/// objc-type-qualifiers:
482/// objc-type-qualifier
483/// objc-type-qualifiers objc-type-qualifier
484///
Steve Naroff304ed392007-09-05 23:30:30 +0000485Parser::TypeTy *Parser::ParseObjCTypeName() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000486 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000487
488 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000489 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000490
Steve Naroffa8ee2262007-08-22 23:18:22 +0000491 while (isObjCTypeQualifier())
492 ConsumeToken();
493
Steve Naroff0bbffd82007-08-22 16:35:03 +0000494 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000495 Ty = ParseTypeName();
496 // FIXME: back when Sema support is in place...
497 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000498 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000499 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000500 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000501 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000502 }
503 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000504 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000505}
506
507/// objc-method-decl:
508/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000509/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000510/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000511/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000512///
513/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000514/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000515/// objc-keyword-selector objc-keyword-decl
516///
517/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000518/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
519/// objc-selector ':' objc-keyword-attributes[opt] identifier
520/// ':' objc-type-name objc-keyword-attributes[opt] identifier
521/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000522///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000523/// objc-parmlist:
524/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000525///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000526/// objc-parms:
527/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000528///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000529/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000530/// , ...
531///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000532/// objc-keyword-attributes: [OBJC2]
533/// __attribute__((unused))
534///
Steve Naroff3774dd92007-10-26 20:53:56 +0000535Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
536 tok::TokenKind mType,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000537 tok::ObjCKeywordKind MethodImplKind)
538{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000539 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000540 TypeTy *ReturnType = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000541 if (Tok.is(tok::l_paren))
Steve Naroff304ed392007-09-05 23:30:30 +0000542 ReturnType = ParseObjCTypeName();
Steve Naroff3774dd92007-10-26 20:53:56 +0000543 SourceLocation selLoc;
544 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000545 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000546 if (!SelIdent) {
547 Diag(Tok, diag::err_expected_ident); // missing selector name.
548 // FIXME: this creates a unary selector with a null identifier, is this
549 // ok?? Maybe we should skip to the next semicolon or something.
550 }
551
552 // If attributes exist after the method, parse them.
553 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000554 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000555 MethodAttrs = ParseAttributes();
556
557 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000558 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
559 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000560 0, 0, MethodAttrs, MethodImplKind);
561 }
Steve Naroff304ed392007-09-05 23:30:30 +0000562
Steve Naroff4ed9d662007-09-27 14:38:14 +0000563 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
564 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
565 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000566
567 Action::TypeTy *TypeInfo;
568 while (1) {
569 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000570
Chris Lattnerd031a452007-10-07 02:00:24 +0000571 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000572 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000573 Diag(Tok, diag::err_expected_colon);
574 break;
575 }
576 ConsumeToken(); // Eat the ':'.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000577 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000578 TypeInfo = ParseObjCTypeName();
579 else
580 TypeInfo = 0;
581 KeyTypes.push_back(TypeInfo);
Steve Naroff304ed392007-09-05 23:30:30 +0000582
Chris Lattnerd031a452007-10-07 02:00:24 +0000583 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000584 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000585 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000586
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000587 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000588 Diag(Tok, diag::err_expected_ident); // missing argument name.
589 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000590 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000591 ArgNames.push_back(Tok.getIdentifierInfo());
592 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000593
Chris Lattnerd031a452007-10-07 02:00:24 +0000594 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000595 SourceLocation Loc;
596 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000597 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000598 break;
599 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000600 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000601
602 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000603 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000604 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000605 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000606 ConsumeToken();
607 break;
608 }
609 // Parse the c-style argument declaration-specifier.
610 DeclSpec DS;
611 ParseDeclarationSpecifiers(DS);
612 // Parse the declarator.
613 Declarator ParmDecl(DS, Declarator::PrototypeContext);
614 ParseDeclarator(ParmDecl);
615 }
616
617 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000618 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000619 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000620 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000621 MethodAttrs = ParseAttributes();
622
623 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
624 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000625 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
626 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000627 &KeyTypes[0], &ArgNames[0],
628 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000629}
630
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000631/// CmpProtocolVals - Comparison predicate for sorting protocols.
632static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
633 const IdentifierInfo* const& rhs) {
634 return strcmp(lhs->getName(), rhs->getName()) < 0;
635}
636
Steve Narofffb367882007-08-20 21:31:48 +0000637/// objc-protocol-refs:
638/// '<' identifier-list '>'
639///
Steve Naroff304ed392007-09-05 23:30:30 +0000640bool Parser::ParseObjCProtocolReferences(
Steve Naroffef20ed32007-10-30 02:23:23 +0000641 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
642{
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000643 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000644
645 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000646
647 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000648 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000649 Diag(Tok, diag::err_expected_ident);
650 SkipUntil(tok::greater);
651 return true;
652 }
653 ProtocolRefs.push_back(Tok.getIdentifierInfo());
654 ConsumeToken();
655
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000656 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000657 break;
658 ConsumeToken();
659 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000660
661 // Sort protocols, keyed by name.
662 // Later on, we remove duplicates.
663 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
664
665 // Make protocol names unique.
666 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
667 ProtocolRefs.end());
Steve Narofffb367882007-08-20 21:31:48 +0000668 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000669 if (Tok.is(tok::greater)) {
670 endLoc = ConsumeAnyToken();
671 return false;
672 }
673 Diag(Tok, diag::err_expected_greater);
674 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000675}
676
677/// objc-class-instance-variables:
678/// '{' objc-instance-variable-decl-list[opt] '}'
679///
680/// objc-instance-variable-decl-list:
681/// objc-visibility-spec
682/// objc-instance-variable-decl ';'
683/// ';'
684/// objc-instance-variable-decl-list objc-visibility-spec
685/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
686/// objc-instance-variable-decl-list ';'
687///
688/// objc-visibility-spec:
689/// @private
690/// @protected
691/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000692/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000693///
694/// objc-instance-variable-decl:
695/// struct-declaration
696///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000697void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
698 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000699 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000700 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000701 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
702 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000703
704 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000705
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000706 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000707 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000708 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000709 // Each iteration of this loop reads one objc-instance-variable-decl.
710
711 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000712 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000713 Diag(Tok, diag::ext_extra_struct_semi);
714 ConsumeToken();
715 continue;
716 }
717 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000718 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000719 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000720 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000721 case tok::objc_private:
722 case tok::objc_public:
723 case tok::objc_protected:
724 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000725 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000726 ConsumeToken();
727 continue;
728 default:
729 Diag(Tok, diag::err_objc_illegal_visibility_spec);
730 ConsumeToken();
731 continue;
732 }
733 }
734 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000735 for (unsigned i = 0; i < IvarDecls.size(); i++) {
736 AllIvarDecls.push_back(IvarDecls[i]);
737 AllVisibilities.push_back(visibility);
738 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000739 IvarDecls.clear();
740
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000741 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000742 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000743 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000744 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
745 break;
746 } else {
747 Diag(Tok, diag::err_expected_semi_decl_list);
748 // Skip to end of block or statement
749 SkipUntil(tok::r_brace, true, true);
750 }
751 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000752 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000753 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000754 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Steve Naroff0acc9c92007-09-15 18:49:24 +0000755 &AllIvarDecls[0], AllIvarDecls.size(),
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000756 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000757 }
Steve Naroffc4474992007-08-21 21:17:12 +0000758 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000759}
Steve Narofffb367882007-08-20 21:31:48 +0000760
761/// objc-protocol-declaration:
762/// objc-protocol-definition
763/// objc-protocol-forward-reference
764///
765/// objc-protocol-definition:
766/// @protocol identifier
767/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000768/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000769/// @end
770///
771/// objc-protocol-forward-reference:
772/// @protocol identifier-list ';'
773///
774/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000775/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000776/// semicolon in the first alternative if objc-protocol-refs are omitted.
777
Steve Naroff72f17fb2007-08-22 22:17:26 +0000778Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000779 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000780 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
781 ConsumeToken(); // the "protocol" identifier
782
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000783 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000784 Diag(Tok, diag::err_expected_ident); // missing protocol name.
785 return 0;
786 }
787 // Save the protocol name, then consume it.
788 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
789 SourceLocation nameLoc = ConsumeToken();
790
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000791 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000792 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000793 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000794 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000795 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000796 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000797 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000798 ProtocolRefs.push_back(protocolName);
799
800 while (1) {
801 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000802 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000803 Diag(Tok, diag::err_expected_ident);
804 SkipUntil(tok::semi);
805 return 0;
806 }
807 ProtocolRefs.push_back(Tok.getIdentifierInfo());
808 ConsumeToken(); // the identifier
809
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000810 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000811 break;
812 }
813 // Consume the ';'.
814 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
815 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000816 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000817 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000818 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000819 &ProtocolRefs[0],
820 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000821 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000822 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000823 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000824 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000825 return 0;
826 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000827
Steve Naroff415c1832007-10-10 17:32:04 +0000828 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000829 protocolName, nameLoc,
830 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000831 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000832 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000833
834 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000835 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000836 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000837 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000838 }
839 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000840 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000841}
Steve Narofffb367882007-08-20 21:31:48 +0000842
843/// objc-implementation:
844/// objc-class-implementation-prologue
845/// objc-category-implementation-prologue
846///
847/// objc-class-implementation-prologue:
848/// @implementation identifier objc-superclass[opt]
849/// objc-class-instance-variables[opt]
850///
851/// objc-category-implementation-prologue:
852/// @implementation identifier ( identifier )
853
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000854Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
855 SourceLocation atLoc) {
856 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
857 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
858 ConsumeToken(); // the "implementation" identifier
859
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000860 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000861 Diag(Tok, diag::err_expected_ident); // missing class or category name.
862 return 0;
863 }
864 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000865 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000866 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
867
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000868 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000869 // we have a category implementation.
870 SourceLocation lparenLoc = ConsumeParen();
871 SourceLocation categoryLoc, rparenLoc;
872 IdentifierInfo *categoryId = 0;
873
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000874 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000875 categoryId = Tok.getIdentifierInfo();
876 categoryLoc = ConsumeToken();
877 } else {
878 Diag(Tok, diag::err_expected_ident); // missing category name.
879 return 0;
880 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000881 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000882 Diag(Tok, diag::err_expected_rparen);
883 SkipUntil(tok::r_paren, false); // don't stop at ';'
884 return 0;
885 }
886 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000887 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000888 atLoc, nameId, nameLoc, categoryId,
889 categoryLoc);
890 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000891 }
892 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000893 SourceLocation superClassLoc;
894 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000895 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000896 // We have a super class
897 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000898 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000899 Diag(Tok, diag::err_expected_ident); // missing super class name.
900 return 0;
901 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000902 superClassId = Tok.getIdentifierInfo();
903 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000904 }
Steve Naroff415c1832007-10-10 17:32:04 +0000905 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000906 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000907 superClassId, superClassLoc);
908
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000909 if (Tok.is(tok::l_brace)) // we have ivars
910 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000911
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000912 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000913}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000914
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000915Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
916 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
917 "ParseObjCAtEndDeclaration(): Expected @end");
918 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000919 if (ObjcImpDecl) {
920 // Checking is not necessary except that a parse error might have caused
921 // @implementation not to have been parsed to completion and ObjcImpDecl
922 // could be 0.
923 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000924 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000925 &AllImplMethods[0], AllImplMethods.size(),
926 atLoc);
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000927 ObjcImpDecl = 0;
928 AllImplMethods.clear();
929 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000930
Steve Narofffb367882007-08-20 21:31:48 +0000931 return 0;
932}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000933
934/// compatibility-alias-decl:
935/// @compatibility_alias alias-name class-name ';'
936///
937Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
938 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
939 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
940 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000941 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000942 Diag(Tok, diag::err_expected_ident);
943 return 0;
944 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000945 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
946 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000947 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000948 Diag(Tok, diag::err_expected_ident);
949 return 0;
950 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000951 IdentifierInfo *classId = Tok.getIdentifierInfo();
952 SourceLocation classLoc = ConsumeToken(); // consume class-name;
953 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000954 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000955 return 0;
956 }
957 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
958 aliasId, aliasLoc,
959 classId, classLoc);
960 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000961}
962
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000963/// property-synthesis:
964/// @synthesize property-ivar-list ';'
965///
966/// property-ivar-list:
967/// property-ivar
968/// property-ivar-list ',' property-ivar
969///
970/// property-ivar:
971/// identifier
972/// identifier '=' identifier
973///
974Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
975 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
976 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
977 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000978 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000979 Diag(Tok, diag::err_expected_ident);
980 return 0;
981 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000982 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000983 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000984 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000985 // property '=' ivar-name
986 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000987 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000988 Diag(Tok, diag::err_expected_ident);
989 break;
990 }
991 ConsumeToken(); // consume ivar-name
992 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000993 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000994 break;
995 ConsumeToken(); // consume ','
996 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000997 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000998 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
999 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001000}
1001
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001002/// property-dynamic:
1003/// @dynamic property-list
1004///
1005/// property-list:
1006/// identifier
1007/// property-list ',' identifier
1008///
1009Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1010 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1011 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1012 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001013 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001014 Diag(Tok, diag::err_expected_ident);
1015 return 0;
1016 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001017 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001018 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001019 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001020 break;
1021 ConsumeToken(); // consume ','
1022 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001023 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001024 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1025 return 0;
1026}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001027
1028/// objc-throw-statement:
1029/// throw expression[opt];
1030///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001031Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001032 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001033 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001034 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001035 if (Res.isInvalid) {
1036 SkipUntil(tok::semi);
1037 return 0;
1038 }
1039 }
1040 return 0;
1041}
1042
1043/// objc-try-catch-statement:
1044/// @try compound-statement objc-catch-list[opt]
1045/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1046///
1047/// objc-catch-list:
1048/// @catch ( parameter-declaration ) compound-statement
1049/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1050/// catch-parameter-declaration:
1051/// parameter-declaration
1052/// '...' [OBJC2]
1053///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001054Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001055 bool catch_or_finally_seen = false;
1056 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001057 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001058 Diag (Tok, diag::err_expected_lbrace);
1059 return 0;
1060 }
1061 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001062 while (Tok.is(tok::at)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001063 ConsumeToken();
1064 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1065 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001066 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001067 ConsumeParen();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001068 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001069 DeclSpec DS;
1070 ParseDeclarationSpecifiers(DS);
1071 // Parse the parameter-declaration.
1072 // FIXME: BlockContext may not be the right context!
1073 Declarator ParmDecl(DS, Declarator::BlockContext);
1074 ParseDeclarator(ParmDecl);
1075 }
1076 else
1077 ConsumeToken(); // consume '...'
1078 ConsumeParen();
1079 StmtResult CatchMody = ParseCompoundStatementBody();
1080 }
1081 else {
1082 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1083 return 0;
1084 }
1085 catch_or_finally_seen = true;
1086 }
1087 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1088 ConsumeToken(); // consume finally
1089 StmtResult FinallyBody = ParseCompoundStatementBody();
1090 catch_or_finally_seen = true;
1091 break;
1092 }
1093 }
1094 if (!catch_or_finally_seen)
1095 Diag(atLoc, diag::err_missing_catch_finally);
1096 return 0;
1097}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001098
Steve Naroff81f1bba2007-09-06 21:24:23 +00001099/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001100///
1101void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001102 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1103
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001104 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001105 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001106 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001107 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001108 ConsumeToken();
1109
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001110 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001111 Diag (Tok, diag::err_expected_lbrace);
1112 return;
1113 }
1114
1115 StmtResult FnBody = ParseCompoundStatementBody();
1116}
1117
Steve Naroff81f1bba2007-09-06 21:24:23 +00001118/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001119///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001120void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001121 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001122 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001123 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001124 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001125 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001126 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001127 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001128 Diag (Tok, diag::err_expected_lbrace);
1129 return;
1130 }
1131
1132 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001133}
Anders Carlssona66cad42007-08-21 17:43:55 +00001134
Steve Narofffb9dd752007-10-15 20:55:58 +00001135Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001136
1137 switch (Tok.getKind()) {
1138 case tok::string_literal: // primary-expression: string-literal
1139 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001140 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001141 default:
1142 break;
1143 }
1144
1145 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001146 case tok::objc_encode:
1147 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1148 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001149 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001150 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001151 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001152 default:
1153 Diag(AtLoc, diag::err_unexpected_at);
1154 SkipUntil(tok::semi);
1155 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001156 }
1157
1158 return 0;
1159}
1160
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001161/// objc-message-expr:
1162/// '[' objc-receiver objc-message-args ']'
1163///
1164/// objc-receiver:
1165/// expression
1166/// class-name
1167/// type-name
1168///
1169/// objc-message-args:
1170/// objc-selector
1171/// objc-keywordarg-list
1172///
1173/// objc-keywordarg-list:
1174/// objc-keywordarg
1175/// objc-keywordarg-list objc-keywordarg
1176///
1177/// objc-keywordarg:
1178/// selector-name[opt] ':' objc-keywordexpr
1179///
1180/// objc-keywordexpr:
1181/// nonempty-expr-list
1182///
1183/// nonempty-expr-list:
1184/// assignment-expression
1185/// nonempty-expr-list , assignment-expression
1186///
1187Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001188 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001189 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001190 IdentifierInfo *ReceiverName = 0;
1191 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001192 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001193 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001194 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1195 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001196 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001197 } else {
1198 ExprResult Res = ParseAssignmentExpression();
1199 if (Res.isInvalid) {
1200 SkipUntil(tok::identifier);
1201 return Res;
1202 }
1203 ReceiverExpr = Res.Val;
1204 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001205 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001206 SourceLocation Loc;
1207 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001208
1209 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1210 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1211
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001212 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001213 while (1) {
1214 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001215 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001216
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001217 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001218 Diag(Tok, diag::err_expected_colon);
1219 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001220 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001221 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001222 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001223 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001224 ExprResult Res = ParseAssignmentExpression();
1225 if (Res.isInvalid) {
1226 SkipUntil(tok::identifier);
1227 return Res;
1228 }
1229 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001230 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001231
1232 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001233 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001234 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001235 break;
1236 // We have a selector or a colon, continue parsing.
1237 }
1238 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001239 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001240 ConsumeToken();
1241 /// Parse the expression after ','
1242 ParseAssignmentExpression();
1243 }
1244 } else if (!selIdent) {
1245 Diag(Tok, diag::err_expected_ident); // missing selector name.
1246 SkipUntil(tok::semi);
1247 return 0;
1248 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001249
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001250 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001251 Diag(Tok, diag::err_expected_rsquare);
1252 SkipUntil(tok::semi);
1253 return 0;
1254 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001255 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001256
Steve Narofff9e80db2007-10-05 18:42:47 +00001257 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001258 if (nKeys == 0)
1259 KeyIdents.push_back(selIdent);
1260 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1261
1262 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001263 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001264 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1265 &KeyExprs[0]);
1266 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1267 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001268}
1269
Anders Carlssona66cad42007-08-21 17:43:55 +00001270Parser::ExprResult Parser::ParseObjCStringLiteral() {
1271 ExprResult Res = ParseStringLiteralExpression();
1272
1273 if (Res.isInvalid) return Res;
1274
1275 return Actions.ParseObjCStringLiteral(Res.Val);
1276}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001277
1278/// objc-encode-expression:
1279/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001280Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001281 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001282
1283 SourceLocation EncLoc = ConsumeToken();
1284
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001285 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001286 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1287 return true;
1288 }
1289
1290 SourceLocation LParenLoc = ConsumeParen();
1291
1292 TypeTy *Ty = ParseTypeName();
1293
Anders Carlsson92faeb82007-08-23 15:31:37 +00001294 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001295
Chris Lattnercfd61c82007-10-16 22:51:17 +00001296 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001297 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001298}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001299
1300/// objc-protocol-expression
1301/// @protocol ( protocol-name )
1302
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001303Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001304{
1305 SourceLocation ProtoLoc = ConsumeToken();
1306
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001307 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001308 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1309 return true;
1310 }
1311
1312 SourceLocation LParenLoc = ConsumeParen();
1313
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001314 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001315 Diag(Tok, diag::err_expected_ident);
1316 return true;
1317 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001318 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001319 ConsumeToken();
1320
Anders Carlsson92faeb82007-08-23 15:31:37 +00001321 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001322
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001323 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1324 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001325}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001326
1327/// objc-selector-expression
1328/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001329Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001330{
1331 SourceLocation SelectorLoc = ConsumeToken();
1332
1333 if (Tok.isNot(tok::l_paren)) {
1334 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1335 return 0;
1336 }
1337
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001338 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001339 SourceLocation LParenLoc = ConsumeParen();
1340 SourceLocation sLoc;
1341 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1342 if (!SelIdent && Tok.isNot(tok::colon)) {
1343
1344 Diag(Tok, diag::err_expected_ident); // missing selector name.
1345 return 0;
1346 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001347 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001348 if (Tok.isNot(tok::r_paren))
1349 while (1) {
1350 if (Tok.isNot(tok::colon)) {
1351 Diag(Tok, diag::err_expected_colon);
1352 break;
1353 }
1354 ConsumeToken(); // Eat the ':'.
1355 if (Tok.is(tok::r_paren))
1356 break;
1357 // Check for another keyword selector.
1358 SourceLocation Loc;
1359 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001360 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001361 if (!SelIdent && Tok.isNot(tok::colon))
1362 break;
1363 }
1364 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001365 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1366 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001367 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001368 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001369 }