blob: eee3cac479f22cb245eb578f5db403d28c3193eb [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 Lattner727fb1f2008-10-20 07:22:18 +0000286 Diag(AtLoc, 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 Lattner727fb1f2008-10-20 07:22:18 +0000302 if (!getLang().ObjC2)
303 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
304
Chris Lattnere48b46b2008-10-20 05:46:22 +0000305 ObjCDeclSpec OCDS;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000306 // Parse property attribute list, if any.
Chris Lattner727fb1f2008-10-20 07:22:18 +0000307 if (Tok.is(tok::l_paren)) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000308 ParseObjCPropertyAttribute(OCDS);
Chris Lattner727fb1f2008-10-20 07:22:18 +0000309 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000310
Chris Lattnere48b46b2008-10-20 05:46:22 +0000311 // Parse all the comma separated declarators.
312 DeclSpec DS;
313 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
314 ParseStructDeclaration(DS, FieldDeclarators);
315
Chris Lattner9019ae52008-10-20 06:15:13 +0000316 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
317 tok::at);
318
Chris Lattnere48b46b2008-10-20 05:46:22 +0000319 // Convert them all to property declarations.
320 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
321 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerbf16a972008-10-20 06:33:53 +0000322 if (FD.D.getIdentifier() == 0) {
323 Diag(AtLoc, diag::err_objc_property_requires_field_name,
324 FD.D.getSourceRange());
325 continue;
326 }
327
Chris Lattnere48b46b2008-10-20 05:46:22 +0000328 // Install the property declarator into interfaceDecl.
Chris Lattnerbf16a972008-10-20 06:33:53 +0000329 IdentifierInfo *SelName =
330 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
331
Chris Lattnere48b46b2008-10-20 05:46:22 +0000332 Selector GetterSel =
Chris Lattnerbf16a972008-10-20 06:33:53 +0000333 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000334 IdentifierInfo *SetterName = OCDS.getSetterName();
335 if (!SetterName)
336 SetterName = constructSetterName(PP.getIdentifierTable(),
337 FD.D.getIdentifier());
338 Selector SetterSel =
339 PP.getSelectorTable().getUnarySelector(SetterName);
Chris Lattnerbf16a972008-10-20 06:33:53 +0000340 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
341 GetterSel, SetterSel,
342 MethodImplKind);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000343 allProperties.push_back(Property);
344 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000345 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000346 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000347 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000348
349 // We break out of the big loop in two cases: when we see @end or when we see
350 // EOF. In the former case, eat the @end. In the later case, emit an error.
351 if (Tok.isObjCAtKeyword(tok::objc_end))
352 ConsumeToken(); // the "end" identifier
353 else
354 Diag(Tok, diag::err_objc_missing_end);
355
Chris Lattnercba730b2008-10-20 05:57:40 +0000356 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000357 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000358 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
359 allMethods.empty() ? 0 : &allMethods[0],
360 allMethods.size(),
361 allProperties.empty() ? 0 : &allProperties[0],
362 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000363}
364
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000365/// Parse property attribute declarations.
366///
367/// property-attr-decl: '(' property-attrlist ')'
368/// property-attrlist:
369/// property-attribute
370/// property-attrlist ',' property-attribute
371/// property-attribute:
372/// getter '=' identifier
373/// setter '=' identifier ':'
374/// readonly
375/// readwrite
376/// assign
377/// retain
378/// copy
379/// nonatomic
380///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000381void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner35cd4b92008-10-20 07:00:43 +0000382 SourceLocation LHSLoc = ConsumeParen(); // consume '('
383
Chris Lattner1e5cc722008-10-20 07:15:22 +0000384 while (1) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000385 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner727fb1f2008-10-20 07:22:18 +0000386
387 // If this is not an identifier at all, bail out early.
388 if (II == 0) {
389 MatchRHSPunctuation(tok::r_paren, LHSLoc);
390 return;
391 }
392
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000393 // getter/setter require extra treatment.
Ted Kremenek42730c52008-01-07 19:49:32 +0000394 if (II == ObjCPropertyAttrs[objc_getter] ||
395 II == ObjCPropertyAttrs[objc_setter]) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000396 // skip getter/setter part.
397 SourceLocation loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000398 if (Tok.is(tok::equal)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000399 loc = ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000400 if (Tok.is(tok::identifier)) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000401 if (II == ObjCPropertyAttrs[objc_setter]) {
402 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000403 DS.setSetterName(Tok.getIdentifierInfo());
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000404 loc = ConsumeToken(); // consume method name
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000405 if (Tok.isNot(tok::colon)) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000406 Diag(loc, diag::err_expected_colon);
Chris Lattner1e5cc722008-10-20 07:15:22 +0000407 SkipUntil(tok::r_paren);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000408 return;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000409 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000410 } else {
Ted Kremenek42730c52008-01-07 19:49:32 +0000411 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000412 DS.setGetterName(Tok.getIdentifierInfo());
413 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000414 } else {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000415 Diag(loc, diag::err_expected_ident);
Chris Lattner1e5cc722008-10-20 07:15:22 +0000416 SkipUntil(tok::r_paren);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000417 return;
Chris Lattner847f5c12007-12-27 19:57:00 +0000418 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000419 }
420 else {
421 Diag(loc, diag::err_objc_expected_equal);
Chris Lattner1e5cc722008-10-20 07:15:22 +0000422 SkipUntil(tok::r_paren);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000423 return;
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000424 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000425 } else if (II == ObjCPropertyAttrs[objc_readonly])
Ted Kremenek42730c52008-01-07 19:49:32 +0000426 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
427 else if (II == ObjCPropertyAttrs[objc_assign])
428 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
429 else if (II == ObjCPropertyAttrs[objc_readwrite])
Chris Lattner727fb1f2008-10-20 07:22:18 +0000430 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Ted Kremenek42730c52008-01-07 19:49:32 +0000431 else if (II == ObjCPropertyAttrs[objc_retain])
432 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
433 else if (II == ObjCPropertyAttrs[objc_copy])
434 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
435 else if (II == ObjCPropertyAttrs[objc_nonatomic])
436 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner727fb1f2008-10-20 07:22:18 +0000437 else {
Chris Lattner1e5cc722008-10-20 07:15:22 +0000438 Diag(Tok.getLocation(), diag::err_objc_expected_property_attr,
439 II->getName());
440 SkipUntil(tok::r_paren);
441 return;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000442 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000443
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000444 ConsumeToken(); // consume last attribute token
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000445 if (Tok.is(tok::comma)) {
Chris Lattner35cd4b92008-10-20 07:00:43 +0000446 ConsumeToken();
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000447 continue;
448 }
Chris Lattner35cd4b92008-10-20 07:00:43 +0000449
450 if (Tok.is(tok::r_paren)) {
451 ConsumeParen();
452 return;
453 }
454
455 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000456 return;
457 }
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000458}
459
Steve Naroff81f1bba2007-09-06 21:24:23 +0000460/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000461/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000462/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000463///
464/// objc-instance-method: '-'
465/// objc-class-method: '+'
466///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000467/// objc-method-attributes: [OBJC2]
468/// __attribute__((deprecated))
469///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000470Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000471 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000472 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000473
474 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000475 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000476
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000477 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000478 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000479 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000480 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000481}
482
483/// objc-selector:
484/// identifier
485/// one of
486/// enum struct union if else while do for switch case default
487/// break continue return goto asm sizeof typeof __alignof
488/// unsigned long const short volatile signed restrict _Complex
489/// in out inout bycopy byref oneway int char float double void _Bool
490///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000491IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000492 switch (Tok.getKind()) {
493 default:
494 return 0;
495 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000496 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000497 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000498 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000499 case tok::kw_break:
500 case tok::kw_case:
501 case tok::kw_catch:
502 case tok::kw_char:
503 case tok::kw_class:
504 case tok::kw_const:
505 case tok::kw_const_cast:
506 case tok::kw_continue:
507 case tok::kw_default:
508 case tok::kw_delete:
509 case tok::kw_do:
510 case tok::kw_double:
511 case tok::kw_dynamic_cast:
512 case tok::kw_else:
513 case tok::kw_enum:
514 case tok::kw_explicit:
515 case tok::kw_export:
516 case tok::kw_extern:
517 case tok::kw_false:
518 case tok::kw_float:
519 case tok::kw_for:
520 case tok::kw_friend:
521 case tok::kw_goto:
522 case tok::kw_if:
523 case tok::kw_inline:
524 case tok::kw_int:
525 case tok::kw_long:
526 case tok::kw_mutable:
527 case tok::kw_namespace:
528 case tok::kw_new:
529 case tok::kw_operator:
530 case tok::kw_private:
531 case tok::kw_protected:
532 case tok::kw_public:
533 case tok::kw_register:
534 case tok::kw_reinterpret_cast:
535 case tok::kw_restrict:
536 case tok::kw_return:
537 case tok::kw_short:
538 case tok::kw_signed:
539 case tok::kw_sizeof:
540 case tok::kw_static:
541 case tok::kw_static_cast:
542 case tok::kw_struct:
543 case tok::kw_switch:
544 case tok::kw_template:
545 case tok::kw_this:
546 case tok::kw_throw:
547 case tok::kw_true:
548 case tok::kw_try:
549 case tok::kw_typedef:
550 case tok::kw_typeid:
551 case tok::kw_typename:
552 case tok::kw_typeof:
553 case tok::kw_union:
554 case tok::kw_unsigned:
555 case tok::kw_using:
556 case tok::kw_virtual:
557 case tok::kw_void:
558 case tok::kw_volatile:
559 case tok::kw_wchar_t:
560 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000561 case tok::kw__Bool:
562 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000563 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000564 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000565 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000566 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000567 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000568}
569
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000570/// objc-for-collection-in: 'in'
571///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000572bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000573 // FIXME: May have to do additional look-ahead to only allow for
574 // valid tokens following an 'in'; such as an identifier, unary operators,
575 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000576 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000577 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000578}
579
Ted Kremenek42730c52008-01-07 19:49:32 +0000580/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000581/// qualifier list and builds their bitmask representation in the input
582/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000583///
584/// objc-type-qualifiers:
585/// objc-type-qualifier
586/// objc-type-qualifiers objc-type-qualifier
587///
Ted Kremenek42730c52008-01-07 19:49:32 +0000588void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000589 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000590 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000591 return;
592
593 const IdentifierInfo *II = Tok.getIdentifierInfo();
594 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000595 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000596 continue;
597
Ted Kremenek42730c52008-01-07 19:49:32 +0000598 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000599 switch (i) {
600 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000601 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
602 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
603 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
604 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
605 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
606 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000607 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000608 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000609 ConsumeToken();
610 II = 0;
611 break;
612 }
613
614 // If this wasn't a recognized qualifier, bail out.
615 if (II) return;
616 }
617}
618
619/// objc-type-name:
620/// '(' objc-type-qualifiers[opt] type-name ')'
621/// '(' objc-type-qualifiers[opt] ')'
622///
Ted Kremenek42730c52008-01-07 19:49:32 +0000623Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000624 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000625
626 SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
Chris Lattnerb5769332008-08-23 01:48:03 +0000627 SourceLocation TypeStartLoc = Tok.getLocation();
Chris Lattner265c8172007-09-27 15:15:46 +0000628 TypeTy *Ty = 0;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000629
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000630 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000631 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000632
Steve Naroff0bbffd82007-08-22 16:35:03 +0000633 if (isTypeSpecifierQualifier()) {
Steve Naroff304ed392007-09-05 23:30:30 +0000634 Ty = ParseTypeName();
635 // FIXME: back when Sema support is in place...
636 // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000637 }
Chris Lattnerb5769332008-08-23 01:48:03 +0000638
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000639 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerb5769332008-08-23 01:48:03 +0000640 // If we didn't eat any tokens, then this isn't a type.
641 if (Tok.getLocation() == TypeStartLoc) {
642 Diag(Tok.getLocation(), diag::err_expected_type);
643 SkipUntil(tok::r_brace);
644 } else {
645 // Otherwise, we found *something*, but didn't get a ')' in the right
646 // place. Emit an error then return what we have as the type.
647 MatchRHSPunctuation(tok::r_paren, LParenLoc);
648 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000649 }
650 RParenLoc = ConsumeParen();
Steve Naroff304ed392007-09-05 23:30:30 +0000651 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000652}
653
654/// objc-method-decl:
655/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000656/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000657/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000658/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000659///
660/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000661/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000662/// objc-keyword-selector objc-keyword-decl
663///
664/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000665/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
666/// objc-selector ':' objc-keyword-attributes[opt] identifier
667/// ':' objc-type-name objc-keyword-attributes[opt] identifier
668/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000669///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000670/// objc-parmlist:
671/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000672///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000673/// objc-parms:
674/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000675///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000676/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000677/// , ...
678///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000679/// objc-keyword-attributes: [OBJC2]
680/// __attribute__((unused))
681///
Steve Naroff3774dd92007-10-26 20:53:56 +0000682Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000683 tok::TokenKind mType,
684 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000685 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000686{
Chris Lattnerb5769332008-08-23 01:48:03 +0000687 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000688 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000689 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000690 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000691 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000692
Steve Naroff3774dd92007-10-26 20:53:56 +0000693 SourceLocation selLoc;
694 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000695
696 if (!SelIdent) { // missing selector name.
697 Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
698 SourceRange(mLoc, Tok.getLocation()));
699 // Skip until we get a ; or {}.
700 SkipUntil(tok::r_brace);
701 return 0;
702 }
703
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000704 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000705 // If attributes exist after the method, parse them.
706 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000707 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000708 MethodAttrs = ParseAttributes();
709
710 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000711 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000712 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000713 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000714 }
Steve Naroff304ed392007-09-05 23:30:30 +0000715
Steve Naroff4ed9d662007-09-27 14:38:14 +0000716 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
717 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000718 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000719 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000720
721 Action::TypeTy *TypeInfo;
722 while (1) {
723 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000724
Chris Lattnerd031a452007-10-07 02:00:24 +0000725 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000726 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000727 Diag(Tok, diag::err_expected_colon);
728 break;
729 }
730 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000731 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000732 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000733 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000734 else
735 TypeInfo = 0;
736 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000737 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000738
Chris Lattnerd031a452007-10-07 02:00:24 +0000739 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000740 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000741 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000742
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000743 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000744 Diag(Tok, diag::err_expected_ident); // missing argument name.
745 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000746 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000747 ArgNames.push_back(Tok.getIdentifierInfo());
748 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000749
Chris Lattnerd031a452007-10-07 02:00:24 +0000750 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000751 SourceLocation Loc;
752 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000753 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000754 break;
755 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000756 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000757
Steve Naroff29fe7462007-11-15 12:35:21 +0000758 bool isVariadic = false;
759
Chris Lattnerd031a452007-10-07 02:00:24 +0000760 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000761 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000762 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000763 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000764 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000765 ConsumeToken();
766 break;
767 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000768 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000769 // Parse the c-style argument declaration-specifier.
770 DeclSpec DS;
771 ParseDeclarationSpecifiers(DS);
772 // Parse the declarator.
773 Declarator ParmDecl(DS, Declarator::PrototypeContext);
774 ParseDeclarator(ParmDecl);
775 }
776
777 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000778 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000779 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000780 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000781 MethodAttrs = ParseAttributes();
782
783 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
784 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000785 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000786 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000787 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000788 &ArgNames[0], MethodAttrs,
789 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000790}
791
Steve Narofffb367882007-08-20 21:31:48 +0000792/// objc-protocol-refs:
793/// '<' identifier-list '>'
794///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000795bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000796ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
797 bool WarnOnDeclarations, SourceLocation &EndLoc) {
798 assert(Tok.is(tok::less) && "expected <");
799
800 ConsumeToken(); // the "<"
801
802 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
803
804 while (1) {
805 if (Tok.isNot(tok::identifier)) {
806 Diag(Tok, diag::err_expected_ident);
807 SkipUntil(tok::greater);
808 return true;
809 }
810 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
811 Tok.getLocation()));
812 ConsumeToken();
813
814 if (Tok.isNot(tok::comma))
815 break;
816 ConsumeToken();
817 }
818
819 // Consume the '>'.
820 if (Tok.isNot(tok::greater)) {
821 Diag(Tok, diag::err_expected_greater);
822 return true;
823 }
824
825 EndLoc = ConsumeAnyToken();
826
827 // Convert the list of protocols identifiers into a list of protocol decls.
828 Actions.FindProtocolDeclaration(WarnOnDeclarations,
829 &ProtocolIdents[0], ProtocolIdents.size(),
830 Protocols);
831 return false;
832}
833
Steve Narofffb367882007-08-20 21:31:48 +0000834/// objc-class-instance-variables:
835/// '{' objc-instance-variable-decl-list[opt] '}'
836///
837/// objc-instance-variable-decl-list:
838/// objc-visibility-spec
839/// objc-instance-variable-decl ';'
840/// ';'
841/// objc-instance-variable-decl-list objc-visibility-spec
842/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
843/// objc-instance-variable-decl-list ';'
844///
845/// objc-visibility-spec:
846/// @private
847/// @protected
848/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000849/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000850///
851/// objc-instance-variable-decl:
852/// struct-declaration
853///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000854void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
855 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000856 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000857 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000858 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
859
Steve Naroffc4474992007-08-21 21:17:12 +0000860 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000861
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000862 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000863 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000864 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000865 // Each iteration of this loop reads one objc-instance-variable-decl.
866
867 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000868 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000869 Diag(Tok, diag::ext_extra_struct_semi);
870 ConsumeToken();
871 continue;
872 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000873
Steve Naroffc4474992007-08-21 21:17:12 +0000874 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000875 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000876 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000877 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000878 case tok::objc_private:
879 case tok::objc_public:
880 case tok::objc_protected:
881 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000882 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000883 ConsumeToken();
884 continue;
885 default:
886 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000887 continue;
888 }
889 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000890
891 // Parse all the comma separated declarators.
892 DeclSpec DS;
893 FieldDeclarators.clear();
894 ParseStructDeclaration(DS, FieldDeclarators);
895
896 // Convert them all to fields.
897 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
898 FieldDeclarator &FD = FieldDeclarators[i];
899 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000900 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000901 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000902 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000903 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000904 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000905
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000906 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000907 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000908 } else {
909 Diag(Tok, diag::err_expected_semi_decl_list);
910 // Skip to end of block or statement
911 SkipUntil(tok::r_brace, true, true);
912 }
913 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000914 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000915 // Call ActOnFields() even if we don't have any decls. This is useful
916 // for code rewriting tools that need to be aware of the empty list.
917 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
918 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000919 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000920 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000921}
Steve Narofffb367882007-08-20 21:31:48 +0000922
923/// objc-protocol-declaration:
924/// objc-protocol-definition
925/// objc-protocol-forward-reference
926///
927/// objc-protocol-definition:
928/// @protocol identifier
929/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000930/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000931/// @end
932///
933/// objc-protocol-forward-reference:
934/// @protocol identifier-list ';'
935///
936/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000937/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000938/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000939Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
940 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000941 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000942 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
943 ConsumeToken(); // the "protocol" identifier
944
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000945 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000946 Diag(Tok, diag::err_expected_ident); // missing protocol name.
947 return 0;
948 }
949 // Save the protocol name, then consume it.
950 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
951 SourceLocation nameLoc = ConsumeToken();
952
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000953 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000954 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000955 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000956 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000957 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000958
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000959 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000960 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
961 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
962
Steve Naroff72f17fb2007-08-22 22:17:26 +0000963 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000964 while (1) {
965 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000966 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000967 Diag(Tok, diag::err_expected_ident);
968 SkipUntil(tok::semi);
969 return 0;
970 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000971 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
972 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000973 ConsumeToken(); // the identifier
974
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000975 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000976 break;
977 }
978 // Consume the ';'.
979 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
980 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000981
Steve Naroff415c1832007-10-10 17:32:04 +0000982 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000983 &ProtocolRefs[0],
984 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000985 }
986
Steve Naroff72f17fb2007-08-22 22:17:26 +0000987 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000988 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000989
Chris Lattner2bdedd62008-07-26 04:03:38 +0000990 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000991 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000992 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000993 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000994
Chris Lattner2bdedd62008-07-26 04:03:38 +0000995 DeclTy *ProtoType =
996 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
997 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000998 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000999 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +00001000 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +00001001}
Steve Narofffb367882007-08-20 21:31:48 +00001002
1003/// objc-implementation:
1004/// objc-class-implementation-prologue
1005/// objc-category-implementation-prologue
1006///
1007/// objc-class-implementation-prologue:
1008/// @implementation identifier objc-superclass[opt]
1009/// objc-class-instance-variables[opt]
1010///
1011/// objc-category-implementation-prologue:
1012/// @implementation identifier ( identifier )
1013
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001014Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1015 SourceLocation atLoc) {
1016 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1017 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1018 ConsumeToken(); // the "implementation" identifier
1019
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001020 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001021 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1022 return 0;
1023 }
1024 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001025 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001026 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1027
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001028 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001029 // we have a category implementation.
1030 SourceLocation lparenLoc = ConsumeParen();
1031 SourceLocation categoryLoc, rparenLoc;
1032 IdentifierInfo *categoryId = 0;
1033
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001034 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001035 categoryId = Tok.getIdentifierInfo();
1036 categoryLoc = ConsumeToken();
1037 } else {
1038 Diag(Tok, diag::err_expected_ident); // missing category name.
1039 return 0;
1040 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001041 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001042 Diag(Tok, diag::err_expected_rparen);
1043 SkipUntil(tok::r_paren, false); // don't stop at ';'
1044 return 0;
1045 }
1046 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001047 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001048 atLoc, nameId, nameLoc, categoryId,
1049 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001050 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001051 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001052 }
1053 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001054 SourceLocation superClassLoc;
1055 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001056 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001057 // We have a super class
1058 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001059 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001060 Diag(Tok, diag::err_expected_ident); // missing super class name.
1061 return 0;
1062 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001063 superClassId = Tok.getIdentifierInfo();
1064 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001065 }
Steve Naroff415c1832007-10-10 17:32:04 +00001066 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001067 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001068 superClassId, superClassLoc);
1069
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001070 if (Tok.is(tok::l_brace)) // we have ivars
1071 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001072 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001073
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001074 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001075}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001076
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001077Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1078 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1079 "ParseObjCAtEndDeclaration(): Expected @end");
1080 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001081 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001082 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001083 else
1084 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001085 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001086}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001087
1088/// compatibility-alias-decl:
1089/// @compatibility_alias alias-name class-name ';'
1090///
1091Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1092 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1093 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1094 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001095 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001096 Diag(Tok, diag::err_expected_ident);
1097 return 0;
1098 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001099 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1100 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001101 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001102 Diag(Tok, diag::err_expected_ident);
1103 return 0;
1104 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001105 IdentifierInfo *classId = Tok.getIdentifierInfo();
1106 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1107 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian6c30fa62007-09-04 21:42:12 +00001108 Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001109 return 0;
1110 }
1111 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1112 aliasId, aliasLoc,
1113 classId, classLoc);
1114 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001115}
1116
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001117/// property-synthesis:
1118/// @synthesize property-ivar-list ';'
1119///
1120/// property-ivar-list:
1121/// property-ivar
1122/// property-ivar-list ',' property-ivar
1123///
1124/// property-ivar:
1125/// identifier
1126/// identifier '=' identifier
1127///
1128Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1129 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1130 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001131 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001132 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001133 Diag(Tok, diag::err_expected_ident);
1134 return 0;
1135 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001136 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001137 IdentifierInfo *propertyIvar = 0;
1138 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1139 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001140 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001141 // property '=' ivar-name
1142 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001143 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001144 Diag(Tok, diag::err_expected_ident);
1145 break;
1146 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001147 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001148 ConsumeToken(); // consume ivar-name
1149 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001150 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1151 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001152 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001153 break;
1154 ConsumeToken(); // consume ','
1155 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001156 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001157 Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1158 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001159}
1160
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001161/// property-dynamic:
1162/// @dynamic property-list
1163///
1164/// property-list:
1165/// identifier
1166/// property-list ',' identifier
1167///
1168Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1169 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1170 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1171 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001172 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001173 Diag(Tok, diag::err_expected_ident);
1174 return 0;
1175 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001176 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001177 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1178 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1179 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1180 propertyId, 0);
1181
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001182 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001183 break;
1184 ConsumeToken(); // consume ','
1185 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001186 if (Tok.isNot(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001187 Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1188 return 0;
1189}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001190
1191/// objc-throw-statement:
1192/// throw expression[opt];
1193///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001194Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1195 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001196 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001197 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001198 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001199 if (Res.isInvalid) {
1200 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001201 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001202 }
1203 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001204 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001205 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001206}
1207
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001208/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001209/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001210///
1211Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001212 ConsumeToken(); // consume synchronized
1213 if (Tok.isNot(tok::l_paren)) {
1214 Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1215 return true;
1216 }
1217 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001218 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001219 if (Res.isInvalid) {
1220 SkipUntil(tok::semi);
1221 return true;
1222 }
1223 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001224 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001225 return true;
1226 }
1227 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001228 if (Tok.isNot(tok::l_brace)) {
1229 Diag (Tok, diag::err_expected_lbrace);
1230 return true;
1231 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001232 // Enter a scope to hold everything within the compound stmt. Compound
1233 // statements can always hold declarations.
1234 EnterScope(Scope::DeclScope);
1235
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001236 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001237
1238 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001239 if (SynchBody.isInvalid)
1240 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1241 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001242}
1243
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001244/// objc-try-catch-statement:
1245/// @try compound-statement objc-catch-list[opt]
1246/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1247///
1248/// objc-catch-list:
1249/// @catch ( parameter-declaration ) compound-statement
1250/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1251/// catch-parameter-declaration:
1252/// parameter-declaration
1253/// '...' [OBJC2]
1254///
Chris Lattner80712392008-03-10 06:06:04 +00001255Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001256 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001257
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001258 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001259 if (Tok.isNot(tok::l_brace)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001260 Diag (Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001261 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001262 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001263 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001264 StmtResult FinallyStmt;
Ted Kremenekba849be2008-09-26 17:32:47 +00001265 EnterScope(Scope::DeclScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001266 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenekba849be2008-09-26 17:32:47 +00001267 ExitScope();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001268 if (TryBody.isInvalid)
1269 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001270
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001271 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001272 // At this point, we need to lookahead to determine if this @ is the start
1273 // of an @catch or @finally. We don't want to consume the @ token if this
1274 // is an @try or @encode or something else.
1275 Token AfterAt = GetLookAheadToken(1);
1276 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1277 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1278 break;
1279
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001280 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001281 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001282 StmtTy *FirstPart = 0;
1283 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001284 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001285 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001286 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001287 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001288 DeclSpec DS;
1289 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001290 // For some odd reason, the name of the exception variable is
1291 // optional. As a result, we need to use PrototypeContext.
1292 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001293 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001294 if (DeclaratorInfo.getIdentifier()) {
1295 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001296 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001297 StmtResult stmtResult =
1298 Actions.ActOnDeclStmt(aBlockVarDecl,
1299 DS.getSourceRange().getBegin(),
1300 DeclaratorInfo.getSourceRange().getEnd());
1301 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1302 }
Steve Naroffc949a462008-02-05 21:27:35 +00001303 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001304 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001305 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001306
1307 StmtResult CatchBody(true);
1308 if (Tok.is(tok::l_brace))
1309 CatchBody = ParseCompoundStatementBody();
1310 else
1311 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001312 if (CatchBody.isInvalid)
1313 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001314 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001315 FirstPart, CatchBody.Val, CatchStmts.Val);
1316 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001317 } else {
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001318 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1319 "@catch clause");
Fariborz Jahanian70952482007-11-01 21:12:44 +00001320 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001321 }
1322 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001323 } else {
1324 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001325 ConsumeToken(); // consume finally
Ted Kremenek3637b892008-09-26 00:31:16 +00001326 EnterScope(Scope::DeclScope);
1327
Chris Lattner8027be62008-02-14 19:27:54 +00001328
1329 StmtResult FinallyBody(true);
1330 if (Tok.is(tok::l_brace))
1331 FinallyBody = ParseCompoundStatementBody();
1332 else
1333 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001334 if (FinallyBody.isInvalid)
1335 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001336 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001337 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001338 catch_or_finally_seen = true;
Ted Kremenek3637b892008-09-26 00:31:16 +00001339 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001340 break;
1341 }
1342 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001343 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001344 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001345 return true;
1346 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001347 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001348 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001349}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001350
Steve Naroff81f1bba2007-09-06 21:24:23 +00001351/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001352///
Steve Naroff18c83382007-11-13 23:01:27 +00001353Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001354 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001355 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001356 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001357 ConsumeToken();
1358
Steve Naroff9191a9e82007-11-11 19:54:21 +00001359 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001360 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001361 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001362
1363 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1364 SkipUntil(tok::l_brace, true, true);
1365
1366 // If we didn't find the '{', bail out.
1367 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001368 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001369 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001370 SourceLocation BraceLoc = Tok.getLocation();
1371
1372 // Enter a scope for the method body.
1373 EnterScope(Scope::FnScope|Scope::DeclScope);
1374
1375 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001376 // specified Declarator for the method.
1377 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001378
1379 StmtResult FnBody = ParseCompoundStatementBody();
1380
1381 // If the function body could not be parsed, make a bogus compoundstmt.
1382 if (FnBody.isInvalid)
1383 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1384
1385 // Leave the function body scope.
1386 ExitScope();
1387
1388 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001389 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001390 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001391}
Anders Carlssona66cad42007-08-21 17:43:55 +00001392
Steve Naroffc949a462008-02-05 21:27:35 +00001393Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1394 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001395 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001396 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1397 return ParseObjCThrowStmt(AtLoc);
1398 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1399 return ParseObjCSynchronizedStmt(AtLoc);
1400 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1401 if (Res.isInvalid) {
1402 // If the expression is invalid, skip ahead to the next semicolon. Not
1403 // doing this opens us up to the possibility of infinite loops if
1404 // ParseExpression does not consume any tokens.
1405 SkipUntil(tok::semi);
1406 return true;
1407 }
1408 // Otherwise, eat the semicolon.
1409 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1410 return Actions.ActOnExprStmt(Res.Val);
1411}
1412
Steve Narofffb9dd752007-10-15 20:55:58 +00001413Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001414 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001415 case tok::string_literal: // primary-expression: string-literal
1416 case tok::wide_string_literal:
1417 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1418 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001419 if (Tok.getIdentifierInfo() == 0)
1420 return Diag(AtLoc, diag::err_unexpected_at);
1421
1422 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1423 case tok::objc_encode:
1424 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1425 case tok::objc_protocol:
1426 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1427 case tok::objc_selector:
1428 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1429 default:
1430 return Diag(AtLoc, diag::err_unexpected_at);
1431 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001432 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001433}
1434
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001435/// objc-message-expr:
1436/// '[' objc-receiver objc-message-args ']'
1437///
1438/// objc-receiver:
1439/// expression
1440/// class-name
1441/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001442Parser::ExprResult Parser::ParseObjCMessageExpression() {
1443 assert(Tok.is(tok::l_square) && "'[' expected");
1444 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1445
1446 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001447 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001448 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1449 ConsumeToken();
1450 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1451 }
1452
Chris Lattnerc185e1a2008-09-19 17:44:00 +00001453 ExprResult Res = ParseExpression();
Chris Lattnered27a532008-01-25 18:59:06 +00001454 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001455 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001456 return Res;
1457 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001458
Chris Lattnered27a532008-01-25 18:59:06 +00001459 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1460}
1461
1462/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1463/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001464///
1465/// objc-message-args:
1466/// objc-selector
1467/// objc-keywordarg-list
1468///
1469/// objc-keywordarg-list:
1470/// objc-keywordarg
1471/// objc-keywordarg-list objc-keywordarg
1472///
1473/// objc-keywordarg:
1474/// selector-name[opt] ':' objc-keywordexpr
1475///
1476/// objc-keywordexpr:
1477/// nonempty-expr-list
1478///
1479/// nonempty-expr-list:
1480/// assignment-expression
1481/// nonempty-expr-list , assignment-expression
1482///
Chris Lattnered27a532008-01-25 18:59:06 +00001483Parser::ExprResult
1484Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1485 IdentifierInfo *ReceiverName,
1486 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001487 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001488 SourceLocation Loc;
1489 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001490
1491 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1492 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1493
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001494 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001495 while (1) {
1496 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001497 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001498
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001499 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001500 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001501 // We must manually skip to a ']', otherwise the expression skipper will
1502 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1503 // the enclosing expression.
1504 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001505 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001506 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001507
Steve Naroff4ed9d662007-09-27 14:38:14 +00001508 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001509 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001510 ExprResult Res = ParseAssignmentExpression();
1511 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001512 // We must manually skip to a ']', otherwise the expression skipper will
1513 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1514 // the enclosing expression.
1515 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001516 return Res;
1517 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001518
Steve Naroff253118b2007-09-17 20:25:27 +00001519 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001520 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001521
1522 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001523 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001524 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001525 break;
1526 // We have a selector or a colon, continue parsing.
1527 }
1528 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001529 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001530 ConsumeToken(); // Eat the ','.
1531 /// Parse the expression after ','
1532 ExprResult Res = ParseAssignmentExpression();
1533 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001534 // We must manually skip to a ']', otherwise the expression skipper will
1535 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1536 // the enclosing expression.
1537 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001538 return Res;
1539 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001540
Steve Naroff9f176d12007-11-15 13:05:42 +00001541 // We have a valid expression.
1542 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001543 }
1544 } else if (!selIdent) {
1545 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001546
1547 // We must manually skip to a ']', otherwise the expression skipper will
1548 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1549 // the enclosing expression.
1550 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001551 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001552 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001553
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001554 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001555 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001556 // We must manually skip to a ']', otherwise the expression skipper will
1557 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1558 // the enclosing expression.
1559 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001560 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001561 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001562
Chris Lattnered27a532008-01-25 18:59:06 +00001563 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001564
Steve Narofff9e80db2007-10-05 18:42:47 +00001565 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001566 if (nKeys == 0)
1567 KeyIdents.push_back(selIdent);
1568 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1569
1570 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001571 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001572 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001573 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001574 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001575 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001576 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001577}
1578
Steve Naroff0add5d22007-11-03 11:27:19 +00001579Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001580 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001581 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001582
1583 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1584 // expressions. At this point, we know that the only valid thing that starts
1585 // with '@' is an @"".
1586 llvm::SmallVector<SourceLocation, 4> AtLocs;
1587 llvm::SmallVector<ExprTy*, 4> AtStrings;
1588 AtLocs.push_back(AtLoc);
1589 AtStrings.push_back(Res.Val);
1590
1591 while (Tok.is(tok::at)) {
1592 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001593
Chris Lattnerddd3e632007-12-12 01:04:12 +00001594 ExprResult Res(true); // Invalid unless there is a string literal.
1595 if (isTokenStringLiteral())
1596 Res = ParseStringLiteralExpression();
1597 else
1598 Diag(Tok, diag::err_objc_concat_string);
1599
1600 if (Res.isInvalid) {
1601 while (!AtStrings.empty()) {
1602 Actions.DeleteExpr(AtStrings.back());
1603 AtStrings.pop_back();
1604 }
1605 return Res;
1606 }
1607
1608 AtStrings.push_back(Res.Val);
1609 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001610
Chris Lattnerddd3e632007-12-12 01:04:12 +00001611 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1612 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001613}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001614
1615/// objc-encode-expression:
1616/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001617Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001618 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001619
1620 SourceLocation EncLoc = ConsumeToken();
1621
Chris Lattnerf9311a92008-08-05 06:19:09 +00001622 if (Tok.isNot(tok::l_paren))
1623 return Diag(Tok, diag::err_expected_lparen_after, "@encode");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001624
1625 SourceLocation LParenLoc = ConsumeParen();
1626
1627 TypeTy *Ty = ParseTypeName();
1628
Anders Carlsson92faeb82007-08-23 15:31:37 +00001629 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001630
Chris Lattnercfd61c82007-10-16 22:51:17 +00001631 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001632 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001633}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001634
1635/// objc-protocol-expression
1636/// @protocol ( protocol-name )
1637
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001638Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001639{
1640 SourceLocation ProtoLoc = ConsumeToken();
1641
Chris Lattnerf9311a92008-08-05 06:19:09 +00001642 if (Tok.isNot(tok::l_paren))
1643 return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001644
1645 SourceLocation LParenLoc = ConsumeParen();
1646
Chris Lattnerf9311a92008-08-05 06:19:09 +00001647 if (Tok.isNot(tok::identifier))
1648 return Diag(Tok, diag::err_expected_ident);
1649
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001650 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001651 ConsumeToken();
1652
Anders Carlsson92faeb82007-08-23 15:31:37 +00001653 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001654
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001655 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1656 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001657}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001658
1659/// objc-selector-expression
1660/// @selector '(' objc-keyword-selector ')'
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001661Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001662{
1663 SourceLocation SelectorLoc = ConsumeToken();
1664
Chris Lattnerf9311a92008-08-05 06:19:09 +00001665 if (Tok.isNot(tok::l_paren))
1666 return Diag(Tok, diag::err_expected_lparen_after, "@selector");
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001667
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001668 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001669 SourceLocation LParenLoc = ConsumeParen();
1670 SourceLocation sLoc;
1671 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001672 if (!SelIdent && Tok.isNot(tok::colon))
1673 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1674
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001675 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001676 unsigned nColons = 0;
1677 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001678 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001679 if (Tok.isNot(tok::colon))
1680 return Diag(Tok, diag::err_expected_colon);
1681
Chris Lattner847f5c12007-12-27 19:57:00 +00001682 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001683 ConsumeToken(); // Eat the ':'.
1684 if (Tok.is(tok::r_paren))
1685 break;
1686 // Check for another keyword selector.
1687 SourceLocation Loc;
1688 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001689 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001690 if (!SelIdent && Tok.isNot(tok::colon))
1691 break;
1692 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001693 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001694 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001695 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001696 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001697 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001698 }