blob: e957db4ed9afa05764319c907c63527adad283b7 [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()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
53 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;
134
Steve Naroff527fe232007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000149
Steve Naroffdac269b2007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000151 SourceLocation EndProtoLoc;
152 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
153 if (Tok.is(tok::less) &&
154 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
155 return 0;
156
Steve Naroffdac269b2007-08-20 21:31:48 +0000157 if (attrList) // categories don't support attributes.
158 Diag(Tok, diag::err_objc_no_attributes_on_category);
159
Steve Naroffe440eb82007-10-10 17:32:04 +0000160 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff3a165b02007-10-03 21:00:46 +0000161 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff423cb562007-10-30 13:30:57 +0000162 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000163 EndProtoLoc);
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000164
165 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Steve Naroffdac269b2007-08-20 21:31:48 +0000166
Steve Naroff294494e2007-08-22 16:35:03 +0000167 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000168 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000169 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000170 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000171 }
Steve Naroff294494e2007-08-22 16:35:03 +0000172 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000173 return 0;
174 }
175 // Parse a class interface.
176 IdentifierInfo *superClassId = 0;
177 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000178
Chris Lattnerdf195262007-10-09 17:51:17 +0000179 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000181 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000182 Diag(Tok, diag::err_expected_ident); // missing super class name.
183 return 0;
184 }
185 superClassId = Tok.getIdentifierInfo();
186 superClassLoc = ConsumeToken();
187 }
188 // Next, we need to check for any protocol references.
Chris Lattner06036d32008-07-26 04:13:19 +0000189 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
190 SourceLocation EndProtoLoc;
191 if (Tok.is(tok::less) &&
192 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
193 return 0;
194
195 DeclTy *ClsType =
196 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
197 superClassId, superClassLoc,
198 &ProtocolRefs[0], ProtocolRefs.size(),
199 EndProtoLoc, attrList);
Steve Narofff28b2642007-09-05 23:30:30 +0000200
Chris Lattnerdf195262007-10-09 17:51:17 +0000201 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000202 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000203
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000204 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Steve Naroff294494e2007-08-22 16:35:03 +0000205
206 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000207 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000208 ConsumeToken(); // the "end" identifier
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000209 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000210 }
Steve Naroff294494e2007-08-22 16:35:03 +0000211 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000212 return 0;
213}
214
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000215/// constructSetterName - Return the setter name for the given
216/// identifier, i.e. "set" + Name where the initial character of Name
217/// has been capitalized.
218static IdentifierInfo *constructSetterName(IdentifierTable &Idents,
219 const IdentifierInfo *Name) {
220 unsigned N = Name->getLength();
221 char *SelectorName = new char[3 + N];
222 memcpy(SelectorName, "set", 3);
223 memcpy(&SelectorName[3], Name->getName(), N);
224 SelectorName[3] = toupper(SelectorName[3]);
225
226 IdentifierInfo *Setter =
227 &Idents.get(SelectorName, &SelectorName[3 + N]);
228 delete[] SelectorName;
229 return Setter;
230}
231
Steve Naroffdac269b2007-08-20 21:31:48 +0000232/// objc-interface-decl-list:
233/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000234/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000235/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000236/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000237/// objc-interface-decl-list declaration
238/// objc-interface-decl-list ';'
239///
Steve Naroff294494e2007-08-22 16:35:03 +0000240/// objc-method-requirement: [OBJC2]
241/// @required
242/// @optional
243///
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000244void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000245 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8a779312008-06-06 16:45:15 +0000246 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000247 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000248 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff60fccee2007-10-29 21:38:07 +0000249 SourceLocation AtEndLoc;
250
Steve Naroff294494e2007-08-22 16:35:03 +0000251 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000252 if (Tok.is(tok::at)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000253 SourceLocation AtLoc = ConsumeToken(); // the "@"
Steve Naroff861cf3e2007-08-23 18:16:40 +0000254 tok::ObjCKeywordKind ocKind = Tok.getObjCKeywordID();
Steve Naroff294494e2007-08-22 16:35:03 +0000255
256 if (ocKind == tok::objc_end) { // terminate list
Steve Naroff60fccee2007-10-29 21:38:07 +0000257 AtEndLoc = AtLoc;
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000258 break;
Steve Naroff294494e2007-08-22 16:35:03 +0000259 } else if (ocKind == tok::objc_required) { // protocols only
260 ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +0000261 MethodImplKind = ocKind;
262 if (contextKey != tok::objc_protocol)
263 Diag(AtLoc, diag::err_objc_protocol_required);
Steve Naroff294494e2007-08-22 16:35:03 +0000264 } else if (ocKind == tok::objc_optional) { // protocols only
265 ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +0000266 MethodImplKind = ocKind;
267 if (contextKey != tok::objc_protocol)
268 Diag(AtLoc, diag::err_objc_protocol_optional);
Steve Naroff294494e2007-08-22 16:35:03 +0000269 } else if (ocKind == tok::objc_property) {
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000270 ObjCDeclSpec OCDS;
271 ConsumeToken(); // the "property" identifier
272 // Parse property attribute list, if any.
273 if (Tok.is(tok::l_paren)) {
274 // property has attribute list.
275 ParseObjCPropertyAttribute(OCDS);
276 }
277 // Parse all the comma separated declarators.
278 DeclSpec DS;
279 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
280 ParseStructDeclaration(DS, FieldDeclarators);
281
282 if (Tok.is(tok::semi))
283 ConsumeToken();
284 else {
285 Diag(Tok, diag::err_expected_semi_decl_list);
286 SkipUntil(tok::r_brace, true, true);
287 }
288 // Convert them all to property declarations.
289 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
290 FieldDeclarator &FD = FieldDeclarators[i];
291 // Install the property declarator into interfaceDecl.
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000292 Selector GetterSel =
Fariborz Jahanian33de3f02008-05-07 17:43:59 +0000293 PP.getSelectorTable().getNullarySelector(OCDS.getGetterName()
294 ? OCDS.getGetterName()
295 : FD.D.getIdentifier());
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000296 IdentifierInfo *SetterName = OCDS.getSetterName();
297 if (!SetterName)
298 SetterName = constructSetterName(PP.getIdentifierTable(),
299 FD.D.getIdentifier());
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000300 Selector SetterSel =
Daniel Dunbar4d7da2f2008-08-26 02:32:45 +0000301 PP.getSelectorTable().getUnarySelector(SetterName);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000302 DeclTy *Property = Actions.ActOnProperty(CurScope,
Steve Naroff8442b5c2008-05-22 23:24:08 +0000303 AtLoc, FD, OCDS,
Fariborz Jahanian5251e132008-05-06 18:09:04 +0000304 GetterSel, SetterSel,
Fariborz Jahanian46b55e52008-05-05 18:51:55 +0000305 MethodImplKind);
Fariborz Jahanian1de1e742008-04-14 23:36:35 +0000306 allProperties.push_back(Property);
307 }
Steve Naroff294494e2007-08-22 16:35:03 +0000308 continue;
309 } else {
310 Diag(Tok, diag::err_objc_illegal_interface_qual);
311 ConsumeToken();
312 }
313 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000314 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
315 DeclTy *methodPrototype =
316 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000317 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000318 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
319 // method definitions.
Steve Naroffd16245b2007-09-17 15:07:43 +0000320 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff294494e2007-08-22 16:35:03 +0000321 continue;
322 }
Fariborz Jahanianf366b4c2007-12-11 18:34:51 +0000323 else if (Tok.is(tok::at))
324 continue;
325
Chris Lattnerdf195262007-10-09 17:51:17 +0000326 if (Tok.is(tok::semi))
Steve Naroff294494e2007-08-22 16:35:03 +0000327 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000328 else if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000329 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000330 else {
Steve Naroff4985ace2007-08-22 18:35:33 +0000331 // FIXME: as the name implies, this rule allows function definitions.
332 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff3536b442007-09-06 21:24:23 +0000333 ParseDeclarationOrFunctionDefinition();
Steve Narofff28b2642007-09-05 23:30:30 +0000334 }
Steve Naroff294494e2007-08-22 16:35:03 +0000335 }
Steve Naroff2feac5e2007-10-30 03:43:13 +0000336 /// Insert collected methods declarations into the @interface object.
Ted Kremenek8a779312008-06-06 16:45:15 +0000337 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
338 allMethods.empty() ? 0 : &allMethods[0],
339 allMethods.size(),
340 allProperties.empty() ? 0 : &allProperties[0],
341 allProperties.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000342}
343
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000344/// Parse property attribute declarations.
345///
346/// property-attr-decl: '(' property-attrlist ')'
347/// property-attrlist:
348/// property-attribute
349/// property-attrlist ',' property-attribute
350/// property-attribute:
351/// getter '=' identifier
352/// setter '=' identifier ':'
353/// readonly
354/// readwrite
355/// assign
356/// retain
357/// copy
358/// nonatomic
359///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000360void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000361 SourceLocation loc = ConsumeParen(); // consume '('
362 while (isObjCPropertyAttribute()) {
363 const IdentifierInfo *II = Tok.getIdentifierInfo();
364 // getter/setter require extra treatment.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000365 if (II == ObjCPropertyAttrs[objc_getter] ||
366 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000367 // skip getter/setter part.
368 SourceLocation loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000369 if (Tok.is(tok::equal)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000370 loc = ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000371 if (Tok.is(tok::identifier)) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000372 if (II == ObjCPropertyAttrs[objc_setter]) {
373 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000374 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000375 loc = ConsumeToken(); // consume method name
Chris Lattnerdf195262007-10-09 17:51:17 +0000376 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000377 Diag(loc, diag::err_expected_colon);
378 SkipUntil(tok::r_paren,true,true);
379 break;
380 }
381 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000382 else {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000383 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000384 DS.setGetterName(Tok.getIdentifierInfo());
385 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000386 }
387 else {
388 Diag(loc, diag::err_expected_ident);
Chris Lattnercb53b362007-12-27 19:57:00 +0000389 SkipUntil(tok::r_paren,true,true);
390 break;
391 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000392 }
393 else {
394 Diag(loc, diag::err_objc_expected_equal);
395 SkipUntil(tok::r_paren,true,true);
396 break;
397 }
398 }
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000399
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000400 else if (II == ObjCPropertyAttrs[objc_readonly])
401 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
402 else if (II == ObjCPropertyAttrs[objc_assign])
403 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
404 else if (II == ObjCPropertyAttrs[objc_readwrite])
405 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
406 else if (II == ObjCPropertyAttrs[objc_retain])
407 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
408 else if (II == ObjCPropertyAttrs[objc_copy])
409 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
410 else if (II == ObjCPropertyAttrs[objc_nonatomic])
411 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian82a5fe32007-11-06 22:01:00 +0000412
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000413 ConsumeToken(); // consume last attribute token
Chris Lattnerdf195262007-10-09 17:51:17 +0000414 if (Tok.is(tok::comma)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000415 loc = ConsumeToken();
416 continue;
417 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000418 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000419 break;
420 Diag(loc, diag::err_expected_rparen);
421 SkipUntil(tok::semi);
422 return;
423 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000424 if (Tok.is(tok::r_paren))
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000425 ConsumeParen();
426 else {
427 Diag(loc, diag::err_objc_expected_property_attr);
428 SkipUntil(tok::r_paren); // recover from error inside attribute list
429 }
430}
431
Steve Naroff3536b442007-09-06 21:24:23 +0000432/// objc-method-proto:
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +0000433/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000434/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000435///
436/// objc-instance-method: '-'
437/// objc-class-method: '+'
438///
Steve Naroff4985ace2007-08-22 18:35:33 +0000439/// objc-method-attributes: [OBJC2]
440/// __attribute__((deprecated))
441///
Fariborz Jahanian00933592007-09-18 00:25:23 +0000442Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000443 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000444 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000445
446 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000447 SourceLocation mLoc = ConsumeToken();
Steve Naroff294494e2007-08-22 16:35:03 +0000448
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000449 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000450 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000451 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000452 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000453}
454
455/// objc-selector:
456/// identifier
457/// one of
458/// enum struct union if else while do for switch case default
459/// break continue return goto asm sizeof typeof __alignof
460/// unsigned long const short volatile signed restrict _Complex
461/// in out inout bycopy byref oneway int char float double void _Bool
462///
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000463IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000464 switch (Tok.getKind()) {
465 default:
466 return 0;
467 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000468 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000469 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000470 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000471 case tok::kw_break:
472 case tok::kw_case:
473 case tok::kw_catch:
474 case tok::kw_char:
475 case tok::kw_class:
476 case tok::kw_const:
477 case tok::kw_const_cast:
478 case tok::kw_continue:
479 case tok::kw_default:
480 case tok::kw_delete:
481 case tok::kw_do:
482 case tok::kw_double:
483 case tok::kw_dynamic_cast:
484 case tok::kw_else:
485 case tok::kw_enum:
486 case tok::kw_explicit:
487 case tok::kw_export:
488 case tok::kw_extern:
489 case tok::kw_false:
490 case tok::kw_float:
491 case tok::kw_for:
492 case tok::kw_friend:
493 case tok::kw_goto:
494 case tok::kw_if:
495 case tok::kw_inline:
496 case tok::kw_int:
497 case tok::kw_long:
498 case tok::kw_mutable:
499 case tok::kw_namespace:
500 case tok::kw_new:
501 case tok::kw_operator:
502 case tok::kw_private:
503 case tok::kw_protected:
504 case tok::kw_public:
505 case tok::kw_register:
506 case tok::kw_reinterpret_cast:
507 case tok::kw_restrict:
508 case tok::kw_return:
509 case tok::kw_short:
510 case tok::kw_signed:
511 case tok::kw_sizeof:
512 case tok::kw_static:
513 case tok::kw_static_cast:
514 case tok::kw_struct:
515 case tok::kw_switch:
516 case tok::kw_template:
517 case tok::kw_this:
518 case tok::kw_throw:
519 case tok::kw_true:
520 case tok::kw_try:
521 case tok::kw_typedef:
522 case tok::kw_typeid:
523 case tok::kw_typename:
524 case tok::kw_typeof:
525 case tok::kw_union:
526 case tok::kw_unsigned:
527 case tok::kw_using:
528 case tok::kw_virtual:
529 case tok::kw_void:
530 case tok::kw_volatile:
531 case tok::kw_wchar_t:
532 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000533 case tok::kw__Bool:
534 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000535 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000536 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000537 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000538 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000539 }
Steve Naroff294494e2007-08-22 16:35:03 +0000540}
541
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000542/// property-attrlist: one of
543/// readonly getter setter assign retain copy nonatomic
544///
545bool Parser::isObjCPropertyAttribute() {
Chris Lattnerdf195262007-10-09 17:51:17 +0000546 if (Tok.is(tok::identifier)) {
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000547 const IdentifierInfo *II = Tok.getIdentifierInfo();
548 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000549 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000550 }
551 return false;
552}
553
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000554/// objc-for-collection-in: 'in'
555///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000556bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000557 // FIXME: May have to do additional look-ahead to only allow for
558 // valid tokens following an 'in'; such as an identifier, unary operators,
559 // '[' etc.
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000560 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000561 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000562}
563
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000564/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000565/// qualifier list and builds their bitmask representation in the input
566/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000567///
568/// objc-type-qualifiers:
569/// objc-type-qualifier
570/// objc-type-qualifiers objc-type-qualifier
571///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000572void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000573 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000574 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000575 return;
576
577 const IdentifierInfo *II = Tok.getIdentifierInfo();
578 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000579 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000580 continue;
581
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000582 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000583 switch (i) {
584 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000585 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
586 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
587 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
588 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
589 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
590 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000591 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000592 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000593 ConsumeToken();
594 II = 0;
595 break;
596 }
597
598 // If this wasn't a recognized qualifier, bail out.
599 if (II) return;
600 }
601}
602
603/// objc-type-name:
604/// '(' objc-type-qualifiers[opt] type-name ')'
605/// '(' objc-type-qualifiers[opt] ')'
606///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000607Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000608 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff294494e2007-08-22 16:35:03 +0000609
610 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattnere8904e92008-08-23 01:48:03 +0000611 SourceLocation TypeStartLoc = Tok.getLocation();
Chris Lattner271f1a62007-09-27 15:15:46 +0000612 TypeTy *Ty = 0;
Steve Naroff294494e2007-08-22 16:35:03 +0000613
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000614 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000615 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000616
Steve Naroff294494e2007-08-22 16:35:03 +0000617 if (isTypeSpecifierQualifier()) {
Steve Narofff28b2642007-09-05 23:30:30 +0000618 Ty = ParseTypeName();
619 // FIXME: back when Sema support is in place...
620 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff294494e2007-08-22 16:35:03 +0000621 }
Chris Lattnere8904e92008-08-23 01:48:03 +0000622
Chris Lattnerdf195262007-10-09 17:51:17 +0000623 if (Tok.isNot(tok::r_paren)) {
Chris Lattnere8904e92008-08-23 01:48:03 +0000624 // If we didn't eat any tokens, then this isn't a type.
625 if (Tok.getLocation() == TypeStartLoc) {
626 Diag(Tok.getLocation(), diag::err_expected_type);
627 SkipUntil(tok::r_brace);
628 } else {
629 // Otherwise, we found *something*, but didn't get a ')' in the right
630 // place. Emit an error then return what we have as the type.
631 MatchRHSPunctuation(tok::r_paren, LParenLoc);
632 }
Steve Naroff294494e2007-08-22 16:35:03 +0000633 }
634 RParenLoc = ConsumeParen();
Steve Narofff28b2642007-09-05 23:30:30 +0000635 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000636}
637
638/// objc-method-decl:
639/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000640/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000641/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000642/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000643///
644/// objc-keyword-selector:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000645/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000646/// objc-keyword-selector objc-keyword-decl
647///
648/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000649/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
650/// objc-selector ':' objc-keyword-attributes[opt] identifier
651/// ':' objc-type-name objc-keyword-attributes[opt] identifier
652/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000653///
Steve Naroff4985ace2007-08-22 18:35:33 +0000654/// objc-parmlist:
655/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000656///
Steve Naroff4985ace2007-08-22 18:35:33 +0000657/// objc-parms:
658/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000659///
Steve Naroff4985ace2007-08-22 18:35:33 +0000660/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000661/// , ...
662///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000663/// objc-keyword-attributes: [OBJC2]
664/// __attribute__((unused))
665///
Steve Naroffbef11852007-10-26 20:53:56 +0000666Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000667 tok::TokenKind mType,
668 DeclTy *IDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000669 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff68d331a2007-09-27 14:38:14 +0000670{
Chris Lattnere8904e92008-08-23 01:48:03 +0000671 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000672 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000673 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000674 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000675 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnere8904e92008-08-23 01:48:03 +0000676
Steve Naroffbef11852007-10-26 20:53:56 +0000677 SourceLocation selLoc;
678 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000679
680 if (!SelIdent) { // missing selector name.
681 Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
682 SourceRange(mLoc, Tok.getLocation()));
683 // Skip until we get a ; or {}.
684 SkipUntil(tok::r_brace);
685 return 0;
686 }
687
Chris Lattnerdf195262007-10-09 17:51:17 +0000688 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000689 // If attributes exist after the method, parse them.
690 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000691 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000692 MethodAttrs = ParseAttributes();
693
694 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
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 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerff384912007-10-07 02:00:24 +0000698 }
Steve Narofff28b2642007-09-05 23:30:30 +0000699
Steve Naroff68d331a2007-09-27 14:38:14 +0000700 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
701 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000702 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff68d331a2007-09-27 14:38:14 +0000703 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerff384912007-10-07 02:00:24 +0000704
705 Action::TypeTy *TypeInfo;
706 while (1) {
707 KeyIdents.push_back(SelIdent);
Steve Naroff68d331a2007-09-27 14:38:14 +0000708
Chris Lattnerff384912007-10-07 02:00:24 +0000709 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000710 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000711 Diag(Tok, diag::err_expected_colon);
712 break;
713 }
714 ConsumeToken(); // Eat the ':'.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000715 ObjCDeclSpec DSType;
Chris Lattnere8904e92008-08-23 01:48:03 +0000716 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000717 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerff384912007-10-07 02:00:24 +0000718 else
719 TypeInfo = 0;
720 KeyTypes.push_back(TypeInfo);
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000721 ArgTypeQuals.push_back(DSType);
Steve Narofff28b2642007-09-05 23:30:30 +0000722
Chris Lattnerff384912007-10-07 02:00:24 +0000723 // If attributes exist before the argument name, parse them.
Chris Lattnerdf195262007-10-09 17:51:17 +0000724 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000725 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000726
Chris Lattnerdf195262007-10-09 17:51:17 +0000727 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000728 Diag(Tok, diag::err_expected_ident); // missing argument name.
729 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000730 }
Chris Lattnerff384912007-10-07 02:00:24 +0000731 ArgNames.push_back(Tok.getIdentifierInfo());
732 ConsumeToken(); // Eat the identifier.
Steve Naroff29238a02007-10-05 18:42:47 +0000733
Chris Lattnerff384912007-10-07 02:00:24 +0000734 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000735 SourceLocation Loc;
736 SelIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000737 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000738 break;
739 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000740 }
Chris Lattnerff384912007-10-07 02:00:24 +0000741
Steve Naroff335eafa2007-11-15 12:35:21 +0000742 bool isVariadic = false;
743
Chris Lattnerff384912007-10-07 02:00:24 +0000744 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000745 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000746 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000747 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000748 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000749 ConsumeToken();
750 break;
751 }
Steve Naroff335eafa2007-11-15 12:35:21 +0000752 // FIXME: implement this...
Chris Lattnerff384912007-10-07 02:00:24 +0000753 // Parse the c-style argument declaration-specifier.
754 DeclSpec DS;
755 ParseDeclarationSpecifiers(DS);
756 // Parse the declarator.
757 Declarator ParmDecl(DS, Declarator::PrototypeContext);
758 ParseDeclarator(ParmDecl);
759 }
760
761 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000762 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000763 AttributeList *MethodAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000764 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000765 MethodAttrs = ParseAttributes();
766
767 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
768 &KeyIdents[0]);
Steve Naroffbef11852007-10-26 20:53:56 +0000769 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000770 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000771 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff335eafa2007-11-15 12:35:21 +0000772 &ArgNames[0], MethodAttrs,
773 MethodImplKind, isVariadic);
Steve Naroff294494e2007-08-22 16:35:03 +0000774}
775
Steve Naroffdac269b2007-08-20 21:31:48 +0000776/// objc-protocol-refs:
777/// '<' identifier-list '>'
778///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000779bool Parser::
Chris Lattnere13b9592008-07-26 04:03:38 +0000780ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
781 bool WarnOnDeclarations, SourceLocation &EndLoc) {
782 assert(Tok.is(tok::less) && "expected <");
783
784 ConsumeToken(); // the "<"
785
786 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
787
788 while (1) {
789 if (Tok.isNot(tok::identifier)) {
790 Diag(Tok, diag::err_expected_ident);
791 SkipUntil(tok::greater);
792 return true;
793 }
794 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
795 Tok.getLocation()));
796 ConsumeToken();
797
798 if (Tok.isNot(tok::comma))
799 break;
800 ConsumeToken();
801 }
802
803 // Consume the '>'.
804 if (Tok.isNot(tok::greater)) {
805 Diag(Tok, diag::err_expected_greater);
806 return true;
807 }
808
809 EndLoc = ConsumeAnyToken();
810
811 // Convert the list of protocols identifiers into a list of protocol decls.
812 Actions.FindProtocolDeclaration(WarnOnDeclarations,
813 &ProtocolIdents[0], ProtocolIdents.size(),
814 Protocols);
815 return false;
816}
817
Steve Naroffdac269b2007-08-20 21:31:48 +0000818/// objc-class-instance-variables:
819/// '{' objc-instance-variable-decl-list[opt] '}'
820///
821/// objc-instance-variable-decl-list:
822/// objc-visibility-spec
823/// objc-instance-variable-decl ';'
824/// ';'
825/// objc-instance-variable-decl-list objc-visibility-spec
826/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
827/// objc-instance-variable-decl-list ';'
828///
829/// objc-visibility-spec:
830/// @private
831/// @protected
832/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000833/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000834///
835/// objc-instance-variable-decl:
836/// struct-declaration
837///
Steve Naroff60fccee2007-10-29 21:38:07 +0000838void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
839 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000840 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000841 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000842 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
843
Steve Naroffddbff782007-08-21 21:17:12 +0000844 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffddbff782007-08-21 21:17:12 +0000845
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000846 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000847 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000848 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000849 // Each iteration of this loop reads one objc-instance-variable-decl.
850
851 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000852 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000853 Diag(Tok, diag::ext_extra_struct_semi);
854 ConsumeToken();
855 continue;
856 }
Chris Lattnere1359422008-04-10 06:46:29 +0000857
Steve Naroffddbff782007-08-21 21:17:12 +0000858 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000859 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000860 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000861 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000862 case tok::objc_private:
863 case tok::objc_public:
864 case tok::objc_protected:
865 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000866 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000867 ConsumeToken();
868 continue;
869 default:
870 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000871 continue;
872 }
873 }
Chris Lattnere1359422008-04-10 06:46:29 +0000874
875 // Parse all the comma separated declarators.
876 DeclSpec DS;
877 FieldDeclarators.clear();
878 ParseStructDeclaration(DS, FieldDeclarators);
879
880 // Convert them all to fields.
881 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
882 FieldDeclarator &FD = FieldDeclarators[i];
883 // Install the declarator into interfaceDecl.
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000884 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattnere1359422008-04-10 06:46:29 +0000885 DS.getSourceRange().getBegin(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000886 FD.D, FD.BitfieldSize, visibility);
Chris Lattnere1359422008-04-10 06:46:29 +0000887 AllIvarDecls.push_back(Field);
Fariborz Jahanian7d6402f2007-09-13 20:56:13 +0000888 }
Steve Naroff3536b442007-09-06 21:24:23 +0000889
Chris Lattnerdf195262007-10-09 17:51:17 +0000890 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000891 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000892 } else {
893 Diag(Tok, diag::err_expected_semi_decl_list);
894 // Skip to end of block or statement
895 SkipUntil(tok::r_brace, true, true);
896 }
897 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000898 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000899 // Call ActOnFields() even if we don't have any decls. This is useful
900 // for code rewriting tools that need to be aware of the empty list.
901 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
902 &AllIvarDecls[0], AllIvarDecls.size(),
Fariborz Jahanian1d78cc42008-04-10 23:32:45 +0000903 LBraceLoc, RBraceLoc);
Steve Naroffddbff782007-08-21 21:17:12 +0000904 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000905}
Steve Naroffdac269b2007-08-20 21:31:48 +0000906
907/// objc-protocol-declaration:
908/// objc-protocol-definition
909/// objc-protocol-forward-reference
910///
911/// objc-protocol-definition:
912/// @protocol identifier
913/// objc-protocol-refs[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000914/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000915/// @end
916///
917/// objc-protocol-forward-reference:
918/// @protocol identifier-list ';'
919///
920/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000921/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000922/// semicolon in the first alternative if objc-protocol-refs are omitted.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000923Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000924 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000925 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
926 ConsumeToken(); // the "protocol" identifier
927
Chris Lattnerdf195262007-10-09 17:51:17 +0000928 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000929 Diag(Tok, diag::err_expected_ident); // missing protocol name.
930 return 0;
931 }
932 // Save the protocol name, then consume it.
933 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
934 SourceLocation nameLoc = ConsumeToken();
935
Chris Lattnerdf195262007-10-09 17:51:17 +0000936 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000937 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000938 ConsumeToken();
Chris Lattner7caeabd2008-07-21 22:17:28 +0000939 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000940 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000941
Chris Lattnerdf195262007-10-09 17:51:17 +0000942 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000943 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
944 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
945
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000946 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000947 while (1) {
948 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +0000949 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000950 Diag(Tok, diag::err_expected_ident);
951 SkipUntil(tok::semi);
952 return 0;
953 }
Chris Lattner7caeabd2008-07-21 22:17:28 +0000954 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
955 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000956 ConsumeToken(); // the identifier
957
Chris Lattnerdf195262007-10-09 17:51:17 +0000958 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000959 break;
960 }
961 // Consume the ';'.
962 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
963 return 0;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000964
Steve Naroffe440eb82007-10-10 17:32:04 +0000965 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroff37e58d12007-10-02 22:39:18 +0000966 &ProtocolRefs[0],
967 ProtocolRefs.size());
Chris Lattner7caeabd2008-07-21 22:17:28 +0000968 }
969
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000970 // Last, and definitely not least, parse a protocol declaration.
Chris Lattnere13b9592008-07-26 04:03:38 +0000971 SourceLocation EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000972
Chris Lattnere13b9592008-07-26 04:03:38 +0000973 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattner7caeabd2008-07-21 22:17:28 +0000974 if (Tok.is(tok::less) &&
Chris Lattnere13b9592008-07-26 04:03:38 +0000975 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattner7caeabd2008-07-21 22:17:28 +0000976 return 0;
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000977
Chris Lattnere13b9592008-07-26 04:03:38 +0000978 DeclTy *ProtoType =
979 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
980 &ProtocolRefs[0], ProtocolRefs.size(),
981 EndProtoLoc);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000982 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000983
984 // The @ sign was already consumed by ParseObjCInterfaceDeclList().
Steve Naroff861cf3e2007-08-23 18:16:40 +0000985 if (Tok.isObjCAtKeyword(tok::objc_end)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000986 ConsumeToken(); // the "end" identifier
Fariborz Jahanianab0aeb02007-10-08 18:53:38 +0000987 return ProtoType;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000988 }
989 Diag(Tok, diag::err_objc_missing_end);
Steve Naroffdac269b2007-08-20 21:31:48 +0000990 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000991}
Steve Naroffdac269b2007-08-20 21:31:48 +0000992
993/// objc-implementation:
994/// objc-class-implementation-prologue
995/// objc-category-implementation-prologue
996///
997/// objc-class-implementation-prologue:
998/// @implementation identifier objc-superclass[opt]
999/// objc-class-instance-variables[opt]
1000///
1001/// objc-category-implementation-prologue:
1002/// @implementation identifier ( identifier )
1003
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001004Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1005 SourceLocation atLoc) {
1006 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1007 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1008 ConsumeToken(); // the "implementation" identifier
1009
Chris Lattnerdf195262007-10-09 17:51:17 +00001010 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001011 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1012 return 0;
1013 }
1014 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001015 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001016 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1017
Chris Lattnerdf195262007-10-09 17:51:17 +00001018 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001019 // we have a category implementation.
1020 SourceLocation lparenLoc = ConsumeParen();
1021 SourceLocation categoryLoc, rparenLoc;
1022 IdentifierInfo *categoryId = 0;
1023
Chris Lattnerdf195262007-10-09 17:51:17 +00001024 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001025 categoryId = Tok.getIdentifierInfo();
1026 categoryLoc = ConsumeToken();
1027 } else {
1028 Diag(Tok, diag::err_expected_ident); // missing category name.
1029 return 0;
1030 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001031 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001032 Diag(Tok, diag::err_expected_rparen);
1033 SkipUntil(tok::r_paren, false); // don't stop at ';'
1034 return 0;
1035 }
1036 rparenLoc = ConsumeParen();
Steve Naroffe440eb82007-10-10 17:32:04 +00001037 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001038 atLoc, nameId, nameLoc, categoryId,
1039 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001040 ObjCImpDecl = ImplCatType;
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001041 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001042 }
1043 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001044 SourceLocation superClassLoc;
1045 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001046 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001047 // We have a super class
1048 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001049 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001050 Diag(Tok, diag::err_expected_ident); // missing super class name.
1051 return 0;
1052 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001053 superClassId = Tok.getIdentifierInfo();
1054 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001055 }
Steve Naroffe440eb82007-10-10 17:32:04 +00001056 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001057 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001058 superClassId, superClassLoc);
1059
Steve Naroff60fccee2007-10-29 21:38:07 +00001060 if (Tok.is(tok::l_brace)) // we have ivars
1061 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001062 ObjCImpDecl = ImplClsType;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001063
Fariborz Jahaniandb8f3d32007-11-10 20:59:13 +00001064 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001065}
Steve Naroff60fccee2007-10-29 21:38:07 +00001066
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001067Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1068 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1069 "ParseObjCAtEndDeclaration(): Expected @end");
1070 ConsumeToken(); // the "end" identifier
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001071 if (ObjCImpDecl)
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001072 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001073 else
1074 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001075 return ObjCImpDecl;
Steve Naroffdac269b2007-08-20 21:31:48 +00001076}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001077
1078/// compatibility-alias-decl:
1079/// @compatibility_alias alias-name class-name ';'
1080///
1081Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1082 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1083 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1084 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001085 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001086 Diag(Tok, diag::err_expected_ident);
1087 return 0;
1088 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001089 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1090 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001091 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001092 Diag(Tok, diag::err_expected_ident);
1093 return 0;
1094 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001095 IdentifierInfo *classId = Tok.getIdentifierInfo();
1096 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1097 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian8cd8c662007-09-04 21:42:12 +00001098 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001099 return 0;
1100 }
1101 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1102 aliasId, aliasLoc,
1103 classId, classLoc);
1104 return ClsType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001105}
1106
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001107/// property-synthesis:
1108/// @synthesize property-ivar-list ';'
1109///
1110/// property-ivar-list:
1111/// property-ivar
1112/// property-ivar-list ',' property-ivar
1113///
1114/// property-ivar:
1115/// identifier
1116/// identifier '=' identifier
1117///
1118Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1119 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1120 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001121 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001122 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001123 Diag(Tok, diag::err_expected_ident);
1124 return 0;
1125 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001126 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001127 IdentifierInfo *propertyIvar = 0;
1128 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1129 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001130 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001131 // property '=' ivar-name
1132 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001133 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001134 Diag(Tok, diag::err_expected_ident);
1135 break;
1136 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001137 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001138 ConsumeToken(); // consume ivar-name
1139 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001140 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1141 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001142 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001143 break;
1144 ConsumeToken(); // consume ','
1145 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001146 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001147 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1148 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001149}
1150
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001151/// property-dynamic:
1152/// @dynamic property-list
1153///
1154/// property-list:
1155/// identifier
1156/// property-list ',' identifier
1157///
1158Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1159 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1160 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1161 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001162 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001163 Diag(Tok, diag::err_expected_ident);
1164 return 0;
1165 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001166 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001167 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1168 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1169 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1170 propertyId, 0);
1171
Chris Lattnerdf195262007-10-09 17:51:17 +00001172 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001173 break;
1174 ConsumeToken(); // consume ','
1175 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001176 if (Tok.isNot(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001177 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1178 return 0;
1179}
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001180
1181/// objc-throw-statement:
1182/// throw expression[opt];
1183///
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001184Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1185 ExprResult Res;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001186 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001187 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001188 Res = ParseExpression();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001189 if (Res.isInvalid) {
1190 SkipUntil(tok::semi);
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001191 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001192 }
1193 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001194 ConsumeToken(); // consume ';'
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001195 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001196}
1197
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001198/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001199/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001200///
1201Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001202 ConsumeToken(); // consume synchronized
1203 if (Tok.isNot(tok::l_paren)) {
1204 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1205 return true;
1206 }
1207 ConsumeParen(); // '('
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001208 ExprResult Res = ParseExpression();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001209 if (Res.isInvalid) {
1210 SkipUntil(tok::semi);
1211 return true;
1212 }
1213 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001214 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001215 return true;
1216 }
1217 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001218 if (Tok.isNot(tok::l_brace)) {
1219 Diag (Tok, diag::err_expected_lbrace);
1220 return true;
1221 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001222 // Enter a scope to hold everything within the compound stmt. Compound
1223 // statements can always hold declarations.
1224 EnterScope(Scope::DeclScope);
1225
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001226 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff3ac438c2008-06-04 20:36:13 +00001227
1228 ExitScope();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001229 if (SynchBody.isInvalid)
1230 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1231 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001232}
1233
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001234/// objc-try-catch-statement:
1235/// @try compound-statement objc-catch-list[opt]
1236/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1237///
1238/// objc-catch-list:
1239/// @catch ( parameter-declaration ) compound-statement
1240/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1241/// catch-parameter-declaration:
1242/// parameter-declaration
1243/// '...' [OBJC2]
1244///
Chris Lattner6b884502008-03-10 06:06:04 +00001245Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001246 bool catch_or_finally_seen = false;
Steve Naroff64515f32008-02-05 21:27:35 +00001247
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001248 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001249 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001250 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001251 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001252 }
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001253 StmtResult CatchStmts;
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001254 StmtResult FinallyStmt;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001255 StmtResult TryBody = ParseCompoundStatementBody();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001256 if (TryBody.isInvalid)
1257 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner6b884502008-03-10 06:06:04 +00001258
Chris Lattnerdf195262007-10-09 17:51:17 +00001259 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001260 // At this point, we need to lookahead to determine if this @ is the start
1261 // of an @catch or @finally. We don't want to consume the @ token if this
1262 // is an @try or @encode or something else.
1263 Token AfterAt = GetLookAheadToken(1);
1264 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1265 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1266 break;
1267
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001268 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001269 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001270 StmtTy *FirstPart = 0;
1271 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001272 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001273 ConsumeParen();
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001274 EnterScope(Scope::DeclScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001275 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001276 DeclSpec DS;
1277 ParseDeclarationSpecifiers(DS);
Steve Naroff459a1e22008-06-03 05:36:54 +00001278 // For some odd reason, the name of the exception variable is
1279 // optional. As a result, we need to use PrototypeContext.
1280 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001281 ParseDeclarator(DeclaratorInfo);
Steve Naroff459a1e22008-06-03 05:36:54 +00001282 if (DeclaratorInfo.getIdentifier()) {
1283 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar914701e2008-08-05 16:28:08 +00001284 DeclaratorInfo, 0);
Steve Naroff459a1e22008-06-03 05:36:54 +00001285 StmtResult stmtResult =
1286 Actions.ActOnDeclStmt(aBlockVarDecl,
1287 DS.getSourceRange().getBegin(),
1288 DeclaratorInfo.getSourceRange().getEnd());
1289 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1290 }
Steve Naroff64515f32008-02-05 21:27:35 +00001291 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001292 ConsumeToken(); // consume '...'
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001293 SourceLocation RParenLoc = ConsumeParen();
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001294
1295 StmtResult CatchBody(true);
1296 if (Tok.is(tok::l_brace))
1297 CatchBody = ParseCompoundStatementBody();
1298 else
1299 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001300 if (CatchBody.isInvalid)
1301 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001302 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001303 FirstPart, CatchBody.Val, CatchStmts.Val);
1304 ExitScope();
Steve Naroff64515f32008-02-05 21:27:35 +00001305 } else {
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001306 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1307 "@catch clause");
Fariborz Jahanianb210bd02007-11-01 21:12:44 +00001308 return true;
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001309 }
1310 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001311 } else {
1312 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001313 ConsumeToken(); // consume finally
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001314
1315 StmtResult FinallyBody(true);
1316 if (Tok.is(tok::l_brace))
1317 FinallyBody = ParseCompoundStatementBody();
1318 else
1319 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001320 if (FinallyBody.isInvalid)
1321 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001322 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001323 FinallyBody.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001324 catch_or_finally_seen = true;
1325 break;
1326 }
1327 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001328 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001329 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001330 return true;
1331 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001332 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001333 FinallyStmt.Val);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001334}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001335
Steve Naroff3536b442007-09-06 21:24:23 +00001336/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001337///
Steve Naroff71c0a952007-11-13 23:01:27 +00001338Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001340 // parse optional ';'
Chris Lattnerdf195262007-10-09 17:51:17 +00001341 if (Tok.is(tok::semi))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001342 ConsumeToken();
1343
Steve Naroff409be832007-11-11 19:54:21 +00001344 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001345 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001346 Diag(Tok, diag::err_expected_method_body);
Steve Naroff409be832007-11-11 19:54:21 +00001347
1348 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1349 SkipUntil(tok::l_brace, true, true);
1350
1351 // If we didn't find the '{', bail out.
1352 if (Tok.isNot(tok::l_brace))
Steve Naroff71c0a952007-11-13 23:01:27 +00001353 return 0;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001354 }
Steve Naroff409be832007-11-11 19:54:21 +00001355 SourceLocation BraceLoc = Tok.getLocation();
1356
1357 // Enter a scope for the method body.
1358 EnterScope(Scope::FnScope|Scope::DeclScope);
1359
1360 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001361 // specified Declarator for the method.
1362 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff409be832007-11-11 19:54:21 +00001363
1364 StmtResult FnBody = ParseCompoundStatementBody();
1365
1366 // If the function body could not be parsed, make a bogus compoundstmt.
1367 if (FnBody.isInvalid)
1368 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1369
1370 // Leave the function body scope.
1371 ExitScope();
1372
1373 // TODO: Pass argument information.
Steve Naroff394f3f42008-07-25 17:57:26 +00001374 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff71c0a952007-11-13 23:01:27 +00001375 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001376}
Anders Carlsson55085182007-08-21 17:43:55 +00001377
Steve Naroff64515f32008-02-05 21:27:35 +00001378Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1379 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001380 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001381 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1382 return ParseObjCThrowStmt(AtLoc);
1383 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1384 return ParseObjCSynchronizedStmt(AtLoc);
1385 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1386 if (Res.isInvalid) {
1387 // If the expression is invalid, skip ahead to the next semicolon. Not
1388 // doing this opens us up to the possibility of infinite loops if
1389 // ParseExpression does not consume any tokens.
1390 SkipUntil(tok::semi);
1391 return true;
1392 }
1393 // Otherwise, eat the semicolon.
1394 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1395 return Actions.ActOnExprStmt(Res.Val);
1396}
1397
Steve Naroffa642beb2007-10-15 20:55:58 +00001398Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001399 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001400 case tok::string_literal: // primary-expression: string-literal
1401 case tok::wide_string_literal:
1402 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1403 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001404 if (Tok.getIdentifierInfo() == 0)
1405 return Diag(AtLoc, diag::err_unexpected_at);
1406
1407 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1408 case tok::objc_encode:
1409 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1410 case tok::objc_protocol:
1411 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1412 case tok::objc_selector:
1413 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1414 default:
1415 return Diag(AtLoc, diag::err_unexpected_at);
1416 }
Anders Carlsson55085182007-08-21 17:43:55 +00001417 }
Anders Carlsson55085182007-08-21 17:43:55 +00001418}
1419
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001420/// objc-message-expr:
1421/// '[' objc-receiver objc-message-args ']'
1422///
1423/// objc-receiver:
1424/// expression
1425/// class-name
1426/// type-name
Chris Lattner699b6612008-01-25 18:59:06 +00001427Parser::ExprResult Parser::ParseObjCMessageExpression() {
1428 assert(Tok.is(tok::l_square) && "'[' expected");
1429 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1430
1431 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001432 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001433 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1434 ConsumeToken();
1435 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1436 }
1437
Chris Lattner69d349a2008-09-19 17:44:00 +00001438 ExprResult Res = ParseExpression();
Chris Lattner699b6612008-01-25 18:59:06 +00001439 if (Res.isInvalid) {
Chris Lattner5c749422008-01-25 19:43:26 +00001440 SkipUntil(tok::r_square);
Chris Lattner699b6612008-01-25 18:59:06 +00001441 return Res;
1442 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001443
Chris Lattner699b6612008-01-25 18:59:06 +00001444 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1445}
1446
1447/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1448/// the rest of a message expression.
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001449///
1450/// objc-message-args:
1451/// objc-selector
1452/// objc-keywordarg-list
1453///
1454/// objc-keywordarg-list:
1455/// objc-keywordarg
1456/// objc-keywordarg-list objc-keywordarg
1457///
1458/// objc-keywordarg:
1459/// selector-name[opt] ':' objc-keywordexpr
1460///
1461/// objc-keywordexpr:
1462/// nonempty-expr-list
1463///
1464/// nonempty-expr-list:
1465/// assignment-expression
1466/// nonempty-expr-list , assignment-expression
1467///
Chris Lattner699b6612008-01-25 18:59:06 +00001468Parser::ExprResult
1469Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1470 IdentifierInfo *ReceiverName,
1471 ExprTy *ReceiverExpr) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001472 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001473 SourceLocation Loc;
1474 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001475
1476 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1477 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1478
Chris Lattnerdf195262007-10-09 17:51:17 +00001479 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001480 while (1) {
1481 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001482 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001483
Chris Lattnerdf195262007-10-09 17:51:17 +00001484 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001485 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001486 // We must manually skip to a ']', otherwise the expression skipper will
1487 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1488 // the enclosing expression.
1489 SkipUntil(tok::r_square);
Steve Naroff37387c92007-09-17 20:25:27 +00001490 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001491 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001492
Steve Naroff68d331a2007-09-27 14:38:14 +00001493 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001494 /// Parse the expression after ':'
Steve Naroff37387c92007-09-17 20:25:27 +00001495 ExprResult Res = ParseAssignmentExpression();
1496 if (Res.isInvalid) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001497 // We must manually skip to a ']', otherwise the expression skipper will
1498 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1499 // the enclosing expression.
1500 SkipUntil(tok::r_square);
Steve Naroff37387c92007-09-17 20:25:27 +00001501 return Res;
1502 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001503
Steve Naroff37387c92007-09-17 20:25:27 +00001504 // We have a valid expression.
Steve Naroff68d331a2007-09-27 14:38:14 +00001505 KeyExprs.push_back(Res.Val);
Steve Naroff37387c92007-09-17 20:25:27 +00001506
1507 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001508 selIdent = ParseObjCSelector(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001509 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001510 break;
1511 // We have a selector or a colon, continue parsing.
1512 }
1513 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001514 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001515 ConsumeToken(); // Eat the ','.
1516 /// Parse the expression after ','
1517 ExprResult Res = ParseAssignmentExpression();
1518 if (Res.isInvalid) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001519 // We must manually skip to a ']', otherwise the expression skipper will
1520 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1521 // the enclosing expression.
1522 SkipUntil(tok::r_square);
Steve Naroff49f109c2007-11-15 13:05:42 +00001523 return Res;
1524 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001525
Steve Naroff49f109c2007-11-15 13:05:42 +00001526 // We have a valid expression.
1527 KeyExprs.push_back(Res.Val);
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001528 }
1529 } else if (!selIdent) {
1530 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattner4fef81d2008-08-05 06:19:09 +00001531
1532 // We must manually skip to a ']', otherwise the expression skipper will
1533 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1534 // the enclosing expression.
1535 SkipUntil(tok::r_square);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001536 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001537 }
Chris Lattnerff384912007-10-07 02:00:24 +00001538
Chris Lattnerdf195262007-10-09 17:51:17 +00001539 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001540 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001541 // We must manually skip to a ']', otherwise the expression skipper will
1542 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1543 // the enclosing expression.
1544 SkipUntil(tok::r_square);
Fariborz Jahanian0ba0aa12008-01-02 18:09:46 +00001545 return true;
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001546 }
Chris Lattner4fef81d2008-08-05 06:19:09 +00001547
Chris Lattner699b6612008-01-25 18:59:06 +00001548 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff37387c92007-09-17 20:25:27 +00001549
Steve Naroff29238a02007-10-05 18:42:47 +00001550 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001551 if (nKeys == 0)
1552 KeyIdents.push_back(selIdent);
1553 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1554
1555 // We've just parsed a keyword message.
Steve Naroff37387c92007-09-17 20:25:27 +00001556 if (ReceiverName)
Fariborz Jahanian0523aaf2007-11-12 20:13:27 +00001557 return Actions.ActOnClassMessage(CurScope,
Chris Lattner699b6612008-01-25 18:59:06 +00001558 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001559 &KeyExprs[0], KeyExprs.size());
Chris Lattner699b6612008-01-25 18:59:06 +00001560 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff49f109c2007-11-15 13:05:42 +00001561 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001562}
1563
Steve Naroffbeaf2992007-11-03 11:27:19 +00001564Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001565 ExprResult Res = ParseStringLiteralExpression();
Anders Carlsson55085182007-08-21 17:43:55 +00001566 if (Res.isInvalid) return Res;
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001567
1568 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1569 // expressions. At this point, we know that the only valid thing that starts
1570 // with '@' is an @"".
1571 llvm::SmallVector<SourceLocation, 4> AtLocs;
1572 llvm::SmallVector<ExprTy*, 4> AtStrings;
1573 AtLocs.push_back(AtLoc);
1574 AtStrings.push_back(Res.Val);
1575
1576 while (Tok.is(tok::at)) {
1577 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001578
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001579 ExprResult Res(true); // Invalid unless there is a string literal.
1580 if (isTokenStringLiteral())
1581 Res = ParseStringLiteralExpression();
1582 else
1583 Diag(Tok, diag::err_objc_concat_string);
1584
1585 if (Res.isInvalid) {
1586 while (!AtStrings.empty()) {
1587 Actions.DeleteExpr(AtStrings.back());
1588 AtStrings.pop_back();
1589 }
1590 return Res;
1591 }
1592
1593 AtStrings.push_back(Res.Val);
1594 }
Fariborz Jahanian79a99f22007-12-12 23:55:49 +00001595
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001596 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1597 AtStrings.size());
Anders Carlsson55085182007-08-21 17:43:55 +00001598}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001599
1600/// objc-encode-expression:
1601/// @encode ( type-name )
Chris Lattner674af952007-10-16 22:51:17 +00001602Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001603 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001604
1605 SourceLocation EncLoc = ConsumeToken();
1606
Chris Lattner4fef81d2008-08-05 06:19:09 +00001607 if (Tok.isNot(tok::l_paren))
1608 return Diag(Tok, diag::err_expected_lparen_after, "@encode");
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001609
1610 SourceLocation LParenLoc = ConsumeParen();
1611
1612 TypeTy *Ty = ParseTypeName();
1613
Anders Carlsson4988ae32007-08-23 15:31:37 +00001614 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001615
Chris Lattner674af952007-10-16 22:51:17 +00001616 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson4988ae32007-08-23 15:31:37 +00001617 RParenLoc);
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001618}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001619
1620/// objc-protocol-expression
1621/// @protocol ( protocol-name )
1622
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001623Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001624{
1625 SourceLocation ProtoLoc = ConsumeToken();
1626
Chris Lattner4fef81d2008-08-05 06:19:09 +00001627 if (Tok.isNot(tok::l_paren))
1628 return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001629
1630 SourceLocation LParenLoc = ConsumeParen();
1631
Chris Lattner4fef81d2008-08-05 06:19:09 +00001632 if (Tok.isNot(tok::identifier))
1633 return Diag(Tok, diag::err_expected_ident);
1634
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001635 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001636 ConsumeToken();
1637
Anders Carlsson4988ae32007-08-23 15:31:37 +00001638 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001639
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001640 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1641 LParenLoc, RParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001642}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001643
1644/// objc-selector-expression
1645/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001646Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001647{
1648 SourceLocation SelectorLoc = ConsumeToken();
1649
Chris Lattner4fef81d2008-08-05 06:19:09 +00001650 if (Tok.isNot(tok::l_paren))
1651 return Diag(Tok, diag::err_expected_lparen_after, "@selector");
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001652
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001653 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001654 SourceLocation LParenLoc = ConsumeParen();
1655 SourceLocation sLoc;
1656 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001657 if (!SelIdent && Tok.isNot(tok::colon))
1658 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1659
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001660 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001661 unsigned nColons = 0;
1662 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001663 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001664 if (Tok.isNot(tok::colon))
1665 return Diag(Tok, diag::err_expected_colon);
1666
Chris Lattnercb53b362007-12-27 19:57:00 +00001667 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001668 ConsumeToken(); // Eat the ':'.
1669 if (Tok.is(tok::r_paren))
1670 break;
1671 // Check for another keyword selector.
1672 SourceLocation Loc;
1673 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001674 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001675 if (!SelIdent && Tok.isNot(tok::colon))
1676 break;
1677 }
Steve Naroff887407e2007-12-05 22:21:29 +00001678 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001679 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001680 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian2a35fa92007-10-16 23:21:02 +00001681 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001682 RParenLoc);
Gabor Greif58065b22007-10-19 15:38:32 +00001683 }