blob: ea48704c01642e2459cc31c6701ab48c6d6af415 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Reid Spencer5f016e22007-07-11 17:01:13 +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 Naroff91fa0b72007-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 Naroffdac269b2007-08-20 21:31:48 +000029Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000030 SourceLocation AtLoc = ConsumeToken(); // the "@"
31
Steve Naroff861cf3e2007-08-23 18:16:40 +000032 switch (Tok.getObjCKeywordID()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000033 case tok::objc_class:
34 return ParseObjCAtClassDeclaration(AtLoc);
35 case tok::objc_interface:
Steve Naroffdac269b2007-08-20 21:31:48 +000036 return ParseObjCAtInterfaceDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000037 case tok::objc_protocol:
Steve Naroff7ef58fd2007-08-22 22:17:26 +000038 return ParseObjCAtProtocolDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000039 case tok::objc_implementation:
Steve Naroff3536b442007-09-06 21:24:23 +000040 return ObjcImpDecl = ParseObjCAtImplementationDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000041 case tok::objc_end:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000042 return ParseObjCAtEndDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000043 case tok::objc_compatibility_alias:
Fariborz Jahaniane992af02007-09-04 19:26:51 +000044 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000045 case tok::objc_synthesize:
46 return ParseObjCPropertySynthesize(AtLoc);
47 case tok::objc_dynamic:
48 return ParseObjCPropertyDynamic(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000049 default:
50 Diag(AtLoc, diag::err_unexpected_at);
51 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000052 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000053 }
54}
55
56///
57/// objc-class-declaration:
58/// '@' 'class' identifier-list ';'
59///
Steve Naroffdac269b2007-08-20 21:31:48 +000060Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000061 ConsumeToken(); // the identifier "class"
62 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
63
64 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000065 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000066 Diag(Tok, diag::err_expected_ident);
67 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000068 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000069 }
Reid Spencer5f016e22007-07-11 17:01:13 +000070 ClassNames.push_back(Tok.getIdentifierInfo());
71 ConsumeToken();
72
Chris Lattnerdf195262007-10-09 17:51:17 +000073 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000074 break;
75
76 ConsumeToken();
77 }
78
79 // Consume the ';'.
80 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Naroffdac269b2007-08-20 21:31:48 +000081 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000082
Steve Naroffe440eb82007-10-10 17:32:04 +000083 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000084 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
Steve Naroffdac269b2007-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 Naroff861cf3e2007-08-23 18:16:40 +0000117 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000118 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
119 ConsumeToken(); // the "interface" identifier
120
Chris Lattnerdf195262007-10-09 17:51:17 +0000121 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-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 Naroff7ef58fd2007-08-22 22:17:26 +0000126 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000127 SourceLocation nameLoc = ConsumeToken();
128
Chris Lattnerdf195262007-10-09 17:51:17 +0000129 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000130 SourceLocation lparenLoc = ConsumeParen();
131 SourceLocation categoryLoc, rparenLoc;
132 IdentifierInfo *categoryId = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000133 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffdac269b2007-08-20 21:31:48 +0000134
Steve Naroff527fe232007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-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 Narofff908a872007-10-30 02:23:23 +0000149 SourceLocation endProtoLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattnerdf195262007-10-09 17:51:17 +0000151 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000152 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroffdac269b2007-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 Naroffe440eb82007-10-10 17:32:04 +0000158 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000159 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000160 &ProtocolRefs[0], ProtocolRefs.size(),
161 endProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000162
163 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Naroffdac269b2007-08-20 21:31:48 +0000164
Steve Naroff294494e2007-08-22 16:35:03 +0000165 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000166 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000167 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000168 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000169 }
Steve Naroff294494e2007-08-22 16:35:03 +0000170 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000171 return 0;
172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000176
Chris Lattnerdf195262007-10-09 17:51:17 +0000177 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-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 Narofff28b2642007-09-05 23:30:30 +0000187 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000188 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000189 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000190 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroffdac269b2007-08-20 21:31:48 +0000191 return 0;
192 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000193 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000194 atLoc, nameId, nameLoc,
Steve Narofff28b2642007-09-05 23:30:30 +0000195 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000196 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000197
Chris Lattnerdf195262007-10-09 17:51:17 +0000198 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000199 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000200
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000201 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff294494e2007-08-22 16:35:03 +0000202
203 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000204 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000205 ConsumeToken(); // the "end" identifier
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000206 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000207 }
Steve Naroff294494e2007-08-22 16:35:03 +0000208 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000209 return 0;
210}
211
212/// objc-interface-decl-list:
213/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000214/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000215/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000216/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000217/// objc-interface-decl-list declaration
218/// objc-interface-decl-list ';'
219///
Steve Naroff294494e2007-08-22 16:35:03 +0000220/// objc-method-requirement: [OBJC2]
221/// @required
222/// @optional
223///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000224void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
225 tok::ObjCKeywordKind contextKey) {
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000226 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000227 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000228 SourceLocation AtEndLoc;
229
Steve Naroff294494e2007-08-22 16:35:03 +0000230 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000231 if (Tok.is(tok::at)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000232 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff861cf3e2007-08-23 18:16:40 +0000233 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff294494e2007-08-22 16:35:03 +0000234
235 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff60fccee2007-10-29 21:38:07 +0000236 AtEndLoc = AtLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000237 break;
Steve Naroff294494e2007-08-22 16:35:03 +0000238 } else if (ocKind == tok::objc_required) { // protocols only
239 ConsumeToken();
Fariborz Jahanian00933592007-09-18 00:25:23 +0000240 MethodImplKind = ocKind;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000241 if (contextKey != tok::objc_protocol)
242 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff294494e2007-08-22 16:35:03 +0000243 } else if (ocKind == tok::objc_optional) { // protocols only
244 ConsumeToken();
Fariborz Jahanian00933592007-09-18 00:25:23 +0000245 MethodImplKind = ocKind;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000246 if (contextKey != tok::objc_protocol)
247 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff294494e2007-08-22 16:35:03 +0000248 } else if (ocKind == tok::objc_property) {
Fariborz Jahaniane55cd002007-09-12 18:23:47 +0000249 ParseObjCPropertyDecl(interfaceDecl);
Steve Naroff294494e2007-08-22 16:35:03 +0000250 continue;
251 } else {
252 Diag(Tok, diag::err_objc_illegal_interface_qual);
253 ConsumeToken();
254 }
255 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000256 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
257 DeclTy *methodPrototype =
258 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000259 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000260 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
261 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000262 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000263 continue;
264 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000265 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000266 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000267 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000268 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000269 else {
Steve Naroff4985ace2007-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 Naroff3536b442007-09-06 21:24:23 +0000272 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000273 }
Steve Naroff294494e2007-08-22 16:35:03 +0000274 }
Steve Naroff2feac5e2007-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 Naroff294494e2007-08-22 16:35:03 +0000280}
281
Fariborz Jahaniand0f97d12007-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 Lattnerdf195262007-10-09 17:51:17 +0000307 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000308 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000309 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000310 if (II == ObjcPropertyAttrs[objc_setter]) {
311 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000312 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-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 Lattnerdf195262007-10-09 17:51:17 +0000332 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000333 loc = ConsumeToken();
334 continue;
335 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000336 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000337 break;
338 Diag(loc, diag::err_expected_rparen);
339 SkipUntil(tok::semi);
340 return;
341 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000342 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-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 Lattnerdf195262007-10-09 17:51:17 +0000359 if (Tok.is(tok::l_paren)) {
Fariborz Jahaniand0f97d12007-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 Lattnerdf195262007-10-09 17:51:17 +0000366 if (Tok.is(tok::semi))
Fariborz Jahaniand0f97d12007-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 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000372}
Steve Naroffdac269b2007-08-20 21:31:48 +0000373
Steve Naroff3536b442007-09-06 21:24:23 +0000374/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000375/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000376/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000377///
378/// objc-instance-method: '-'
379/// objc-class-method: '+'
380///
Steve Naroff4985ace2007-08-22 18:35:33 +0000381/// objc-method-attributes: [OBJC2]
382/// __attribute__((deprecated))
383///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000384Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
385 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000386 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000387
388 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000389 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000390
Steve Naroffbef11852007-10-26 20:53:56 +0000391 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000392 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000393 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000394 return MDecl;
Steve Naroff294494e2007-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 Jahanian4b6c9052007-10-11 00:55:41 +0000405IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-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 Jahanian4b6c9052007-10-11 00:55:41 +0000449 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000450 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000451 }
Steve Naroff294494e2007-08-22 16:35:03 +0000452}
453
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000454/// objc-type-qualifier: one of
455/// in out inout bycopy byref oneway
456///
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000457bool Parser::isObjCTypeQualifier() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000458 if (Tok.is(tok::identifier)) {
Chris Lattner34870da2007-08-29 22:54:08 +0000459 const IdentifierInfo *II = Tok.getIdentifierInfo();
460 for (unsigned i = 0; i < objc_NumQuals; ++i)
461 if (II == ObjcTypeQuals[i]) return true;
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000462 }
463 return false;
464}
465
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000466/// property-attrlist: one of
467/// readonly getter setter assign retain copy nonatomic
468///
469bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000470 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000471 const IdentifierInfo *II = Tok.getIdentifierInfo();
472 for (unsigned i = 0; i < objc_NumAttrs; ++i)
473 if (II == ObjcPropertyAttrs[i]) return true;
474 }
475 return false;
476}
477
Steve Naroff294494e2007-08-22 16:35:03 +0000478/// objc-type-name:
479/// '(' objc-type-qualifiers[opt] type-name ')'
480/// '(' objc-type-qualifiers[opt] ')'
481///
482/// objc-type-qualifiers:
483/// objc-type-qualifier
484/// objc-type-qualifiers objc-type-qualifier
485///
Steve Narofff28b2642007-09-05 23:30:30 +0000486Parser::TypeTy *Parser::ParseObjCTypeName() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000487 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000488
489 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000490 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000491
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000492 while (isObjCTypeQualifier())
493 ConsumeToken();
494
Steve Naroff294494e2007-08-22 16:35:03 +0000495 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000496 Ty = ParseTypeName();
497 // FIXME: back when Sema support is in place...
498 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000499 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000500 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000501 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000502 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000503 }
504 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000505 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000506}
507
508/// objc-method-decl:
509/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000510/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000511/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000512/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000513///
514/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000515/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000516/// objc-keyword-selector objc-keyword-decl
517///
518/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000519/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
520/// objc-selector ':' objc-keyword-attributes[opt] identifier
521/// ':' objc-type-name objc-keyword-attributes[opt] identifier
522/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000523///
Steve Naroff4985ace2007-08-22 18:35:33 +0000524/// objc-parmlist:
525/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000526///
Steve Naroff4985ace2007-08-22 18:35:33 +0000527/// objc-parms:
528/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000529///
Steve Naroff4985ace2007-08-22 18:35:33 +0000530/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000531/// , ...
532///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000533/// objc-keyword-attributes: [OBJC2]
534/// __attribute__((unused))
535///
Steve Naroffbef11852007-10-26 20:53:56 +0000536Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
537 tok::TokenKind mType,
Steve Naroff68d331a2007-09-27 14:38:14 +0000538 tok::ObjCKeywordKind MethodImplKind)
539{
Steve Naroff4985ace2007-08-22 18:35:33 +0000540 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000541 TypeTy *ReturnType = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000542 if (Tok.is(tok::l_paren))
Steve Narofff28b2642007-09-05 23:30:30 +0000543 ReturnType = ParseObjCTypeName();
Steve Naroffbef11852007-10-26 20:53:56 +0000544 SourceLocation selLoc;
545 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000546 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000547 if (!SelIdent) {
548 Diag(Tok, diag::err_expected_ident); // missing selector name.
549 // FIXME: this creates a unary selector with a null identifier, is this
550 // ok?? Maybe we should skip to the next semicolon or something.
551 }
552
553 // If attributes exist after the method, parse them.
554 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000555 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000556 MethodAttrs = ParseAttributes();
557
558 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000559 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
560 mType, ReturnType, Sel,
Chris Lattnerff384912007-10-07 02:00:24 +0000561 0, 0, MethodAttrs, MethodImplKind);
562 }
Steve Narofff28b2642007-09-05 23:30:30 +0000563
Steve Naroff68d331a2007-09-27 14:38:14 +0000564 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
565 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
566 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000567
568 Action::TypeTy *TypeInfo;
569 while (1) {
570 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000571
Chris Lattnerff384912007-10-07 02:00:24 +0000572 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000573 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000574 Diag(Tok, diag::err_expected_colon);
575 break;
576 }
577 ConsumeToken(); // Eat the ':'.
Chris Lattnerdf195262007-10-09 17:51:17 +0000578 if (Tok.is(tok::l_paren)) // Parse the argument type.
Chris Lattnerff384912007-10-07 02:00:24 +0000579 TypeInfo = ParseObjCTypeName();
580 else
581 TypeInfo = 0;
582 KeyTypes.push_back(TypeInfo);
Steve Narofff28b2642007-09-05 23:30:30 +0000583
Chris Lattnerff384912007-10-07 02:00:24 +0000584 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000585 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000586 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000587
Chris Lattnerdf195262007-10-09 17:51:17 +0000588 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000589 Diag(Tok, diag::err_expected_ident); // missing argument name.
590 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000591 }
Chris Lattnerff384912007-10-07 02:00:24 +0000592 ArgNames.push_back(Tok.getIdentifierInfo());
593 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000594
Chris Lattnerff384912007-10-07 02:00:24 +0000595 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000596 SourceLocation Loc;
597 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000598 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000599 break;
600 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000601 }
Chris Lattnerff384912007-10-07 02:00:24 +0000602
603 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000604 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000605 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000606 if (Tok.is(tok::ellipsis)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000607 ConsumeToken();
608 break;
609 }
610 // Parse the c-style argument declaration-specifier.
611 DeclSpec DS;
612 ParseDeclarationSpecifiers(DS);
613 // Parse the declarator.
614 Declarator ParmDecl(DS, Declarator::PrototypeContext);
615 ParseDeclarator(ParmDecl);
616 }
617
618 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000619 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000620 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000621 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000622 MethodAttrs = ParseAttributes();
623
624 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
625 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000626 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
627 mType, ReturnType, Sel,
Chris Lattnerff384912007-10-07 02:00:24 +0000628 &KeyTypes[0], &ArgNames[0],
629 MethodAttrs, MethodImplKind);
Steve Naroff294494e2007-08-22 16:35:03 +0000630}
631
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000632/// CmpProtocolVals - Comparison predicate for sorting protocols.
633static bool CmpProtocolVals(const IdentifierInfo* const& lhs,
634 const IdentifierInfo* const& rhs) {
635 return strcmp(lhs->getName(), rhs->getName()) < 0;
636}
637
Steve Naroffdac269b2007-08-20 21:31:48 +0000638/// objc-protocol-refs:
639/// '<' identifier-list '>'
640///
Steve Narofff28b2642007-09-05 23:30:30 +0000641bool Parser::ParseObjCProtocolReferences(
Steve Narofff908a872007-10-30 02:23:23 +0000642 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc)
643{
Chris Lattnerdf195262007-10-09 17:51:17 +0000644 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000645
646 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000647
648 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000649 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000650 Diag(Tok, diag::err_expected_ident);
651 SkipUntil(tok::greater);
652 return true;
653 }
654 ProtocolRefs.push_back(Tok.getIdentifierInfo());
655 ConsumeToken();
656
Chris Lattnerdf195262007-10-09 17:51:17 +0000657 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000658 break;
659 ConsumeToken();
660 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000661
662 // Sort protocols, keyed by name.
663 // Later on, we remove duplicates.
664 std::stable_sort(ProtocolRefs.begin(), ProtocolRefs.end(), CmpProtocolVals);
665
666 // Make protocol names unique.
667 ProtocolRefs.erase(std::unique(ProtocolRefs.begin(), ProtocolRefs.end()),
668 ProtocolRefs.end());
Steve Naroffdac269b2007-08-20 21:31:48 +0000669 // Consume the '>'.
Steve Narofff908a872007-10-30 02:23:23 +0000670 if (Tok.is(tok::greater)) {
671 endLoc = ConsumeAnyToken();
672 return false;
673 }
674 Diag(Tok, diag::err_expected_greater);
675 return true;
Steve Naroffdac269b2007-08-20 21:31:48 +0000676}
677
678/// objc-class-instance-variables:
679/// '{' objc-instance-variable-decl-list[opt] '}'
680///
681/// objc-instance-variable-decl-list:
682/// objc-visibility-spec
683/// objc-instance-variable-decl ';'
684/// ';'
685/// objc-instance-variable-decl-list objc-visibility-spec
686/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
687/// objc-instance-variable-decl-list ';'
688///
689/// objc-visibility-spec:
690/// @private
691/// @protected
692/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000693/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000694///
695/// objc-instance-variable-decl:
696/// struct-declaration
697///
Steve Naroff60fccee2007-10-29 21:38:07 +0000698void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
699 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000700 assert(Tok.is(tok::l_brace) && "expected {");
Steve Naroff3536b442007-09-06 21:24:23 +0000701 llvm::SmallVector<DeclTy*, 16> IvarDecls;
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000702 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
703 llvm::SmallVector<tok::ObjCKeywordKind, 32> AllVisibilities;
Steve Naroffddbff782007-08-21 21:17:12 +0000704
705 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000706
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000707 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffddbff782007-08-21 21:17:12 +0000708 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000709 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000710 // Each iteration of this loop reads one objc-instance-variable-decl.
711
712 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000713 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000714 Diag(Tok, diag::ext_extra_struct_semi);
715 ConsumeToken();
716 continue;
717 }
718 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000719 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000720 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000721 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000722 case tok::objc_private:
723 case tok::objc_public:
724 case tok::objc_protected:
725 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000726 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000727 ConsumeToken();
728 continue;
729 default:
730 Diag(Tok, diag::err_objc_illegal_visibility_spec);
731 ConsumeToken();
732 continue;
733 }
734 }
735 ParseStructDeclaration(interfaceDecl, IvarDecls);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000736 for (unsigned i = 0; i < IvarDecls.size(); i++) {
737 AllIvarDecls.push_back(IvarDecls[i]);
738 AllVisibilities.push_back(visibility);
739 }
Steve Naroff3536b442007-09-06 21:24:23 +0000740 IvarDecls.clear();
741
Chris Lattnerdf195262007-10-09 17:51:17 +0000742 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000743 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000744 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000745 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
746 break;
747 } else {
748 Diag(Tok, diag::err_expected_semi_decl_list);
749 // Skip to end of block or statement
750 SkipUntil(tok::r_brace, true, true);
751 }
752 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000753 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000754 if (AllIvarDecls.size()) { // Check for {} - no ivars in braces
Steve Naroff60fccee2007-10-29 21:38:07 +0000755 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Steve Naroff08d92e42007-09-15 18:49:24 +0000756 &AllIvarDecls[0], AllIvarDecls.size(),
Steve Naroff60fccee2007-10-29 21:38:07 +0000757 LBraceLoc, RBraceLoc, &AllVisibilities[0]);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000758 }
Steve Naroffddbff782007-08-21 21:17:12 +0000759 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000760}
Steve Naroffdac269b2007-08-20 21:31:48 +0000761
762/// objc-protocol-declaration:
763/// objc-protocol-definition
764/// objc-protocol-forward-reference
765///
766/// objc-protocol-definition:
767/// @protocol identifier
768/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000769/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000770/// @end
771///
772/// objc-protocol-forward-reference:
773/// @protocol identifier-list ';'
774///
775/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000776/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000777/// semicolon in the first alternative if objc-protocol-refs are omitted.
778
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000779Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000780 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000781 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
782 ConsumeToken(); // the "protocol" identifier
783
Chris Lattnerdf195262007-10-09 17:51:17 +0000784 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000785 Diag(Tok, diag::err_expected_ident); // missing protocol name.
786 return 0;
787 }
788 // Save the protocol name, then consume it.
789 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
790 SourceLocation nameLoc = ConsumeToken();
791
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000792 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000793 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000794 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000795 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000796 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000797 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000798 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000799 ProtocolRefs.push_back(protocolName);
800
801 while (1) {
802 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000803 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000804 Diag(Tok, diag::err_expected_ident);
805 SkipUntil(tok::semi);
806 return 0;
807 }
808 ProtocolRefs.push_back(Tok.getIdentifierInfo());
809 ConsumeToken(); // the identifier
810
Chris Lattnerdf195262007-10-09 17:51:17 +0000811 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000812 break;
813 }
814 // Consume the ';'.
815 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
816 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000817 }
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000818 if (ProtocolRefs.size() > 0)
Steve Naroffe440eb82007-10-10 17:32:04 +0000819 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000820 &ProtocolRefs[0],
821 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000822 // Last, and definitely not least, parse a protocol declaration.
Steve Narofff908a872007-10-30 02:23:23 +0000823 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000824 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000825 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000826 return 0;
827 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000828
Steve Naroffe440eb82007-10-10 17:32:04 +0000829 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000830 protocolName, nameLoc,
831 &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000832 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000833 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000834
835 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000836 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000837 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000838 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000839 }
840 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000841 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000842}
Steve Naroffdac269b2007-08-20 21:31:48 +0000843
844/// objc-implementation:
845/// objc-class-implementation-prologue
846/// objc-category-implementation-prologue
847///
848/// objc-class-implementation-prologue:
849/// @implementation identifier objc-superclass[opt]
850/// objc-class-instance-variables[opt]
851///
852/// objc-category-implementation-prologue:
853/// @implementation identifier ( identifier )
854
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000855Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
856 SourceLocation atLoc) {
857 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
858 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
859 ConsumeToken(); // the "implementation" identifier
860
Chris Lattnerdf195262007-10-09 17:51:17 +0000861 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000862 Diag(Tok, diag::err_expected_ident); // missing class or category name.
863 return 0;
864 }
865 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000866 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000867 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
868
Chris Lattnerdf195262007-10-09 17:51:17 +0000869 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000870 // we have a category implementation.
871 SourceLocation lparenLoc = ConsumeParen();
872 SourceLocation categoryLoc, rparenLoc;
873 IdentifierInfo *categoryId = 0;
874
Chris Lattnerdf195262007-10-09 17:51:17 +0000875 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000876 categoryId = Tok.getIdentifierInfo();
877 categoryLoc = ConsumeToken();
878 } else {
879 Diag(Tok, diag::err_expected_ident); // missing category name.
880 return 0;
881 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000882 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000883 Diag(Tok, diag::err_expected_rparen);
884 SkipUntil(tok::r_paren, false); // don't stop at ';'
885 return 0;
886 }
887 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000888 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000889 atLoc, nameId, nameLoc, categoryId,
890 categoryLoc);
891 return ImplCatType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000892 }
893 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000894 SourceLocation superClassLoc;
895 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000896 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000897 // We have a super class
898 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000899 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000900 Diag(Tok, diag::err_expected_ident); // missing super class name.
901 return 0;
902 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000903 superClassId = Tok.getIdentifierInfo();
904 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000905 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000906 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Steve Naroff3a165b02007-10-03 21:00:46 +0000907 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000908 superClassId, superClassLoc);
909
Steve Naroff60fccee2007-10-29 21:38:07 +0000910 if (Tok.is(tok::l_brace)) // we have ivars
911 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000912
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000913 return ImplClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000914}
Steve Naroff60fccee2007-10-29 21:38:07 +0000915
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000916Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
917 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
918 "ParseObjCAtEndDeclaration(): Expected @end");
919 ConsumeToken(); // the "end" identifier
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000920 if (ObjcImpDecl) {
921 // Checking is not necessary except that a parse error might have caused
922 // @implementation not to have been parsed to completion and ObjcImpDecl
923 // could be 0.
924 /// Insert collected methods declarations into the @interface object.
Steve Naroff3a165b02007-10-03 21:00:46 +0000925 Actions.ActOnAddMethodsToObjcDecl(CurScope, ObjcImpDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000926 &AllImplMethods[0], AllImplMethods.size(),
927 atLoc);
Fariborz Jahaniand0b01542007-09-27 18:57:03 +0000928 ObjcImpDecl = 0;
929 AllImplMethods.clear();
930 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000931
Steve Naroffdac269b2007-08-20 21:31:48 +0000932 return 0;
933}
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000934
935/// compatibility-alias-decl:
936/// @compatibility_alias alias-name class-name ';'
937///
938Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
939 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
940 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
941 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +0000942 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000943 Diag(Tok, diag::err_expected_ident);
944 return 0;
945 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000946 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
947 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +0000948 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000949 Diag(Tok, diag::err_expected_ident);
950 return 0;
951 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000952 IdentifierInfo *classId = Tok.getIdentifierInfo();
953 SourceLocation classLoc = ConsumeToken(); // consume class-name;
954 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +0000955 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +0000956 return 0;
957 }
958 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
959 aliasId, aliasLoc,
960 classId, classLoc);
961 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +0000962}
963
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000964/// property-synthesis:
965/// @synthesize property-ivar-list ';'
966///
967/// property-ivar-list:
968/// property-ivar
969/// property-ivar-list ',' property-ivar
970///
971/// property-ivar:
972/// identifier
973/// identifier '=' identifier
974///
975Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
976 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
977 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
978 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +0000979 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000980 Diag(Tok, diag::err_expected_ident);
981 return 0;
982 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000983 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000984 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +0000985 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000986 // property '=' ivar-name
987 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +0000988 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000989 Diag(Tok, diag::err_expected_ident);
990 break;
991 }
992 ConsumeToken(); // consume ivar-name
993 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000994 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000995 break;
996 ConsumeToken(); // consume ','
997 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000998 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000999 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1000 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001001}
1002
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001003/// property-dynamic:
1004/// @dynamic property-list
1005///
1006/// property-list:
1007/// identifier
1008/// property-list ',' identifier
1009///
1010Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1011 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1012 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1013 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001014 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001015 Diag(Tok, diag::err_expected_ident);
1016 return 0;
1017 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001018 while (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001019 ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001020 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001021 break;
1022 ConsumeToken(); // consume ','
1023 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001024 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001025 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1026 return 0;
1027}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001028
1029/// objc-throw-statement:
1030/// throw expression[opt];
1031///
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001032Parser::DeclTy *Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001033 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001034 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001035 ExprResult Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001036 if (Res.isInvalid) {
1037 SkipUntil(tok::semi);
1038 return 0;
1039 }
1040 }
1041 return 0;
1042}
1043
1044/// objc-try-catch-statement:
1045/// @try compound-statement objc-catch-list[opt]
1046/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1047///
1048/// objc-catch-list:
1049/// @catch ( parameter-declaration ) compound-statement
1050/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1051/// catch-parameter-declaration:
1052/// parameter-declaration
1053/// '...' [OBJC2]
1054///
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001055Parser::DeclTy *Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001056 bool catch_or_finally_seen = false;
1057 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001058 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001059 Diag (Tok, diag::err_expected_lbrace);
1060 return 0;
1061 }
1062 StmtResult TryBody = ParseCompoundStatementBody();
Chris Lattnerdf195262007-10-09 17:51:17 +00001063 while (Tok.is(tok::at)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001064 ConsumeToken();
1065 if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_catch) {
1066 SourceLocation catchLoc = ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001067 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001068 ConsumeParen();
Chris Lattnerdf195262007-10-09 17:51:17 +00001069 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001070 DeclSpec DS;
1071 ParseDeclarationSpecifiers(DS);
1072 // Parse the parameter-declaration.
1073 // FIXME: BlockContext may not be the right context!
1074 Declarator ParmDecl(DS, Declarator::BlockContext);
1075 ParseDeclarator(ParmDecl);
1076 }
1077 else
1078 ConsumeToken(); // consume '...'
1079 ConsumeParen();
1080 StmtResult CatchMody = ParseCompoundStatementBody();
1081 }
1082 else {
1083 Diag(catchLoc, diag::err_expected_lparen_after, "@catch clause");
1084 return 0;
1085 }
1086 catch_or_finally_seen = true;
1087 }
1088 else if (Tok.getIdentifierInfo()->getObjCKeywordID() == tok::objc_finally) {
1089 ConsumeToken(); // consume finally
1090 StmtResult FinallyBody = ParseCompoundStatementBody();
1091 catch_or_finally_seen = true;
1092 break;
1093 }
1094 }
1095 if (!catch_or_finally_seen)
1096 Diag(atLoc, diag::err_missing_catch_finally);
1097 return 0;
1098}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001099
Steve Naroff3536b442007-09-06 21:24:23 +00001100/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001101///
1102void Parser::ParseObjCInstanceMethodDefinition() {
Chris Lattnerdf195262007-10-09 17:51:17 +00001103 assert(Tok.is(tok::minus) && "Method definitions should start with '-'");
1104
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001105 // FIXME: @optional/@protocol??
Fariborz Jahaniand0b01542007-09-27 18:57:03 +00001106 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001107 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001108 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001109 ConsumeToken();
1110
Chris Lattnerdf195262007-10-09 17:51:17 +00001111 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001112 Diag (Tok, diag::err_expected_lbrace);
1113 return;
1114 }
1115
1116 StmtResult FnBody = ParseCompoundStatementBody();
1117}
1118
Steve Naroff3536b442007-09-06 21:24:23 +00001119/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001120///
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001121void Parser::ParseObjCClassMethodDefinition() {
Chris Lattnerdf195262007-10-09 17:51:17 +00001122 assert(Tok.is(tok::plus) && "Class method definitions should start with '+'");
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001123 // FIXME: @optional/@protocol??
Fariborz Jahaniand0b01542007-09-27 18:57:03 +00001124 AllImplMethods.push_back(ParseObjCMethodPrototype(ObjcImpDecl));
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001125 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001126 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001127 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001128 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001129 Diag (Tok, diag::err_expected_lbrace);
1130 return;
1131 }
1132
1133 StmtResult FnBody = ParseCompoundStatementBody();
Reid Spencer5f016e22007-07-11 17:01:13 +00001134}
Anders Carlsson55085182007-08-21 17:43:55 +00001135
Steve Naroffa642beb2007-10-15 20:55:58 +00001136Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001137
1138 switch (Tok.getKind()) {
1139 case tok::string_literal: // primary-expression: string-literal
1140 case tok::wide_string_literal:
Fariborz Jahanianb384d322007-10-04 20:19:06 +00001141 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral());
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001142 default:
1143 break;
1144 }
1145
1146 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattner674af952007-10-16 22:51:17 +00001147 case tok::objc_encode:
1148 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1149 case tok::objc_protocol:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001150 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001151 case tok::objc_selector:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001152 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001153 default:
1154 Diag(AtLoc, diag::err_unexpected_at);
1155 SkipUntil(tok::semi);
1156 break;
Anders Carlsson55085182007-08-21 17:43:55 +00001157 }
1158
1159 return 0;
1160}
1161
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001162/// objc-message-expr:
1163/// '[' objc-receiver objc-message-args ']'
1164///
1165/// objc-receiver:
1166/// expression
1167/// class-name
1168/// type-name
1169///
1170/// objc-message-args:
1171/// objc-selector
1172/// objc-keywordarg-list
1173///
1174/// objc-keywordarg-list:
1175/// objc-keywordarg
1176/// objc-keywordarg-list objc-keywordarg
1177///
1178/// objc-keywordarg:
1179/// selector-name[opt] ':' objc-keywordexpr
1180///
1181/// objc-keywordexpr:
1182/// nonempty-expr-list
1183///
1184/// nonempty-expr-list:
1185/// assignment-expression
1186/// nonempty-expr-list , assignment-expression
1187///
1188Parser::ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattnerdf195262007-10-09 17:51:17 +00001189 assert(Tok.is(tok::l_square) && "'[' expected");
Steve Naroff563477d2007-09-18 23:55:05 +00001190 SourceLocation LBracloc = ConsumeBracket(); // consume '['
Steve Naroff37387c92007-09-17 20:25:27 +00001191 IdentifierInfo *ReceiverName = 0;
1192 ExprTy *ReceiverExpr = 0;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001193 // Parse receiver
Chris Lattnerdf195262007-10-09 17:51:17 +00001194 if (Tok.is(tok::identifier) &&
Steve Naroff37387c92007-09-17 20:25:27 +00001195 Actions.isTypeName(*Tok.getIdentifierInfo(), CurScope)) {
1196 ReceiverName = Tok.getIdentifierInfo();
Steve Naroff8c9f13e2007-09-16 16:16:00 +00001197 ConsumeToken();
Steve Naroff37387c92007-09-17 20:25:27 +00001198 } else {
1199 ExprResult Res = ParseAssignmentExpression();
1200 if (Res.isInvalid) {
1201 SkipUntil(tok::identifier);
1202 return Res;
1203 }
1204 ReceiverExpr = Res.Val;
1205 }
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001206 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001207 SourceLocation Loc;
1208 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001209
1210 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1211 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1212
Chris Lattnerdf195262007-10-09 17:51:17 +00001213 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001214 while (1) {
1215 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001216 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001217
Chris Lattnerdf195262007-10-09 17:51:17 +00001218 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001219 Diag(Tok, diag::err_expected_colon);
1220 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001221 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001222 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001223 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001224 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001225 ExprResult Res = ParseAssignmentExpression();
1226 if (Res.isInvalid) {
1227 SkipUntil(tok::identifier);
1228 return Res;
1229 }
1230 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001231 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001232
1233 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001234 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001235 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001236 break;
1237 // We have a selector or a colon, continue parsing.
1238 }
1239 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001240 while (Tok.is(tok::comma)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001241 ConsumeToken();
1242 /// Parse the expression after ','
1243 ParseAssignmentExpression();
1244 }
1245 } else if (!selIdent) {
1246 Diag(Tok, diag::err_expected_ident); // missing selector name.
1247 SkipUntil(tok::semi);
1248 return 0;
1249 }
Chris Lattnerff384912007-10-07 02:00:24 +00001250
Chris Lattnerdf195262007-10-09 17:51:17 +00001251 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001252 Diag(Tok, diag::err_expected_rsquare);
1253 SkipUntil(tok::semi);
1254 return 0;
1255 }
Steve Naroff563477d2007-09-18 23:55:05 +00001256 SourceLocation RBracloc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001257
Steve Naroff29238a02007-10-05 18:42:47 +00001258 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001259 if (nKeys == 0)
1260 KeyIdents.push_back(selIdent);
1261 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1262
1263 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001264 if (ReceiverName)
Chris Lattnerff384912007-10-07 02:00:24 +00001265 return Actions.ActOnClassMessage(ReceiverName, Sel, LBracloc, RBracloc,
1266 &KeyExprs[0]);
1267 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracloc, RBracloc,
1268 &KeyExprs[0]);
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001269}
1270
Anders Carlsson55085182007-08-21 17:43:55 +00001271Parser::ExprResult Parser::ParseObjCStringLiteral() {
1272 ExprResult Res = ParseStringLiteralExpression();
1273
1274 if (Res.isInvalid) return Res;
1275
1276 return Actions.ParseObjCStringLiteral(Res.Val);
1277}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001278
1279/// objc-encode-expression:
1280/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001281Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001282 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001283
1284 SourceLocation EncLoc = ConsumeToken();
1285
Chris Lattnerdf195262007-10-09 17:51:17 +00001286 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001287 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1288 return true;
1289 }
1290
1291 SourceLocation LParenLoc = ConsumeParen();
1292
1293 TypeTy *Ty = ParseTypeName();
1294
Anders Carlsson4988ae32007-08-23 15:31:37 +00001295 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001296
Chris Lattner674af952007-10-16 22:51:17 +00001297 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001298 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001299}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001300
1301/// objc-protocol-expression
1302/// @protocol ( protocol-name )
1303
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001304Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001305{
1306 SourceLocation ProtoLoc = ConsumeToken();
1307
Chris Lattnerdf195262007-10-09 17:51:17 +00001308 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001309 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1310 return true;
1311 }
1312
1313 SourceLocation LParenLoc = ConsumeParen();
1314
Chris Lattnerdf195262007-10-09 17:51:17 +00001315 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001316 Diag(Tok, diag::err_expected_ident);
1317 return true;
1318 }
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001319 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001320 ConsumeToken();
1321
Anders Carlsson4988ae32007-08-23 15:31:37 +00001322 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001323
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001324 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1325 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001326}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001327
1328/// objc-selector-expression
1329/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001330Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001331{
1332 SourceLocation SelectorLoc = ConsumeToken();
1333
1334 if (Tok.isNot(tok::l_paren)) {
1335 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1336 return 0;
1337 }
1338
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001339 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001340 SourceLocation LParenLoc = ConsumeParen();
1341 SourceLocation sLoc;
1342 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1343 if (!SelIdent && Tok.isNot(tok::colon)) {
1344
1345 Diag(Tok, diag::err_expected_ident); // missing selector name.
1346 return 0;
1347 }
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001348 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001349 if (Tok.isNot(tok::r_paren))
1350 while (1) {
1351 if (Tok.isNot(tok::colon)) {
1352 Diag(Tok, diag::err_expected_colon);
1353 break;
1354 }
1355 ConsumeToken(); // Eat the ':'.
1356 if (Tok.is(tok::r_paren))
1357 break;
1358 // Check for another keyword selector.
1359 SourceLocation Loc;
1360 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001361 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001362 if (!SelIdent && Tok.isNot(tok::colon))
1363 break;
1364 }
1365 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001366 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1367 &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001368 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001369 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001370 }