blob: 301b04148a314ef6a4f7bd67072487fb5daebfe1 [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///
Steve Naroff304ed392007-09-05 23:30:30 +0000474Parser::TypeTy *Parser::ParseObjCTypeName() {
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.
481 ObjcDeclSpec DS;
482 ParseObjcTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000483
Steve Naroff0bbffd82007-08-22 16:35:03 +0000484 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000485 Ty = ParseTypeName();
486 // FIXME: back when Sema support is in place...
487 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000488 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000489 if (Tok.isNot(tok::r_paren)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000490 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff304ed392007-09-05 23:30:30 +0000491 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff0bbffd82007-08-22 16:35:03 +0000492 }
493 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000494 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000495}
496
497/// objc-method-decl:
498/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000499/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000500/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000501/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000502///
503/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000504/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000505/// objc-keyword-selector objc-keyword-decl
506///
507/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000508/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
509/// objc-selector ':' objc-keyword-attributes[opt] identifier
510/// ':' objc-type-name objc-keyword-attributes[opt] identifier
511/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000512///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000513/// objc-parmlist:
514/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000515///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000516/// objc-parms:
517/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000518///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000519/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000520/// , ...
521///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000522/// objc-keyword-attributes: [OBJC2]
523/// __attribute__((unused))
524///
Steve Naroff3774dd92007-10-26 20:53:56 +0000525Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
526 tok::TokenKind mType,
Steve Naroff4ed9d662007-09-27 14:38:14 +0000527 tok::ObjCKeywordKind MethodImplKind)
528{
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000529 // Parse the return type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000530 TypeTy *ReturnType = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000531 if (Tok.is(tok::l_paren))
Steve Naroff304ed392007-09-05 23:30:30 +0000532 ReturnType = ParseObjCTypeName();
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(),
549 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000550 0, 0, MethodAttrs, MethodImplKind);
551 }
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;
555 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000556
557 Action::TypeTy *TypeInfo;
558 while (1) {
559 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000560
Chris Lattnerd031a452007-10-07 02:00:24 +0000561 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000562 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000563 Diag(Tok, diag::err_expected_colon);
564 break;
565 }
566 ConsumeToken(); // Eat the ':'.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000567 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerd031a452007-10-07 02:00:24 +0000568 TypeInfo = ParseObjCTypeName();
569 else
570 TypeInfo = 0;
571 KeyTypes.push_back(TypeInfo);
Steve Naroff304ed392007-09-05 23:30:30 +0000572
Chris Lattnerd031a452007-10-07 02:00:24 +0000573 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000574 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000575 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000576
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000577 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000578 Diag(Tok, diag::err_expected_ident); // missing argument name.
579 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000580 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000581 ArgNames.push_back(Tok.getIdentifierInfo());
582 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000583
Chris Lattnerd031a452007-10-07 02:00:24 +0000584 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000585 SourceLocation Loc;
586 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000587 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000588 break;
589 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000590 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000591
592 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000593 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000594 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000595 if (Tok.is(tok::ellipsis)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000596 ConsumeToken();
597 break;
598 }
599 // Parse the c-style argument declaration-specifier.
600 DeclSpec DS;
601 ParseDeclarationSpecifiers(DS);
602 // Parse the declarator.
603 Declarator ParmDecl(DS, Declarator::PrototypeContext);
604 ParseDeclarator(ParmDecl);
605 }
606
607 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000608 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000609 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000610 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000611 MethodAttrs = ParseAttributes();
612
613 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
614 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000615 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
616 mType, ReturnType, Sel,
Chris Lattnerd031a452007-10-07 02:00:24 +0000617 &KeyTypes[0], &ArgNames[0],
618 MethodAttrs, MethodImplKind);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000619}
620
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000621/// CmpProtocolVals - Comparison predicate for sorting protocols.
622static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
623 const IdentifierInfo* const& rhs) {
624 return strcmp(lhs->getName(), rhs->getName()) < 0;
625}
626
Steve Narofffb367882007-08-20 21:31:48 +0000627/// objc-protocol-refs:
628/// '<' identifier-list '>'
629///
Steve Naroff304ed392007-09-05 23:30:30 +0000630bool Parser::ParseObjCProtocolReferences(
Steve Naroffef20ed32007-10-30 02:23:23 +0000631 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
632{
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000633 assert(Tok.is(tok::less) && "expected <");
Steve Narofffb367882007-08-20 21:31:48 +0000634
635 ConsumeToken(); // the "<"
Steve Narofffb367882007-08-20 21:31:48 +0000636
637 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000638 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000639 Diag(Tok, diag::err_expected_ident);
640 SkipUntil(tok::greater);
641 return true;
642 }
643 ProtocolRefs.push_back(Tok.getIdentifierInfo());
644 ConsumeToken();
645
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000646 if (Tok.isNot(tok::comma))
Steve Narofffb367882007-08-20 21:31:48 +0000647 break;
648 ConsumeToken();
649 }
Fariborz Jahanianc04aff12007-10-08 23:06:41 +0000650
651 // Sort protocols, keyed by name.
652 // Later on, we remove duplicates.
653 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
654
655 // Make protocol names unique.
656 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
657 ProtocolRefs.end());
Steve Narofffb367882007-08-20 21:31:48 +0000658 // Consume the '>'.
Steve Naroffef20ed32007-10-30 02:23:23 +0000659 if (Tok.is(tok::greater)) {
660 endLoc = ConsumeAnyToken();
661 return false;
662 }
663 Diag(Tok, diag::err_expected_greater);
664 return true;
Steve Narofffb367882007-08-20 21:31:48 +0000665}
666
667/// objc-class-instance-variables:
668/// '{' objc-instance-variable-decl-list[opt] '}'
669///
670/// objc-instance-variable-decl-list:
671/// objc-visibility-spec
672/// objc-instance-variable-decl ';'
673/// ';'
674/// objc-instance-variable-decl-list objc-visibility-spec
675/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
676/// objc-instance-variable-decl-list ';'
677///
678/// objc-visibility-spec:
679/// @private
680/// @protected
681/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000682/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000683///
684/// objc-instance-variable-decl:
685/// struct-declaration
686///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000687void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
688 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000689 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff81f1bba2007-09-06 21:24:23 +0000690 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000691 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
692 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffc4474992007-08-21 21:17:12 +0000693
694 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000695
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000696 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffc4474992007-08-21 21:17:12 +0000697 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000698 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000699 // Each iteration of this loop reads one objc-instance-variable-decl.
700
701 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000702 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000703 Diag(Tok, diag::ext_extra_struct_semi);
704 ConsumeToken();
705 continue;
706 }
707 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000708 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000709 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000710 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000711 case tok::objc_private:
712 case tok::objc_public:
713 case tok::objc_protected:
714 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000715 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000716 ConsumeToken();
717 continue;
718 default:
719 Diag(Tok, diag::err_objc_illegal_visibility_spec);
720 ConsumeToken();
721 continue;
722 }
723 }
724 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000725 for (unsigned i = 0; i < IvarDecls.size(); i++) {
726 AllIvarDecls.push_back(IvarDecls[i]);
727 AllVisibilities.push_back(visibility);
728 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000729 IvarDecls.clear();
730
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000731 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000732 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000733 } else if (Tok.is(tok::r_brace)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000734 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
735 break;
736 } else {
737 Diag(Tok, diag::err_expected_semi_decl_list);
738 // Skip to end of block or statement
739 SkipUntil(tok::r_brace, true, true);
740 }
741 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000742 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000743 // Call ActOnFields() even if we don't have any decls. This is useful
744 // for code rewriting tools that need to be aware of the empty list.
745 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
746 &AllIvarDecls[0], AllIvarDecls.size(),
747 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Steve Naroffc4474992007-08-21 21:17:12 +0000748 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000749}
Steve Narofffb367882007-08-20 21:31:48 +0000750
751/// objc-protocol-declaration:
752/// objc-protocol-definition
753/// objc-protocol-forward-reference
754///
755/// objc-protocol-definition:
756/// @protocol identifier
757/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000758/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000759/// @end
760///
761/// objc-protocol-forward-reference:
762/// @protocol identifier-list ';'
763///
764/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000765/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000766/// semicolon in the first alternative if objc-protocol-refs are omitted.
767
Steve Naroff72f17fb2007-08-22 22:17:26 +0000768Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000769 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000770 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
771 ConsumeToken(); // the "protocol" identifier
772
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000773 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000774 Diag(Tok, diag::err_expected_ident); // missing protocol name.
775 return 0;
776 }
777 // Save the protocol name, then consume it.
778 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
779 SourceLocation nameLoc = ConsumeToken();
780
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000781 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000782 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000783 ConsumeToken();
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000784 ProtocolRefs.push_back(protocolName);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000785 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000786 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000787 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000788 ProtocolRefs.push_back(protocolName);
789
790 while (1) {
791 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000792 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000793 Diag(Tok, diag::err_expected_ident);
794 SkipUntil(tok::semi);
795 return 0;
796 }
797 ProtocolRefs.push_back(Tok.getIdentifierInfo());
798 ConsumeToken(); // the identifier
799
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000800 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000801 break;
802 }
803 // Consume the ';'.
804 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
805 return 0;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000806 }
Fariborz Jahanianc716c942007-09-21 15:40:54 +0000807 if (ProtocolRefs.size() > 0)
Steve Naroff415c1832007-10-10 17:32:04 +0000808 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000809 &ProtocolRefs[0],
810 ProtocolRefs.size());
Steve Naroff72f17fb2007-08-22 22:17:26 +0000811 // Last, and definitely not least, parse a protocol declaration.
Steve Naroffef20ed32007-10-30 02:23:23 +0000812 SourceLocation endProtoLoc;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000813 if (Tok.is(tok::less)) {
Steve Naroffef20ed32007-10-30 02:23:23 +0000814 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000815 return 0;
816 }
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000817
Steve Naroff415c1832007-10-10 17:32:04 +0000818 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000819 protocolName, nameLoc,
820 &ProtocolRefs[0],
Steve Naroffef20ed32007-10-30 02:23:23 +0000821 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000822 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000823
824 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff87c329f2007-08-23 18:16:40 +0000825 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000826 ConsumeToken(); // the "end" identifier
Fariborz Jahanianac20be22007-10-08 18:53:38 +0000827 return ProtoType;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000828 }
829 Diag(Tok, diag::err_objc_missing_end);
Steve Narofffb367882007-08-20 21:31:48 +0000830 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000831}
Steve Narofffb367882007-08-20 21:31:48 +0000832
833/// objc-implementation:
834/// objc-class-implementation-prologue
835/// objc-category-implementation-prologue
836///
837/// objc-class-implementation-prologue:
838/// @implementation identifier objc-superclass[opt]
839/// objc-class-instance-variables[opt]
840///
841/// objc-category-implementation-prologue:
842/// @implementation identifier ( identifier )
843
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000844Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
845 SourceLocation atLoc) {
846 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
847 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
848 ConsumeToken(); // the "implementation" identifier
849
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000850 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000851 Diag(Tok, diag::err_expected_ident); // missing class or category name.
852 return 0;
853 }
854 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000855 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000856 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
857
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000858 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000859 // we have a category implementation.
860 SourceLocation lparenLoc = ConsumeParen();
861 SourceLocation categoryLoc, rparenLoc;
862 IdentifierInfo *categoryId = 0;
863
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000864 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000865 categoryId = Tok.getIdentifierInfo();
866 categoryLoc = ConsumeToken();
867 } else {
868 Diag(Tok, diag::err_expected_ident); // missing category name.
869 return 0;
870 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000871 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000872 Diag(Tok, diag::err_expected_rparen);
873 SkipUntil(tok::r_paren, false); // don't stop at ';'
874 return 0;
875 }
876 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +0000877 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +0000878 atLoc, nameId, nameLoc, categoryId,
879 categoryLoc);
880 return ImplCatType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000881 }
882 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000883 SourceLocation superClassLoc;
884 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000885 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000886 // We have a super class
887 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000888 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000889 Diag(Tok, diag::err_expected_ident); // missing super class name.
890 return 0;
891 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000892 superClassId = Tok.getIdentifierInfo();
893 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000894 }
Steve Naroff415c1832007-10-10 17:32:04 +0000895 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff25aace82007-10-03 21:00:46 +0000896 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +0000897 superClassId, superClassLoc);
898
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000899 if (Tok.is(tok::l_brace)) // we have ivars
900 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000901
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000902 return ImplClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000903}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000904
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000905Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
906 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
907 "ParseObjCAtEndDeclaration(): Expected @end");
908 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000909 if (ObjcImpDecl) {
910 // Checking is not necessary except that a parse error might have caused
911 // @implementation not to have been parsed to completion and ObjcImpDecl
912 // could be 0.
913 /// Insert collected methods declarations into the @interface object.
Steve Naroff25aace82007-10-03 21:00:46 +0000914 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000915 &AllImplMethods[0], AllImplMethods.size(),
916 atLoc);
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +0000917 ObjcImpDecl = 0;
918 AllImplMethods.clear();
919 }
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000920
Steve Narofffb367882007-08-20 21:31:48 +0000921 return 0;
922}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000923
924/// compatibility-alias-decl:
925/// @compatibility_alias alias-name class-name ';'
926///
927Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
928 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
929 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
930 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000931 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000932 Diag(Tok, diag::err_expected_ident);
933 return 0;
934 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000935 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
936 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000937 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +0000938 Diag(Tok, diag::err_expected_ident);
939 return 0;
940 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000941 IdentifierInfo *classId = Tok.getIdentifierInfo();
942 SourceLocation classLoc = ConsumeToken(); // consume class-name;
943 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +0000944 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +0000945 return 0;
946 }
947 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
948 aliasId, aliasLoc,
949 classId, classLoc);
950 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +0000951}
952
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000953/// property-synthesis:
954/// @synthesize property-ivar-list ';'
955///
956/// property-ivar-list:
957/// property-ivar
958/// property-ivar-list ',' property-ivar
959///
960/// property-ivar:
961/// identifier
962/// identifier '=' identifier
963///
964Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
965 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
966 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
967 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000968 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000969 Diag(Tok, diag::err_expected_ident);
970 return 0;
971 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000972 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000973 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000974 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000975 // property '=' ivar-name
976 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000977 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000978 Diag(Tok, diag::err_expected_ident);
979 break;
980 }
981 ConsumeToken(); // consume ivar-name
982 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000983 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000984 break;
985 ConsumeToken(); // consume ','
986 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000987 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000988 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
989 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000990}
991
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000992/// property-dynamic:
993/// @dynamic property-list
994///
995/// property-list:
996/// identifier
997/// property-list ',' identifier
998///
999Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1000 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1001 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1002 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001003 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001004 Diag(Tok, diag::err_expected_ident);
1005 return 0;
1006 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001007 while (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001008 ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001009 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001010 break;
1011 ConsumeToken(); // consume ','
1012 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001013 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001014 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1015 return 0;
1016}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001017
1018/// objc-throw-statement:
1019/// throw expression[opt];
1020///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001021Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001022 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001023 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001024 ExprResult Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001025 if (Res.isInvalid) {
1026 SkipUntil(tok::semi);
1027 return 0;
1028 }
1029 }
1030 return 0;
1031}
1032
1033/// objc-try-catch-statement:
1034/// @try compound-statement objc-catch-list[opt]
1035/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1036///
1037/// objc-catch-list:
1038/// @catch ( parameter-declaration ) compound-statement
1039/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1040/// catch-parameter-declaration:
1041/// parameter-declaration
1042/// '...' [OBJC2]
1043///
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001044Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001045 bool catch_or_finally_seen = false;
1046 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001047 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001048 Diag (Tok, diag::err_expected_lbrace);
1049 return 0;
1050 }
1051 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001052 while (Tok.is(tok::at)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001053 ConsumeToken();
1054 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1055 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001056 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001057 ConsumeParen();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001058 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001059 DeclSpec DS;
1060 ParseDeclarationSpecifiers(DS);
1061 // Parse the parameter-declaration.
1062 // FIXME: BlockContext may not be the right context!
1063 Declarator ParmDecl(DS, Declarator::BlockContext);
1064 ParseDeclarator(ParmDecl);
1065 }
1066 else
1067 ConsumeToken(); // consume '...'
1068 ConsumeParen();
1069 StmtResult CatchMody = ParseCompoundStatementBody();
1070 }
1071 else {
1072 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1073 return 0;
1074 }
1075 catch_or_finally_seen = true;
1076 }
1077 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1078 ConsumeToken(); // consume finally
1079 StmtResult FinallyBody = ParseCompoundStatementBody();
1080 catch_or_finally_seen = true;
1081 break;
1082 }
1083 }
1084 if (!catch_or_finally_seen)
1085 Diag(atLoc, diag::err_missing_catch_finally);
1086 return 0;
1087}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001088
Steve Naroff81f1bba2007-09-06 21:24:23 +00001089/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001090///
1091void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001092 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1093
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001094 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001095 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001096 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001097 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001098 ConsumeToken();
1099
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001100 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001101 Diag (Tok, diag::err_expected_lbrace);
1102 return;
1103 }
1104
1105 StmtResult FnBody = ParseCompoundStatementBody();
1106}
1107
Steve Naroff81f1bba2007-09-06 21:24:23 +00001108/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001109///
Steve Naroff72f17fb2007-08-22 22:17:26 +00001110void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001111 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +00001112 // FIXME: @optional/@protocol??
Fariborz Jahanian1e4e82f2007-09-27 18:57:03 +00001113 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001114 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001115 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001116 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001117 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001118 Diag (Tok, diag::err_expected_lbrace);
1119 return;
1120 }
1121
1122 StmtResult FnBody = ParseCompoundStatementBody();
Chris Lattner4b009652007-07-25 00:24:17 +00001123}
Anders Carlssona66cad42007-08-21 17:43:55 +00001124
Steve Narofffb9dd752007-10-15 20:55:58 +00001125Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001126
1127 switch (Tok.getKind()) {
1128 case tok::string_literal: // primary-expression: string-literal
1129 case tok::wide_string_literal:
Fariborz Jahanian37c9c612007-10-04 20:19:06 +00001130 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001131 default:
1132 break;
1133 }
1134
1135 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattnercfd61c82007-10-16 22:51:17 +00001136 case tok::objc_encode:
1137 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1138 case tok::objc_protocol:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001139 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001140 case tok::objc_selector:
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001141 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattnercfd61c82007-10-16 22:51:17 +00001142 default:
1143 Diag(AtLoc, diag::err_unexpected_at);
1144 SkipUntil(tok::semi);
1145 break;
Anders Carlssona66cad42007-08-21 17:43:55 +00001146 }
1147
1148 return 0;
1149}
1150
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001151/// objc-message-expr:
1152/// '[' objc-receiver objc-message-args ']'
1153///
1154/// objc-receiver:
1155/// expression
1156/// class-name
1157/// type-name
1158///
1159/// objc-message-args:
1160/// objc-selector
1161/// objc-keywordarg-list
1162///
1163/// objc-keywordarg-list:
1164/// objc-keywordarg
1165/// objc-keywordarg-list objc-keywordarg
1166///
1167/// objc-keywordarg:
1168/// selector-name[opt] ':' objc-keywordexpr
1169///
1170/// objc-keywordexpr:
1171/// nonempty-expr-list
1172///
1173/// nonempty-expr-list:
1174/// assignment-expression
1175/// nonempty-expr-list , assignment-expression
1176///
1177Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001178 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroffc39ca262007-09-18 23:55:05 +00001179 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff253118b2007-09-17 20:25:27 +00001180 IdentifierInfo *ReceiverName = 0;
1181 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001182 // Parse receiver
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001183 if (Tok.is(tok::identifier) &&
Steve Naroff253118b2007-09-17 20:25:27 +00001184 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1185 ReceiverName = Tok.getIdentifierInfo();
Steve Narofff0c31dd2007-09-16 16:16:00 +00001186 ConsumeToken();
Steve Naroff253118b2007-09-17 20:25:27 +00001187 } else {
1188 ExprResult Res = ParseAssignmentExpression();
1189 if (Res.isInvalid) {
1190 SkipUntil(tok::identifier);
1191 return Res;
1192 }
1193 ReceiverExpr = Res.Val;
1194 }
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001195 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001196 SourceLocation Loc;
1197 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001198
1199 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1200 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1201
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001202 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001203 while (1) {
1204 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001205 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001206
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001207 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001208 Diag(Tok, diag::err_expected_colon);
1209 SkipUntil(tok::semi);
Steve Naroff253118b2007-09-17 20:25:27 +00001210 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001211 }
Steve Naroff4ed9d662007-09-27 14:38:14 +00001212 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001213 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001214 ExprResult Res = ParseAssignmentExpression();
1215 if (Res.isInvalid) {
1216 SkipUntil(tok::identifier);
1217 return Res;
1218 }
1219 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001220 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001221
1222 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001223 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001224 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001225 break;
1226 // We have a selector or a colon, continue parsing.
1227 }
1228 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001229 while (Tok.is(tok::comma)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001230 ConsumeToken();
1231 /// Parse the expression after ','
1232 ParseAssignmentExpression();
1233 }
1234 } else if (!selIdent) {
1235 Diag(Tok, diag::err_expected_ident); // missing selector name.
1236 SkipUntil(tok::semi);
1237 return 0;
1238 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001239
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001240 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001241 Diag(Tok, diag::err_expected_rsquare);
1242 SkipUntil(tok::semi);
1243 return 0;
1244 }
Steve Naroffc39ca262007-09-18 23:55:05 +00001245 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001246
Steve Narofff9e80db2007-10-05 18:42:47 +00001247 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001248 if (nKeys == 0)
1249 KeyIdents.push_back(selIdent);
1250 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1251
1252 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001253 if (ReceiverName)
Chris Lattnerd031a452007-10-07 02:00:24 +00001254 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1255 &KeyExprs[0]);
1256 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1257 &KeyExprs[0]);
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001258}
1259
Anders Carlssona66cad42007-08-21 17:43:55 +00001260Parser::ExprResult Parser::ParseObjCStringLiteral() {
1261 ExprResult Res = ParseStringLiteralExpression();
1262
1263 if (Res.isInvalid) return Res;
1264
1265 return Actions.ParseObjCStringLiteral(Res.Val);
1266}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001267
1268/// objc-encode-expression:
1269/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001270Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001271 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001272
1273 SourceLocation EncLoc = ConsumeToken();
1274
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001275 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson8be1d402007-08-22 15:14:15 +00001276 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1277 return true;
1278 }
1279
1280 SourceLocation LParenLoc = ConsumeParen();
1281
1282 TypeTy *Ty = ParseTypeName();
1283
Anders Carlsson92faeb82007-08-23 15:31:37 +00001284 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001285
Chris Lattnercfd61c82007-10-16 22:51:17 +00001286 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001287 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001288}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001289
1290/// objc-protocol-expression
1291/// @protocol ( protocol-name )
1292
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001293Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001294{
1295 SourceLocation ProtoLoc = ConsumeToken();
1296
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001297 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001298 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1299 return true;
1300 }
1301
1302 SourceLocation LParenLoc = ConsumeParen();
1303
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001304 if (Tok.isNot(tok::identifier)) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001305 Diag(Tok, diag::err_expected_ident);
1306 return true;
1307 }
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001308 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001309 ConsumeToken();
1310
Anders Carlsson92faeb82007-08-23 15:31:37 +00001311 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001312
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001313 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1314 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001315}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001316
1317/// objc-selector-expression
1318/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001319Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001320{
1321 SourceLocation SelectorLoc = ConsumeToken();
1322
1323 if (Tok.isNot(tok::l_paren)) {
1324 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1325 return 0;
1326 }
1327
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001328 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001329 SourceLocation LParenLoc = ConsumeParen();
1330 SourceLocation sLoc;
1331 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1332 if (!SelIdent && Tok.isNot(tok::colon)) {
1333
1334 Diag(Tok, diag::err_expected_ident); // missing selector name.
1335 return 0;
1336 }
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001337 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001338 if (Tok.isNot(tok::r_paren))
1339 while (1) {
1340 if (Tok.isNot(tok::colon)) {
1341 Diag(Tok, diag::err_expected_colon);
1342 break;
1343 }
1344 ConsumeToken(); // Eat the ':'.
1345 if (Tok.is(tok::r_paren))
1346 break;
1347 // Check for another keyword selector.
1348 SourceLocation Loc;
1349 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001350 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001351 if (!SelIdent && Tok.isNot(tok::colon))
1352 break;
1353 }
1354 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001355 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1356 &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001357 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001358 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001359 }