blob: 8d404a9aafcd2bbe48b2d6f3c9f86c6145918b12 [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,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000274 DS.getSourceRange().getBegin(), FD, OCDS,
275 MethodImplKind);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000276 allProperties.push_back(Property);
277 }
Steve Naroff294494e2007-08-22 16:35:03 +0000278 continue;
279 } else {
280 Diag(Tok, diag::err_objc_illegal_interface_qual);
281 ConsumeToken();
282 }
283 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000284 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
285 DeclTy *methodPrototype =
286 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000287 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000288 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
289 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000290 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000291 continue;
292 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000293 else if (Tok.is(tok::at))
294 continue;
295
Chris Lattnerdf195262007-10-09 17:51:17 +0000296 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000297 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000298 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000299 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000300 else {
Steve Naroff4985ace2007-08-22 18:35:33 +0000301 // FIXME: as the name implies, this rule allows function definitions.
302 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000303 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000304 }
Steve Naroff294494e2007-08-22 16:35:03 +0000305 }
Steve Naroff2feac5e2007-10-30 03:43:13 +0000306 /// Insert collected methods declarations into the @interface object.
Steve Naroff0416fb92007-11-11 17:19:15 +0000307 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl, &allMethods[0], allMethods.size(),
308 &allProperties[0], allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000309}
310
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000311/// Parse property attribute declarations.
312///
313/// property-attr-decl: '(' property-attrlist ')'
314/// property-attrlist:
315/// property-attribute
316/// property-attrlist ',' property-attribute
317/// property-attribute:
318/// getter '=' identifier
319/// setter '=' identifier ':'
320/// readonly
321/// readwrite
322/// assign
323/// retain
324/// copy
325/// nonatomic
326///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000327void Parser::ParseObjCPropertyAttribute (ObjCDeclSpec &DS) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000328 SourceLocation loc = ConsumeParen(); // consume '('
329 while (isObjCPropertyAttribute()) {
330 const IdentifierInfo *II = Tok.getIdentifierInfo();
331 // getter/setter require extra treatment.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000332 if (II == ObjCPropertyAttrs[objc_getter] ||
333 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000334 // skip getter/setter part.
335 SourceLocation loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000336 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000337 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000338 if (Tok.is(tok::identifier)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000339 if (II == ObjCPropertyAttrs[objc_setter]) {
340 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000341 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000342 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000343 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000344 Diag(loc, diag::err_expected_colon);
345 SkipUntil(tok::r_paren,true,true);
346 break;
347 }
348 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000349 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000350 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000351 DS.setGetterName(Tok.getIdentifierInfo());
352 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000353 }
354 else {
355 Diag(loc, diag::err_expected_ident);
Chris Lattnercb53b362007-12-27 19:57:00 +0000356 SkipUntil(tok::r_paren,true,true);
357 break;
358 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000359 }
360 else {
361 Diag(loc, diag::err_objc_expected_equal);
362 SkipUntil(tok::r_paren,true,true);
363 break;
364 }
365 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000366
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000367 else if (II == ObjCPropertyAttrs[objc_readonly])
368 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
369 else if (II == ObjCPropertyAttrs[objc_assign])
370 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
371 else if (II == ObjCPropertyAttrs[objc_readwrite])
372 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
373 else if (II == ObjCPropertyAttrs[objc_retain])
374 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
375 else if (II == ObjCPropertyAttrs[objc_copy])
376 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
377 else if (II == ObjCPropertyAttrs[objc_nonatomic])
378 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000379
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000380 ConsumeToken(); // consume last attribute token
Chris Lattnerdf195262007-10-09 17:51:17 +0000381 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000382 loc = ConsumeToken();
383 continue;
384 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000385 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000386 break;
387 Diag(loc, diag::err_expected_rparen);
388 SkipUntil(tok::semi);
389 return;
390 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000391 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000392 ConsumeParen();
393 else {
394 Diag(loc, diag::err_objc_expected_property_attr);
395 SkipUntil(tok::r_paren); // recover from error inside attribute list
396 }
397}
398
Steve Naroff3536b442007-09-06 21:24:23 +0000399/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000400/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000401/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000402///
403/// objc-instance-method: '-'
404/// objc-class-method: '+'
405///
Steve Naroff4985ace2007-08-22 18:35:33 +0000406/// objc-method-attributes: [OBJC2]
407/// __attribute__((deprecated))
408///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000409Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000410 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000411 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000412
413 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000414 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000415
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000416 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000417 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000418 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000419 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000420}
421
422/// objc-selector:
423/// identifier
424/// one of
425/// enum struct union if else while do for switch case default
426/// break continue return goto asm sizeof typeof __alignof
427/// unsigned long const short volatile signed restrict _Complex
428/// in out inout bycopy byref oneway int char float double void _Bool
429///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000430IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000431 switch (Tok.getKind()) {
432 default:
433 return 0;
434 case tok::identifier:
435 case tok::kw_typeof:
436 case tok::kw___alignof:
437 case tok::kw_auto:
438 case tok::kw_break:
439 case tok::kw_case:
440 case tok::kw_char:
441 case tok::kw_const:
442 case tok::kw_continue:
443 case tok::kw_default:
444 case tok::kw_do:
445 case tok::kw_double:
446 case tok::kw_else:
447 case tok::kw_enum:
448 case tok::kw_extern:
449 case tok::kw_float:
450 case tok::kw_for:
451 case tok::kw_goto:
452 case tok::kw_if:
453 case tok::kw_inline:
454 case tok::kw_int:
455 case tok::kw_long:
456 case tok::kw_register:
457 case tok::kw_restrict:
458 case tok::kw_return:
459 case tok::kw_short:
460 case tok::kw_signed:
461 case tok::kw_sizeof:
462 case tok::kw_static:
463 case tok::kw_struct:
464 case tok::kw_switch:
465 case tok::kw_typedef:
466 case tok::kw_union:
467 case tok::kw_unsigned:
468 case tok::kw_void:
469 case tok::kw_volatile:
470 case tok::kw_while:
Chris Lattner9298d962007-11-15 05:25:19 +0000471 case tok::kw_bool:
Chris Lattnerff384912007-10-07 02:00:24 +0000472 case tok::kw__Bool:
473 case tok::kw__Complex:
474 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000475 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000476 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000477 }
Steve Naroff294494e2007-08-22 16:35:03 +0000478}
479
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000480/// property-attrlist: one of
481/// readonly getter setter assign retain copy nonatomic
482///
483bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000484 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000485 const IdentifierInfo *II = Tok.getIdentifierInfo();
486 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000487 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000488 }
489 return false;
490}
491
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000492/// objc-for-collection-in: 'in'
493///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000494bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000495 // FIXME: May have to do additional look-ahead to only allow for
496 // valid tokens following an 'in'; such as an identifier, unary operators,
497 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000498 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
499 Tok.getIdentifierInfo() == ObjCForCollectionInKW);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000500}
501
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000502/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000503/// qualifier list and builds their bitmask representation in the input
504/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000505///
506/// objc-type-qualifiers:
507/// objc-type-qualifier
508/// objc-type-qualifiers objc-type-qualifier
509///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000510void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000511 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000512 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000513 return;
514
515 const IdentifierInfo *II = Tok.getIdentifierInfo();
516 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000517 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000518 continue;
519
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000520 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000521 switch (i) {
522 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000523 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
524 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
525 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
526 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
527 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
528 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000529 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000530 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000531 ConsumeToken();
532 II = 0;
533 break;
534 }
535
536 // If this wasn't a recognized qualifier, bail out.
537 if (II) return;
538 }
539}
540
541/// objc-type-name:
542/// '(' objc-type-qualifiers[opt] type-name ')'
543/// '(' objc-type-qualifiers[opt] ')'
544///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000545Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000546 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000547
548 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattner271f1a62007-09-27 15:15:46 +0000549 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000550
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000551 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000552 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000553
Steve Naroff294494e2007-08-22 16:35:03 +0000554 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000555 Ty = ParseTypeName();
556 // FIXME: back when Sema support is in place...
557 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000558 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000559 if (Tok.isNot(tok::r_paren)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000560 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Narofff28b2642007-09-05 23:30:30 +0000561 return 0; // FIXME: decide how we want to handle this error...
Steve Naroff294494e2007-08-22 16:35:03 +0000562 }
563 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000564 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000565}
566
567/// objc-method-decl:
568/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000569/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000570/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000571/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000572///
573/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000574/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000575/// objc-keyword-selector objc-keyword-decl
576///
577/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000578/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
579/// objc-selector ':' objc-keyword-attributes[opt] identifier
580/// ':' objc-type-name objc-keyword-attributes[opt] identifier
581/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000582///
Steve Naroff4985ace2007-08-22 18:35:33 +0000583/// objc-parmlist:
584/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000585///
Steve Naroff4985ace2007-08-22 18:35:33 +0000586/// objc-parms:
587/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000588///
Steve Naroff4985ace2007-08-22 18:35:33 +0000589/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000590/// , ...
591///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000592/// objc-keyword-attributes: [OBJC2]
593/// __attribute__((unused))
594///
Steve Naroffbef11852007-10-26 20:53:56 +0000595Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000596 tok::TokenKind mType,
597 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000598 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000599{
Steve Naroff4985ace2007-08-22 18:35:33 +0000600 // Parse the return type.
Chris Lattnerff384912007-10-07 02:00:24 +0000601 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000602 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000603 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000604 ReturnType = ParseObjCTypeName(DSRet);
Steve Naroffbef11852007-10-26 20:53:56 +0000605 SourceLocation selLoc;
606 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000607 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000608 if (!SelIdent) {
609 Diag(Tok, diag::err_expected_ident); // missing selector name.
610 // FIXME: this creates a unary selector with a null identifier, is this
611 // ok?? Maybe we should skip to the next semicolon or something.
612 }
613
614 // If attributes exist after the method, parse them.
615 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000616 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000617 MethodAttrs = ParseAttributes();
618
619 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroffbef11852007-10-26 20:53:56 +0000620 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000621 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000622 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000623 }
Steve Narofff28b2642007-09-05 23:30:30 +0000624
Steve Naroff68d331a2007-09-27 14:38:14 +0000625 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
626 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000628 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000629
630 Action::TypeTy *TypeInfo;
631 while (1) {
632 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000633
Chris Lattnerff384912007-10-07 02:00:24 +0000634 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000635 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000636 Diag(Tok, diag::err_expected_colon);
637 break;
638 }
639 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640 ObjCDeclSpec DSType;
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000641 if (Tok.is(tok::l_paren)) { // Parse the argument type.
642 TypeInfo = ParseObjCTypeName(DSType);
643 }
Chris Lattnerff384912007-10-07 02:00:24 +0000644 else
645 TypeInfo = 0;
646 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000647 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000648
Chris Lattnerff384912007-10-07 02:00:24 +0000649 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000650 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000651 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000652
Chris Lattnerdf195262007-10-09 17:51:17 +0000653 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000654 Diag(Tok, diag::err_expected_ident); // missing argument name.
655 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000656 }
Chris Lattnerff384912007-10-07 02:00:24 +0000657 ArgNames.push_back(Tok.getIdentifierInfo());
658 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000659
Chris Lattnerff384912007-10-07 02:00:24 +0000660 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000661 SourceLocation Loc;
662 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000663 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000664 break;
665 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000666 }
Chris Lattnerff384912007-10-07 02:00:24 +0000667
Steve Naroff335eafa2007-11-15 12:35:21 +0000668 bool isVariadic = false;
669
Chris Lattnerff384912007-10-07 02:00:24 +0000670 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000671 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000672 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000673 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000674 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000675 ConsumeToken();
676 break;
677 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000678 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000679 // Parse the c-style argument declaration-specifier.
680 DeclSpec DS;
681 ParseDeclarationSpecifiers(DS);
682 // Parse the declarator.
683 Declarator ParmDecl(DS, Declarator::PrototypeContext);
684 ParseDeclarator(ParmDecl);
685 }
686
687 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000688 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000689 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000690 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000691 MethodAttrs = ParseAttributes();
692
693 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
694 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000695 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000696 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000697 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000698 &ArgNames[0], MethodAttrs,
699 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000700}
701
Steve Naroffdac269b2007-08-20 21:31:48 +0000702/// objc-protocol-refs:
703/// '<' identifier-list '>'
704///
Steve Narofff28b2642007-09-05 23:30:30 +0000705bool Parser::ParseObjCProtocolReferences(
Chris Lattner88cb27a2008-04-07 04:56:42 +0000706 llvm::SmallVectorImpl<IdentifierInfo*> &ProtocolRefs, SourceLocation &endLoc){
Chris Lattnerdf195262007-10-09 17:51:17 +0000707 assert(Tok.is(tok::less) && "expected <");
Steve Naroffdac269b2007-08-20 21:31:48 +0000708
709 ConsumeToken(); // the "<"
Steve Naroffdac269b2007-08-20 21:31:48 +0000710
711 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000712 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000713 Diag(Tok, diag::err_expected_ident);
714 SkipUntil(tok::greater);
715 return true;
716 }
717 ProtocolRefs.push_back(Tok.getIdentifierInfo());
718 ConsumeToken();
719
Chris Lattnerdf195262007-10-09 17:51:17 +0000720 if (Tok.isNot(tok::comma))
Steve Naroffdac269b2007-08-20 21:31:48 +0000721 break;
722 ConsumeToken();
723 }
Fariborz Jahaniane37882a2007-10-08 23:06:41 +0000724
Steve Naroffdac269b2007-08-20 21:31:48 +0000725 // Consume the '>'.
Steve Narofff908a872007-10-30 02:23:23 +0000726 if (Tok.is(tok::greater)) {
727 endLoc = ConsumeAnyToken();
728 return false;
729 }
730 Diag(Tok, diag::err_expected_greater);
731 return true;
Steve Naroffdac269b2007-08-20 21:31:48 +0000732}
733
734/// objc-class-instance-variables:
735/// '{' objc-instance-variable-decl-list[opt] '}'
736///
737/// objc-instance-variable-decl-list:
738/// objc-visibility-spec
739/// objc-instance-variable-decl ';'
740/// ';'
741/// objc-instance-variable-decl-list objc-visibility-spec
742/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
743/// objc-instance-variable-decl-list ';'
744///
745/// objc-visibility-spec:
746/// @private
747/// @protected
748/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000749/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000750///
751/// objc-instance-variable-decl:
752/// struct-declaration
753///
Steve Naroff60fccee2007-10-29 21:38:07 +0000754void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
755 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000756 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000757 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000758 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
759
Steve Naroffddbff782007-08-21 21:17:12 +0000760 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000761
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000762 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000763 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000764 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000765 // Each iteration of this loop reads one objc-instance-variable-decl.
766
767 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000768 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000769 Diag(Tok, diag::ext_extra_struct_semi);
770 ConsumeToken();
771 continue;
772 }
Chris Lattnere1359422008-04-10 06:46:29 +0000773
Steve Naroffddbff782007-08-21 21:17:12 +0000774 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000775 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000776 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000777 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000778 case tok::objc_private:
779 case tok::objc_public:
780 case tok::objc_protected:
781 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000782 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000783 ConsumeToken();
784 continue;
785 default:
786 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000787 continue;
788 }
789 }
Chris Lattnere1359422008-04-10 06:46:29 +0000790
791 // Parse all the comma separated declarators.
792 DeclSpec DS;
793 FieldDeclarators.clear();
794 ParseStructDeclaration(DS, FieldDeclarators);
795
796 // Convert them all to fields.
797 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
798 FieldDeclarator &FD = FieldDeclarators[i];
799 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000800 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000801 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000802 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000803 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000804 }
Steve Naroff3536b442007-09-06 21:24:23 +0000805
Chris Lattnerdf195262007-10-09 17:51:17 +0000806 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000807 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000808 } else if (Tok.is(tok::r_brace)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000809 Diag(Tok.getLocation(), diag::ext_expected_semi_decl_list);
810 break;
811 } else {
812 Diag(Tok, diag::err_expected_semi_decl_list);
813 // Skip to end of block or statement
814 SkipUntil(tok::r_brace, true, true);
815 }
816 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000817 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000818 // Call ActOnFields() even if we don't have any decls. This is useful
819 // for code rewriting tools that need to be aware of the empty list.
820 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
821 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000822 LBraceLoc, RBraceLoc);
Steve Naroffddbff782007-08-21 21:17:12 +0000823 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000824}
Steve Naroffdac269b2007-08-20 21:31:48 +0000825
826/// objc-protocol-declaration:
827/// objc-protocol-definition
828/// objc-protocol-forward-reference
829///
830/// objc-protocol-definition:
831/// @protocol identifier
832/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000833/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000834/// @end
835///
836/// objc-protocol-forward-reference:
837/// @protocol identifier-list ';'
838///
839/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000840/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000841/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000842Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000843 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000844 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
845 ConsumeToken(); // the "protocol" identifier
846
Chris Lattnerdf195262007-10-09 17:51:17 +0000847 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000848 Diag(Tok, diag::err_expected_ident); // missing protocol name.
849 return 0;
850 }
851 // Save the protocol name, then consume it.
852 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
853 SourceLocation nameLoc = ConsumeToken();
854
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000855 llvm::SmallVector<IdentifierInfo *, 8> ProtocolRefs;
Chris Lattnerdf195262007-10-09 17:51:17 +0000856 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000857 ConsumeToken();
Fariborz Jahanian894c57f2007-09-21 15:40:54 +0000858 ProtocolRefs.push_back(protocolName);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000859 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000860 if (Tok.is(tok::comma)) { // list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000861 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000862 ProtocolRefs.push_back(protocolName);
863
864 while (1) {
865 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000866 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000867 Diag(Tok, diag::err_expected_ident);
868 SkipUntil(tok::semi);
869 return 0;
870 }
871 ProtocolRefs.push_back(Tok.getIdentifierInfo());
872 ConsumeToken(); // the identifier
873
Chris Lattnerdf195262007-10-09 17:51:17 +0000874 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000875 break;
876 }
877 // Consume the ';'.
878 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
879 return 0;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000880 }
Chris Lattnerc8581052008-03-16 20:19:15 +0000881 if (!ProtocolRefs.empty())
Steve Naroffe440eb82007-10-10 17:32:04 +0000882 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000883 &ProtocolRefs[0],
884 ProtocolRefs.size());
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000885 // Last, and definitely not least, parse a protocol declaration.
Steve Narofff908a872007-10-30 02:23:23 +0000886 SourceLocation endProtoLoc;
Chris Lattnerdf195262007-10-09 17:51:17 +0000887 if (Tok.is(tok::less)) {
Steve Narofff908a872007-10-30 02:23:23 +0000888 if (ParseObjCProtocolReferences(ProtocolRefs, endProtoLoc))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000889 return 0;
890 }
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000891
Steve Naroffe440eb82007-10-10 17:32:04 +0000892 DeclTy *ProtoType = Actions.ActOnStartProtocolInterface(AtLoc,
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000893 protocolName, nameLoc,
894 &ProtocolRefs[0],
Steve Narofff908a872007-10-30 02:23:23 +0000895 ProtocolRefs.size(), endProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000896 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000897
898 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000899 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000900 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000901 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000902 }
903 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000904 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000905}
Steve Naroffdac269b2007-08-20 21:31:48 +0000906
907/// objc-implementation:
908/// objc-class-implementation-prologue
909/// objc-category-implementation-prologue
910///
911/// objc-class-implementation-prologue:
912/// @implementation identifier objc-superclass[opt]
913/// objc-class-instance-variables[opt]
914///
915/// objc-category-implementation-prologue:
916/// @implementation identifier ( identifier )
917
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000918Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
919 SourceLocation atLoc) {
920 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
921 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
922 ConsumeToken(); // the "implementation" identifier
923
Chris Lattnerdf195262007-10-09 17:51:17 +0000924 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000925 Diag(Tok, diag::err_expected_ident); // missing class or category name.
926 return 0;
927 }
928 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000929 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000930 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
931
Chris Lattnerdf195262007-10-09 17:51:17 +0000932 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000933 // we have a category implementation.
934 SourceLocation lparenLoc = ConsumeParen();
935 SourceLocation categoryLoc, rparenLoc;
936 IdentifierInfo *categoryId = 0;
937
Chris Lattnerdf195262007-10-09 17:51:17 +0000938 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000939 categoryId = Tok.getIdentifierInfo();
940 categoryLoc = ConsumeToken();
941 } else {
942 Diag(Tok, diag::err_expected_ident); // missing category name.
943 return 0;
944 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000945 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000946 Diag(Tok, diag::err_expected_rparen);
947 SkipUntil(tok::r_paren, false); // don't stop at ';'
948 return 0;
949 }
950 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +0000951 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +0000952 atLoc, nameId, nameLoc, categoryId,
953 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000954 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000955 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000956 }
957 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000958 SourceLocation superClassLoc;
959 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000960 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000961 // We have a super class
962 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000963 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000964 Diag(Tok, diag::err_expected_ident); // missing super class name.
965 return 0;
966 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000967 superClassId = Tok.getIdentifierInfo();
968 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000969 }
Steve Naroffe440eb82007-10-10 17:32:04 +0000970 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +0000971 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +0000972 superClassId, superClassLoc);
973
Steve Naroff60fccee2007-10-29 21:38:07 +0000974 if (Tok.is(tok::l_brace)) // we have ivars
975 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000976 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000977
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +0000978 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000979}
Steve Naroff60fccee2007-10-29 21:38:07 +0000980
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000981Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
982 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
983 "ParseObjCAtEndDeclaration(): Expected @end");
984 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +0000985 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +0000987 else
988 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +0000990}
Fariborz Jahaniane992af02007-09-04 19:26:51 +0000991
992/// compatibility-alias-decl:
993/// @compatibility_alias alias-name class-name ';'
994///
995Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
996 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
997 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
998 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +0000999 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001000 Diag(Tok, diag::err_expected_ident);
1001 return 0;
1002 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001003 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1004 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001005 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001006 Diag(Tok, diag::err_expected_ident);
1007 return 0;
1008 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001009 IdentifierInfo *classId = Tok.getIdentifierInfo();
1010 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1011 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +00001012 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001013 return 0;
1014 }
1015 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1016 aliasId, aliasLoc,
1017 classId, classLoc);
1018 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001019}
1020
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001021/// property-synthesis:
1022/// @synthesize property-ivar-list ';'
1023///
1024/// property-ivar-list:
1025/// property-ivar
1026/// property-ivar-list ',' property-ivar
1027///
1028/// property-ivar:
1029/// identifier
1030/// identifier '=' identifier
1031///
1032Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1033 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1034 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001035 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001036 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001037 Diag(Tok, diag::err_expected_ident);
1038 return 0;
1039 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001040 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001041 IdentifierInfo *propertyIvar = 0;
1042 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1043 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001044 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001045 // property '=' ivar-name
1046 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001047 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001048 Diag(Tok, diag::err_expected_ident);
1049 break;
1050 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001051 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001052 ConsumeToken(); // consume ivar-name
1053 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001054 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1055 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001056 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001057 break;
1058 ConsumeToken(); // consume ','
1059 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001060 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001061 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1062 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001063}
1064
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001065/// property-dynamic:
1066/// @dynamic property-list
1067///
1068/// property-list:
1069/// identifier
1070/// property-list ',' identifier
1071///
1072Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1073 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1074 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1075 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001076 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001077 Diag(Tok, diag::err_expected_ident);
1078 return 0;
1079 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001080 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001081 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1082 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1083 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1084 propertyId, 0);
1085
Chris Lattnerdf195262007-10-09 17:51:17 +00001086 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001087 break;
1088 ConsumeToken(); // consume ','
1089 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001090 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001091 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1092 return 0;
1093}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001094
1095/// objc-throw-statement:
1096/// throw expression[opt];
1097///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001098Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1099 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001100 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001101 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001102 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001103 if (Res.isInvalid) {
1104 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001105 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001106 }
1107 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001108 ConsumeToken(); // consume ';'
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001109 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001110}
1111
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001112/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001113/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001114///
1115Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001116 ConsumeToken(); // consume synchronized
1117 if (Tok.isNot(tok::l_paren)) {
1118 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1119 return true;
1120 }
1121 ConsumeParen(); // '('
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001122 ExprResult Res = ParseExpression();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001123 if (Res.isInvalid) {
1124 SkipUntil(tok::semi);
1125 return true;
1126 }
1127 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001128 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001129 return true;
1130 }
1131 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001132 if (Tok.isNot(tok::l_brace)) {
1133 Diag (Tok, diag::err_expected_lbrace);
1134 return true;
1135 }
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001136 StmtResult SynchBody = ParseCompoundStatementBody();
1137 if (SynchBody.isInvalid)
1138 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1139 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001140}
1141
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001142/// objc-try-catch-statement:
1143/// @try compound-statement objc-catch-list[opt]
1144/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1145///
1146/// objc-catch-list:
1147/// @catch ( parameter-declaration ) compound-statement
1148/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1149/// catch-parameter-declaration:
1150/// parameter-declaration
1151/// '...' [OBJC2]
1152///
Chris Lattner6b884502008-03-10 06:06:04 +00001153Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001154 bool catch_or_finally_seen = false;
Steve Naroff64515f32008-02-05 21:27:35 +00001155
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001156 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001157 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001158 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001159 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001160 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001161 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001162 StmtResult FinallyStmt;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001163 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001164 if (TryBody.isInvalid)
1165 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner6b884502008-03-10 06:06:04 +00001166
Chris Lattnerdf195262007-10-09 17:51:17 +00001167 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001168 // At this point, we need to lookahead to determine if this @ is the start
1169 // of an @catch or @finally. We don't want to consume the @ token if this
1170 // is an @try or @encode or something else.
1171 Token AfterAt = GetLookAheadToken(1);
1172 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1173 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1174 break;
1175
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001176 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001177 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001178 StmtTy *FirstPart = 0;
1179 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001180 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001181 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001182 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001183 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001184 DeclSpec DS;
1185 ParseDeclarationSpecifiers(DS);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001186 // FIXME: Is BlockContext right?
1187 Declarator DeclaratorInfo(DS, Declarator::BlockContext);
1188 ParseDeclarator(DeclaratorInfo);
Chris Lattner81c018d2008-03-13 06:29:04 +00001189 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1190 DeclaratorInfo, 0);
1191 StmtResult stmtResult =
1192 Actions.ActOnDeclStmt(aBlockVarDecl, DS.getSourceRange().getBegin(),
1193 DeclaratorInfo.getSourceRange().getEnd());
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001194 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
Steve Naroff64515f32008-02-05 21:27:35 +00001195 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001196 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001197 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001198
1199 StmtResult CatchBody(true);
1200 if (Tok.is(tok::l_brace))
1201 CatchBody = ParseCompoundStatementBody();
1202 else
1203 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001204 if (CatchBody.isInvalid)
1205 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001206 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001207 FirstPart, CatchBody.Val, CatchStmts.Val);
1208 ExitScope();
Steve Naroff64515f32008-02-05 21:27:35 +00001209 } else {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001210 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1211 "@catch clause");
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001212 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001213 }
1214 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001215 } else {
1216 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001217 ConsumeToken(); // consume finally
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001218
1219 StmtResult FinallyBody(true);
1220 if (Tok.is(tok::l_brace))
1221 FinallyBody = ParseCompoundStatementBody();
1222 else
1223 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001224 if (FinallyBody.isInvalid)
1225 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001226 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001227 FinallyBody.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001228 catch_or_finally_seen = true;
1229 break;
1230 }
1231 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001232 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001233 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001234 return true;
1235 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001236 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001237 FinallyStmt.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001238}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001239
Steve Naroff3536b442007-09-06 21:24:23 +00001240/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001241///
Steve Naroff71c0a952007-11-13 23:01:27 +00001242Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001243 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001244 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001245 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001246 ConsumeToken();
1247
Steve Naroff409be832007-11-11 19:54:21 +00001248 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001249 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001250 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001251
1252 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1253 SkipUntil(tok::l_brace, true, true);
1254
1255 // If we didn't find the '{', bail out.
1256 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001257 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001258 }
Steve Naroff409be832007-11-11 19:54:21 +00001259 SourceLocation BraceLoc = Tok.getLocation();
1260
1261 // Enter a scope for the method body.
1262 EnterScope(Scope::FnScope|Scope::DeclScope);
1263
1264 // Tell the actions module that we have entered a method definition with the
1265 // specified Declarator for the method.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001266 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff409be832007-11-11 19:54:21 +00001267
1268 StmtResult FnBody = ParseCompoundStatementBody();
1269
1270 // If the function body could not be parsed, make a bogus compoundstmt.
1271 if (FnBody.isInvalid)
1272 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1273
1274 // Leave the function body scope.
1275 ExitScope();
1276
1277 // TODO: Pass argument information.
Steve Naroffd6d054d2007-11-11 23:20:51 +00001278 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001279 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280}
Anders Carlsson55085182007-08-21 17:43:55 +00001281
Steve Naroff64515f32008-02-05 21:27:35 +00001282Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1283 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001284 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001285 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1286 return ParseObjCThrowStmt(AtLoc);
1287 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1288 return ParseObjCSynchronizedStmt(AtLoc);
1289 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1290 if (Res.isInvalid) {
1291 // If the expression is invalid, skip ahead to the next semicolon. Not
1292 // doing this opens us up to the possibility of infinite loops if
1293 // ParseExpression does not consume any tokens.
1294 SkipUntil(tok::semi);
1295 return true;
1296 }
1297 // Otherwise, eat the semicolon.
1298 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1299 return Actions.ActOnExprStmt(Res.Val);
1300}
1301
Steve Naroffa642beb2007-10-15 20:55:58 +00001302Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001303
1304 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001305 case tok::string_literal: // primary-expression: string-literal
1306 case tok::wide_string_literal:
1307 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1308 default:
1309 break;
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001310 }
1311
1312 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
Chris Lattner674af952007-10-16 22:51:17 +00001313 case tok::objc_encode:
1314 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1315 case tok::objc_protocol:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001316 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001317 case tok::objc_selector:
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001318 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner674af952007-10-16 22:51:17 +00001319 default:
1320 Diag(AtLoc, diag::err_unexpected_at);
1321 SkipUntil(tok::semi);
Chris Lattner9d493952008-01-30 21:20:25 +00001322 return true;
Anders Carlsson55085182007-08-21 17:43:55 +00001323 }
Anders Carlsson55085182007-08-21 17:43:55 +00001324}
1325
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001326/// objc-message-expr:
1327/// '[' objc-receiver objc-message-args ']'
1328///
1329/// objc-receiver:
1330/// expression
1331/// class-name
1332/// type-name
Chris Lattner699b6612008-01-25 18:59:06 +00001333Parser::ExprResult Parser::ParseObjCMessageExpression() {
1334 assert(Tok.is(tok::l_square) && "'[' expected");
1335 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1336
1337 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001338 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001339 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1340 ConsumeToken();
1341 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1342 }
1343
1344 ExprResult Res = ParseAssignmentExpression();
1345 if (Res.isInvalid) {
1346 Diag(Tok, diag::err_invalid_receiver_to_message);
Chris Lattner5c749422008-01-25 19:43:26 +00001347 SkipUntil(tok::r_square);
Chris Lattner699b6612008-01-25 18:59:06 +00001348 return Res;
1349 }
1350 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1351}
1352
1353/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1354/// the rest of a message expression.
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001355///
1356/// objc-message-args:
1357/// objc-selector
1358/// objc-keywordarg-list
1359///
1360/// objc-keywordarg-list:
1361/// objc-keywordarg
1362/// objc-keywordarg-list objc-keywordarg
1363///
1364/// objc-keywordarg:
1365/// selector-name[opt] ':' objc-keywordexpr
1366///
1367/// objc-keywordexpr:
1368/// nonempty-expr-list
1369///
1370/// nonempty-expr-list:
1371/// assignment-expression
1372/// nonempty-expr-list , assignment-expression
1373///
Chris Lattner699b6612008-01-25 18:59:06 +00001374Parser::ExprResult
1375Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1376 IdentifierInfo *ReceiverName,
1377 ExprTy *ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001378 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001379 SourceLocation Loc;
1380 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001381
1382 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1383 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1384
Chris Lattnerdf195262007-10-09 17:51:17 +00001385 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001386 while (1) {
1387 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001388 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001389
Chris Lattnerdf195262007-10-09 17:51:17 +00001390 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001391 Diag(Tok, diag::err_expected_colon);
1392 SkipUntil(tok::semi);
Steve Naroff37387c92007-09-17 20:25:27 +00001393 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001394 }
Steve Naroff68d331a2007-09-27 14:38:14 +00001395 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001396 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001397 ExprResult Res = ParseAssignmentExpression();
1398 if (Res.isInvalid) {
1399 SkipUntil(tok::identifier);
1400 return Res;
1401 }
1402 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001403 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001404
1405 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001406 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001407 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001408 break;
1409 // We have a selector or a colon, continue parsing.
1410 }
1411 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001412 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001413 ConsumeToken(); // Eat the ','.
1414 /// Parse the expression after ','
1415 ExprResult Res = ParseAssignmentExpression();
1416 if (Res.isInvalid) {
1417 SkipUntil(tok::identifier);
1418 return Res;
1419 }
1420 // We have a valid expression.
1421 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001422 }
1423 } else if (!selIdent) {
1424 Diag(Tok, diag::err_expected_ident); // missing selector name.
1425 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001426 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001427 }
Chris Lattnerff384912007-10-07 02:00:24 +00001428
Chris Lattnerdf195262007-10-09 17:51:17 +00001429 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001430 Diag(Tok, diag::err_expected_rsquare);
1431 SkipUntil(tok::semi);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001432 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001433 }
Chris Lattner699b6612008-01-25 18:59:06 +00001434 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001435
Steve Naroff29238a02007-10-05 18:42:47 +00001436 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001437 if (nKeys == 0)
1438 KeyIdents.push_back(selIdent);
1439 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1440
1441 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001442 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001443 return Actions.ActOnClassMessage(CurScope,
Chris Lattner699b6612008-01-25 18:59:06 +00001444 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001445 &KeyExprs[0], KeyExprs.size());
Chris Lattner699b6612008-01-25 18:59:06 +00001446 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001447 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001448}
1449
Steve Naroffbeaf2992007-11-03 11:27:19 +00001450Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001451 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001452 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001453
1454 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1455 // expressions. At this point, we know that the only valid thing that starts
1456 // with '@' is an @"".
1457 llvm::SmallVector<SourceLocation, 4> AtLocs;
1458 llvm::SmallVector<ExprTy*, 4> AtStrings;
1459 AtLocs.push_back(AtLoc);
1460 AtStrings.push_back(Res.Val);
1461
1462 while (Tok.is(tok::at)) {
1463 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001464
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001465 ExprResult Res(true); // Invalid unless there is a string literal.
1466 if (isTokenStringLiteral())
1467 Res = ParseStringLiteralExpression();
1468 else
1469 Diag(Tok, diag::err_objc_concat_string);
1470
1471 if (Res.isInvalid) {
1472 while (!AtStrings.empty()) {
1473 Actions.DeleteExpr(AtStrings.back());
1474 AtStrings.pop_back();
1475 }
1476 return Res;
1477 }
1478
1479 AtStrings.push_back(Res.Val);
1480 }
Fariborz Jahanian79a99f22007-12-12 23:55:49 +00001481
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001482 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1483 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001484}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001485
1486/// objc-encode-expression:
1487/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001488Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001489 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001490
1491 SourceLocation EncLoc = ConsumeToken();
1492
Chris Lattnerdf195262007-10-09 17:51:17 +00001493 if (Tok.isNot(tok::l_paren)) {
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001494 Diag(Tok, diag::err_expected_lparen_after, "@encode");
1495 return true;
1496 }
1497
1498 SourceLocation LParenLoc = ConsumeParen();
1499
1500 TypeTy *Ty = ParseTypeName();
1501
Anders Carlsson4988ae32007-08-23 15:31:37 +00001502 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001503
Chris Lattner674af952007-10-16 22:51:17 +00001504 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001505 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001506}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001507
1508/// objc-protocol-expression
1509/// @protocol ( protocol-name )
1510
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001511Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001512{
1513 SourceLocation ProtoLoc = ConsumeToken();
1514
Chris Lattnerdf195262007-10-09 17:51:17 +00001515 if (Tok.isNot(tok::l_paren)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001516 Diag(Tok, diag::err_expected_lparen_after, "@protocol");
1517 return true;
1518 }
1519
1520 SourceLocation LParenLoc = ConsumeParen();
1521
Chris Lattnerdf195262007-10-09 17:51:17 +00001522 if (Tok.isNot(tok::identifier)) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001523 Diag(Tok, diag::err_expected_ident);
1524 return true;
1525 }
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001526 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001527 ConsumeToken();
1528
Anders Carlsson4988ae32007-08-23 15:31:37 +00001529 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001530
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001531 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1532 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001533}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001534
1535/// objc-selector-expression
1536/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001537Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001538{
1539 SourceLocation SelectorLoc = ConsumeToken();
1540
1541 if (Tok.isNot(tok::l_paren)) {
1542 Diag(Tok, diag::err_expected_lparen_after, "@selector");
1543 return 0;
1544 }
1545
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001546 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001547 SourceLocation LParenLoc = ConsumeParen();
1548 SourceLocation sLoc;
1549 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
1550 if (!SelIdent && Tok.isNot(tok::colon)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001551 Diag(Tok, diag::err_expected_ident); // missing selector name.
1552 return 0;
1553 }
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001554 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001555 unsigned nColons = 0;
1556 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001557 while (1) {
1558 if (Tok.isNot(tok::colon)) {
1559 Diag(Tok, diag::err_expected_colon);
1560 break;
1561 }
Chris Lattnercb53b362007-12-27 19:57:00 +00001562 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001563 ConsumeToken(); // Eat the ':'.
1564 if (Tok.is(tok::r_paren))
1565 break;
1566 // Check for another keyword selector.
1567 SourceLocation Loc;
1568 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001569 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001570 if (!SelIdent && Tok.isNot(tok::colon))
1571 break;
1572 }
Steve Naroff887407e2007-12-05 22:21:29 +00001573 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001574 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001575 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001576 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001577 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001578 }