blob: 81eacf6544cf61790f3eda1623d52933eeb56c16 [file] [log] [blame]
Ted Kremenek42730c52008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattner4b009652007-07-25 00:24:17 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff09a0c4c2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian06798362007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner4b009652007-07-25 00:24:17 +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 Naroff5b96e2e2007-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 Narofffb367882007-08-20 21:31:48 +000030Parser::DeclTy *Parser::ParseObjCAtDirectives() {
Chris Lattner4b009652007-07-25 00:24:17 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
32
Steve Naroff87c329f2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner818350c2008-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;
Chris Lattner4b009652007-07-25 00:24:17 +000054 }
55}
56
57///
58/// objc-class-declaration:
59/// '@' 'class' identifier-list ';'
60///
Steve Narofffb367882007-08-20 21:31:48 +000061Parser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattner4b009652007-07-25 00:24:17 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64
65 while (1) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Chris Lattner4b009652007-07-25 00:24:17 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Steve Narofffb367882007-08-20 21:31:48 +000069 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000070 }
Chris Lattner4b009652007-07-25 00:24:17 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
73
Chris Lattnera1d2bb72007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Chris Lattner4b009652007-07-25 00:24:17 +000075 break;
76
77 ConsumeToken();
78 }
79
80 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Steve Narofffb367882007-08-20 21:31:48 +000082 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +000083
Steve Naroff415c1832007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff81f1bba2007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Steve Narofffb367882007-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 Naroff87c329f2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Narofffb367882007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
121
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-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 Naroff72f17fb2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Narofffb367882007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
129
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Narofffb367882007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
134
Steve Naroffa7f62782007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroffa7f62782007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
141 return 0;
Steve Narofffb367882007-08-20 21:31:48 +0000142 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Narofffb367882007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
146 return 0;
147 }
148 rparenLoc = ConsumeParen();
Chris Lattner45142b92008-07-26 04:07:02 +0000149
Steve Narofffb367882007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Chris Lattner45142b92008-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 Narofffb367882007-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 Naroff415c1832007-10-10 17:32:04 +0000160 DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
Steve Naroff25aace82007-10-03 21:00:46 +0000161 nameId, nameLoc, categoryId, categoryLoc,
Steve Naroff667f1682007-10-30 13:30:57 +0000162 &ProtocolRefs[0], ProtocolRefs.size(),
Chris Lattner45142b92008-07-26 04:07:02 +0000163 EndProtoLoc);
Fariborz Jahanianf25220e2007-09-18 20:26:58 +0000164
165 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnera40577e2008-10-20 06:10:06 +0000166 return CategoryType;
Steve Narofffb367882007-08-20 21:31:48 +0000167 }
168 // Parse a class interface.
169 IdentifierInfo *superClassId = 0;
170 SourceLocation superClassLoc;
Steve Naroff72f17fb2007-08-22 22:17:26 +0000171
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000172 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Narofffb367882007-08-20 21:31:48 +0000173 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000174 if (Tok.isNot(tok::identifier)) {
Steve Narofffb367882007-08-20 21:31:48 +0000175 Diag(Tok, diag::err_expected_ident); // missing super class name.
176 return 0;
177 }
178 superClassId = Tok.getIdentifierInfo();
179 superClassLoc = ConsumeToken();
180 }
181 // Next, we need to check for any protocol references.
Chris Lattnerae1ae492008-07-26 04:13:19 +0000182 llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
183 SourceLocation EndProtoLoc;
184 if (Tok.is(tok::less) &&
185 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
186 return 0;
187
188 DeclTy *ClsType =
189 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
190 superClassId, superClassLoc,
191 &ProtocolRefs[0], ProtocolRefs.size(),
192 EndProtoLoc, attrList);
Steve Naroff304ed392007-09-05 23:30:30 +0000193
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000194 if (Tok.is(tok::l_brace))
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000195 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Narofffb367882007-08-20 21:31:48 +0000196
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000197 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnera40577e2008-10-20 06:10:06 +0000198 return ClsType;
Steve Narofffb367882007-08-20 21:31:48 +0000199}
200
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000201/// constructSetterName - Return the setter name for the given
202/// identifier, i.e. "set" + Name where the initial character of Name
203/// has been capitalized.
204static IdentifierInfo *constructSetterName(IdentifierTable &Idents,
205 const IdentifierInfo *Name) {
206 unsigned N = Name->getLength();
207 char *SelectorName = new char[3 + N];
208 memcpy(SelectorName, "set", 3);
209 memcpy(&SelectorName[3], Name->getName(), N);
210 SelectorName[3] = toupper(SelectorName[3]);
211
212 IdentifierInfo *Setter =
213 &Idents.get(SelectorName, &SelectorName[3 + N]);
214 delete[] SelectorName;
215 return Setter;
216}
217
Steve Narofffb367882007-08-20 21:31:48 +0000218/// objc-interface-decl-list:
219/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000220/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000221/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000222/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000223/// objc-interface-decl-list declaration
224/// objc-interface-decl-list ';'
225///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000226/// objc-method-requirement: [OBJC2]
227/// @required
228/// @optional
229///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000230void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000231 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8c945b12008-06-06 16:45:15 +0000232 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000233 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000234 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000235
Chris Lattnera40577e2008-10-20 06:10:06 +0000236 SourceLocation AtEndLoc;
237
Steve Naroff0bbffd82007-08-22 16:35:03 +0000238 while (1) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000239 // If this is a method prototype, parse it.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000240 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
241 DeclTy *methodPrototype =
242 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000243 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000244 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
245 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000246 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000247 continue;
248 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000249
Chris Lattnere48b46b2008-10-20 05:46:22 +0000250 // Ignore excess semicolons.
251 if (Tok.is(tok::semi)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000252 ConsumeToken();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000253 continue;
254 }
255
Chris Lattnera40577e2008-10-20 06:10:06 +0000256 // If we got to the end of the file, exit the loop.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000257 if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000258 break;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000259
260 // If we don't have an @ directive, parse it as a function definition.
261 if (Tok.isNot(tok::at)) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000262 // FIXME: as the name implies, this rule allows function definitions.
263 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000264 ParseDeclarationOrFunctionDefinition();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000265 continue;
266 }
267
268 // Otherwise, we have an @ directive, eat the @.
269 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnercba730b2008-10-20 05:57:40 +0000270 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000271
Chris Lattnercba730b2008-10-20 05:57:40 +0000272 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere48b46b2008-10-20 05:46:22 +0000273 AtEndLoc = AtLoc;
274 break;
Chris Lattnera40577e2008-10-20 06:10:06 +0000275 }
Chris Lattnere48b46b2008-10-20 05:46:22 +0000276
Chris Lattnera40577e2008-10-20 06:10:06 +0000277 // Eat the identifier.
278 ConsumeToken();
279
Chris Lattnercba730b2008-10-20 05:57:40 +0000280 switch (DirectiveKind) {
281 default:
Chris Lattnera40577e2008-10-20 06:10:06 +0000282 // FIXME: If someone forgets an @end on a protocol, this loop will
283 // continue to eat up tons of stuff and spew lots of nonsense errors. It
284 // would probably be better to bail out if we saw an @class or @interface
285 // or something like that.
Chris Lattnercba730b2008-10-20 05:57:40 +0000286 Diag(Tok, diag::err_objc_illegal_interface_qual);
Chris Lattnera40577e2008-10-20 06:10:06 +0000287 // Skip until we see an '@' or '}' or ';'.
Chris Lattnercba730b2008-10-20 05:57:40 +0000288 SkipUntil(tok::r_brace, tok::at);
289 break;
290
291 case tok::objc_required:
Chris Lattnercba730b2008-10-20 05:57:40 +0000292 case tok::objc_optional:
Chris Lattnercba730b2008-10-20 05:57:40 +0000293 // This is only valid on protocols.
Chris Lattnera40577e2008-10-20 06:10:06 +0000294 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere48b46b2008-10-20 05:46:22 +0000295 if (contextKey != tok::objc_protocol)
Chris Lattnera40577e2008-10-20 06:10:06 +0000296 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnercba730b2008-10-20 05:57:40 +0000297 else
Chris Lattnera40577e2008-10-20 06:10:06 +0000298 MethodImplKind = DirectiveKind;
Chris Lattnercba730b2008-10-20 05:57:40 +0000299 break;
300
301 case tok::objc_property:
Chris Lattnere48b46b2008-10-20 05:46:22 +0000302 ObjCDeclSpec OCDS;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000303 // Parse property attribute list, if any.
Chris Lattner35cd4b92008-10-20 07:00:43 +0000304 if (Tok.is(tok::l_paren))
Chris Lattnere48b46b2008-10-20 05:46:22 +0000305 ParseObjCPropertyAttribute(OCDS);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000306
Chris Lattnere48b46b2008-10-20 05:46:22 +0000307 // Parse all the comma separated declarators.
308 DeclSpec DS;
309 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
310 ParseStructDeclaration(DS, FieldDeclarators);
311
Chris Lattner9019ae52008-10-20 06:15:13 +0000312 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
313 tok::at);
314
Chris Lattnere48b46b2008-10-20 05:46:22 +0000315 // Convert them all to property declarations.
316 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
317 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerbf16a972008-10-20 06:33:53 +0000318 if (FD.D.getIdentifier() == 0) {
319 Diag(AtLoc, diag::err_objc_property_requires_field_name,
320 FD.D.getSourceRange());
321 continue;
322 }
323
Chris Lattnere48b46b2008-10-20 05:46:22 +0000324 // Install the property declarator into interfaceDecl.
Chris Lattnerbf16a972008-10-20 06:33:53 +0000325 IdentifierInfo *SelName =
326 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
327
Chris Lattnere48b46b2008-10-20 05:46:22 +0000328 Selector GetterSel =
Chris Lattnerbf16a972008-10-20 06:33:53 +0000329 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000330 IdentifierInfo *SetterName = OCDS.getSetterName();
331 if (!SetterName)
332 SetterName = constructSetterName(PP.getIdentifierTable(),
333 FD.D.getIdentifier());
334 Selector SetterSel =
335 PP.getSelectorTable().getUnarySelector(SetterName);
Chris Lattnerbf16a972008-10-20 06:33:53 +0000336 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
337 GetterSel, SetterSel,
338 MethodImplKind);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000339 allProperties.push_back(Property);
340 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000341 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000342 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000343 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000344
345 // We break out of the big loop in two cases: when we see @end or when we see
346 // EOF. In the former case, eat the @end. In the later case, emit an error.
347 if (Tok.isObjCAtKeyword(tok::objc_end))
348 ConsumeToken(); // the "end" identifier
349 else
350 Diag(Tok, diag::err_objc_missing_end);
351
Chris Lattnercba730b2008-10-20 05:57:40 +0000352 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000353 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000354 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
355 allMethods.empty() ? 0 : &allMethods[0],
356 allMethods.size(),
357 allProperties.empty() ? 0 : &allProperties[0],
358 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000359}
360
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000361/// Parse property attribute declarations.
362///
363/// property-attr-decl: '(' property-attrlist ')'
364/// property-attrlist:
365/// property-attribute
366/// property-attrlist ',' property-attribute
367/// property-attribute:
368/// getter '=' identifier
369/// setter '=' identifier ':'
370/// readonly
371/// readwrite
372/// assign
373/// retain
374/// copy
375/// nonatomic
376///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000377void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner35cd4b92008-10-20 07:00:43 +0000378 SourceLocation LHSLoc = ConsumeParen(); // consume '('
379
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000380 while (isObjCPropertyAttribute()) {
381 const IdentifierInfo *II = Tok.getIdentifierInfo();
382 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000383 if (II == ObjCPropertyAttrs[objc_getter] ||
384 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000385 // skip getter/setter part.
386 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000387 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000388 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000389 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000390 if (II == ObjCPropertyAttrs[objc_setter]) {
391 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000392 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000393 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000394 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000395 Diag(loc, diag::err_expected_colon);
396 SkipUntil(tok::r_paren,true,true);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000397 return;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000398 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000399 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000400 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000401 DS.setGetterName(Tok.getIdentifierInfo());
402 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000403 } else {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000404 Diag(loc, diag::err_expected_ident);
Chris Lattner847f5c12007-12-27 19:57:00 +0000405 SkipUntil(tok::r_paren,true,true);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000406 return;
Chris Lattner847f5c12007-12-27 19:57:00 +0000407 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000408 }
409 else {
410 Diag(loc, diag::err_objc_expected_equal);
411 SkipUntil(tok::r_paren,true,true);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000412 return;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000413 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000414 } else if (II == ObjCPropertyAttrs[objc_readonly])
Ted Kremenek42730c52008-01-07 19:49:32 +0000415 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
416 else if (II == ObjCPropertyAttrs[objc_assign])
417 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
418 else if (II == ObjCPropertyAttrs[objc_readwrite])
419 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
420 else if (II == ObjCPropertyAttrs[objc_retain])
421 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
422 else if (II == ObjCPropertyAttrs[objc_copy])
423 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
424 else if (II == ObjCPropertyAttrs[objc_nonatomic])
425 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000426
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000427 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000428 if (Tok.is(tok::comma)) {
Chris Lattner35cd4b92008-10-20 07:00:43 +0000429 ConsumeToken();
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000430 continue;
431 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000432
433 if (Tok.is(tok::r_paren)) {
434 ConsumeParen();
435 return;
436 }
437
438 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000439 return;
440 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000441}
442
Steve Naroff81f1bba2007-09-06 21:24:23 +0000443/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000444/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000445/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000446///
447/// objc-instance-method: '-'
448/// objc-class-method: '+'
449///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000450/// objc-method-attributes: [OBJC2]
451/// __attribute__((deprecated))
452///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000453Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000454 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000455 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000456
457 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000458 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000459
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000460 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000461 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000462 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000463 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000464}
465
466/// objc-selector:
467/// identifier
468/// one of
469/// enum struct union if else while do for switch case default
470/// break continue return goto asm sizeof typeof __alignof
471/// unsigned long const short volatile signed restrict _Complex
472/// in out inout bycopy byref oneway int char float double void _Bool
473///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000474IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000475 switch (Tok.getKind()) {
476 default:
477 return 0;
478 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000479 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000480 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000481 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000482 case tok::kw_break:
483 case tok::kw_case:
484 case tok::kw_catch:
485 case tok::kw_char:
486 case tok::kw_class:
487 case tok::kw_const:
488 case tok::kw_const_cast:
489 case tok::kw_continue:
490 case tok::kw_default:
491 case tok::kw_delete:
492 case tok::kw_do:
493 case tok::kw_double:
494 case tok::kw_dynamic_cast:
495 case tok::kw_else:
496 case tok::kw_enum:
497 case tok::kw_explicit:
498 case tok::kw_export:
499 case tok::kw_extern:
500 case tok::kw_false:
501 case tok::kw_float:
502 case tok::kw_for:
503 case tok::kw_friend:
504 case tok::kw_goto:
505 case tok::kw_if:
506 case tok::kw_inline:
507 case tok::kw_int:
508 case tok::kw_long:
509 case tok::kw_mutable:
510 case tok::kw_namespace:
511 case tok::kw_new:
512 case tok::kw_operator:
513 case tok::kw_private:
514 case tok::kw_protected:
515 case tok::kw_public:
516 case tok::kw_register:
517 case tok::kw_reinterpret_cast:
518 case tok::kw_restrict:
519 case tok::kw_return:
520 case tok::kw_short:
521 case tok::kw_signed:
522 case tok::kw_sizeof:
523 case tok::kw_static:
524 case tok::kw_static_cast:
525 case tok::kw_struct:
526 case tok::kw_switch:
527 case tok::kw_template:
528 case tok::kw_this:
529 case tok::kw_throw:
530 case tok::kw_true:
531 case tok::kw_try:
532 case tok::kw_typedef:
533 case tok::kw_typeid:
534 case tok::kw_typename:
535 case tok::kw_typeof:
536 case tok::kw_union:
537 case tok::kw_unsigned:
538 case tok::kw_using:
539 case tok::kw_virtual:
540 case tok::kw_void:
541 case tok::kw_volatile:
542 case tok::kw_wchar_t:
543 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000544 case tok::kw__Bool:
545 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000546 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000547 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000548 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000549 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000550 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000551}
552
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000553/// property-attrlist: one of
554/// readonly getter setter assign retain copy nonatomic
555///
556bool Parser::isObjCPropertyAttribute() {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000557 if (Tok.is(tok::identifier)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000558 const IdentifierInfo *II = Tok.getIdentifierInfo();
559 for (unsigned i = 0; i < objc_NumAttrs; ++i)
Ted Kremenek42730c52008-01-07 19:49:32 +0000560 if (II == ObjCPropertyAttrs[i]) return true;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000561 }
562 return false;
563}
564
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000565/// objc-for-collection-in: 'in'
566///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000567bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000568 // FIXME: May have to do additional look-ahead to only allow for
569 // valid tokens following an 'in'; such as an identifier, unary operators,
570 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000571 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000572 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000573}
574
Ted Kremenek42730c52008-01-07 19:49:32 +0000575/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000576/// qualifier list and builds their bitmask representation in the input
577/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000578///
579/// objc-type-qualifiers:
580/// objc-type-qualifier
581/// objc-type-qualifiers objc-type-qualifier
582///
Ted Kremenek42730c52008-01-07 19:49:32 +0000583void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000584 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000585 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000586 return;
587
588 const IdentifierInfo *II = Tok.getIdentifierInfo();
589 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000590 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000591 continue;
592
Ted Kremenek42730c52008-01-07 19:49:32 +0000593 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000594 switch (i) {
595 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000596 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
597 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
598 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
599 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
600 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
601 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000602 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000603 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000604 ConsumeToken();
605 II = 0;
606 break;
607 }
608
609 // If this wasn't a recognized qualifier, bail out.
610 if (II) return;
611 }
612}
613
614/// objc-type-name:
615/// '(' objc-type-qualifiers[opt] type-name ')'
616/// '(' objc-type-qualifiers[opt] ')'
617///
Ted Kremenek42730c52008-01-07 19:49:32 +0000618Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000619 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000620
621 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattnerb5769332008-08-23 01:48:03 +0000622 SourceLocation TypeStartLoc = Tok.getLocation();
Chris Lattner265c8172007-09-27 15:15:46 +0000623 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000624
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000625 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000626 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000627
Steve Naroff0bbffd82007-08-22 16:35:03 +0000628 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000629 Ty = ParseTypeName();
630 // FIXME: back when Sema support is in place...
631 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000632 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000633
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000634 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000635 // If we didn't eat any tokens, then this isn't a type.
636 if (Tok.getLocation() == TypeStartLoc) {
637 Diag(Tok.getLocation(), diag::err_expected_type);
638 SkipUntil(tok::r_brace);
639 } else {
640 // Otherwise, we found *something*, but didn't get a ')' in the right
641 // place. Emit an error then return what we have as the type.
642 MatchRHSPunctuation(tok::r_paren, LParenLoc);
643 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000644 }
645 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000646 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000647}
648
649/// objc-method-decl:
650/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000651/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000652/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000653/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000654///
655/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000656/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000657/// objc-keyword-selector objc-keyword-decl
658///
659/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000660/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
661/// objc-selector ':' objc-keyword-attributes[opt] identifier
662/// ':' objc-type-name objc-keyword-attributes[opt] identifier
663/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000664///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000665/// objc-parmlist:
666/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000667///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000668/// objc-parms:
669/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000670///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000671/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000672/// , ...
673///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000674/// objc-keyword-attributes: [OBJC2]
675/// __attribute__((unused))
676///
Steve Naroff3774dd92007-10-26 20:53:56 +0000677Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000678 tok::TokenKind mType,
679 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000680 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000681{
Chris Lattnerb5769332008-08-23 01:48:03 +0000682 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000683 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000684 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000685 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000686 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000687
Steve Naroff3774dd92007-10-26 20:53:56 +0000688 SourceLocation selLoc;
689 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000690
691 if (!SelIdent) { // missing selector name.
692 Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
693 SourceRange(mLoc, Tok.getLocation()));
694 // Skip until we get a ; or {}.
695 SkipUntil(tok::r_brace);
696 return 0;
697 }
698
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000699 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000700 // If attributes exist after the method, parse them.
701 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000702 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000703 MethodAttrs = ParseAttributes();
704
705 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000706 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000707 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000708 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000709 }
Steve Naroff304ed392007-09-05 23:30:30 +0000710
Steve Naroff4ed9d662007-09-27 14:38:14 +0000711 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
712 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000713 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000714 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000715
716 Action::TypeTy *TypeInfo;
717 while (1) {
718 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000719
Chris Lattnerd031a452007-10-07 02:00:24 +0000720 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000721 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000722 Diag(Tok, diag::err_expected_colon);
723 break;
724 }
725 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000726 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000727 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000728 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000729 else
730 TypeInfo = 0;
731 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000732 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000733
Chris Lattnerd031a452007-10-07 02:00:24 +0000734 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000735 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000736 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000737
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000738 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000739 Diag(Tok, diag::err_expected_ident); // missing argument name.
740 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000741 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000742 ArgNames.push_back(Tok.getIdentifierInfo());
743 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000744
Chris Lattnerd031a452007-10-07 02:00:24 +0000745 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000746 SourceLocation Loc;
747 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000748 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000749 break;
750 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000751 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000752
Steve Naroff29fe7462007-11-15 12:35:21 +0000753 bool isVariadic = false;
754
Chris Lattnerd031a452007-10-07 02:00:24 +0000755 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000756 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000757 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000758 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000759 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000760 ConsumeToken();
761 break;
762 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000763 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000764 // Parse the c-style argument declaration-specifier.
765 DeclSpec DS;
766 ParseDeclarationSpecifiers(DS);
767 // Parse the declarator.
768 Declarator ParmDecl(DS, Declarator::PrototypeContext);
769 ParseDeclarator(ParmDecl);
770 }
771
772 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000773 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000774 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000775 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000776 MethodAttrs = ParseAttributes();
777
778 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
779 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000780 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000781 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000782 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000783 &ArgNames[0], MethodAttrs,
784 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000785}
786
Steve Narofffb367882007-08-20 21:31:48 +0000787/// objc-protocol-refs:
788/// '<' identifier-list '>'
789///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000790bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000791ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
792 bool WarnOnDeclarations, SourceLocation &EndLoc) {
793 assert(Tok.is(tok::less) && "expected <");
794
795 ConsumeToken(); // the "<"
796
797 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
798
799 while (1) {
800 if (Tok.isNot(tok::identifier)) {
801 Diag(Tok, diag::err_expected_ident);
802 SkipUntil(tok::greater);
803 return true;
804 }
805 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
806 Tok.getLocation()));
807 ConsumeToken();
808
809 if (Tok.isNot(tok::comma))
810 break;
811 ConsumeToken();
812 }
813
814 // Consume the '>'.
815 if (Tok.isNot(tok::greater)) {
816 Diag(Tok, diag::err_expected_greater);
817 return true;
818 }
819
820 EndLoc = ConsumeAnyToken();
821
822 // Convert the list of protocols identifiers into a list of protocol decls.
823 Actions.FindProtocolDeclaration(WarnOnDeclarations,
824 &ProtocolIdents[0], ProtocolIdents.size(),
825 Protocols);
826 return false;
827}
828
Steve Narofffb367882007-08-20 21:31:48 +0000829/// objc-class-instance-variables:
830/// '{' objc-instance-variable-decl-list[opt] '}'
831///
832/// objc-instance-variable-decl-list:
833/// objc-visibility-spec
834/// objc-instance-variable-decl ';'
835/// ';'
836/// objc-instance-variable-decl-list objc-visibility-spec
837/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
838/// objc-instance-variable-decl-list ';'
839///
840/// objc-visibility-spec:
841/// @private
842/// @protected
843/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000844/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000845///
846/// objc-instance-variable-decl:
847/// struct-declaration
848///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000849void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
850 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000851 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000852 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000853 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
854
Steve Naroffc4474992007-08-21 21:17:12 +0000855 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000856
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000857 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000858 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000859 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000860 // Each iteration of this loop reads one objc-instance-variable-decl.
861
862 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000863 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000864 Diag(Tok, diag::ext_extra_struct_semi);
865 ConsumeToken();
866 continue;
867 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000868
Steve Naroffc4474992007-08-21 21:17:12 +0000869 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000870 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000871 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000872 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000873 case tok::objc_private:
874 case tok::objc_public:
875 case tok::objc_protected:
876 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000877 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000878 ConsumeToken();
879 continue;
880 default:
881 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000882 continue;
883 }
884 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000885
886 // Parse all the comma separated declarators.
887 DeclSpec DS;
888 FieldDeclarators.clear();
889 ParseStructDeclaration(DS, FieldDeclarators);
890
891 // Convert them all to fields.
892 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
893 FieldDeclarator &FD = FieldDeclarators[i];
894 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000895 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000896 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000897 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000898 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000899 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000900
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000901 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000902 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000903 } else {
904 Diag(Tok, diag::err_expected_semi_decl_list);
905 // Skip to end of block or statement
906 SkipUntil(tok::r_brace, true, true);
907 }
908 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000909 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000910 // Call ActOnFields() even if we don't have any decls. This is useful
911 // for code rewriting tools that need to be aware of the empty list.
912 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
913 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000914 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000915 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000916}
Steve Narofffb367882007-08-20 21:31:48 +0000917
918/// objc-protocol-declaration:
919/// objc-protocol-definition
920/// objc-protocol-forward-reference
921///
922/// objc-protocol-definition:
923/// @protocol identifier
924/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000925/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000926/// @end
927///
928/// objc-protocol-forward-reference:
929/// @protocol identifier-list ';'
930///
931/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000932/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000933/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000934Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
935 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000936 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000937 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
938 ConsumeToken(); // the "protocol" identifier
939
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000940 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000941 Diag(Tok, diag::err_expected_ident); // missing protocol name.
942 return 0;
943 }
944 // Save the protocol name, then consume it.
945 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
946 SourceLocation nameLoc = ConsumeToken();
947
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000948 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000949 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000950 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000951 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000952 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000953
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000954 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000955 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
956 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
957
Steve Naroff72f17fb2007-08-22 22:17:26 +0000958 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000959 while (1) {
960 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000961 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000962 Diag(Tok, diag::err_expected_ident);
963 SkipUntil(tok::semi);
964 return 0;
965 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000966 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
967 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000968 ConsumeToken(); // the identifier
969
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000970 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000971 break;
972 }
973 // Consume the ';'.
974 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
975 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000976
Steve Naroff415c1832007-10-10 17:32:04 +0000977 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000978 &ProtocolRefs[0],
979 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000980 }
981
Steve Naroff72f17fb2007-08-22 22:17:26 +0000982 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000983 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000984
Chris Lattner2bdedd62008-07-26 04:03:38 +0000985 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000986 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000987 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000988 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000989
Chris Lattner2bdedd62008-07-26 04:03:38 +0000990 DeclTy *ProtoType =
991 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
992 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000993 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000994 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +0000995 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +0000996}
Steve Narofffb367882007-08-20 21:31:48 +0000997
998/// objc-implementation:
999/// objc-class-implementation-prologue
1000/// objc-category-implementation-prologue
1001///
1002/// objc-class-implementation-prologue:
1003/// @implementation identifier objc-superclass[opt]
1004/// objc-class-instance-variables[opt]
1005///
1006/// objc-category-implementation-prologue:
1007/// @implementation identifier ( identifier )
1008
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001009Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1010 SourceLocation atLoc) {
1011 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1012 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1013 ConsumeToken(); // the "implementation" identifier
1014
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001015 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001016 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1017 return 0;
1018 }
1019 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001020 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001021 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1022
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001023 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001024 // we have a category implementation.
1025 SourceLocation lparenLoc = ConsumeParen();
1026 SourceLocation categoryLoc, rparenLoc;
1027 IdentifierInfo *categoryId = 0;
1028
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001029 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001030 categoryId = Tok.getIdentifierInfo();
1031 categoryLoc = ConsumeToken();
1032 } else {
1033 Diag(Tok, diag::err_expected_ident); // missing category name.
1034 return 0;
1035 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001036 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001037 Diag(Tok, diag::err_expected_rparen);
1038 SkipUntil(tok::r_paren, false); // don't stop at ';'
1039 return 0;
1040 }
1041 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001042 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001043 atLoc, nameId, nameLoc, categoryId,
1044 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001045 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001046 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001047 }
1048 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001049 SourceLocation superClassLoc;
1050 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001051 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001052 // We have a super class
1053 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001054 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001055 Diag(Tok, diag::err_expected_ident); // missing super class name.
1056 return 0;
1057 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001058 superClassId = Tok.getIdentifierInfo();
1059 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001060 }
Steve Naroff415c1832007-10-10 17:32:04 +00001061 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001062 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001063 superClassId, superClassLoc);
1064
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001065 if (Tok.is(tok::l_brace)) // we have ivars
1066 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001067 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001068
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001069 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001070}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001071
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001072Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1073 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1074 "ParseObjCAtEndDeclaration(): Expected @end");
1075 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001076 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001077 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001078 else
1079 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001080 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001081}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001082
1083/// compatibility-alias-decl:
1084/// @compatibility_alias alias-name class-name ';'
1085///
1086Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1087 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1088 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1089 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001090 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001091 Diag(Tok, diag::err_expected_ident);
1092 return 0;
1093 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001094 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1095 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001096 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001097 Diag(Tok, diag::err_expected_ident);
1098 return 0;
1099 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001100 IdentifierInfo *classId = Tok.getIdentifierInfo();
1101 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1102 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001103 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001104 return 0;
1105 }
1106 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1107 aliasId, aliasLoc,
1108 classId, classLoc);
1109 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001110}
1111
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001112/// property-synthesis:
1113/// @synthesize property-ivar-list ';'
1114///
1115/// property-ivar-list:
1116/// property-ivar
1117/// property-ivar-list ',' property-ivar
1118///
1119/// property-ivar:
1120/// identifier
1121/// identifier '=' identifier
1122///
1123Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1124 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1125 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001126 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001127 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001128 Diag(Tok, diag::err_expected_ident);
1129 return 0;
1130 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001131 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001132 IdentifierInfo *propertyIvar = 0;
1133 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1134 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001135 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001136 // property '=' ivar-name
1137 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001138 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001139 Diag(Tok, diag::err_expected_ident);
1140 break;
1141 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001142 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001143 ConsumeToken(); // consume ivar-name
1144 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001145 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1146 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001147 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001148 break;
1149 ConsumeToken(); // consume ','
1150 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001152 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1153 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001154}
1155
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001156/// property-dynamic:
1157/// @dynamic property-list
1158///
1159/// property-list:
1160/// identifier
1161/// property-list ',' identifier
1162///
1163Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1164 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1165 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1166 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001167 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001168 Diag(Tok, diag::err_expected_ident);
1169 return 0;
1170 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001171 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001172 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1173 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1174 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1175 propertyId, 0);
1176
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001177 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001178 break;
1179 ConsumeToken(); // consume ','
1180 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001181 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001182 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1183 return 0;
1184}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001185
1186/// objc-throw-statement:
1187/// throw expression[opt];
1188///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001189Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1190 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001191 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001192 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001193 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001194 if (Res.isInvalid) {
1195 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001196 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001197 }
1198 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001199 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001200 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001201}
1202
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001203/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001204/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001205///
1206Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001207 ConsumeToken(); // consume synchronized
1208 if (Tok.isNot(tok::l_paren)) {
1209 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1210 return true;
1211 }
1212 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001213 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001214 if (Res.isInvalid) {
1215 SkipUntil(tok::semi);
1216 return true;
1217 }
1218 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001219 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001220 return true;
1221 }
1222 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001223 if (Tok.isNot(tok::l_brace)) {
1224 Diag (Tok, diag::err_expected_lbrace);
1225 return true;
1226 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001227 // Enter a scope to hold everything within the compound stmt. Compound
1228 // statements can always hold declarations.
1229 EnterScope(Scope::DeclScope);
1230
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001231 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001232
1233 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001234 if (SynchBody.isInvalid)
1235 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1236 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001237}
1238
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001239/// objc-try-catch-statement:
1240/// @try compound-statement objc-catch-list[opt]
1241/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1242///
1243/// objc-catch-list:
1244/// @catch ( parameter-declaration ) compound-statement
1245/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1246/// catch-parameter-declaration:
1247/// parameter-declaration
1248/// '...' [OBJC2]
1249///
Chris Lattner80712392008-03-10 06:06:04 +00001250Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001251 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001252
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001253 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001254 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001255 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001256 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001257 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001258 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001259 StmtResult FinallyStmt;
Ted Kremenekba849be2008-09-26 17:32:47 +00001260 EnterScope(Scope::DeclScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001261 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenekba849be2008-09-26 17:32:47 +00001262 ExitScope();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001263 if (TryBody.isInvalid)
1264 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001265
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001266 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001267 // At this point, we need to lookahead to determine if this @ is the start
1268 // of an @catch or @finally. We don't want to consume the @ token if this
1269 // is an @try or @encode or something else.
1270 Token AfterAt = GetLookAheadToken(1);
1271 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1272 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1273 break;
1274
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001275 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001276 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001277 StmtTy *FirstPart = 0;
1278 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001279 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001280 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001281 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001282 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001283 DeclSpec DS;
1284 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001285 // For some odd reason, the name of the exception variable is
1286 // optional. As a result, we need to use PrototypeContext.
1287 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001288 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001289 if (DeclaratorInfo.getIdentifier()) {
1290 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001291 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001292 StmtResult stmtResult =
1293 Actions.ActOnDeclStmt(aBlockVarDecl,
1294 DS.getSourceRange().getBegin(),
1295 DeclaratorInfo.getSourceRange().getEnd());
1296 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1297 }
Steve Naroffc949a462008-02-05 21:27:35 +00001298 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001299 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001300 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001301
1302 StmtResult CatchBody(true);
1303 if (Tok.is(tok::l_brace))
1304 CatchBody = ParseCompoundStatementBody();
1305 else
1306 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001307 if (CatchBody.isInvalid)
1308 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001309 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001310 FirstPart, CatchBody.Val, CatchStmts.Val);
1311 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001312 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001313 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1314 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001315 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001316 }
1317 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001318 } else {
1319 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001320 ConsumeToken(); // consume finally
Ted Kremenek3637b892008-09-26 00:31:16 +00001321 EnterScope(Scope::DeclScope);
1322
Chris Lattner8027be62008-02-14 19:27:54 +00001323
1324 StmtResult FinallyBody(true);
1325 if (Tok.is(tok::l_brace))
1326 FinallyBody = ParseCompoundStatementBody();
1327 else
1328 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001329 if (FinallyBody.isInvalid)
1330 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001331 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001332 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001333 catch_or_finally_seen = true;
Ted Kremenek3637b892008-09-26 00:31:16 +00001334 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001335 break;
1336 }
1337 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001338 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001339 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001340 return true;
1341 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001342 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001343 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001344}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001345
Steve Naroff81f1bba2007-09-06 21:24:23 +00001346/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001347///
Steve Naroff18c83382007-11-13 23:01:27 +00001348Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001349 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001350 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001351 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001352 ConsumeToken();
1353
Steve Naroff9191a9e82007-11-11 19:54:21 +00001354 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001355 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001356 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001357
1358 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1359 SkipUntil(tok::l_brace, true, true);
1360
1361 // If we didn't find the '{', bail out.
1362 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001363 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001364 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001365 SourceLocation BraceLoc = Tok.getLocation();
1366
1367 // Enter a scope for the method body.
1368 EnterScope(Scope::FnScope|Scope::DeclScope);
1369
1370 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001371 // specified Declarator for the method.
1372 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001373
1374 StmtResult FnBody = ParseCompoundStatementBody();
1375
1376 // If the function body could not be parsed, make a bogus compoundstmt.
1377 if (FnBody.isInvalid)
1378 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1379
1380 // Leave the function body scope.
1381 ExitScope();
1382
1383 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001384 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001385 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001386}
Anders Carlssona66cad42007-08-21 17:43:55 +00001387
Steve Naroffc949a462008-02-05 21:27:35 +00001388Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1389 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001390 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001391 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1392 return ParseObjCThrowStmt(AtLoc);
1393 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1394 return ParseObjCSynchronizedStmt(AtLoc);
1395 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1396 if (Res.isInvalid) {
1397 // If the expression is invalid, skip ahead to the next semicolon. Not
1398 // doing this opens us up to the possibility of infinite loops if
1399 // ParseExpression does not consume any tokens.
1400 SkipUntil(tok::semi);
1401 return true;
1402 }
1403 // Otherwise, eat the semicolon.
1404 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1405 return Actions.ActOnExprStmt(Res.Val);
1406}
1407
Steve Narofffb9dd752007-10-15 20:55:58 +00001408Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001409 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001410 case tok::string_literal: // primary-expression: string-literal
1411 case tok::wide_string_literal:
1412 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1413 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001414 if (Tok.getIdentifierInfo() == 0)
1415 return Diag(AtLoc, diag::err_unexpected_at);
1416
1417 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1418 case tok::objc_encode:
1419 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1420 case tok::objc_protocol:
1421 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1422 case tok::objc_selector:
1423 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1424 default:
1425 return Diag(AtLoc, diag::err_unexpected_at);
1426 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001427 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001428}
1429
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001430/// objc-message-expr:
1431/// '[' objc-receiver objc-message-args ']'
1432///
1433/// objc-receiver:
1434/// expression
1435/// class-name
1436/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001437Parser::ExprResult Parser::ParseObjCMessageExpression() {
1438 assert(Tok.is(tok::l_square) && "'[' expected");
1439 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1440
1441 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001442 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001443 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1444 ConsumeToken();
1445 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1446 }
1447
Chris Lattnerc185e1a2008-09-19 17:44:00 +00001448 ExprResult Res = ParseExpression();
Chris Lattnered27a532008-01-25 18:59:06 +00001449 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001450 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001451 return Res;
1452 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001453
Chris Lattnered27a532008-01-25 18:59:06 +00001454 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1455}
1456
1457/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1458/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001459///
1460/// objc-message-args:
1461/// objc-selector
1462/// objc-keywordarg-list
1463///
1464/// objc-keywordarg-list:
1465/// objc-keywordarg
1466/// objc-keywordarg-list objc-keywordarg
1467///
1468/// objc-keywordarg:
1469/// selector-name[opt] ':' objc-keywordexpr
1470///
1471/// objc-keywordexpr:
1472/// nonempty-expr-list
1473///
1474/// nonempty-expr-list:
1475/// assignment-expression
1476/// nonempty-expr-list , assignment-expression
1477///
Chris Lattnered27a532008-01-25 18:59:06 +00001478Parser::ExprResult
1479Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1480 IdentifierInfo *ReceiverName,
1481 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001482 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001483 SourceLocation Loc;
1484 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001485
1486 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1487 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1488
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001489 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001490 while (1) {
1491 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001492 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001493
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001494 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001495 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001496 // We must manually skip to a ']', otherwise the expression skipper will
1497 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1498 // the enclosing expression.
1499 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001500 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001501 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001502
Steve Naroff4ed9d662007-09-27 14:38:14 +00001503 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001504 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001505 ExprResult Res = ParseAssignmentExpression();
1506 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001507 // We must manually skip to a ']', otherwise the expression skipper will
1508 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1509 // the enclosing expression.
1510 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001511 return Res;
1512 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001513
Steve Naroff253118b2007-09-17 20:25:27 +00001514 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001515 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001516
1517 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001518 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001519 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001520 break;
1521 // We have a selector or a colon, continue parsing.
1522 }
1523 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001524 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001525 ConsumeToken(); // Eat the ','.
1526 /// Parse the expression after ','
1527 ExprResult Res = ParseAssignmentExpression();
1528 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001529 // We must manually skip to a ']', otherwise the expression skipper will
1530 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1531 // the enclosing expression.
1532 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001533 return Res;
1534 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001535
Steve Naroff9f176d12007-11-15 13:05:42 +00001536 // We have a valid expression.
1537 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001538 }
1539 } else if (!selIdent) {
1540 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001541
1542 // We must manually skip to a ']', otherwise the expression skipper will
1543 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1544 // the enclosing expression.
1545 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001546 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001547 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001548
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001549 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001550 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001551 // We must manually skip to a ']', otherwise the expression skipper will
1552 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1553 // the enclosing expression.
1554 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001555 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001556 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001557
Chris Lattnered27a532008-01-25 18:59:06 +00001558 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001559
Steve Narofff9e80db2007-10-05 18:42:47 +00001560 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001561 if (nKeys == 0)
1562 KeyIdents.push_back(selIdent);
1563 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1564
1565 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001566 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001567 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001568 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001569 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001570 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001571 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001572}
1573
Steve Naroff0add5d22007-11-03 11:27:19 +00001574Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001575 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001576 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001577
1578 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1579 // expressions. At this point, we know that the only valid thing that starts
1580 // with '@' is an @"".
1581 llvm::SmallVector<SourceLocation, 4> AtLocs;
1582 llvm::SmallVector<ExprTy*, 4> AtStrings;
1583 AtLocs.push_back(AtLoc);
1584 AtStrings.push_back(Res.Val);
1585
1586 while (Tok.is(tok::at)) {
1587 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001588
Chris Lattnerddd3e632007-12-12 01:04:12 +00001589 ExprResult Res(true); // Invalid unless there is a string literal.
1590 if (isTokenStringLiteral())
1591 Res = ParseStringLiteralExpression();
1592 else
1593 Diag(Tok, diag::err_objc_concat_string);
1594
1595 if (Res.isInvalid) {
1596 while (!AtStrings.empty()) {
1597 Actions.DeleteExpr(AtStrings.back());
1598 AtStrings.pop_back();
1599 }
1600 return Res;
1601 }
1602
1603 AtStrings.push_back(Res.Val);
1604 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001605
Chris Lattnerddd3e632007-12-12 01:04:12 +00001606 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1607 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001608}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001609
1610/// objc-encode-expression:
1611/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001612Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001613 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001614
1615 SourceLocation EncLoc = ConsumeToken();
1616
Chris Lattnerf9311a92008-08-05 06:19:09 +00001617 if (Tok.isNot(tok::l_paren))
1618 return Diag(Tok, diag::err_expected_lparen_after, "@encode");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001619
1620 SourceLocation LParenLoc = ConsumeParen();
1621
1622 TypeTy *Ty = ParseTypeName();
1623
Anders Carlsson92faeb82007-08-23 15:31:37 +00001624 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001625
Chris Lattnercfd61c82007-10-16 22:51:17 +00001626 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001627 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001628}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001629
1630/// objc-protocol-expression
1631/// @protocol ( protocol-name )
1632
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001633Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001634{
1635 SourceLocation ProtoLoc = ConsumeToken();
1636
Chris Lattnerf9311a92008-08-05 06:19:09 +00001637 if (Tok.isNot(tok::l_paren))
1638 return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001639
1640 SourceLocation LParenLoc = ConsumeParen();
1641
Chris Lattnerf9311a92008-08-05 06:19:09 +00001642 if (Tok.isNot(tok::identifier))
1643 return Diag(Tok, diag::err_expected_ident);
1644
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001645 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001646 ConsumeToken();
1647
Anders Carlsson92faeb82007-08-23 15:31:37 +00001648 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001649
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001650 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1651 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001652}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001653
1654/// objc-selector-expression
1655/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001656Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001657{
1658 SourceLocation SelectorLoc = ConsumeToken();
1659
Chris Lattnerf9311a92008-08-05 06:19:09 +00001660 if (Tok.isNot(tok::l_paren))
1661 return Diag(Tok, diag::err_expected_lparen_after, "@selector");
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001662
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001663 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001664 SourceLocation LParenLoc = ConsumeParen();
1665 SourceLocation sLoc;
1666 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001667 if (!SelIdent && Tok.isNot(tok::colon))
1668 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1669
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001670 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001671 unsigned nColons = 0;
1672 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001673 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001674 if (Tok.isNot(tok::colon))
1675 return Diag(Tok, diag::err_expected_colon);
1676
Chris Lattner847f5c12007-12-27 19:57:00 +00001677 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001678 ConsumeToken(); // Eat the ':'.
1679 if (Tok.is(tok::r_paren))
1680 break;
1681 // Check for another keyword selector.
1682 SourceLocation Loc;
1683 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001684 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001685 if (!SelIdent && Tok.isNot(tok::colon))
1686 break;
1687 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001688 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001689 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001690 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001691 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001692 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001693 }