blob: f323d9fe234ced14e5c4ec7393f6b69298222d6d [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
Chris Lattner8f7db152008-11-19 07:37:42 +0000212 IdentifierInfo *Setter = &Idents.get(SelectorName, &SelectorName[3 + N]);
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000213 delete[] SelectorName;
214 return Setter;
215}
216
Steve Narofffb367882007-08-20 21:31:48 +0000217/// objc-interface-decl-list:
218/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000219/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000220/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000221/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000222/// objc-interface-decl-list declaration
223/// objc-interface-decl-list ';'
224///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000225/// objc-method-requirement: [OBJC2]
226/// @required
227/// @optional
228///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000229void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000230 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8c945b12008-06-06 16:45:15 +0000231 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000232 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000233 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000234
Chris Lattnera40577e2008-10-20 06:10:06 +0000235 SourceLocation AtEndLoc;
236
Steve Naroff0bbffd82007-08-22 16:35:03 +0000237 while (1) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000238 // If this is a method prototype, parse it.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000239 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
240 DeclTy *methodPrototype =
241 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000242 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000243 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
244 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000245 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000246 continue;
247 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000248
Chris Lattnere48b46b2008-10-20 05:46:22 +0000249 // Ignore excess semicolons.
250 if (Tok.is(tok::semi)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000251 ConsumeToken();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000252 continue;
253 }
254
Chris Lattnera40577e2008-10-20 06:10:06 +0000255 // If we got to the end of the file, exit the loop.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000256 if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000257 break;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000258
259 // If we don't have an @ directive, parse it as a function definition.
260 if (Tok.isNot(tok::at)) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000261 // FIXME: as the name implies, this rule allows function definitions.
262 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000263 ParseDeclarationOrFunctionDefinition();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000264 continue;
265 }
266
267 // Otherwise, we have an @ directive, eat the @.
268 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnercba730b2008-10-20 05:57:40 +0000269 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000270
Chris Lattnercba730b2008-10-20 05:57:40 +0000271 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere48b46b2008-10-20 05:46:22 +0000272 AtEndLoc = AtLoc;
273 break;
Chris Lattnera40577e2008-10-20 06:10:06 +0000274 }
Chris Lattnere48b46b2008-10-20 05:46:22 +0000275
Chris Lattnera40577e2008-10-20 06:10:06 +0000276 // Eat the identifier.
277 ConsumeToken();
278
Chris Lattnercba730b2008-10-20 05:57:40 +0000279 switch (DirectiveKind) {
280 default:
Chris Lattnera40577e2008-10-20 06:10:06 +0000281 // FIXME: If someone forgets an @end on a protocol, this loop will
282 // continue to eat up tons of stuff and spew lots of nonsense errors. It
283 // would probably be better to bail out if we saw an @class or @interface
284 // or something like that.
Chris Lattner727fb1f2008-10-20 07:22:18 +0000285 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnera40577e2008-10-20 06:10:06 +0000286 // Skip until we see an '@' or '}' or ';'.
Chris Lattnercba730b2008-10-20 05:57:40 +0000287 SkipUntil(tok::r_brace, tok::at);
288 break;
289
290 case tok::objc_required:
Chris Lattnercba730b2008-10-20 05:57:40 +0000291 case tok::objc_optional:
Chris Lattnercba730b2008-10-20 05:57:40 +0000292 // This is only valid on protocols.
Chris Lattnera40577e2008-10-20 06:10:06 +0000293 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere48b46b2008-10-20 05:46:22 +0000294 if (contextKey != tok::objc_protocol)
Chris Lattnera40577e2008-10-20 06:10:06 +0000295 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnercba730b2008-10-20 05:57:40 +0000296 else
Chris Lattnera40577e2008-10-20 06:10:06 +0000297 MethodImplKind = DirectiveKind;
Chris Lattnercba730b2008-10-20 05:57:40 +0000298 break;
299
300 case tok::objc_property:
Chris Lattner727fb1f2008-10-20 07:22:18 +0000301 if (!getLang().ObjC2)
302 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
303
Chris Lattnere48b46b2008-10-20 05:46:22 +0000304 ObjCDeclSpec OCDS;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000305 // Parse property attribute list, if any.
Chris Lattner22f9d262008-10-20 07:24:39 +0000306 if (Tok.is(tok::l_paren))
Chris Lattnere48b46b2008-10-20 05:46:22 +0000307 ParseObjCPropertyAttribute(OCDS);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000308
Chris Lattnere48b46b2008-10-20 05:46:22 +0000309 // Parse all the comma separated declarators.
310 DeclSpec DS;
311 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
312 ParseStructDeclaration(DS, FieldDeclarators);
313
Chris Lattner9019ae52008-10-20 06:15:13 +0000314 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
315 tok::at);
316
Chris Lattnere48b46b2008-10-20 05:46:22 +0000317 // Convert them all to property declarations.
318 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
319 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerbf16a972008-10-20 06:33:53 +0000320 if (FD.D.getIdentifier() == 0) {
Chris Lattner194e7002008-11-18 07:50:21 +0000321 Diag(AtLoc, diag::err_objc_property_requires_field_name)
322 << FD.D.getSourceRange();
Chris Lattnerbf16a972008-10-20 06:33:53 +0000323 continue;
324 }
325
Chris Lattnere48b46b2008-10-20 05:46:22 +0000326 // Install the property declarator into interfaceDecl.
Chris Lattnerbf16a972008-10-20 06:33:53 +0000327 IdentifierInfo *SelName =
328 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
329
Chris Lattnere48b46b2008-10-20 05:46:22 +0000330 Selector GetterSel =
Chris Lattnerbf16a972008-10-20 06:33:53 +0000331 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000332 IdentifierInfo *SetterName = OCDS.getSetterName();
333 if (!SetterName)
334 SetterName = constructSetterName(PP.getIdentifierTable(),
335 FD.D.getIdentifier());
336 Selector SetterSel =
337 PP.getSelectorTable().getUnarySelector(SetterName);
Chris Lattnerbf16a972008-10-20 06:33:53 +0000338 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
339 GetterSel, SetterSel,
340 MethodImplKind);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000341 allProperties.push_back(Property);
342 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000343 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000344 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000345 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000346
347 // We break out of the big loop in two cases: when we see @end or when we see
348 // EOF. In the former case, eat the @end. In the later case, emit an error.
349 if (Tok.isObjCAtKeyword(tok::objc_end))
350 ConsumeToken(); // the "end" identifier
351 else
352 Diag(Tok, diag::err_objc_missing_end);
353
Chris Lattnercba730b2008-10-20 05:57:40 +0000354 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000355 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000356 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
357 allMethods.empty() ? 0 : &allMethods[0],
358 allMethods.size(),
359 allProperties.empty() ? 0 : &allProperties[0],
360 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000361}
362
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000363/// Parse property attribute declarations.
364///
365/// property-attr-decl: '(' property-attrlist ')'
366/// property-attrlist:
367/// property-attribute
368/// property-attrlist ',' property-attribute
369/// property-attribute:
370/// getter '=' identifier
371/// setter '=' identifier ':'
372/// readonly
373/// readwrite
374/// assign
375/// retain
376/// copy
377/// nonatomic
378///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000379void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner22f9d262008-10-20 07:24:39 +0000380 assert(Tok.getKind() == tok::l_paren);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000381 SourceLocation LHSLoc = ConsumeParen(); // consume '('
382
Chris Lattner1e5cc722008-10-20 07:15:22 +0000383 while (1) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000384 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner727fb1f2008-10-20 07:22:18 +0000385
386 // If this is not an identifier at all, bail out early.
387 if (II == 0) {
388 MatchRHSPunctuation(tok::r_paren, LHSLoc);
389 return;
390 }
391
Chris Lattner9ba0b222008-10-20 07:37:22 +0000392 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
393
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000394 if (!strcmp(II->getName(), "readonly"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000395 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000396 else if (!strcmp(II->getName(), "assign"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000397 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000398 else if (!strcmp(II->getName(), "readwrite"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000399 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000400 else if (!strcmp(II->getName(), "retain"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000401 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000402 else if (!strcmp(II->getName(), "copy"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000403 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000404 else if (!strcmp(II->getName(), "nonatomic"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000405 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000406 else if (!strcmp(II->getName(), "getter") ||
407 !strcmp(II->getName(), "setter")) {
Chris Lattner2cc2b872008-10-20 07:39:53 +0000408 // getter/setter require extra treatment.
Chris Lattner9ba0b222008-10-20 07:37:22 +0000409 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
410 tok::r_paren))
Chris Lattner35cd4b92008-10-20 07:00:43 +0000411 return;
Chris Lattner9ba0b222008-10-20 07:37:22 +0000412
Chris Lattner22f9d262008-10-20 07:24:39 +0000413 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000414 Diag(Tok, diag::err_expected_ident);
Chris Lattner22f9d262008-10-20 07:24:39 +0000415 SkipUntil(tok::r_paren);
416 return;
417 }
418
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000419 if (II->getName()[0] == 's') {
Chris Lattner22f9d262008-10-20 07:24:39 +0000420 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
421 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000422 ConsumeToken(); // consume method name
423
424 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
425 tok::r_paren))
Chris Lattner22f9d262008-10-20 07:24:39 +0000426 return;
Chris Lattner22f9d262008-10-20 07:24:39 +0000427 } else {
428 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
429 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000430 ConsumeToken(); // consume method name
Chris Lattner22f9d262008-10-20 07:24:39 +0000431 }
Chris Lattner2cc2b872008-10-20 07:39:53 +0000432 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +0000433 Diag(AttrName, diag::err_objc_expected_property_attr) << II->getName();
Chris Lattner1e5cc722008-10-20 07:15:22 +0000434 SkipUntil(tok::r_paren);
435 return;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000436 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000437
Chris Lattner9ba0b222008-10-20 07:37:22 +0000438 if (Tok.isNot(tok::comma))
439 break;
Chris Lattner35cd4b92008-10-20 07:00:43 +0000440
Chris Lattner9ba0b222008-10-20 07:37:22 +0000441 ConsumeToken();
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000442 }
Chris Lattner9ba0b222008-10-20 07:37:22 +0000443
444 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000445}
446
Steve Naroff81f1bba2007-09-06 21:24:23 +0000447/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000448/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000449/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000450///
451/// objc-instance-method: '-'
452/// objc-class-method: '+'
453///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000454/// objc-method-attributes: [OBJC2]
455/// __attribute__((deprecated))
456///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000457Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000458 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000459 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000460
461 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000462 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000463
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000464 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000465 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000466 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000467 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000468}
469
470/// objc-selector:
471/// identifier
472/// one of
473/// enum struct union if else while do for switch case default
474/// break continue return goto asm sizeof typeof __alignof
475/// unsigned long const short volatile signed restrict _Complex
476/// in out inout bycopy byref oneway int char float double void _Bool
477///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000478IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000479 switch (Tok.getKind()) {
480 default:
481 return 0;
482 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000483 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000484 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000485 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000486 case tok::kw_break:
487 case tok::kw_case:
488 case tok::kw_catch:
489 case tok::kw_char:
490 case tok::kw_class:
491 case tok::kw_const:
492 case tok::kw_const_cast:
493 case tok::kw_continue:
494 case tok::kw_default:
495 case tok::kw_delete:
496 case tok::kw_do:
497 case tok::kw_double:
498 case tok::kw_dynamic_cast:
499 case tok::kw_else:
500 case tok::kw_enum:
501 case tok::kw_explicit:
502 case tok::kw_export:
503 case tok::kw_extern:
504 case tok::kw_false:
505 case tok::kw_float:
506 case tok::kw_for:
507 case tok::kw_friend:
508 case tok::kw_goto:
509 case tok::kw_if:
510 case tok::kw_inline:
511 case tok::kw_int:
512 case tok::kw_long:
513 case tok::kw_mutable:
514 case tok::kw_namespace:
515 case tok::kw_new:
516 case tok::kw_operator:
517 case tok::kw_private:
518 case tok::kw_protected:
519 case tok::kw_public:
520 case tok::kw_register:
521 case tok::kw_reinterpret_cast:
522 case tok::kw_restrict:
523 case tok::kw_return:
524 case tok::kw_short:
525 case tok::kw_signed:
526 case tok::kw_sizeof:
527 case tok::kw_static:
528 case tok::kw_static_cast:
529 case tok::kw_struct:
530 case tok::kw_switch:
531 case tok::kw_template:
532 case tok::kw_this:
533 case tok::kw_throw:
534 case tok::kw_true:
535 case tok::kw_try:
536 case tok::kw_typedef:
537 case tok::kw_typeid:
538 case tok::kw_typename:
539 case tok::kw_typeof:
540 case tok::kw_union:
541 case tok::kw_unsigned:
542 case tok::kw_using:
543 case tok::kw_virtual:
544 case tok::kw_void:
545 case tok::kw_volatile:
546 case tok::kw_wchar_t:
547 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000548 case tok::kw__Bool:
549 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000550 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000551 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000552 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000553 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000554 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000555}
556
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000557/// objc-for-collection-in: 'in'
558///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000559bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000560 // FIXME: May have to do additional look-ahead to only allow for
561 // valid tokens following an 'in'; such as an identifier, unary operators,
562 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000563 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000564 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000565}
566
Ted Kremenek42730c52008-01-07 19:49:32 +0000567/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000568/// qualifier list and builds their bitmask representation in the input
569/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000570///
571/// objc-type-qualifiers:
572/// objc-type-qualifier
573/// objc-type-qualifiers objc-type-qualifier
574///
Ted Kremenek42730c52008-01-07 19:49:32 +0000575void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000576 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000577 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000578 return;
579
580 const IdentifierInfo *II = Tok.getIdentifierInfo();
581 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000582 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000583 continue;
584
Ted Kremenek42730c52008-01-07 19:49:32 +0000585 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000586 switch (i) {
587 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000588 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
589 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
590 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
591 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
592 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
593 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000594 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000595 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000596 ConsumeToken();
597 II = 0;
598 break;
599 }
600
601 // If this wasn't a recognized qualifier, bail out.
602 if (II) return;
603 }
604}
605
606/// objc-type-name:
607/// '(' objc-type-qualifiers[opt] type-name ')'
608/// '(' objc-type-qualifiers[opt] ')'
609///
Ted Kremenek42730c52008-01-07 19:49:32 +0000610Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000611 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000612
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000613 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnerb5769332008-08-23 01:48:03 +0000614 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000615
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000616 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000617 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000618
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000619 TypeTy *Ty = 0;
620 if (isTypeSpecifierQualifier())
Steve Naroff304ed392007-09-05 23:30:30 +0000621 Ty = ParseTypeName();
Chris Lattnerb5769332008-08-23 01:48:03 +0000622
Steve Naroffc6235e82008-10-21 14:15:04 +0000623 if (Tok.is(tok::r_paren))
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000624 ConsumeParen();
625 else if (Tok.getLocation() == TypeStartLoc) {
626 // If we didn't eat any tokens, then this isn't a type.
Chris Lattnerf006a222008-11-18 07:48:38 +0000627 Diag(Tok, diag::err_expected_type);
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000628 SkipUntil(tok::r_paren);
629 } else {
630 // Otherwise, we found *something*, but didn't get a ')' in the right
631 // place. Emit an error then return what we have as the type.
632 MatchRHSPunctuation(tok::r_paren, LParenLoc);
633 }
Steve Naroff304ed392007-09-05 23:30:30 +0000634 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000635}
636
637/// objc-method-decl:
638/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000639/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000640/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000641/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000642///
643/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000644/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000645/// objc-keyword-selector objc-keyword-decl
646///
647/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000648/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
649/// objc-selector ':' objc-keyword-attributes[opt] identifier
650/// ':' objc-type-name objc-keyword-attributes[opt] identifier
651/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000652///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000653/// objc-parmlist:
654/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000655///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000656/// objc-parms:
657/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000658///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000659/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000660/// , ...
661///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000662/// objc-keyword-attributes: [OBJC2]
663/// __attribute__((unused))
664///
Steve Naroff3774dd92007-10-26 20:53:56 +0000665Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000666 tok::TokenKind mType,
667 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000668 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000669{
Chris Lattnerb5769332008-08-23 01:48:03 +0000670 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000671 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000672 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000673 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000674 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000675
Steve Naroff3774dd92007-10-26 20:53:56 +0000676 SourceLocation selLoc;
677 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000678
679 if (!SelIdent) { // missing selector name.
Chris Lattnerf006a222008-11-18 07:48:38 +0000680 Diag(Tok, diag::err_expected_selector_for_method)
681 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnerb5769332008-08-23 01:48:03 +0000682 // Skip until we get a ; or {}.
683 SkipUntil(tok::r_brace);
684 return 0;
685 }
686
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000687 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000688 // If attributes exist after the method, parse them.
689 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000690 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000691 MethodAttrs = ParseAttributes();
692
693 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000694 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000695 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000696 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000697 }
Steve Naroff304ed392007-09-05 23:30:30 +0000698
Steve Naroff4ed9d662007-09-27 14:38:14 +0000699 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
700 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000701 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000702 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000703
704 Action::TypeTy *TypeInfo;
705 while (1) {
706 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000707
Chris Lattnerd031a452007-10-07 02:00:24 +0000708 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000709 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000710 Diag(Tok, diag::err_expected_colon);
711 break;
712 }
713 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000714 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000715 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000716 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000717 else
718 TypeInfo = 0;
719 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000720 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000721
Chris Lattnerd031a452007-10-07 02:00:24 +0000722 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000723 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000724 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000725
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000726 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000727 Diag(Tok, diag::err_expected_ident); // missing argument name.
728 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000729 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000730 ArgNames.push_back(Tok.getIdentifierInfo());
731 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000732
Chris Lattnerd031a452007-10-07 02:00:24 +0000733 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000734 SourceLocation Loc;
735 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000736 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000737 break;
738 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000739 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000740
Steve Naroff29fe7462007-11-15 12:35:21 +0000741 bool isVariadic = false;
742
Chris Lattnerd031a452007-10-07 02:00:24 +0000743 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000744 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000745 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000746 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000747 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000748 ConsumeToken();
749 break;
750 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000751 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000752 // Parse the c-style argument declaration-specifier.
753 DeclSpec DS;
754 ParseDeclarationSpecifiers(DS);
755 // Parse the declarator.
756 Declarator ParmDecl(DS, Declarator::PrototypeContext);
757 ParseDeclarator(ParmDecl);
758 }
759
760 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000761 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000762 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000763 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000764 MethodAttrs = ParseAttributes();
765
766 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
767 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000768 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000769 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000770 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000771 &ArgNames[0], MethodAttrs,
772 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000773}
774
Steve Narofffb367882007-08-20 21:31:48 +0000775/// objc-protocol-refs:
776/// '<' identifier-list '>'
777///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000778bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000779ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
780 bool WarnOnDeclarations, SourceLocation &EndLoc) {
781 assert(Tok.is(tok::less) && "expected <");
782
783 ConsumeToken(); // the "<"
784
785 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
786
787 while (1) {
788 if (Tok.isNot(tok::identifier)) {
789 Diag(Tok, diag::err_expected_ident);
790 SkipUntil(tok::greater);
791 return true;
792 }
793 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
794 Tok.getLocation()));
795 ConsumeToken();
796
797 if (Tok.isNot(tok::comma))
798 break;
799 ConsumeToken();
800 }
801
802 // Consume the '>'.
803 if (Tok.isNot(tok::greater)) {
804 Diag(Tok, diag::err_expected_greater);
805 return true;
806 }
807
808 EndLoc = ConsumeAnyToken();
809
810 // Convert the list of protocols identifiers into a list of protocol decls.
811 Actions.FindProtocolDeclaration(WarnOnDeclarations,
812 &ProtocolIdents[0], ProtocolIdents.size(),
813 Protocols);
814 return false;
815}
816
Steve Narofffb367882007-08-20 21:31:48 +0000817/// objc-class-instance-variables:
818/// '{' objc-instance-variable-decl-list[opt] '}'
819///
820/// objc-instance-variable-decl-list:
821/// objc-visibility-spec
822/// objc-instance-variable-decl ';'
823/// ';'
824/// objc-instance-variable-decl-list objc-visibility-spec
825/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
826/// objc-instance-variable-decl-list ';'
827///
828/// objc-visibility-spec:
829/// @private
830/// @protected
831/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000832/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000833///
834/// objc-instance-variable-decl:
835/// struct-declaration
836///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000837void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
838 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000839 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000840 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000841 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
842
Steve Naroffc4474992007-08-21 21:17:12 +0000843 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000844
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000845 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000846 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000847 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000848 // Each iteration of this loop reads one objc-instance-variable-decl.
849
850 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000851 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000852 Diag(Tok, diag::ext_extra_struct_semi);
853 ConsumeToken();
854 continue;
855 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000856
Steve Naroffc4474992007-08-21 21:17:12 +0000857 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000858 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000859 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000860 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000861 case tok::objc_private:
862 case tok::objc_public:
863 case tok::objc_protected:
864 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000865 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000866 ConsumeToken();
867 continue;
868 default:
869 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000870 continue;
871 }
872 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000873
874 // Parse all the comma separated declarators.
875 DeclSpec DS;
876 FieldDeclarators.clear();
877 ParseStructDeclaration(DS, FieldDeclarators);
878
879 // Convert them all to fields.
880 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
881 FieldDeclarator &FD = FieldDeclarators[i];
882 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000883 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000884 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000885 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000886 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000887 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000888
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000889 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000890 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000891 } else {
892 Diag(Tok, diag::err_expected_semi_decl_list);
893 // Skip to end of block or statement
894 SkipUntil(tok::r_brace, true, true);
895 }
896 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000897 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000898 // Call ActOnFields() even if we don't have any decls. This is useful
899 // for code rewriting tools that need to be aware of the empty list.
900 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
901 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000902 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000903 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000904}
Steve Narofffb367882007-08-20 21:31:48 +0000905
906/// objc-protocol-declaration:
907/// objc-protocol-definition
908/// objc-protocol-forward-reference
909///
910/// objc-protocol-definition:
911/// @protocol identifier
912/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000913/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000914/// @end
915///
916/// objc-protocol-forward-reference:
917/// @protocol identifier-list ';'
918///
919/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000920/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000921/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000922Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
923 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000924 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000925 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
926 ConsumeToken(); // the "protocol" identifier
927
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000928 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000929 Diag(Tok, diag::err_expected_ident); // missing protocol name.
930 return 0;
931 }
932 // Save the protocol name, then consume it.
933 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
934 SourceLocation nameLoc = ConsumeToken();
935
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000936 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000937 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000938 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000939 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000940 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000941
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000942 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000943 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
944 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
945
Steve Naroff72f17fb2007-08-22 22:17:26 +0000946 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000947 while (1) {
948 ConsumeToken(); // the ','
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000949 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000950 Diag(Tok, diag::err_expected_ident);
951 SkipUntil(tok::semi);
952 return 0;
953 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000954 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
955 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000956 ConsumeToken(); // the identifier
957
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000958 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000959 break;
960 }
961 // Consume the ';'.
962 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
963 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000964
Steve Naroff415c1832007-10-10 17:32:04 +0000965 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000966 &ProtocolRefs[0],
967 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000968 }
969
Steve Naroff72f17fb2007-08-22 22:17:26 +0000970 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000971 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000972
Chris Lattner2bdedd62008-07-26 04:03:38 +0000973 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000974 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000975 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000976 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000977
Chris Lattner2bdedd62008-07-26 04:03:38 +0000978 DeclTy *ProtoType =
979 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
980 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000981 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000982 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +0000983 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +0000984}
Steve Narofffb367882007-08-20 21:31:48 +0000985
986/// objc-implementation:
987/// objc-class-implementation-prologue
988/// objc-category-implementation-prologue
989///
990/// objc-class-implementation-prologue:
991/// @implementation identifier objc-superclass[opt]
992/// objc-class-instance-variables[opt]
993///
994/// objc-category-implementation-prologue:
995/// @implementation identifier ( identifier )
996
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000997Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
998 SourceLocation atLoc) {
999 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1000 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1001 ConsumeToken(); // the "implementation" identifier
1002
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001003 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001004 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1005 return 0;
1006 }
1007 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001008 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001009 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1010
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001011 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001012 // we have a category implementation.
1013 SourceLocation lparenLoc = ConsumeParen();
1014 SourceLocation categoryLoc, rparenLoc;
1015 IdentifierInfo *categoryId = 0;
1016
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001017 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001018 categoryId = Tok.getIdentifierInfo();
1019 categoryLoc = ConsumeToken();
1020 } else {
1021 Diag(Tok, diag::err_expected_ident); // missing category name.
1022 return 0;
1023 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001024 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001025 Diag(Tok, diag::err_expected_rparen);
1026 SkipUntil(tok::r_paren, false); // don't stop at ';'
1027 return 0;
1028 }
1029 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001030 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001031 atLoc, nameId, nameLoc, categoryId,
1032 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001033 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001034 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001035 }
1036 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001037 SourceLocation superClassLoc;
1038 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001039 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001040 // We have a super class
1041 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001042 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001043 Diag(Tok, diag::err_expected_ident); // missing super class name.
1044 return 0;
1045 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001046 superClassId = Tok.getIdentifierInfo();
1047 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001048 }
Steve Naroff415c1832007-10-10 17:32:04 +00001049 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001050 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001051 superClassId, superClassLoc);
1052
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001053 if (Tok.is(tok::l_brace)) // we have ivars
1054 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001055 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001056
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001057 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001058}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001059
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001060Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1061 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1062 "ParseObjCAtEndDeclaration(): Expected @end");
1063 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001064 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001065 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001066 else
1067 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001068 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001069}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001070
1071/// compatibility-alias-decl:
1072/// @compatibility_alias alias-name class-name ';'
1073///
1074Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1075 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1076 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1077 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001078 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001079 Diag(Tok, diag::err_expected_ident);
1080 return 0;
1081 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001082 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1083 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001084 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001085 Diag(Tok, diag::err_expected_ident);
1086 return 0;
1087 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001088 IdentifierInfo *classId = Tok.getIdentifierInfo();
1089 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1090 if (Tok.isNot(tok::semi)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001091 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001092 return 0;
1093 }
1094 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1095 aliasId, aliasLoc,
1096 classId, classLoc);
1097 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001098}
1099
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001100/// property-synthesis:
1101/// @synthesize property-ivar-list ';'
1102///
1103/// property-ivar-list:
1104/// property-ivar
1105/// property-ivar-list ',' property-ivar
1106///
1107/// property-ivar:
1108/// identifier
1109/// identifier '=' identifier
1110///
1111Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1112 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1113 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001114 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001115 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001116 Diag(Tok, diag::err_expected_ident);
1117 return 0;
1118 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001119 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001120 IdentifierInfo *propertyIvar = 0;
1121 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1122 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001123 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001124 // property '=' ivar-name
1125 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001126 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001127 Diag(Tok, diag::err_expected_ident);
1128 break;
1129 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001130 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001131 ConsumeToken(); // consume ivar-name
1132 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001133 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1134 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001135 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001136 break;
1137 ConsumeToken(); // consume ','
1138 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001139 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001140 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001141 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001142}
1143
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001144/// property-dynamic:
1145/// @dynamic property-list
1146///
1147/// property-list:
1148/// identifier
1149/// property-list ',' identifier
1150///
1151Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1152 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1153 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1154 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001155 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001156 Diag(Tok, diag::err_expected_ident);
1157 return 0;
1158 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001159 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001160 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1161 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1162 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1163 propertyId, 0);
1164
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001165 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001166 break;
1167 ConsumeToken(); // consume ','
1168 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001169 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001170 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001171 return 0;
1172}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001173
1174/// objc-throw-statement:
1175/// throw expression[opt];
1176///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001177Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1178 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001179 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001180 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001181 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001182 if (Res.isInvalid) {
1183 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001184 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001185 }
1186 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001187 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001188 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001189}
1190
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001191/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001192/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001193///
1194Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001195 ConsumeToken(); // consume synchronized
1196 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001197 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001198 return true;
1199 }
1200 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001201 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001202 if (Res.isInvalid) {
1203 SkipUntil(tok::semi);
1204 return true;
1205 }
1206 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001207 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001208 return true;
1209 }
1210 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001211 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001212 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001213 return true;
1214 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001215 // Enter a scope to hold everything within the compound stmt. Compound
1216 // statements can always hold declarations.
1217 EnterScope(Scope::DeclScope);
1218
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001219 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001220
1221 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001222 if (SynchBody.isInvalid)
1223 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1224 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001225}
1226
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001227/// objc-try-catch-statement:
1228/// @try compound-statement objc-catch-list[opt]
1229/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1230///
1231/// objc-catch-list:
1232/// @catch ( parameter-declaration ) compound-statement
1233/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1234/// catch-parameter-declaration:
1235/// parameter-declaration
1236/// '...' [OBJC2]
1237///
Chris Lattner80712392008-03-10 06:06:04 +00001238Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001239 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001240
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001241 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001242 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001243 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001244 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001245 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001246 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001247 StmtResult FinallyStmt;
Ted Kremenekba849be2008-09-26 17:32:47 +00001248 EnterScope(Scope::DeclScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001249 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenekba849be2008-09-26 17:32:47 +00001250 ExitScope();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001251 if (TryBody.isInvalid)
1252 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001253
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001254 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001255 // At this point, we need to lookahead to determine if this @ is the start
1256 // of an @catch or @finally. We don't want to consume the @ token if this
1257 // is an @try or @encode or something else.
1258 Token AfterAt = GetLookAheadToken(1);
1259 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1260 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1261 break;
1262
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001263 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001264 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001265 StmtTy *FirstPart = 0;
1266 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001267 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001268 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001269 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001270 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001271 DeclSpec DS;
1272 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001273 // For some odd reason, the name of the exception variable is
1274 // optional. As a result, we need to use PrototypeContext.
1275 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001276 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001277 if (DeclaratorInfo.getIdentifier()) {
1278 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001279 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001280 StmtResult stmtResult =
1281 Actions.ActOnDeclStmt(aBlockVarDecl,
1282 DS.getSourceRange().getBegin(),
1283 DeclaratorInfo.getSourceRange().getEnd());
1284 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1285 }
Steve Naroffc949a462008-02-05 21:27:35 +00001286 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001287 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001288 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001289
1290 StmtResult CatchBody(true);
1291 if (Tok.is(tok::l_brace))
1292 CatchBody = ParseCompoundStatementBody();
1293 else
1294 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001295 if (CatchBody.isInvalid)
1296 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001297 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001298 FirstPart, CatchBody.Val, CatchStmts.Val);
1299 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001300 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +00001301 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1302 << "@catch clause";
Fariborz Jahanian70952482007-11-01 21:12:44 +00001303 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001304 }
1305 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001306 } else {
1307 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001308 ConsumeToken(); // consume finally
Ted Kremenek3637b892008-09-26 00:31:16 +00001309 EnterScope(Scope::DeclScope);
1310
Chris Lattner8027be62008-02-14 19:27:54 +00001311
1312 StmtResult FinallyBody(true);
1313 if (Tok.is(tok::l_brace))
1314 FinallyBody = ParseCompoundStatementBody();
1315 else
1316 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001317 if (FinallyBody.isInvalid)
1318 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001319 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001320 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001321 catch_or_finally_seen = true;
Ted Kremenek3637b892008-09-26 00:31:16 +00001322 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001323 break;
1324 }
1325 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001326 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001327 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001328 return true;
1329 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001330 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001331 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001332}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001333
Steve Naroff81f1bba2007-09-06 21:24:23 +00001334/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001335///
Steve Naroff18c83382007-11-13 23:01:27 +00001336Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001337 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001338 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001339 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001340 ConsumeToken();
1341
Steve Naroff9191a9e82007-11-11 19:54:21 +00001342 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001343 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001344 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001345
1346 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1347 SkipUntil(tok::l_brace, true, true);
1348
1349 // If we didn't find the '{', bail out.
1350 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001351 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001352 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001353 SourceLocation BraceLoc = Tok.getLocation();
1354
1355 // Enter a scope for the method body.
1356 EnterScope(Scope::FnScope|Scope::DeclScope);
1357
1358 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001359 // specified Declarator for the method.
1360 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001361
1362 StmtResult FnBody = ParseCompoundStatementBody();
1363
1364 // If the function body could not be parsed, make a bogus compoundstmt.
1365 if (FnBody.isInvalid)
1366 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1367
1368 // Leave the function body scope.
1369 ExitScope();
1370
1371 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001372 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001373 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001374}
Anders Carlssona66cad42007-08-21 17:43:55 +00001375
Steve Naroffc949a462008-02-05 21:27:35 +00001376Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1377 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001378 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001379 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1380 return ParseObjCThrowStmt(AtLoc);
1381 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1382 return ParseObjCSynchronizedStmt(AtLoc);
1383 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1384 if (Res.isInvalid) {
1385 // If the expression is invalid, skip ahead to the next semicolon. Not
1386 // doing this opens us up to the possibility of infinite loops if
1387 // ParseExpression does not consume any tokens.
1388 SkipUntil(tok::semi);
1389 return true;
1390 }
1391 // Otherwise, eat the semicolon.
1392 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1393 return Actions.ActOnExprStmt(Res.Val);
1394}
1395
Steve Narofffb9dd752007-10-15 20:55:58 +00001396Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001397 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001398 case tok::string_literal: // primary-expression: string-literal
1399 case tok::wide_string_literal:
1400 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1401 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001402 if (Tok.getIdentifierInfo() == 0)
1403 return Diag(AtLoc, diag::err_unexpected_at);
1404
1405 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1406 case tok::objc_encode:
1407 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1408 case tok::objc_protocol:
1409 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1410 case tok::objc_selector:
1411 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1412 default:
Chris Lattnerf006a222008-11-18 07:48:38 +00001413 return Diag(AtLoc, diag::err_unexpected_at);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001414 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001415 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001416}
1417
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001418/// objc-message-expr:
1419/// '[' objc-receiver objc-message-args ']'
1420///
1421/// objc-receiver:
1422/// expression
1423/// class-name
1424/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001425Parser::ExprResult Parser::ParseObjCMessageExpression() {
1426 assert(Tok.is(tok::l_square) && "'[' expected");
1427 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1428
1429 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001430 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001431 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1432 ConsumeToken();
1433 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1434 }
1435
Chris Lattnerc185e1a2008-09-19 17:44:00 +00001436 ExprResult Res = ParseExpression();
Chris Lattnered27a532008-01-25 18:59:06 +00001437 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001438 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001439 return Res;
1440 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001441
Chris Lattnered27a532008-01-25 18:59:06 +00001442 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1443}
1444
1445/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1446/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001447///
1448/// objc-message-args:
1449/// objc-selector
1450/// objc-keywordarg-list
1451///
1452/// objc-keywordarg-list:
1453/// objc-keywordarg
1454/// objc-keywordarg-list objc-keywordarg
1455///
1456/// objc-keywordarg:
1457/// selector-name[opt] ':' objc-keywordexpr
1458///
1459/// objc-keywordexpr:
1460/// nonempty-expr-list
1461///
1462/// nonempty-expr-list:
1463/// assignment-expression
1464/// nonempty-expr-list , assignment-expression
1465///
Chris Lattnered27a532008-01-25 18:59:06 +00001466Parser::ExprResult
1467Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1468 IdentifierInfo *ReceiverName,
1469 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001470 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001471 SourceLocation Loc;
1472 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001473
1474 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1475 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1476
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001477 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001478 while (1) {
1479 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001480 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001481
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001482 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001483 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001484 // We must manually skip to a ']', otherwise the expression skipper will
1485 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1486 // the enclosing expression.
1487 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001488 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001489 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001490
Steve Naroff4ed9d662007-09-27 14:38:14 +00001491 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001492 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001493 ExprResult Res = ParseAssignmentExpression();
1494 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001495 // We must manually skip to a ']', otherwise the expression skipper will
1496 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1497 // the enclosing expression.
1498 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001499 return Res;
1500 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001501
Steve Naroff253118b2007-09-17 20:25:27 +00001502 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001503 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001504
1505 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001506 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001507 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001508 break;
1509 // We have a selector or a colon, continue parsing.
1510 }
1511 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001512 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001513 ConsumeToken(); // Eat the ','.
1514 /// Parse the expression after ','
1515 ExprResult Res = ParseAssignmentExpression();
1516 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001517 // We must manually skip to a ']', otherwise the expression skipper will
1518 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1519 // the enclosing expression.
1520 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001521 return Res;
1522 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001523
Steve Naroff9f176d12007-11-15 13:05:42 +00001524 // We have a valid expression.
1525 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001526 }
1527 } else if (!selIdent) {
1528 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001529
1530 // We must manually skip to a ']', otherwise the expression skipper will
1531 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1532 // the enclosing expression.
1533 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001534 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001535 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001536
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001537 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001538 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001539 // We must manually skip to a ']', otherwise the expression skipper will
1540 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1541 // the enclosing expression.
1542 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001543 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001544 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001545
Chris Lattnered27a532008-01-25 18:59:06 +00001546 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001547
Steve Narofff9e80db2007-10-05 18:42:47 +00001548 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001549 if (nKeys == 0)
1550 KeyIdents.push_back(selIdent);
1551 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1552
1553 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001554 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001555 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001556 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001557 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001558 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001559 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001560}
1561
Steve Naroff0add5d22007-11-03 11:27:19 +00001562Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001563 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001564 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001565
1566 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1567 // expressions. At this point, we know that the only valid thing that starts
1568 // with '@' is an @"".
1569 llvm::SmallVector<SourceLocation, 4> AtLocs;
1570 llvm::SmallVector<ExprTy*, 4> AtStrings;
1571 AtLocs.push_back(AtLoc);
1572 AtStrings.push_back(Res.Val);
1573
1574 while (Tok.is(tok::at)) {
1575 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001576
Chris Lattnerddd3e632007-12-12 01:04:12 +00001577 ExprResult Res(true); // Invalid unless there is a string literal.
1578 if (isTokenStringLiteral())
1579 Res = ParseStringLiteralExpression();
1580 else
1581 Diag(Tok, diag::err_objc_concat_string);
1582
1583 if (Res.isInvalid) {
1584 while (!AtStrings.empty()) {
1585 Actions.DeleteExpr(AtStrings.back());
1586 AtStrings.pop_back();
1587 }
1588 return Res;
1589 }
1590
1591 AtStrings.push_back(Res.Val);
1592 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001593
Chris Lattnerddd3e632007-12-12 01:04:12 +00001594 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1595 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001596}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001597
1598/// objc-encode-expression:
1599/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001600Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001601 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001602
1603 SourceLocation EncLoc = ConsumeToken();
1604
Chris Lattnerf9311a92008-08-05 06:19:09 +00001605 if (Tok.isNot(tok::l_paren))
Chris Lattnerf006a222008-11-18 07:48:38 +00001606 return Diag(Tok, diag::err_expected_lparen_after) << "@encode";
Anders Carlsson8be1d402007-08-22 15:14:15 +00001607
1608 SourceLocation LParenLoc = ConsumeParen();
1609
1610 TypeTy *Ty = ParseTypeName();
1611
Anders Carlsson92faeb82007-08-23 15:31:37 +00001612 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001613
Chris Lattnercfd61c82007-10-16 22:51:17 +00001614 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001615 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001616}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001617
1618/// objc-protocol-expression
1619/// @protocol ( protocol-name )
1620
Chris Lattnerf006a222008-11-18 07:48:38 +00001621Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001622 SourceLocation ProtoLoc = ConsumeToken();
1623
Chris Lattnerf9311a92008-08-05 06:19:09 +00001624 if (Tok.isNot(tok::l_paren))
Chris Lattnerf006a222008-11-18 07:48:38 +00001625 return Diag(Tok, diag::err_expected_lparen_after) << "@protocol";
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001626
1627 SourceLocation LParenLoc = ConsumeParen();
1628
Chris Lattnerf9311a92008-08-05 06:19:09 +00001629 if (Tok.isNot(tok::identifier))
1630 return Diag(Tok, diag::err_expected_ident);
1631
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001632 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001633 ConsumeToken();
1634
Anders Carlsson92faeb82007-08-23 15:31:37 +00001635 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001636
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001637 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1638 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001639}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001640
1641/// objc-selector-expression
1642/// @selector '(' objc-keyword-selector ')'
Chris Lattnerf006a222008-11-18 07:48:38 +00001643Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001644 SourceLocation SelectorLoc = ConsumeToken();
1645
Chris Lattnerf9311a92008-08-05 06:19:09 +00001646 if (Tok.isNot(tok::l_paren))
Chris Lattnerf006a222008-11-18 07:48:38 +00001647 return Diag(Tok, diag::err_expected_lparen_after) << "@selector";
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001648
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001649 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001650 SourceLocation LParenLoc = ConsumeParen();
1651 SourceLocation sLoc;
1652 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001653 if (!SelIdent && Tok.isNot(tok::colon))
1654 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1655
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001656 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001657 unsigned nColons = 0;
1658 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001659 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001660 if (Tok.isNot(tok::colon))
1661 return Diag(Tok, diag::err_expected_colon);
1662
Chris Lattner847f5c12007-12-27 19:57:00 +00001663 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001664 ConsumeToken(); // Eat the ':'.
1665 if (Tok.is(tok::r_paren))
1666 break;
1667 // Check for another keyword selector.
1668 SourceLocation Loc;
1669 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001670 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001671 if (!SelIdent && Tok.isNot(tok::colon))
1672 break;
1673 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001674 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001675 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001676 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001677 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001678 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001679 }