blob: 69124f2939d24572acc5694634915c65e8278825 [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,
Steve Naroff667f1682007-10-30 13:30:57 +0000160 &ProtocolRefs[0], ProtocolRefs.size(),
161 endProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000162
163 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Narofffb367882007-08-20 21:31:48 +0000164
Steve Naroff0bbffd82007-08-22 16:35:03 +0000165 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000166 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000167 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000168 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000169 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000170 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000171 return 0;
172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000176
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000177 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000178 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing super class name.
181 return 0;
182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Steve Naroff304ed392007-09-05 23:30:30 +0000187 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffef20ed32007-10-30 02:23:23 +0000188 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000189 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000190 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Narofffb367882007-08-20 21:31:48 +0000191 return 0;
192 }
Steve Naroff415c1832007-10-10 17:32:04 +0000193 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000194 atLoc, nameId, nameLoc,
Steve Naroff304ed392007-09-05 23:30:30 +0000195 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000196 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000197
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000198 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000199 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000200
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000201 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000202
203 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000204 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000205 ConsumeToken(); // the "end" identifier
Steve Narofffaed3bf2007-09-10 20:51:04 +0000206 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000207 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000208 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000209 return 0;
210}
211
212/// objc-interface-decl-list:
213/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000214/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000215/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000216/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000217/// objc-interface-decl-list declaration
218/// objc-interface-decl-list ';'
219///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000220/// objc-method-requirement: [OBJC2]
221/// @required
222/// @optional
223///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000224void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
225 tok::ObjCKeywordKind contextKey) {
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000226 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000227 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000228 SourceLocation AtEndLoc;
229
Steve Naroff0bbffd82007-08-22 16:35:03 +0000230 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000231 if (Tok.is(tok::at)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000232 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff87c329f2007-08-23 18:16:40 +0000233 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000234
235 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000236 AtEndLoc = AtLoc;
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000237 break;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000238 } else if (ocKind == tok::objc_required) { // protocols only
239 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000240 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000241 if (contextKey != tok::objc_protocol)
242 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000243 } else if (ocKind == tok::objc_optional) { // protocols only
244 ConsumeToken();
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000245 MethodImplKind = ocKind;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000246 if (contextKey != tok::objc_protocol)
247 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000248 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian86f74a42007-09-12 18:23:47 +0000249 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000250 continue;
251 } else {
252 Diag(Tok, diag::err_objc_illegal_interface_qual);
253 ConsumeToken();
254 }
255 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000256 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
257 DeclTy *methodPrototype =
258 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000259 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000260 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
261 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000262 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000263 continue;
264 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000265 if (Tok.is(tok::semi))
Steve Naroff0bbffd82007-08-22 16:35:03 +0000266 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000267 else if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000268 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000269 else {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000270 // FIXME: as the name implies, this rule allows function definitions.
271 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000272 ParseDeclarationOrFunctionDefinition();
Steve Naroff304ed392007-09-05 23:30:30 +0000273 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000274 }
Steve Naroff1ccf4632007-10-30 03:43:13 +0000275 /// Insert collected methods declarations into the @interface object.
276 /// This action is executed even if we don't have any methods (so the @end
277 /// can be recorded properly).
278 Actions.ActOnAddMethodsToObjcDecl(CurScope, interfaceDecl, &allMethods[0],
279 allMethods.size(), AtEndLoc);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000280}
281
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000282/// Parse property attribute declarations.
283///
284/// property-attr-decl: '(' property-attrlist ')'
285/// property-attrlist:
286/// property-attribute
287/// property-attrlist ',' property-attribute
288/// property-attribute:
289/// getter '=' identifier
290/// setter '=' identifier ':'
291/// readonly
292/// readwrite
293/// assign
294/// retain
295/// copy
296/// nonatomic
297///
298void Parser::ParseObjCPropertyAttribute (DeclTy *interfaceDecl) {
299 SourceLocation loc = ConsumeParen(); // consume '('
300 while (isObjCPropertyAttribute()) {
301 const IdentifierInfo *II = Tok.getIdentifierInfo();
302 // getter/setter require extra treatment.
303 if (II == ObjcPropertyAttrs[objc_getter] ||
304 II == ObjcPropertyAttrs[objc_setter]) {
305 // skip getter/setter part.
306 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000307 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000308 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000309 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000310 if (II == ObjcPropertyAttrs[objc_setter]) {
311 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000312 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000313 Diag(loc, diag::err_expected_colon);
314 SkipUntil(tok::r_paren,true,true);
315 break;
316 }
317 }
318 }
319 else {
320 Diag(loc, diag::err_expected_ident);
321 SkipUntil(tok::r_paren,true,true);
322 break;
323 }
324 }
325 else {
326 Diag(loc, diag::err_objc_expected_equal);
327 SkipUntil(tok::r_paren,true,true);
328 break;
329 }
330 }
331 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000332 if (Tok.is(tok::comma)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000333 loc = ConsumeToken();
334 continue;
335 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000336 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000337 break;
338 Diag(loc, diag::err_expected_rparen);
339 SkipUntil(tok::semi);
340 return;
341 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000342 if (Tok.is(tok::r_paren))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000343 ConsumeParen();
344 else {
345 Diag(loc, diag::err_objc_expected_property_attr);
346 SkipUntil(tok::r_paren); // recover from error inside attribute list
347 }
348}
349
350/// Main routine to parse property declaration.
351///
352/// @property property-attr-decl[opt] property-component-decl ';'
353///
354void Parser::ParseObjCPropertyDecl(DeclTy *interfaceDecl) {
355 assert(Tok.isObjCAtKeyword(tok::objc_property) &&
356 "ParseObjCPropertyDecl(): Expected @property");
357 ConsumeToken(); // the "property" identifier
358 // Parse property attribute list, if any.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000359 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000360 // property has attribute list.
361 ParseObjCPropertyAttribute(0/*FIXME*/);
362 }
363 // Parse declaration portion of @property.
364 llvm::SmallVector<DeclTy*, 32> PropertyDecls;
365 ParseStructDeclaration(interfaceDecl, PropertyDecls);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000366 if (Tok.is(tok::semi))
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000367 ConsumeToken();
368 else {
369 Diag(Tok, diag::err_expected_semi_decl_list);
370 SkipUntil(tok::r_brace, true, true);
371 }
Chris Lattner4b009652007-07-25 00:24:17 +0000372}
Steve Narofffb367882007-08-20 21:31:48 +0000373
Steve Naroff81f1bba2007-09-06 21:24:23 +0000374/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000375/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000376/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000377///
378/// objc-instance-method: '-'
379/// objc-class-method: '+'
380///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000381/// objc-method-attributes: [OBJC2]
382/// __attribute__((deprecated))
383///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000384Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
385 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000386 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000387
388 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000389 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000390
Steve Naroff3774dd92007-10-26 20:53:56 +0000391 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000392 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000393 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000394 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000395}
396
397/// objc-selector:
398/// identifier
399/// one of
400/// enum struct union if else while do for switch case default
401/// break continue return goto asm sizeof typeof __alignof
402/// unsigned long const short volatile signed restrict _Complex
403/// in out inout bycopy byref oneway int char float double void _Bool
404///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000405IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000406 switch (Tok.getKind()) {
407 default:
408 return 0;
409 case tok::identifier:
410 case tok::kw_typeof:
411 case tok::kw___alignof:
412 case tok::kw_auto:
413 case tok::kw_break:
414 case tok::kw_case:
415 case tok::kw_char:
416 case tok::kw_const:
417 case tok::kw_continue:
418 case tok::kw_default:
419 case tok::kw_do:
420 case tok::kw_double:
421 case tok::kw_else:
422 case tok::kw_enum:
423 case tok::kw_extern:
424 case tok::kw_float:
425 case tok::kw_for:
426 case tok::kw_goto:
427 case tok::kw_if:
428 case tok::kw_inline:
429 case tok::kw_int:
430 case tok::kw_long:
431 case tok::kw_register:
432 case tok::kw_restrict:
433 case tok::kw_return:
434 case tok::kw_short:
435 case tok::kw_signed:
436 case tok::kw_sizeof:
437 case tok::kw_static:
438 case tok::kw_struct:
439 case tok::kw_switch:
440 case tok::kw_typedef:
441 case tok::kw_union:
442 case tok::kw_unsigned:
443 case tok::kw_void:
444 case tok::kw_volatile:
445 case tok::kw_while:
446 case tok::kw__Bool:
447 case tok::kw__Complex:
448 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000449 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000450 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000451 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000452}
453
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000454/// property-attrlist: one of
455/// readonly getter setter assign retain copy nonatomic
456///
457bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000458 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000459 const IdentifierInfo *II = Tok.getIdentifierInfo();
460 for (unsigned i = 0; i < objc_NumAttrs; ++i)
461 if (II == ObjcPropertyAttrs[i]) return true;
462 }
463 return false;
464}
465
Steve Naroff0bbffd82007-08-22 16:35:03 +0000466/// objc-type-name:
467/// '(' objc-type-qualifiers[opt] type-name ')'
468/// '(' objc-type-qualifiers[opt] ')'
469///
470/// objc-type-qualifiers:
471/// objc-type-qualifier
472/// objc-type-qualifiers objc-type-qualifier
473///
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000474Parser::TypeTy *Parser::ParseObjCTypeName(ObjcDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000475 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000476
477 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner265c8172007-09-27 15:15:46 +0000478 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000479
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000480 // Parse type qualifiers, in, inout, etc.
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000481 ParseObjcTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000482
Steve Naroff0bbffd82007-08-22 16:35:03 +0000483 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000484 Ty = ParseTypeName();
485 // FIXME: back when Sema support is in place...
486 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000487 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000488 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000489 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000490 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000491 }
492 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000493 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000494}
495
496/// objc-method-decl:
497/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000498/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000499/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000500/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000501///
502/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000503/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000504/// objc-keyword-selector objc-keyword-decl
505///
506/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000507/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
508/// objc-selector ':' objc-keyword-attributes[opt] identifier
509/// ':' objc-type-name objc-keyword-attributes[opt] identifier
510/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000511///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000512/// objc-parmlist:
513/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000514///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000515/// objc-parms:
516/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000517///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000518/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000519/// , ...
520///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000521/// objc-keyword-attributes: [OBJC2]
522/// __attribute__((unused))
523///
Steve Naroff3774dd92007-10-26 20:53:56 +0000524Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
525 tok::TokenKind mType,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000526 tok::ObjCKeywordKind MethodImplKind)
527{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000528 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000529 TypeTy *ReturnType = 0;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000530 ObjcDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000531 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000532 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroff3774dd92007-10-26 20:53:56 +0000533 SourceLocation selLoc;
534 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000535 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000536 if (!SelIdent) {
537 Diag(Tok, diag::err_expected_ident); // missing selector name.
538 // FIXME: this creates a unary selector with a null identifier, is this
539 // ok?? Maybe we should skip to the next semicolon or something.
540 }
541
542 // If attributes exist after the method, parse them.
543 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000544 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000545 MethodAttrs = ParseAttributes();
546
547 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000548 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000549 mType, DSRet, ReturnType, Sel,
550 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000551 }
Steve Naroff304ed392007-09-05 23:30:30 +0000552
Steve Naroff4ed9d662007-09-27 14:38:14 +0000553 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
554 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000555 llvm::SmallVector<ObjcDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000556 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000557
558 Action::TypeTy *TypeInfo;
559 while (1) {
560 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000561
Chris Lattnerd031a452007-10-07 02:00:24 +0000562 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000563 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000564 Diag(Tok, diag::err_expected_colon);
565 break;
566 }
567 ConsumeToken(); // Eat the ':'.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000568 ObjcDeclSpec DSType;
569 if (Tok.is(tok::l_paren)) { // Parse the argument type.
570 TypeInfo = ParseObjCTypeName(DSType);
571 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000572 else
573 TypeInfo = 0;
574 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000575 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000576
Chris Lattnerd031a452007-10-07 02:00:24 +0000577 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000578 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000579 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000580
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000581 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000582 Diag(Tok, diag::err_expected_ident); // missing argument name.
583 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000584 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000585 ArgNames.push_back(Tok.getIdentifierInfo());
586 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000587
Chris Lattnerd031a452007-10-07 02:00:24 +0000588 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000589 SourceLocation Loc;
590 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000591 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000592 break;
593 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000594 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000595
596 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000597 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000598 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000599 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000600 ConsumeToken();
601 break;
602 }
603 // Parse the c-style argument declaration-specifier.
604 DeclSpec DS;
605 ParseDeclarationSpecifiers(DS);
606 // Parse the declarator.
607 Declarator ParmDecl(DS, Declarator::PrototypeContext);
608 ParseDeclarator(ParmDecl);
609 }
610
611 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000612 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000613 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000614 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000615 MethodAttrs = ParseAttributes();
616
617 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
618 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000619 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000620 mType, DSRet, ReturnType, Sel,
621 &ArgTypeQuals[0], &KeyTypes[0],
622 &ArgNames[0],
Chris Lattnerd031a452007-10-07 02:00:24 +0000623 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000624}
625
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000626/// CmpProtocolVals - Comparison predicate for sorting protocols.
627static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
628 const IdentifierInfo* const& rhs) {
629 return strcmp(lhs->getName(), rhs->getName()) < 0;
630}
631
Steve Narofffb367882007-08-20 21:31:48 +0000632/// objc-protocol-refs:
633/// '<' identifier-list '>'
634///
Steve Naroff304ed392007-09-05 23:30:30 +0000635bool Parser::ParseObjCProtocolReferences(
Steve Naroffef20ed32007-10-30 02:23:23 +0000636 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
637{
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000638 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000639
640 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000641
642 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000643 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000644 Diag(Tok, diag::err_expected_ident);
645 SkipUntil(tok::greater);
646 return true;
647 }
648 ProtocolRefs.push_back(Tok.getIdentifierInfo());
649 ConsumeToken();
650
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000651 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000652 break;
653 ConsumeToken();
654 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000655
656 // Sort protocols, keyed by name.
657 // Later on, we remove duplicates.
658 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
659
660 // Make protocol names unique.
661 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
662 ProtocolRefs.end());
Steve Narofffb367882007-08-20 21:31:48 +0000663 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000664 if (Tok.is(tok::greater)) {
665 endLoc = ConsumeAnyToken();
666 return false;
667 }
668 Diag(Tok, diag::err_expected_greater);
669 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000670}
671
672/// objc-class-instance-variables:
673/// '{' objc-instance-variable-decl-list[opt] '}'
674///
675/// objc-instance-variable-decl-list:
676/// objc-visibility-spec
677/// objc-instance-variable-decl ';'
678/// ';'
679/// objc-instance-variable-decl-list objc-visibility-spec
680/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
681/// objc-instance-variable-decl-list ';'
682///
683/// objc-visibility-spec:
684/// @private
685/// @protected
686/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000687/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000688///
689/// objc-instance-variable-decl:
690/// struct-declaration
691///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000692void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
693 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000694 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000695 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000696 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
697 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000698
699 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000700
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000701 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000702 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000703 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000704 // Each iteration of this loop reads one objc-instance-variable-decl.
705
706 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000707 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000708 Diag(Tok, diag::ext_extra_struct_semi);
709 ConsumeToken();
710 continue;
711 }
712 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000713 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000714 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000715 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000716 case tok::objc_private:
717 case tok::objc_public:
718 case tok::objc_protected:
719 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000720 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000721 ConsumeToken();
722 continue;
723 default:
724 Diag(Tok, diag::err_objc_illegal_visibility_spec);
725 ConsumeToken();
726 continue;
727 }
728 }
729 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000730 for (unsigned i = 0; i < IvarDecls.size(); i++) {
731 AllIvarDecls.push_back(IvarDecls[i]);
732 AllVisibilities.push_back(visibility);
733 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000734 IvarDecls.clear();
735
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000736 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000737 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000738 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000739 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
740 break;
741 } else {
742 Diag(Tok, diag::err_expected_semi_decl_list);
743 // Skip to end of block or statement
744 SkipUntil(tok::r_brace, true, true);
745 }
746 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000747 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000748 // Call ActOnFields() even if we don't have any decls. This is useful
749 // for code rewriting tools that need to be aware of the empty list.
750 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
751 &AllIvarDecls[0], AllIvarDecls.size(),
752 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Steve Naroffc4474992007-08-21 21:17:12 +0000753 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000754}
Steve Narofffb367882007-08-20 21:31:48 +0000755
756/// objc-protocol-declaration:
757/// objc-protocol-definition
758/// objc-protocol-forward-reference
759///
760/// objc-protocol-definition:
761/// @protocol identifier
762/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000763/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000764/// @end
765///
766/// objc-protocol-forward-reference:
767/// @protocol identifier-list ';'
768///
769/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000770/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000771/// semicolon in the first alternative if objc-protocol-refs are omitted.
772
Steve Naroff72f17fb2007-08-22 22:17:26 +0000773Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000774 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000775 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
776 ConsumeToken(); // the "protocol" identifier
777
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000778 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000779 Diag(Tok, diag::err_expected_ident); // missing protocol name.
780 return 0;
781 }
782 // Save the protocol name, then consume it.
783 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
784 SourceLocation nameLoc = ConsumeToken();
785
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000786 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000787 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000788 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000789 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000790 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000791 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000792 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000793 ProtocolRefs.push_back(protocolName);
794
795 while (1) {
796 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000797 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000798 Diag(Tok, diag::err_expected_ident);
799 SkipUntil(tok::semi);
800 return 0;
801 }
802 ProtocolRefs.push_back(Tok.getIdentifierInfo());
803 ConsumeToken(); // the identifier
804
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000805 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000806 break;
807 }
808 // Consume the ';'.
809 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
810 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000811 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000812 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000813 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000814 &ProtocolRefs[0],
815 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000816 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000817 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000818 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000819 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000820 return 0;
821 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000822
Steve Naroff415c1832007-10-10 17:32:04 +0000823 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000824 protocolName, nameLoc,
825 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000826 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000827 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000828
829 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000830 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000831 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000832 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000833 }
834 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000835 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000836}
Steve Narofffb367882007-08-20 21:31:48 +0000837
838/// objc-implementation:
839/// objc-class-implementation-prologue
840/// objc-category-implementation-prologue
841///
842/// objc-class-implementation-prologue:
843/// @implementation identifier objc-superclass[opt]
844/// objc-class-instance-variables[opt]
845///
846/// objc-category-implementation-prologue:
847/// @implementation identifier ( identifier )
848
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000849Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
850 SourceLocation atLoc) {
851 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
852 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
853 ConsumeToken(); // the "implementation" identifier
854
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000855 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000856 Diag(Tok, diag::err_expected_ident); // missing class or category name.
857 return 0;
858 }
859 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000860 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000861 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
862
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000863 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000864 // we have a category implementation.
865 SourceLocation lparenLoc = ConsumeParen();
866 SourceLocation categoryLoc, rparenLoc;
867 IdentifierInfo *categoryId = 0;
868
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000869 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000870 categoryId = Tok.getIdentifierInfo();
871 categoryLoc = ConsumeToken();
872 } else {
873 Diag(Tok, diag::err_expected_ident); // missing category name.
874 return 0;
875 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000876 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000877 Diag(Tok, diag::err_expected_rparen);
878 SkipUntil(tok::r_paren, false); // don't stop at ';'
879 return 0;
880 }
881 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000882 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000883 atLoc, nameId, nameLoc, categoryId,
884 categoryLoc);
885 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000886 }
887 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000888 SourceLocation superClassLoc;
889 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000890 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000891 // We have a super class
892 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000893 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000894 Diag(Tok, diag::err_expected_ident); // missing super class name.
895 return 0;
896 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000897 superClassId = Tok.getIdentifierInfo();
898 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000899 }
Steve Naroff415c1832007-10-10 17:32:04 +0000900 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000901 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000902 superClassId, superClassLoc);
903
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000904 if (Tok.is(tok::l_brace)) // we have ivars
905 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000906
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000907 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000908}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000909
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000910Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
911 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
912 "ParseObjCAtEndDeclaration(): Expected @end");
913 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000914 if (ObjcImpDecl) {
915 // Checking is not necessary except that a parse error might have caused
916 // @implementation not to have been parsed to completion and ObjcImpDecl
917 // could be 0.
918 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000919 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000920 &AllImplMethods[0], AllImplMethods.size(),
921 atLoc);
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000922 ObjcImpDecl = 0;
923 AllImplMethods.clear();
924 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000925
Steve Narofffb367882007-08-20 21:31:48 +0000926 return 0;
927}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000928
929/// compatibility-alias-decl:
930/// @compatibility_alias alias-name class-name ';'
931///
932Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
933 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
934 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
935 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000936 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000937 Diag(Tok, diag::err_expected_ident);
938 return 0;
939 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000940 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
941 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000942 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000943 Diag(Tok, diag::err_expected_ident);
944 return 0;
945 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000946 IdentifierInfo *classId = Tok.getIdentifierInfo();
947 SourceLocation classLoc = ConsumeToken(); // consume class-name;
948 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000949 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000950 return 0;
951 }
952 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
953 aliasId, aliasLoc,
954 classId, classLoc);
955 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000956}
957
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000958/// property-synthesis:
959/// @synthesize property-ivar-list ';'
960///
961/// property-ivar-list:
962/// property-ivar
963/// property-ivar-list ',' property-ivar
964///
965/// property-ivar:
966/// identifier
967/// identifier '=' identifier
968///
969Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
970 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
971 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
972 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000973 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000974 Diag(Tok, diag::err_expected_ident);
975 return 0;
976 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000977 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000978 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000979 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000980 // property '=' ivar-name
981 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000982 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000983 Diag(Tok, diag::err_expected_ident);
984 break;
985 }
986 ConsumeToken(); // consume ivar-name
987 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000988 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000989 break;
990 ConsumeToken(); // consume ','
991 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000992 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000993 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
994 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000995}
996
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000997/// property-dynamic:
998/// @dynamic property-list
999///
1000/// property-list:
1001/// identifier
1002/// property-list ',' identifier
1003///
1004Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1005 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1006 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1007 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001008 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001009 Diag(Tok, diag::err_expected_ident);
1010 return 0;
1011 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001012 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001013 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001014 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001015 break;
1016 ConsumeToken(); // consume ','
1017 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001018 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001019 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1020 return 0;
1021}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001022
1023/// objc-throw-statement:
1024/// throw expression[opt];
1025///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001026Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001027 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001028 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001029 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001030 if (Res.isInvalid) {
1031 SkipUntil(tok::semi);
1032 return 0;
1033 }
1034 }
1035 return 0;
1036}
1037
1038/// objc-try-catch-statement:
1039/// @try compound-statement objc-catch-list[opt]
1040/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1041///
1042/// objc-catch-list:
1043/// @catch ( parameter-declaration ) compound-statement
1044/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1045/// catch-parameter-declaration:
1046/// parameter-declaration
1047/// '...' [OBJC2]
1048///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001049Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001050 bool catch_or_finally_seen = false;
1051 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001052 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001053 Diag (Tok, diag::err_expected_lbrace);
1054 return 0;
1055 }
1056 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001057 while (Tok.is(tok::at)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001058 ConsumeToken();
1059 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1060 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001061 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001062 ConsumeParen();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001063 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001064 DeclSpec DS;
1065 ParseDeclarationSpecifiers(DS);
1066 // Parse the parameter-declaration.
1067 // FIXME: BlockContext may not be the right context!
1068 Declarator ParmDecl(DS, Declarator::BlockContext);
1069 ParseDeclarator(ParmDecl);
1070 }
1071 else
1072 ConsumeToken(); // consume '...'
1073 ConsumeParen();
1074 StmtResult CatchMody = ParseCompoundStatementBody();
1075 }
1076 else {
1077 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1078 return 0;
1079 }
1080 catch_or_finally_seen = true;
1081 }
1082 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1083 ConsumeToken(); // consume finally
1084 StmtResult FinallyBody = ParseCompoundStatementBody();
1085 catch_or_finally_seen = true;
1086 break;
1087 }
1088 }
1089 if (!catch_or_finally_seen)
1090 Diag(atLoc, diag::err_missing_catch_finally);
1091 return 0;
1092}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001093
Steve Naroff81f1bba2007-09-06 21:24:23 +00001094/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001095///
1096void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001097 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1098
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001099 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001100 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001101 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001102 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001103 ConsumeToken();
1104
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001105 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001106 Diag (Tok, diag::err_expected_lbrace);
1107 return;
1108 }
1109
1110 StmtResult FnBody = ParseCompoundStatementBody();
1111}
1112
Steve Naroff81f1bba2007-09-06 21:24:23 +00001113/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001114///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001115void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001116 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001117 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001118 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001119 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001120 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001121 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001122 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001123 Diag (Tok, diag::err_expected_lbrace);
1124 return;
1125 }
1126
1127 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001128}
Anders Carlssona66cad42007-08-21 17:43:55 +00001129
Steve Narofffb9dd752007-10-15 20:55:58 +00001130Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001131
1132 switch (Tok.getKind()) {
1133 case tok::string_literal: // primary-expression: string-literal
1134 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001135 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001136 default:
1137 break;
1138 }
1139
1140 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001141 case tok::objc_encode:
1142 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1143 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001144 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001145 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001146 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001147 default:
1148 Diag(AtLoc, diag::err_unexpected_at);
1149 SkipUntil(tok::semi);
1150 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001151 }
1152
1153 return 0;
1154}
1155
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001156/// objc-message-expr:
1157/// '[' objc-receiver objc-message-args ']'
1158///
1159/// objc-receiver:
1160/// expression
1161/// class-name
1162/// type-name
1163///
1164/// objc-message-args:
1165/// objc-selector
1166/// objc-keywordarg-list
1167///
1168/// objc-keywordarg-list:
1169/// objc-keywordarg
1170/// objc-keywordarg-list objc-keywordarg
1171///
1172/// objc-keywordarg:
1173/// selector-name[opt] ':' objc-keywordexpr
1174///
1175/// objc-keywordexpr:
1176/// nonempty-expr-list
1177///
1178/// nonempty-expr-list:
1179/// assignment-expression
1180/// nonempty-expr-list , assignment-expression
1181///
1182Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001183 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001184 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001185 IdentifierInfo *ReceiverName = 0;
1186 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001187 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001188 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001189 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1190 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001191 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001192 } else {
1193 ExprResult Res = ParseAssignmentExpression();
1194 if (Res.isInvalid) {
1195 SkipUntil(tok::identifier);
1196 return Res;
1197 }
1198 ReceiverExpr = Res.Val;
1199 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001200 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001201 SourceLocation Loc;
1202 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001203
1204 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1205 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1206
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001207 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001208 while (1) {
1209 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001210 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001211
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001212 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001213 Diag(Tok, diag::err_expected_colon);
1214 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001215 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001216 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001217 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001218 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001219 ExprResult Res = ParseAssignmentExpression();
1220 if (Res.isInvalid) {
1221 SkipUntil(tok::identifier);
1222 return Res;
1223 }
1224 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001225 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001226
1227 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001228 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001229 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001230 break;
1231 // We have a selector or a colon, continue parsing.
1232 }
1233 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001234 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001235 ConsumeToken();
1236 /// Parse the expression after ','
1237 ParseAssignmentExpression();
1238 }
1239 } else if (!selIdent) {
1240 Diag(Tok, diag::err_expected_ident); // missing selector name.
1241 SkipUntil(tok::semi);
1242 return 0;
1243 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001244
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001245 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001246 Diag(Tok, diag::err_expected_rsquare);
1247 SkipUntil(tok::semi);
1248 return 0;
1249 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001250 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001251
Steve Narofff9e80db2007-10-05 18:42:47 +00001252 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001253 if (nKeys == 0)
1254 KeyIdents.push_back(selIdent);
1255 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1256
1257 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001258 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001259 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1260 &KeyExprs[0]);
1261 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1262 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001263}
1264
Anders Carlssona66cad42007-08-21 17:43:55 +00001265Parser::ExprResult Parser::ParseObjCStringLiteral() {
1266 ExprResult Res = ParseStringLiteralExpression();
1267
1268 if (Res.isInvalid) return Res;
1269
1270 return Actions.ParseObjCStringLiteral(Res.Val);
1271}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001272
1273/// objc-encode-expression:
1274/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001275Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001276 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001277
1278 SourceLocation EncLoc = ConsumeToken();
1279
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001280 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001281 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1282 return true;
1283 }
1284
1285 SourceLocation LParenLoc = ConsumeParen();
1286
1287 TypeTy *Ty = ParseTypeName();
1288
Anders Carlsson92faeb82007-08-23 15:31:37 +00001289 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001290
Chris Lattnercfd61c82007-10-16 22:51:17 +00001291 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001292 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001293}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001294
1295/// objc-protocol-expression
1296/// @protocol ( protocol-name )
1297
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001298Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001299{
1300 SourceLocation ProtoLoc = ConsumeToken();
1301
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001302 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001303 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1304 return true;
1305 }
1306
1307 SourceLocation LParenLoc = ConsumeParen();
1308
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001309 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001310 Diag(Tok, diag::err_expected_ident);
1311 return true;
1312 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001313 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001314 ConsumeToken();
1315
Anders Carlsson92faeb82007-08-23 15:31:37 +00001316 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001317
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001318 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1319 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001320}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001321
1322/// objc-selector-expression
1323/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001324Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001325{
1326 SourceLocation SelectorLoc = ConsumeToken();
1327
1328 if (Tok.isNot(tok::l_paren)) {
1329 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1330 return 0;
1331 }
1332
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001333 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001334 SourceLocation LParenLoc = ConsumeParen();
1335 SourceLocation sLoc;
1336 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1337 if (!SelIdent && Tok.isNot(tok::colon)) {
1338
1339 Diag(Tok, diag::err_expected_ident); // missing selector name.
1340 return 0;
1341 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001342 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001343 if (Tok.isNot(tok::r_paren))
1344 while (1) {
1345 if (Tok.isNot(tok::colon)) {
1346 Diag(Tok, diag::err_expected_colon);
1347 break;
1348 }
1349 ConsumeToken(); // Eat the ':'.
1350 if (Tok.is(tok::r_paren))
1351 break;
1352 // Check for another keyword selector.
1353 SourceLocation Loc;
1354 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001355 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001356 if (!SelIdent && Tok.isNot(tok::colon))
1357 break;
1358 }
1359 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001360 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1361 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001362 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001363 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001364 }