blob: 0c5cf20ef8eb13ef61ca70ffe0bef1168722ca85 [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/Diagnostic.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
22/// ParseExternalDeclaration:
23/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Steve Naroffdac269b2007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff861cf3e2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Reid Spencer5f016e22007-07-11 17:01:13 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
Steve Naroffdac269b2007-08-20 21:31:48 +000037 return ParseObjCAtInterfaceDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000038 case tok::objc_protocol:
Steve Naroff7ef58fd2007-08-22 22:17:26 +000039 return ParseObjCAtProtocolDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000040 case tok::objc_implementation:
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +000041 return ParseObjCAtImplementationDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000042 case tok::objc_end:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000043 return ParseObjCAtEndDeclaration(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000044 case tok::objc_compatibility_alias:
Fariborz Jahaniane992af02007-09-04 19:26:51 +000045 return ParseObjCAtAliasDeclaration(AtLoc);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +000046 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000053 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Naroffdac269b2007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Naroffdac269b2007-08-20 21:31:48 +000069 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
Reid Spencer5f016e22007-07-11 17:01:13 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnerdf195262007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Naroffdac269b2007-08-20 21:31:48 +000082 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000083
Steve Naroffe440eb82007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Steve Naroffdac269b2007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
94/// '@' 'interface' identifier objc-superclass[opt]
95/// objc-protocol-refs[opt]
96/// objc-class-instance-variables[opt]
97/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
101/// '@' 'interface' identifier '(' identifier[opt] ')'
102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
116Parser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnerdf195262007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
124 return 0;
125 }
126 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnerdf195262007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000134 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Naroffdac269b2007-08-20 21:31:48 +0000135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
142 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
147 return 0;
148 }
149 rparenLoc = ConsumeParen();
Steve Narofff908a872007-10-30 02:23:23 +0000150 SourceLocation endProtoLoc;
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Chris Lattnerdf195262007-10-09 17:51:17 +0000152 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000153 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroffdac269b2007-08-20 21:31:48 +0000154 return 0;
155 }
156 if (attrList) // categories don't support attributes.
157 Diag(Tok, diag::err_objc_no_attributes_on_category);
158
Steve Naroffe440eb82007-10-10 17:32:04 +0000159 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000160 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000161 &ProtocolRefs[0], ProtocolRefs.size(),
162 endProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000163
164 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Naroffdac269b2007-08-20 21:31:48 +0000165
Steve Naroff294494e2007-08-22 16:35:03 +0000166 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000167 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000168 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000169 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000170 }
Steve Naroff294494e2007-08-22 16:35:03 +0000171 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000172 return 0;
173 }
174 // Parse a class interface.
175 IdentifierInfo *superClassId = 0;
176 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000177
Chris Lattnerdf195262007-10-09 17:51:17 +0000178 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000179 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000181 Diag(Tok, diag::err_expected_ident); // missing super class name.
182 return 0;
183 }
184 superClassId = Tok.getIdentifierInfo();
185 superClassLoc = ConsumeToken();
186 }
187 // Next, we need to check for any protocol references.
Steve Narofff28b2642007-09-05 23:30:30 +0000188 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Steve Narofff908a872007-10-30 02:23:23 +0000189 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000190 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000191 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroffdac269b2007-08-20 21:31:48 +0000192 return 0;
193 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000194 DeclTy *ClsType = Actions.ActOnStartClassInterface(
Chris Lattnercb53b362007-12-27 19:57:00 +0000195 atLoc, nameId, nameLoc,
Steve Narofff28b2642007-09-05 23:30:30 +0000196 superClassId, superClassLoc, &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000197 ProtocolRefs.size(), endProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000198
Chris Lattnerdf195262007-10-09 17:51:17 +0000199 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000200 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000201
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000202 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff294494e2007-08-22 16:35:03 +0000203
204 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000205 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000206 ConsumeToken(); // the "end" identifier
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000207 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000208 }
Steve Naroff294494e2007-08-22 16:35:03 +0000209 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000210 return 0;
211}
212
213/// objc-interface-decl-list:
214/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff294494e2007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000225void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000226 tok::ObjCKeywordKind contextKey) {
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000227 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000228 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000229 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000230 SourceLocation AtEndLoc;
231
Steve Naroff294494e2007-08-22 16:35:03 +0000232 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 if (Tok.is(tok::at)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000234 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff861cf3e2007-08-23 18:16:40 +0000235 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff294494e2007-08-22 16:35:03 +0000236
237 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff60fccee2007-10-29 21:38:07 +0000238 AtEndLoc = AtLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000239 break;
Steve Naroff294494e2007-08-22 16:35:03 +0000240 } else if (ocKind == tok::objc_required) { // protocols only
241 ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +0000242 MethodImplKind = ocKind;
243 if (contextKey != tok::objc_protocol)
244 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff294494e2007-08-22 16:35:03 +0000245 } else if (ocKind == tok::objc_optional) { // protocols only
246 ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +0000247 MethodImplKind = ocKind;
248 if (contextKey != tok::objc_protocol)
249 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff294494e2007-08-22 16:35:03 +0000250 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000251 ObjCDeclSpec OCDS;
252 ConsumeToken(); // the "property" identifier
253 // Parse property attribute list, if any.
254 if (Tok.is(tok::l_paren)) {
255 // property has attribute list.
256 ParseObjCPropertyAttribute(OCDS);
257 }
258 // Parse all the comma separated declarators.
259 DeclSpec DS;
260 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
261 ParseStructDeclaration(DS, FieldDeclarators);
262
263 if (Tok.is(tok::semi))
264 ConsumeToken();
265 else {
266 Diag(Tok, diag::err_expected_semi_decl_list);
267 SkipUntil(tok::r_brace, true, true);
268 }
269 // Convert them all to property declarations.
270 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
271 FieldDeclarator &FD = FieldDeclarators[i];
272 // Install the property declarator into interfaceDecl.
273 DeclTy *Property = Actions.ActOnProperty(CurScope,
274 DS.getSourceRange().getBegin(), FD, OCDS);
275 allProperties.push_back(Property);
276 }
Steve Naroff294494e2007-08-22 16:35:03 +0000277 continue;
278 } else {
279 Diag(Tok, diag::err_objc_illegal_interface_qual);
280 ConsumeToken();
281 }
282 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000283 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
284 DeclTy *methodPrototype =
285 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000286 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000287 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
288 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000289 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000290 continue;
291 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000292 else if (Tok.is(tok::at))
293 continue;
294
Chris Lattnerdf195262007-10-09 17:51:17 +0000295 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000296 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000297 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000298 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000299 else {
Steve Naroff4985ace2007-08-22 18:35:33 +0000300 // FIXME: as the name implies, this rule allows function definitions.
301 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000302 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000303 }
Steve Naroff294494e2007-08-22 16:35:03 +0000304 }
Steve Naroff2feac5e2007-10-30 03:43:13 +0000305 /// Insert collected methods declarations into the @interface object.
Steve Naroff0416fb92007-11-11 17:19:15 +0000306 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
307 &allProperties[0], allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000308}
309
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000310/// Parse property attribute declarations.
311///
312/// property-attr-decl: '(' property-attrlist ')'
313/// property-attrlist:
314/// property-attribute
315/// property-attrlist ',' property-attribute
316/// property-attribute:
317/// getter '=' identifier
318/// setter '=' identifier ':'
319/// readonly
320/// readwrite
321/// assign
322/// retain
323/// copy
324/// nonatomic
325///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000326void Parser::ParseObjCPropertyAttribute (ObjCDeclSpec &DS) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000327 SourceLocation loc = ConsumeParen(); // consume '('
328 while (isObjCPropertyAttribute()) {
329 const IdentifierInfo *II = Tok.getIdentifierInfo();
330 // getter/setter require extra treatment.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000331 if (II == ObjCPropertyAttrs[objc_getter] ||
332 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000333 // skip getter/setter part.
334 SourceLocation loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000335 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000336 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000337 if (Tok.is(tok::identifier)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000338 if (II == ObjCPropertyAttrs[objc_setter]) {
339 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000340 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000341 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000342 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000343 Diag(loc, diag::err_expected_colon);
344 SkipUntil(tok::r_paren,true,true);
345 break;
346 }
347 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000348 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000349 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000350 DS.setGetterName(Tok.getIdentifierInfo());
351 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000352 }
353 else {
354 Diag(loc, diag::err_expected_ident);
Chris Lattnercb53b362007-12-27 19:57:00 +0000355 SkipUntil(tok::r_paren,true,true);
356 break;
357 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000358 }
359 else {
360 Diag(loc, diag::err_objc_expected_equal);
361 SkipUntil(tok::r_paren,true,true);
362 break;
363 }
364 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000365
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000366 else if (II == ObjCPropertyAttrs[objc_readonly])
367 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
368 else if (II == ObjCPropertyAttrs[objc_assign])
369 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
370 else if (II == ObjCPropertyAttrs[objc_readwrite])
371 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
372 else if (II == ObjCPropertyAttrs[objc_retain])
373 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
374 else if (II == ObjCPropertyAttrs[objc_copy])
375 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
376 else if (II == ObjCPropertyAttrs[objc_nonatomic])
377 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000378
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000379 ConsumeToken(); // consume last attribute token
Chris Lattnerdf195262007-10-09 17:51:17 +0000380 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000381 loc = ConsumeToken();
382 continue;
383 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000384 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000385 break;
386 Diag(loc, diag::err_expected_rparen);
387 SkipUntil(tok::semi);
388 return;
389 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000390 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000391 ConsumeParen();
392 else {
393 Diag(loc, diag::err_objc_expected_property_attr);
394 SkipUntil(tok::r_paren); // recover from error inside attribute list
395 }
396}
397
Steve Naroff3536b442007-09-06 21:24:23 +0000398/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000399/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000400/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000401///
402/// objc-instance-method: '-'
403/// objc-class-method: '+'
404///
Steve Naroff4985ace2007-08-22 18:35:33 +0000405/// objc-method-attributes: [OBJC2]
406/// __attribute__((deprecated))
407///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000408Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000409 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000410 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000411
412 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000413 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000414
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000415 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000416 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000417 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000418 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000419}
420
421/// objc-selector:
422/// identifier
423/// one of
424/// enum struct union if else while do for switch case default
425/// break continue return goto asm sizeof typeof __alignof
426/// unsigned long const short volatile signed restrict _Complex
427/// in out inout bycopy byref oneway int char float double void _Bool
428///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000429IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000430 switch (Tok.getKind()) {
431 default:
432 return 0;
433 case tok::identifier:
434 case tok::kw_typeof:
435 case tok::kw___alignof:
436 case tok::kw_auto:
437 case tok::kw_break:
438 case tok::kw_case:
439 case tok::kw_char:
440 case tok::kw_const:
441 case tok::kw_continue:
442 case tok::kw_default:
443 case tok::kw_do:
444 case tok::kw_double:
445 case tok::kw_else:
446 case tok::kw_enum:
447 case tok::kw_extern:
448 case tok::kw_float:
449 case tok::kw_for:
450 case tok::kw_goto:
451 case tok::kw_if:
452 case tok::kw_inline:
453 case tok::kw_int:
454 case tok::kw_long:
455 case tok::kw_register:
456 case tok::kw_restrict:
457 case tok::kw_return:
458 case tok::kw_short:
459 case tok::kw_signed:
460 case tok::kw_sizeof:
461 case tok::kw_static:
462 case tok::kw_struct:
463 case tok::kw_switch:
464 case tok::kw_typedef:
465 case tok::kw_union:
466 case tok::kw_unsigned:
467 case tok::kw_void:
468 case tok::kw_volatile:
469 case tok::kw_while:
Chris Lattner9298d962007-11-15 05:25:19 +0000470 case tok::kw_bool:
Chris Lattnerff384912007-10-07 02:00:24 +0000471 case tok::kw__Bool:
472 case tok::kw__Complex:
473 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000474 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000475 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000476 }
Steve Naroff294494e2007-08-22 16:35:03 +0000477}
478
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000479/// property-attrlist: one of
480/// readonly getter setter assign retain copy nonatomic
481///
482bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000483 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000484 const IdentifierInfo *II = Tok.getIdentifierInfo();
485 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000486 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000487 }
488 return false;
489}
490
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000491/// objc-for-collection-in: 'in'
492///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000493bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000494 // FIXME: May have to do additional look-ahead to only allow for
495 // valid tokens following an 'in'; such as an identifier, unary operators,
496 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000497 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
498 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000499}
500
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000501/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000502/// qualifier list and builds their bitmask representation in the input
503/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000504///
505/// objc-type-qualifiers:
506/// objc-type-qualifier
507/// objc-type-qualifiers objc-type-qualifier
508///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000509void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000510 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000511 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000512 return;
513
514 const IdentifierInfo *II = Tok.getIdentifierInfo();
515 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000516 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000517 continue;
518
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000519 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000520 switch (i) {
521 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000522 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
523 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
524 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
525 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
526 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
527 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000528 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000529 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000530 ConsumeToken();
531 II = 0;
532 break;
533 }
534
535 // If this wasn't a recognized qualifier, bail out.
536 if (II) return;
537 }
538}
539
540/// objc-type-name:
541/// '(' objc-type-qualifiers[opt] type-name ')'
542/// '(' objc-type-qualifiers[opt] ')'
543///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000544Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000545 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000546
547 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000548 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000549
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000550 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000551 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000552
Steve Naroff294494e2007-08-22 16:35:03 +0000553 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000554 Ty = ParseTypeName();
555 // FIXME: back when Sema support is in place...
556 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000557 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000558 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000559 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000560 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000561 }
562 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000563 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000564}
565
566/// objc-method-decl:
567/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000568/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000569/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000570/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000571///
572/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000573/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000574/// objc-keyword-selector objc-keyword-decl
575///
576/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000577/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
578/// objc-selector ':' objc-keyword-attributes[opt] identifier
579/// ':' objc-type-name objc-keyword-attributes[opt] identifier
580/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000581///
Steve Naroff4985ace2007-08-22 18:35:33 +0000582/// objc-parmlist:
583/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000584///
Steve Naroff4985ace2007-08-22 18:35:33 +0000585/// objc-parms:
586/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000587///
Steve Naroff4985ace2007-08-22 18:35:33 +0000588/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000589/// , ...
590///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000591/// objc-keyword-attributes: [OBJC2]
592/// __attribute__((unused))
593///
Steve Naroffbef11852007-10-26 20:53:56 +0000594Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000595 tok::TokenKind mType,
596 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000597 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000598{
Steve Naroff4985ace2007-08-22 18:35:33 +0000599 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000600 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000601 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000602 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000603 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroffbef11852007-10-26 20:53:56 +0000604 SourceLocation selLoc;
605 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000606 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000607 if (!SelIdent) {
608 Diag(Tok, diag::err_expected_ident); // missing selector name.
609 // FIXME: this creates a unary selector with a null identifier, is this
610 // ok?? Maybe we should skip to the next semicolon or something.
611 }
612
613 // If attributes exist after the method, parse them.
614 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000615 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000616 MethodAttrs = ParseAttributes();
617
618 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000619 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000620 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000621 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000622 }
Steve Narofff28b2642007-09-05 23:30:30 +0000623
Steve Naroff68d331a2007-09-27 14:38:14 +0000624 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
625 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000626 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000627 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000628
629 Action::TypeTy *TypeInfo;
630 while (1) {
631 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000632
Chris Lattnerff384912007-10-07 02:00:24 +0000633 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000634 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000635 Diag(Tok, diag::err_expected_colon);
636 break;
637 }
638 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000639 ObjCDeclSpec DSType;
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000640 if (Tok.is(tok::l_paren)) { // Parse the argument type.
641 TypeInfo = ParseObjCTypeName(DSType);
642 }
Chris Lattnerff384912007-10-07 02:00:24 +0000643 else
644 TypeInfo = 0;
645 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000646 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000647
Chris Lattnerff384912007-10-07 02:00:24 +0000648 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000649 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000650 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000651
Chris Lattnerdf195262007-10-09 17:51:17 +0000652 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000653 Diag(Tok, diag::err_expected_ident); // missing argument name.
654 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000655 }
Chris Lattnerff384912007-10-07 02:00:24 +0000656 ArgNames.push_back(Tok.getIdentifierInfo());
657 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000658
Chris Lattnerff384912007-10-07 02:00:24 +0000659 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000660 SourceLocation Loc;
661 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000662 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000663 break;
664 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000665 }
Chris Lattnerff384912007-10-07 02:00:24 +0000666
Steve Naroff335eafa2007-11-15 12:35:21 +0000667 bool isVariadic = false;
668
Chris Lattnerff384912007-10-07 02:00:24 +0000669 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000670 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000671 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000672 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000673 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000674 ConsumeToken();
675 break;
676 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000677 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000678 // Parse the c-style argument declaration-specifier.
679 DeclSpec DS;
680 ParseDeclarationSpecifiers(DS);
681 // Parse the declarator.
682 Declarator ParmDecl(DS, Declarator::PrototypeContext);
683 ParseDeclarator(ParmDecl);
684 }
685
686 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000687 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000688 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000689 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000690 MethodAttrs = ParseAttributes();
691
692 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
693 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000694 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000695 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000696 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000697 &ArgNames[0], MethodAttrs,
698 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000699}
700
Steve Naroffdac269b2007-08-20 21:31:48 +0000701/// objc-protocol-refs:
702/// '<' identifier-list '>'
703///
Steve Narofff28b2642007-09-05 23:30:30 +0000704bool Parser::ParseObjCProtocolReferences(
Chris Lattner88cb27a2008-04-07 04:56:42 +0000705 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc){
Chris Lattnerdf195262007-10-09 17:51:17 +0000706 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000707
708 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000709
710 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000711 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000712 Diag(Tok, diag::err_expected_ident);
713 SkipUntil(tok::greater);
714 return true;
715 }
716 ProtocolRefs.push_back(Tok.getIdentifierInfo());
717 ConsumeToken();
718
Chris Lattnerdf195262007-10-09 17:51:17 +0000719 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000720 break;
721 ConsumeToken();
722 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000723
Steve Naroffdac269b2007-08-20 21:31:48 +0000724 // Consume the '>'.
Steve Narofff908a872007-10-30 02:23:23 +0000725 if (Tok.is(tok::greater)) {
726 endLoc = ConsumeAnyToken();
727 return false;
728 }
729 Diag(Tok, diag::err_expected_greater);
730 return true;
Steve Naroffdac269b2007-08-20 21:31:48 +0000731}
732
733/// objc-class-instance-variables:
734/// '{' objc-instance-variable-decl-list[opt] '}'
735///
736/// objc-instance-variable-decl-list:
737/// objc-visibility-spec
738/// objc-instance-variable-decl ';'
739/// ';'
740/// objc-instance-variable-decl-list objc-visibility-spec
741/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
742/// objc-instance-variable-decl-list ';'
743///
744/// objc-visibility-spec:
745/// @private
746/// @protected
747/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000748/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000749///
750/// objc-instance-variable-decl:
751/// struct-declaration
752///
Steve Naroff60fccee2007-10-29 21:38:07 +0000753void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
754 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000755 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000756 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000757 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
758
Steve Naroffddbff782007-08-21 21:17:12 +0000759 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000760
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000761 tok::ObjCKeywordKind visibility = tok::objc_private;
Steve Naroffddbff782007-08-21 21:17:12 +0000762 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000763 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000764 // Each iteration of this loop reads one objc-instance-variable-decl.
765
766 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000767 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000768 Diag(Tok, diag::ext_extra_struct_semi);
769 ConsumeToken();
770 continue;
771 }
Chris Lattnere1359422008-04-10 06:46:29 +0000772
Steve Naroffddbff782007-08-21 21:17:12 +0000773 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000774 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000775 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000776 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000777 case tok::objc_private:
778 case tok::objc_public:
779 case tok::objc_protected:
780 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000781 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000782 ConsumeToken();
783 continue;
784 default:
785 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000786 continue;
787 }
788 }
Chris Lattnere1359422008-04-10 06:46:29 +0000789
790 // Parse all the comma separated declarators.
791 DeclSpec DS;
792 FieldDeclarators.clear();
793 ParseStructDeclaration(DS, FieldDeclarators);
794
795 // Convert them all to fields.
796 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
797 FieldDeclarator &FD = FieldDeclarators[i];
798 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000799 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000800 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000801 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000802 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000803 }
Steve Naroff3536b442007-09-06 21:24:23 +0000804
Chris Lattnerdf195262007-10-09 17:51:17 +0000805 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000806 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000807 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000808 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
809 break;
810 } else {
811 Diag(Tok, diag::err_expected_semi_decl_list);
812 // Skip to end of block or statement
813 SkipUntil(tok::r_brace, true, true);
814 }
815 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000816 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000817 // Call ActOnFields() even if we don't have any decls. This is useful
818 // for code rewriting tools that need to be aware of the empty list.
819 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
820 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000821 LBraceLoc, RBraceLoc);
Steve Naroffddbff782007-08-21 21:17:12 +0000822 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000823}
Steve Naroffdac269b2007-08-20 21:31:48 +0000824
825/// objc-protocol-declaration:
826/// objc-protocol-definition
827/// objc-protocol-forward-reference
828///
829/// objc-protocol-definition:
830/// @protocol identifier
831/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000832/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000833/// @end
834///
835/// objc-protocol-forward-reference:
836/// @protocol identifier-list ';'
837///
838/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000839/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000840/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000841Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000842 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000843 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
844 ConsumeToken(); // the "protocol" identifier
845
Chris Lattnerdf195262007-10-09 17:51:17 +0000846 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000847 Diag(Tok, diag::err_expected_ident); // missing protocol name.
848 return 0;
849 }
850 // Save the protocol name, then consume it.
851 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
852 SourceLocation nameLoc = ConsumeToken();
853
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000854 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000855 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000856 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000857 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000858 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000859 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000860 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000861 ProtocolRefs.push_back(protocolName);
862
863 while (1) {
864 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000865 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000866 Diag(Tok, diag::err_expected_ident);
867 SkipUntil(tok::semi);
868 return 0;
869 }
870 ProtocolRefs.push_back(Tok.getIdentifierInfo());
871 ConsumeToken(); // the identifier
872
Chris Lattnerdf195262007-10-09 17:51:17 +0000873 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000874 break;
875 }
876 // Consume the ';'.
877 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
878 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000879 }
Chris Lattnerc8581052008-03-16 20:19:15 +0000880 if (!ProtocolRefs.empty())
Steve Naroffe440eb82007-10-10 17:32:04 +0000881 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000882 &ProtocolRefs[0],
883 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000884 // Last, and definitely not least, parse a protocol declaration.
Steve Narofff908a872007-10-30 02:23:23 +0000885 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000886 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000887 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000888 return 0;
889 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000890
Steve Naroffe440eb82007-10-10 17:32:04 +0000891 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000892 protocolName, nameLoc,
893 &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000894 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000895 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000896
897 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000898 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000899 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000900 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000901 }
902 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000903 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000904}
Steve Naroffdac269b2007-08-20 21:31:48 +0000905
906/// objc-implementation:
907/// objc-class-implementation-prologue
908/// objc-category-implementation-prologue
909///
910/// objc-class-implementation-prologue:
911/// @implementation identifier objc-superclass[opt]
912/// objc-class-instance-variables[opt]
913///
914/// objc-category-implementation-prologue:
915/// @implementation identifier ( identifier )
916
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000917Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
918 SourceLocation atLoc) {
919 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
920 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
921 ConsumeToken(); // the "implementation" identifier
922
Chris Lattnerdf195262007-10-09 17:51:17 +0000923 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000924 Diag(Tok, diag::err_expected_ident); // missing class or category name.
925 return 0;
926 }
927 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000928 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000929 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
930
Chris Lattnerdf195262007-10-09 17:51:17 +0000931 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000932 // we have a category implementation.
933 SourceLocation lparenLoc = ConsumeParen();
934 SourceLocation categoryLoc, rparenLoc;
935 IdentifierInfo *categoryId = 0;
936
Chris Lattnerdf195262007-10-09 17:51:17 +0000937 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000938 categoryId = Tok.getIdentifierInfo();
939 categoryLoc = ConsumeToken();
940 } else {
941 Diag(Tok, diag::err_expected_ident); // missing category name.
942 return 0;
943 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000944 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000945 Diag(Tok, diag::err_expected_rparen);
946 SkipUntil(tok::r_paren, false); // don't stop at ';'
947 return 0;
948 }
949 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000950 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000951 atLoc, nameId, nameLoc, categoryId,
952 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000953 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000954 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000955 }
956 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000957 SourceLocation superClassLoc;
958 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000959 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000960 // We have a super class
961 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000962 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000963 Diag(Tok, diag::err_expected_ident); // missing super class name.
964 return 0;
965 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000966 superClassId = Tok.getIdentifierInfo();
967 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000968 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000969 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +0000970 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000971 superClassId, superClassLoc);
972
Steve Naroff60fccee2007-10-29 21:38:07 +0000973 if (Tok.is(tok::l_brace)) // we have ivars
974 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000975 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000976
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000977 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000978}
Steve Naroff60fccee2007-10-29 21:38:07 +0000979
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000980Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
981 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
982 "ParseObjCAtEndDeclaration(): Expected @end");
983 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +0000984 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000985 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +0000986 else
987 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000988 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +0000989}
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000990
991/// compatibility-alias-decl:
992/// @compatibility_alias alias-name class-name ';'
993///
994Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
995 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
996 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
997 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +0000998 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000999 Diag(Tok, diag::err_expected_ident);
1000 return 0;
1001 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001002 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1003 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001004 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001005 Diag(Tok, diag::err_expected_ident);
1006 return 0;
1007 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001008 IdentifierInfo *classId = Tok.getIdentifierInfo();
1009 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1010 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +00001011 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001012 return 0;
1013 }
1014 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1015 aliasId, aliasLoc,
1016 classId, classLoc);
1017 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001018}
1019
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001020/// property-synthesis:
1021/// @synthesize property-ivar-list ';'
1022///
1023/// property-ivar-list:
1024/// property-ivar
1025/// property-ivar-list ',' property-ivar
1026///
1027/// property-ivar:
1028/// identifier
1029/// identifier '=' identifier
1030///
1031Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1032 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1033 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001034 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001035 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001036 Diag(Tok, diag::err_expected_ident);
1037 return 0;
1038 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001039 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001040 IdentifierInfo *propertyIvar = 0;
1041 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1042 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001043 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001044 // property '=' ivar-name
1045 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001046 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001047 Diag(Tok, diag::err_expected_ident);
1048 break;
1049 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001050 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001051 ConsumeToken(); // consume ivar-name
1052 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001053 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1054 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001055 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001056 break;
1057 ConsumeToken(); // consume ','
1058 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001059 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001060 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1061 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001062}
1063
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001064/// property-dynamic:
1065/// @dynamic property-list
1066///
1067/// property-list:
1068/// identifier
1069/// property-list ',' identifier
1070///
1071Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1072 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1073 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1074 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001075 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001076 Diag(Tok, diag::err_expected_ident);
1077 return 0;
1078 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001079 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001080 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1081 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1082 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1083 propertyId, 0);
1084
Chris Lattnerdf195262007-10-09 17:51:17 +00001085 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001086 break;
1087 ConsumeToken(); // consume ','
1088 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001089 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001090 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1091 return 0;
1092}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001093
1094/// objc-throw-statement:
1095/// throw expression[opt];
1096///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001097Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1098 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001099 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001100 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001101 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001102 if (Res.isInvalid) {
1103 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001104 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001105 }
1106 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001107 ConsumeToken(); // consume ';'
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001109}
1110
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001111/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001112/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001113///
1114Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001115 ConsumeToken(); // consume synchronized
1116 if (Tok.isNot(tok::l_paren)) {
1117 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1118 return true;
1119 }
1120 ConsumeParen(); // '('
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001121 ExprResult Res = ParseExpression();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001122 if (Res.isInvalid) {
1123 SkipUntil(tok::semi);
1124 return true;
1125 }
1126 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001127 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001128 return true;
1129 }
1130 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001131 if (Tok.isNot(tok::l_brace)) {
1132 Diag (Tok, diag::err_expected_lbrace);
1133 return true;
1134 }
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001135 StmtResult SynchBody = ParseCompoundStatementBody();
1136 if (SynchBody.isInvalid)
1137 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1138 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001139}
1140
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001141/// objc-try-catch-statement:
1142/// @try compound-statement objc-catch-list[opt]
1143/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1144///
1145/// objc-catch-list:
1146/// @catch ( parameter-declaration ) compound-statement
1147/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1148/// catch-parameter-declaration:
1149/// parameter-declaration
1150/// '...' [OBJC2]
1151///
Chris Lattner6b884502008-03-10 06:06:04 +00001152Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001153 bool catch_or_finally_seen = false;
Steve Naroff64515f32008-02-05 21:27:35 +00001154
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001155 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001156 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001157 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001158 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001159 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001160 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001161 StmtResult FinallyStmt;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001162 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001163 if (TryBody.isInvalid)
1164 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner6b884502008-03-10 06:06:04 +00001165
Chris Lattnerdf195262007-10-09 17:51:17 +00001166 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001167 // At this point, we need to lookahead to determine if this @ is the start
1168 // of an @catch or @finally. We don't want to consume the @ token if this
1169 // is an @try or @encode or something else.
1170 Token AfterAt = GetLookAheadToken(1);
1171 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1172 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1173 break;
1174
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001175 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001176 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001177 StmtTy *FirstPart = 0;
1178 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001179 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001180 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001181 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001182 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001183 DeclSpec DS;
1184 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001185 // FIXME: Is BlockContext right?
1186 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1187 ParseDeclarator(DeclaratorInfo);
Chris Lattner81c018d2008-03-13 06:29:04 +00001188 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1189 DeclaratorInfo, 0);
1190 StmtResult stmtResult =
1191 Actions.ActOnDeclStmt(aBlockVarDecl, DS.getSourceRange().getBegin(),
1192 DeclaratorInfo.getSourceRange().getEnd());
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001193 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Steve Naroff64515f32008-02-05 21:27:35 +00001194 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001195 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001196 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001197
1198 StmtResult CatchBody(true);
1199 if (Tok.is(tok::l_brace))
1200 CatchBody = ParseCompoundStatementBody();
1201 else
1202 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001203 if (CatchBody.isInvalid)
1204 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001205 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001206 FirstPart, CatchBody.Val, CatchStmts.Val);
1207 ExitScope();
Steve Naroff64515f32008-02-05 21:27:35 +00001208 } else {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001209 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1210 "@catch clause");
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001211 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001212 }
1213 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001214 } else {
1215 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001216 ConsumeToken(); // consume finally
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001217
1218 StmtResult FinallyBody(true);
1219 if (Tok.is(tok::l_brace))
1220 FinallyBody = ParseCompoundStatementBody();
1221 else
1222 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001223 if (FinallyBody.isInvalid)
1224 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001225 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001226 FinallyBody.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001227 catch_or_finally_seen = true;
1228 break;
1229 }
1230 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001231 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001232 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001233 return true;
1234 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001235 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001236 FinallyStmt.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001237}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001238
Steve Naroff3536b442007-09-06 21:24:23 +00001239/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001240///
Steve Naroff71c0a952007-11-13 23:01:27 +00001241Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001242 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001243 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001244 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001245 ConsumeToken();
1246
Steve Naroff409be832007-11-11 19:54:21 +00001247 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001248 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001249 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001250
1251 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1252 SkipUntil(tok::l_brace, true, true);
1253
1254 // If we didn't find the '{', bail out.
1255 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001256 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001257 }
Steve Naroff409be832007-11-11 19:54:21 +00001258 SourceLocation BraceLoc = Tok.getLocation();
1259
1260 // Enter a scope for the method body.
1261 EnterScope(Scope::FnScope|Scope::DeclScope);
1262
1263 // Tell the actions module that we have entered a method definition with the
1264 // specified Declarator for the method.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001265 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff409be832007-11-11 19:54:21 +00001266
1267 StmtResult FnBody = ParseCompoundStatementBody();
1268
1269 // If the function body could not be parsed, make a bogus compoundstmt.
1270 if (FnBody.isInvalid)
1271 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1272
1273 // Leave the function body scope.
1274 ExitScope();
1275
1276 // TODO: Pass argument information.
Steve Naroffd6d054d2007-11-11 23:20:51 +00001277 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001278 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001279}
Anders Carlsson55085182007-08-21 17:43:55 +00001280
Steve Naroff64515f32008-02-05 21:27:35 +00001281Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1282 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001283 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001284 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1285 return ParseObjCThrowStmt(AtLoc);
1286 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1287 return ParseObjCSynchronizedStmt(AtLoc);
1288 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1289 if (Res.isInvalid) {
1290 // If the expression is invalid, skip ahead to the next semicolon. Not
1291 // doing this opens us up to the possibility of infinite loops if
1292 // ParseExpression does not consume any tokens.
1293 SkipUntil(tok::semi);
1294 return true;
1295 }
1296 // Otherwise, eat the semicolon.
1297 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1298 return Actions.ActOnExprStmt(Res.Val);
1299}
1300
Steve Naroffa642beb2007-10-15 20:55:58 +00001301Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001302
1303 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001304 case tok::string_literal: // primary-expression: string-literal
1305 case tok::wide_string_literal:
1306 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1307 default:
1308 break;
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001309 }
1310
1311 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattner674af952007-10-16 22:51:17 +00001312 case tok::objc_encode:
1313 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1314 case tok::objc_protocol:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001315 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001316 case tok::objc_selector:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001317 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001318 default:
1319 Diag(AtLoc, diag::err_unexpected_at);
1320 SkipUntil(tok::semi);
Chris Lattner9d493952008-01-30 21:20:25 +00001321 return true;
Anders Carlsson55085182007-08-21 17:43:55 +00001322 }
Anders Carlsson55085182007-08-21 17:43:55 +00001323}
1324
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001325/// objc-message-expr:
1326/// '[' objc-receiver objc-message-args ']'
1327///
1328/// objc-receiver:
1329/// expression
1330/// class-name
1331/// type-name
Chris Lattner699b6612008-01-25 18:59:06 +00001332Parser::ExprResult Parser::ParseObjCMessageExpression() {
1333 assert(Tok.is(tok::l_square) && "'[' expected");
1334 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1335
1336 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001337 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001338 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1339 ConsumeToken();
1340 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1341 }
1342
1343 ExprResult Res = ParseAssignmentExpression();
1344 if (Res.isInvalid) {
1345 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattner5c749422008-01-25 19:43:26 +00001346 SkipUntil(tok::r_square);
Chris Lattner699b6612008-01-25 18:59:06 +00001347 return Res;
1348 }
1349 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1350}
1351
1352/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1353/// the rest of a message expression.
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001354///
1355/// objc-message-args:
1356/// objc-selector
1357/// objc-keywordarg-list
1358///
1359/// objc-keywordarg-list:
1360/// objc-keywordarg
1361/// objc-keywordarg-list objc-keywordarg
1362///
1363/// objc-keywordarg:
1364/// selector-name[opt] ':' objc-keywordexpr
1365///
1366/// objc-keywordexpr:
1367/// nonempty-expr-list
1368///
1369/// nonempty-expr-list:
1370/// assignment-expression
1371/// nonempty-expr-list , assignment-expression
1372///
Chris Lattner699b6612008-01-25 18:59:06 +00001373Parser::ExprResult
1374Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1375 IdentifierInfo *ReceiverName,
1376 ExprTy *ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001377 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001378 SourceLocation Loc;
1379 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001380
1381 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1382 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1383
Chris Lattnerdf195262007-10-09 17:51:17 +00001384 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001385 while (1) {
1386 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001387 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001388
Chris Lattnerdf195262007-10-09 17:51:17 +00001389 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001390 Diag(Tok, diag::err_expected_colon);
1391 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001392 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001393 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001394 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001395 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001396 ExprResult Res = ParseAssignmentExpression();
1397 if (Res.isInvalid) {
1398 SkipUntil(tok::identifier);
1399 return Res;
1400 }
1401 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001402 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001403
1404 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001405 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001406 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001407 break;
1408 // We have a selector or a colon, continue parsing.
1409 }
1410 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001411 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001412 ConsumeToken(); // Eat the ','.
1413 /// Parse the expression after ','
1414 ExprResult Res = ParseAssignmentExpression();
1415 if (Res.isInvalid) {
1416 SkipUntil(tok::identifier);
1417 return Res;
1418 }
1419 // We have a valid expression.
1420 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001421 }
1422 } else if (!selIdent) {
1423 Diag(Tok, diag::err_expected_ident); // missing selector name.
1424 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001425 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001426 }
Chris Lattnerff384912007-10-07 02:00:24 +00001427
Chris Lattnerdf195262007-10-09 17:51:17 +00001428 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001429 Diag(Tok, diag::err_expected_rsquare);
1430 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001431 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001432 }
Chris Lattner699b6612008-01-25 18:59:06 +00001433 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001434
Steve Naroff29238a02007-10-05 18:42:47 +00001435 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001436 if (nKeys == 0)
1437 KeyIdents.push_back(selIdent);
1438 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1439
1440 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001441 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001442 return Actions.ActOnClassMessage(CurScope,
Chris Lattner699b6612008-01-25 18:59:06 +00001443 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001444 &KeyExprs[0], KeyExprs.size());
Chris Lattner699b6612008-01-25 18:59:06 +00001445 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001446 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001447}
1448
Steve Naroffbeaf2992007-11-03 11:27:19 +00001449Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001450 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001451 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001452
1453 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1454 // expressions. At this point, we know that the only valid thing that starts
1455 // with '@' is an @"".
1456 llvm::SmallVector<SourceLocation, 4> AtLocs;
1457 llvm::SmallVector<ExprTy*, 4> AtStrings;
1458 AtLocs.push_back(AtLoc);
1459 AtStrings.push_back(Res.Val);
1460
1461 while (Tok.is(tok::at)) {
1462 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001463
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001464 ExprResult Res(true); // Invalid unless there is a string literal.
1465 if (isTokenStringLiteral())
1466 Res = ParseStringLiteralExpression();
1467 else
1468 Diag(Tok, diag::err_objc_concat_string);
1469
1470 if (Res.isInvalid) {
1471 while (!AtStrings.empty()) {
1472 Actions.DeleteExpr(AtStrings.back());
1473 AtStrings.pop_back();
1474 }
1475 return Res;
1476 }
1477
1478 AtStrings.push_back(Res.Val);
1479 }
Fariborz Jahanian79a99f22007-12-12 23:55:49 +00001480
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001481 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1482 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001483}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001484
1485/// objc-encode-expression:
1486/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001487Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001488 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001489
1490 SourceLocation EncLoc = ConsumeToken();
1491
Chris Lattnerdf195262007-10-09 17:51:17 +00001492 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001493 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1494 return true;
1495 }
1496
1497 SourceLocation LParenLoc = ConsumeParen();
1498
1499 TypeTy *Ty = ParseTypeName();
1500
Anders Carlsson4988ae32007-08-23 15:31:37 +00001501 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001502
Chris Lattner674af952007-10-16 22:51:17 +00001503 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001504 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001505}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001506
1507/// objc-protocol-expression
1508/// @protocol ( protocol-name )
1509
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001510Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001511{
1512 SourceLocation ProtoLoc = ConsumeToken();
1513
Chris Lattnerdf195262007-10-09 17:51:17 +00001514 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001515 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1516 return true;
1517 }
1518
1519 SourceLocation LParenLoc = ConsumeParen();
1520
Chris Lattnerdf195262007-10-09 17:51:17 +00001521 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001522 Diag(Tok, diag::err_expected_ident);
1523 return true;
1524 }
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001525 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001526 ConsumeToken();
1527
Anders Carlsson4988ae32007-08-23 15:31:37 +00001528 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001529
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001530 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1531 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001532}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001533
1534/// objc-selector-expression
1535/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001536Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001537{
1538 SourceLocation SelectorLoc = ConsumeToken();
1539
1540 if (Tok.isNot(tok::l_paren)) {
1541 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1542 return 0;
1543 }
1544
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001545 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001546 SourceLocation LParenLoc = ConsumeParen();
1547 SourceLocation sLoc;
1548 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1549 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001550 Diag(Tok, diag::err_expected_ident); // missing selector name.
1551 return 0;
1552 }
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001553 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001554 unsigned nColons = 0;
1555 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001556 while (1) {
1557 if (Tok.isNot(tok::colon)) {
1558 Diag(Tok, diag::err_expected_colon);
1559 break;
1560 }
Chris Lattnercb53b362007-12-27 19:57:00 +00001561 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001562 ConsumeToken(); // Eat the ':'.
1563 if (Tok.is(tok::r_paren))
1564 break;
1565 // Check for another keyword selector.
1566 SourceLocation Loc;
1567 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001568 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001569 if (!SelIdent && Tok.isNot(tok::colon))
1570 break;
1571 }
Steve Naroff887407e2007-12-05 22:21:29 +00001572 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001573 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001574 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001575 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001576 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001577 }