blob: 9d4d6fb2481466b8d500fa8aa4861e20c6c56c48 [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) {
Chris Lattner7963c1d2008-11-19 07:41:27 +0000206 llvm::SmallString<100> SelectorName;
207 SelectorName += "set";
208 SelectorName.append(Name->getName(), Name->getName()+Name->getLength());
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000209 SelectorName[3] = toupper(SelectorName[3]);
Chris Lattner7963c1d2008-11-19 07:41:27 +0000210 return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]);
Daniel Dunbar70cdeaa2008-08-26 02:32:45 +0000211}
212
Steve Narofffb367882007-08-20 21:31:48 +0000213/// objc-interface-decl-list:
214/// empty
Steve Narofffb367882007-08-20 21:31:48 +0000215/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000216/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000217/// objc-interface-decl-list objc-method-proto ';'
Steve Narofffb367882007-08-20 21:31:48 +0000218/// objc-interface-decl-list declaration
219/// objc-interface-decl-list ';'
220///
Steve Naroff0bbffd82007-08-22 16:35:03 +0000221/// objc-method-requirement: [OBJC2]
222/// @required
223/// @optional
224///
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000225void Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000226 tok::ObjCKeywordKind contextKey) {
Ted Kremenek8c945b12008-06-06 16:45:15 +0000227 llvm::SmallVector<DeclTy*, 32> allMethods;
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000228 llvm::SmallVector<DeclTy*, 16> allProperties;
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000229 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000230
Chris Lattnera40577e2008-10-20 06:10:06 +0000231 SourceLocation AtEndLoc;
232
Steve Naroff0bbffd82007-08-22 16:35:03 +0000233 while (1) {
Chris Lattnere48b46b2008-10-20 05:46:22 +0000234 // If this is a method prototype, parse it.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000235 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
236 DeclTy *methodPrototype =
237 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000238 allMethods.push_back(methodPrototype);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000239 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
240 // method definitions.
Steve Naroffaa1b6d42007-09-17 15:07:43 +0000241 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000242 continue;
243 }
Fariborz Jahanian5d175c32007-12-11 18:34:51 +0000244
Chris Lattnere48b46b2008-10-20 05:46:22 +0000245 // Ignore excess semicolons.
246 if (Tok.is(tok::semi)) {
Steve Naroff0bbffd82007-08-22 16:35:03 +0000247 ConsumeToken();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000248 continue;
249 }
250
Chris Lattnera40577e2008-10-20 06:10:06 +0000251 // If we got to the end of the file, exit the loop.
Chris Lattnere48b46b2008-10-20 05:46:22 +0000252 if (Tok.is(tok::eof))
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000253 break;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000254
255 // If we don't have an @ directive, parse it as a function definition.
256 if (Tok.isNot(tok::at)) {
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000257 // FIXME: as the name implies, this rule allows function definitions.
258 // We could pass a flag or check for functions during semantic analysis.
Steve Naroff81f1bba2007-09-06 21:24:23 +0000259 ParseDeclarationOrFunctionDefinition();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000260 continue;
261 }
262
263 // Otherwise, we have an @ directive, eat the @.
264 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnercba730b2008-10-20 05:57:40 +0000265 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Chris Lattnere48b46b2008-10-20 05:46:22 +0000266
Chris Lattnercba730b2008-10-20 05:57:40 +0000267 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere48b46b2008-10-20 05:46:22 +0000268 AtEndLoc = AtLoc;
269 break;
Chris Lattnera40577e2008-10-20 06:10:06 +0000270 }
Chris Lattnere48b46b2008-10-20 05:46:22 +0000271
Chris Lattnera40577e2008-10-20 06:10:06 +0000272 // Eat the identifier.
273 ConsumeToken();
274
Chris Lattnercba730b2008-10-20 05:57:40 +0000275 switch (DirectiveKind) {
276 default:
Chris Lattnera40577e2008-10-20 06:10:06 +0000277 // FIXME: If someone forgets an @end on a protocol, this loop will
278 // continue to eat up tons of stuff and spew lots of nonsense errors. It
279 // would probably be better to bail out if we saw an @class or @interface
280 // or something like that.
Chris Lattner727fb1f2008-10-20 07:22:18 +0000281 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnera40577e2008-10-20 06:10:06 +0000282 // Skip until we see an '@' or '}' or ';'.
Chris Lattnercba730b2008-10-20 05:57:40 +0000283 SkipUntil(tok::r_brace, tok::at);
284 break;
285
286 case tok::objc_required:
Chris Lattnercba730b2008-10-20 05:57:40 +0000287 case tok::objc_optional:
Chris Lattnercba730b2008-10-20 05:57:40 +0000288 // This is only valid on protocols.
Chris Lattnera40577e2008-10-20 06:10:06 +0000289 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere48b46b2008-10-20 05:46:22 +0000290 if (contextKey != tok::objc_protocol)
Chris Lattnera40577e2008-10-20 06:10:06 +0000291 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnercba730b2008-10-20 05:57:40 +0000292 else
Chris Lattnera40577e2008-10-20 06:10:06 +0000293 MethodImplKind = DirectiveKind;
Chris Lattnercba730b2008-10-20 05:57:40 +0000294 break;
295
296 case tok::objc_property:
Chris Lattner727fb1f2008-10-20 07:22:18 +0000297 if (!getLang().ObjC2)
298 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
299
Chris Lattnere48b46b2008-10-20 05:46:22 +0000300 ObjCDeclSpec OCDS;
Chris Lattnere48b46b2008-10-20 05:46:22 +0000301 // Parse property attribute list, if any.
Chris Lattner22f9d262008-10-20 07:24:39 +0000302 if (Tok.is(tok::l_paren))
Chris Lattnere48b46b2008-10-20 05:46:22 +0000303 ParseObjCPropertyAttribute(OCDS);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000304
Chris Lattnere48b46b2008-10-20 05:46:22 +0000305 // Parse all the comma separated declarators.
306 DeclSpec DS;
307 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
308 ParseStructDeclaration(DS, FieldDeclarators);
309
Chris Lattner9019ae52008-10-20 06:15:13 +0000310 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
311 tok::at);
312
Chris Lattnere48b46b2008-10-20 05:46:22 +0000313 // Convert them all to property declarations.
314 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
315 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattnerbf16a972008-10-20 06:33:53 +0000316 if (FD.D.getIdentifier() == 0) {
Chris Lattner194e7002008-11-18 07:50:21 +0000317 Diag(AtLoc, diag::err_objc_property_requires_field_name)
318 << FD.D.getSourceRange();
Chris Lattnerbf16a972008-10-20 06:33:53 +0000319 continue;
320 }
321
Chris Lattnere48b46b2008-10-20 05:46:22 +0000322 // Install the property declarator into interfaceDecl.
Chris Lattnerbf16a972008-10-20 06:33:53 +0000323 IdentifierInfo *SelName =
324 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
325
Chris Lattnere48b46b2008-10-20 05:46:22 +0000326 Selector GetterSel =
Chris Lattnerbf16a972008-10-20 06:33:53 +0000327 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000328 IdentifierInfo *SetterName = OCDS.getSetterName();
329 if (!SetterName)
330 SetterName = constructSetterName(PP.getIdentifierTable(),
331 FD.D.getIdentifier());
332 Selector SetterSel =
333 PP.getSelectorTable().getUnarySelector(SetterName);
Chris Lattnerbf16a972008-10-20 06:33:53 +0000334 DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
335 GetterSel, SetterSel,
336 MethodImplKind);
Chris Lattnere48b46b2008-10-20 05:46:22 +0000337 allProperties.push_back(Property);
338 }
Chris Lattnercba730b2008-10-20 05:57:40 +0000339 break;
Steve Naroff304ed392007-09-05 23:30:30 +0000340 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000341 }
Chris Lattnera40577e2008-10-20 06:10:06 +0000342
343 // We break out of the big loop in two cases: when we see @end or when we see
344 // EOF. In the former case, eat the @end. In the later case, emit an error.
345 if (Tok.isObjCAtKeyword(tok::objc_end))
346 ConsumeToken(); // the "end" identifier
347 else
348 Diag(Tok, diag::err_objc_missing_end);
349
Chris Lattnercba730b2008-10-20 05:57:40 +0000350 // Insert collected methods declarations into the @interface object.
Chris Lattnera40577e2008-10-20 06:10:06 +0000351 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8c945b12008-06-06 16:45:15 +0000352 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
353 allMethods.empty() ? 0 : &allMethods[0],
354 allMethods.size(),
355 allProperties.empty() ? 0 : &allProperties[0],
356 allProperties.size());
Steve Naroff0bbffd82007-08-22 16:35:03 +0000357}
358
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000359/// Parse property attribute declarations.
360///
361/// property-attr-decl: '(' property-attrlist ')'
362/// property-attrlist:
363/// property-attribute
364/// property-attrlist ',' property-attribute
365/// property-attribute:
366/// getter '=' identifier
367/// setter '=' identifier ':'
368/// readonly
369/// readwrite
370/// assign
371/// retain
372/// copy
373/// nonatomic
374///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000375void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner22f9d262008-10-20 07:24:39 +0000376 assert(Tok.getKind() == tok::l_paren);
Chris Lattner35cd4b92008-10-20 07:00:43 +0000377 SourceLocation LHSLoc = ConsumeParen(); // consume '('
378
Chris Lattner1e5cc722008-10-20 07:15:22 +0000379 while (1) {
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000380 const IdentifierInfo *II = Tok.getIdentifierInfo();
Chris Lattner727fb1f2008-10-20 07:22:18 +0000381
382 // If this is not an identifier at all, bail out early.
383 if (II == 0) {
384 MatchRHSPunctuation(tok::r_paren, LHSLoc);
385 return;
386 }
387
Chris Lattner9ba0b222008-10-20 07:37:22 +0000388 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
389
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000390 if (!strcmp(II->getName(), "readonly"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000391 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000392 else if (!strcmp(II->getName(), "assign"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000393 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000394 else if (!strcmp(II->getName(), "readwrite"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000395 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000396 else if (!strcmp(II->getName(), "retain"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000397 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000398 else if (!strcmp(II->getName(), "copy"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000399 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000400 else if (!strcmp(II->getName(), "nonatomic"))
Chris Lattner2cc2b872008-10-20 07:39:53 +0000401 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000402 else if (!strcmp(II->getName(), "getter") ||
403 !strcmp(II->getName(), "setter")) {
Chris Lattner2cc2b872008-10-20 07:39:53 +0000404 // getter/setter require extra treatment.
Chris Lattner9ba0b222008-10-20 07:37:22 +0000405 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
406 tok::r_paren))
Chris Lattner35cd4b92008-10-20 07:00:43 +0000407 return;
Chris Lattner9ba0b222008-10-20 07:37:22 +0000408
Chris Lattner22f9d262008-10-20 07:24:39 +0000409 if (Tok.isNot(tok::identifier)) {
Chris Lattnerf006a222008-11-18 07:48:38 +0000410 Diag(Tok, diag::err_expected_ident);
Chris Lattner22f9d262008-10-20 07:24:39 +0000411 SkipUntil(tok::r_paren);
412 return;
413 }
414
Chris Lattnerf54dbea2008-10-20 07:43:01 +0000415 if (II->getName()[0] == 's') {
Chris Lattner22f9d262008-10-20 07:24:39 +0000416 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
417 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000418 ConsumeToken(); // consume method name
419
420 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
421 tok::r_paren))
Chris Lattner22f9d262008-10-20 07:24:39 +0000422 return;
Chris Lattner22f9d262008-10-20 07:24:39 +0000423 } else {
424 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
425 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner9ba0b222008-10-20 07:37:22 +0000426 ConsumeToken(); // consume method name
Chris Lattner22f9d262008-10-20 07:24:39 +0000427 }
Chris Lattner2cc2b872008-10-20 07:39:53 +0000428 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +0000429 Diag(AttrName, diag::err_objc_expected_property_attr) << II->getName();
Chris Lattner1e5cc722008-10-20 07:15:22 +0000430 SkipUntil(tok::r_paren);
431 return;
Chris Lattner1e5cc722008-10-20 07:15:22 +0000432 }
Fariborz Jahaniand8df6d82007-11-06 22:01:00 +0000433
Chris Lattner9ba0b222008-10-20 07:37:22 +0000434 if (Tok.isNot(tok::comma))
435 break;
Chris Lattner35cd4b92008-10-20 07:00:43 +0000436
Chris Lattner9ba0b222008-10-20 07:37:22 +0000437 ConsumeToken();
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000438 }
Chris Lattner9ba0b222008-10-20 07:37:22 +0000439
440 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian6668b8c2007-08-31 16:11:31 +0000441}
442
Steve Naroff81f1bba2007-09-06 21:24:23 +0000443/// objc-method-proto:
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000444/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000445/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000446///
447/// objc-instance-method: '-'
448/// objc-class-method: '+'
449///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000450/// objc-method-attributes: [OBJC2]
451/// __attribute__((deprecated))
452///
Fariborz Jahanian8b5ab6f2007-09-18 00:25:23 +0000453Parser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000454 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000455 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000456
457 tok::TokenKind methodType = Tok.getKind();
Steve Naroff3774dd92007-10-26 20:53:56 +0000458 SourceLocation mLoc = ConsumeToken();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000459
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000460 DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000461 // Since this rule is used for both method declarations and definitions,
Steve Narofffaed3bf2007-09-10 20:51:04 +0000462 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroff304ed392007-09-05 23:30:30 +0000463 return MDecl;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000464}
465
466/// objc-selector:
467/// identifier
468/// one of
469/// enum struct union if else while do for switch case default
470/// break continue return goto asm sizeof typeof __alignof
471/// unsigned long const short volatile signed restrict _Complex
472/// in out inout bycopy byref oneway int char float double void _Bool
473///
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000474IdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000475 switch (Tok.getKind()) {
476 default:
477 return 0;
478 case tok::identifier:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000479 case tok::kw_asm:
Chris Lattnerd031a452007-10-07 02:00:24 +0000480 case tok::kw_auto:
Chris Lattner2baef2e2007-11-15 05:25:19 +0000481 case tok::kw_bool:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000482 case tok::kw_break:
483 case tok::kw_case:
484 case tok::kw_catch:
485 case tok::kw_char:
486 case tok::kw_class:
487 case tok::kw_const:
488 case tok::kw_const_cast:
489 case tok::kw_continue:
490 case tok::kw_default:
491 case tok::kw_delete:
492 case tok::kw_do:
493 case tok::kw_double:
494 case tok::kw_dynamic_cast:
495 case tok::kw_else:
496 case tok::kw_enum:
497 case tok::kw_explicit:
498 case tok::kw_export:
499 case tok::kw_extern:
500 case tok::kw_false:
501 case tok::kw_float:
502 case tok::kw_for:
503 case tok::kw_friend:
504 case tok::kw_goto:
505 case tok::kw_if:
506 case tok::kw_inline:
507 case tok::kw_int:
508 case tok::kw_long:
509 case tok::kw_mutable:
510 case tok::kw_namespace:
511 case tok::kw_new:
512 case tok::kw_operator:
513 case tok::kw_private:
514 case tok::kw_protected:
515 case tok::kw_public:
516 case tok::kw_register:
517 case tok::kw_reinterpret_cast:
518 case tok::kw_restrict:
519 case tok::kw_return:
520 case tok::kw_short:
521 case tok::kw_signed:
522 case tok::kw_sizeof:
523 case tok::kw_static:
524 case tok::kw_static_cast:
525 case tok::kw_struct:
526 case tok::kw_switch:
527 case tok::kw_template:
528 case tok::kw_this:
529 case tok::kw_throw:
530 case tok::kw_true:
531 case tok::kw_try:
532 case tok::kw_typedef:
533 case tok::kw_typeid:
534 case tok::kw_typename:
535 case tok::kw_typeof:
536 case tok::kw_union:
537 case tok::kw_unsigned:
538 case tok::kw_using:
539 case tok::kw_virtual:
540 case tok::kw_void:
541 case tok::kw_volatile:
542 case tok::kw_wchar_t:
543 case tok::kw_while:
Chris Lattnerd031a452007-10-07 02:00:24 +0000544 case tok::kw__Bool:
545 case tok::kw__Complex:
Anders Carlsson28075fa2008-08-23 21:00:01 +0000546 case tok::kw___alignof:
Chris Lattnerd031a452007-10-07 02:00:24 +0000547 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000548 SelectorLoc = ConsumeToken();
Chris Lattnerd031a452007-10-07 02:00:24 +0000549 return II;
Fariborz Jahanian171ceb52007-09-27 19:52:15 +0000550 }
Steve Naroff0bbffd82007-08-22 16:35:03 +0000551}
552
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000553/// objc-for-collection-in: 'in'
554///
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000555bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian1300bc72008-01-03 17:55:25 +0000556 // FIXME: May have to do additional look-ahead to only allow for
557 // valid tokens following an 'in'; such as an identifier, unary operators,
558 // '[' etc.
Fariborz Jahaniancadb0702008-01-04 23:04:08 +0000559 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner818350c2008-08-23 02:02:23 +0000560 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian9e920f32008-01-02 22:54:34 +0000561}
562
Ted Kremenek42730c52008-01-07 19:49:32 +0000563/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner2b740db2007-12-12 06:56:32 +0000564/// qualifier list and builds their bitmask representation in the input
565/// argument.
Steve Naroff0bbffd82007-08-22 16:35:03 +0000566///
567/// objc-type-qualifiers:
568/// objc-type-qualifier
569/// objc-type-qualifiers objc-type-qualifier
570///
Ted Kremenek42730c52008-01-07 19:49:32 +0000571void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner2b740db2007-12-12 06:56:32 +0000572 while (1) {
Chris Lattner847f5c12007-12-27 19:57:00 +0000573 if (Tok.isNot(tok::identifier))
Chris Lattner2b740db2007-12-12 06:56:32 +0000574 return;
575
576 const IdentifierInfo *II = Tok.getIdentifierInfo();
577 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000578 if (II != ObjCTypeQuals[i])
Chris Lattner2b740db2007-12-12 06:56:32 +0000579 continue;
580
Ted Kremenek42730c52008-01-07 19:49:32 +0000581 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner2b740db2007-12-12 06:56:32 +0000582 switch (i) {
583 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek42730c52008-01-07 19:49:32 +0000584 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
585 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
586 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
587 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
588 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
589 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner2b740db2007-12-12 06:56:32 +0000590 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000591 DS.setObjCDeclQualifier(Qual);
Chris Lattner2b740db2007-12-12 06:56:32 +0000592 ConsumeToken();
593 II = 0;
594 break;
595 }
596
597 // If this wasn't a recognized qualifier, bail out.
598 if (II) return;
599 }
600}
601
602/// objc-type-name:
603/// '(' objc-type-qualifiers[opt] type-name ')'
604/// '(' objc-type-qualifiers[opt] ')'
605///
Ted Kremenek42730c52008-01-07 19:49:32 +0000606Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000607 assert(Tok.is(tok::l_paren) && "expected (");
Steve Naroff0bbffd82007-08-22 16:35:03 +0000608
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000609 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnerb5769332008-08-23 01:48:03 +0000610 SourceLocation TypeStartLoc = Tok.getLocation();
Steve Naroff0bbffd82007-08-22 16:35:03 +0000611
Fariborz Jahanian6dab49b2007-10-31 21:59:43 +0000612 // Parse type qualifiers, in, inout, etc.
Ted Kremenek42730c52008-01-07 19:49:32 +0000613 ParseObjCTypeQualifierList(DS);
Steve Naroffa8ee2262007-08-22 23:18:22 +0000614
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000615 TypeTy *Ty = 0;
616 if (isTypeSpecifierQualifier())
Steve Naroff304ed392007-09-05 23:30:30 +0000617 Ty = ParseTypeName();
Chris Lattnerb5769332008-08-23 01:48:03 +0000618
Steve Naroffc6235e82008-10-21 14:15:04 +0000619 if (Tok.is(tok::r_paren))
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000620 ConsumeParen();
621 else if (Tok.getLocation() == TypeStartLoc) {
622 // If we didn't eat any tokens, then this isn't a type.
Chris Lattnerf006a222008-11-18 07:48:38 +0000623 Diag(Tok, diag::err_expected_type);
Chris Lattner6d9cdf42008-10-22 03:52:06 +0000624 SkipUntil(tok::r_paren);
625 } else {
626 // Otherwise, we found *something*, but didn't get a ')' in the right
627 // place. Emit an error then return what we have as the type.
628 MatchRHSPunctuation(tok::r_paren, LParenLoc);
629 }
Steve Naroff304ed392007-09-05 23:30:30 +0000630 return Ty;
Steve Naroff0bbffd82007-08-22 16:35:03 +0000631}
632
633/// objc-method-decl:
634/// objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000635/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000636/// objc-type-name objc-selector
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000637/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000638///
639/// objc-keyword-selector:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000640/// objc-keyword-decl
Steve Naroff0bbffd82007-08-22 16:35:03 +0000641/// objc-keyword-selector objc-keyword-decl
642///
643/// objc-keyword-decl:
Steve Naroff72f17fb2007-08-22 22:17:26 +0000644/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
645/// objc-selector ':' objc-keyword-attributes[opt] identifier
646/// ':' objc-type-name objc-keyword-attributes[opt] identifier
647/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff0bbffd82007-08-22 16:35:03 +0000648///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000649/// objc-parmlist:
650/// objc-parms objc-ellipsis[opt]
Steve Naroff0bbffd82007-08-22 16:35:03 +0000651///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000652/// objc-parms:
653/// objc-parms , parameter-declaration
Steve Naroff0bbffd82007-08-22 16:35:03 +0000654///
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000655/// objc-ellipsis:
Steve Naroff0bbffd82007-08-22 16:35:03 +0000656/// , ...
657///
Steve Naroff72f17fb2007-08-22 22:17:26 +0000658/// objc-keyword-attributes: [OBJC2]
659/// __attribute__((unused))
660///
Steve Naroff3774dd92007-10-26 20:53:56 +0000661Parser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000662 tok::TokenKind mType,
663 DeclTy *IDecl,
Chris Lattner847f5c12007-12-27 19:57:00 +0000664 tok::ObjCKeywordKind MethodImplKind)
Steve Naroff4ed9d662007-09-27 14:38:14 +0000665{
Chris Lattnerb5769332008-08-23 01:48:03 +0000666 // Parse the return type if present.
Chris Lattnerd031a452007-10-07 02:00:24 +0000667 TypeTy *ReturnType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000668 ObjCDeclSpec DSRet;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000669 if (Tok.is(tok::l_paren))
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000670 ReturnType = ParseObjCTypeName(DSRet);
Chris Lattnerb5769332008-08-23 01:48:03 +0000671
Steve Naroff3774dd92007-10-26 20:53:56 +0000672 SourceLocation selLoc;
673 IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
Chris Lattnerb5769332008-08-23 01:48:03 +0000674
675 if (!SelIdent) { // missing selector name.
Chris Lattnerf006a222008-11-18 07:48:38 +0000676 Diag(Tok, diag::err_expected_selector_for_method)
677 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnerb5769332008-08-23 01:48:03 +0000678 // Skip until we get a ; or {}.
679 SkipUntil(tok::r_brace);
680 return 0;
681 }
682
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000683 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000684 // If attributes exist after the method, parse them.
685 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000686 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000687 MethodAttrs = ParseAttributes();
688
689 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff3774dd92007-10-26 20:53:56 +0000690 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000691 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000692 0, 0, 0, MethodAttrs, MethodImplKind);
Chris Lattnerd031a452007-10-07 02:00:24 +0000693 }
Steve Naroff304ed392007-09-05 23:30:30 +0000694
Steve Naroff4ed9d662007-09-27 14:38:14 +0000695 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
696 llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
Ted Kremenek42730c52008-01-07 19:49:32 +0000697 llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
Steve Naroff4ed9d662007-09-27 14:38:14 +0000698 llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
Chris Lattnerd031a452007-10-07 02:00:24 +0000699
700 Action::TypeTy *TypeInfo;
701 while (1) {
702 KeyIdents.push_back(SelIdent);
Steve Naroff4ed9d662007-09-27 14:38:14 +0000703
Chris Lattnerd031a452007-10-07 02:00:24 +0000704 // Each iteration parses a single keyword argument.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000705 if (Tok.isNot(tok::colon)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000706 Diag(Tok, diag::err_expected_colon);
707 break;
708 }
709 ConsumeToken(); // Eat the ':'.
Ted Kremenek42730c52008-01-07 19:49:32 +0000710 ObjCDeclSpec DSType;
Chris Lattnerb5769332008-08-23 01:48:03 +0000711 if (Tok.is(tok::l_paren)) // Parse the argument type.
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000712 TypeInfo = ParseObjCTypeName(DSType);
Chris Lattnerd031a452007-10-07 02:00:24 +0000713 else
714 TypeInfo = 0;
715 KeyTypes.push_back(TypeInfo);
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000716 ArgTypeQuals.push_back(DSType);
Steve Naroff304ed392007-09-05 23:30:30 +0000717
Chris Lattnerd031a452007-10-07 02:00:24 +0000718 // If attributes exist before the argument name, parse them.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000719 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000720 ParseAttributes(); // FIXME: pass attributes through.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000721
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000722 if (Tok.isNot(tok::identifier)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000723 Diag(Tok, diag::err_expected_ident); // missing argument name.
724 break;
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000725 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000726 ArgNames.push_back(Tok.getIdentifierInfo());
727 ConsumeToken(); // Eat the identifier.
Steve Narofff9e80db2007-10-05 18:42:47 +0000728
Chris Lattnerd031a452007-10-07 02:00:24 +0000729 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000730 SourceLocation Loc;
731 SelIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000732 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerd031a452007-10-07 02:00:24 +0000733 break;
734 // We have a selector or a colon, continue parsing.
Steve Naroff09a0c4c2007-08-22 18:35:33 +0000735 }
Chris Lattnerd031a452007-10-07 02:00:24 +0000736
Steve Naroff29fe7462007-11-15 12:35:21 +0000737 bool isVariadic = false;
738
Chris Lattnerd031a452007-10-07 02:00:24 +0000739 // Parse the (optional) parameter list.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000740 while (Tok.is(tok::comma)) {
Chris Lattnerd031a452007-10-07 02:00:24 +0000741 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000742 if (Tok.is(tok::ellipsis)) {
Steve Naroff29fe7462007-11-15 12:35:21 +0000743 isVariadic = true;
Chris Lattnerd031a452007-10-07 02:00:24 +0000744 ConsumeToken();
745 break;
746 }
Steve Naroff29fe7462007-11-15 12:35:21 +0000747 // FIXME: implement this...
Chris Lattnerd031a452007-10-07 02:00:24 +0000748 // Parse the c-style argument declaration-specifier.
749 DeclSpec DS;
750 ParseDeclarationSpecifiers(DS);
751 // Parse the declarator.
752 Declarator ParmDecl(DS, Declarator::PrototypeContext);
753 ParseDeclarator(ParmDecl);
754 }
755
756 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian3dc7cbc2007-09-10 20:33:04 +0000757 // If attributes exist after the method, parse them.
Chris Lattnerd031a452007-10-07 02:00:24 +0000758 AttributeList *MethodAttrs = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000759 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd031a452007-10-07 02:00:24 +0000760 MethodAttrs = ParseAttributes();
761
762 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
763 &KeyIdents[0]);
Steve Naroff3774dd92007-10-26 20:53:56 +0000764 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian8473b222007-11-09 19:52:12 +0000765 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian2fd0daa2007-10-31 23:53:01 +0000766 &ArgTypeQuals[0], &KeyTypes[0],
Steve Naroff29fe7462007-11-15 12:35:21 +0000767 &ArgNames[0], MethodAttrs,
768 MethodImplKind, isVariadic);
Steve Naroff0bbffd82007-08-22 16:35:03 +0000769}
770
Steve Narofffb367882007-08-20 21:31:48 +0000771/// objc-protocol-refs:
772/// '<' identifier-list '>'
773///
Chris Lattnere705e5e2008-07-21 22:17:28 +0000774bool Parser::
Chris Lattner2bdedd62008-07-26 04:03:38 +0000775ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
776 bool WarnOnDeclarations, SourceLocation &EndLoc) {
777 assert(Tok.is(tok::less) && "expected <");
778
779 ConsumeToken(); // the "<"
780
781 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
782
783 while (1) {
784 if (Tok.isNot(tok::identifier)) {
785 Diag(Tok, diag::err_expected_ident);
786 SkipUntil(tok::greater);
787 return true;
788 }
789 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
790 Tok.getLocation()));
791 ConsumeToken();
792
793 if (Tok.isNot(tok::comma))
794 break;
795 ConsumeToken();
796 }
797
798 // Consume the '>'.
799 if (Tok.isNot(tok::greater)) {
800 Diag(Tok, diag::err_expected_greater);
801 return true;
802 }
803
804 EndLoc = ConsumeAnyToken();
805
806 // Convert the list of protocols identifiers into a list of protocol decls.
807 Actions.FindProtocolDeclaration(WarnOnDeclarations,
808 &ProtocolIdents[0], ProtocolIdents.size(),
809 Protocols);
810 return false;
811}
812
Steve Narofffb367882007-08-20 21:31:48 +0000813/// objc-class-instance-variables:
814/// '{' objc-instance-variable-decl-list[opt] '}'
815///
816/// objc-instance-variable-decl-list:
817/// objc-visibility-spec
818/// objc-instance-variable-decl ';'
819/// ';'
820/// objc-instance-variable-decl-list objc-visibility-spec
821/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
822/// objc-instance-variable-decl-list ';'
823///
824/// objc-visibility-spec:
825/// @private
826/// @protected
827/// @public
Steve Naroffc4474992007-08-21 21:17:12 +0000828/// @package [OBJC2]
Steve Narofffb367882007-08-20 21:31:48 +0000829///
830/// objc-instance-variable-decl:
831/// struct-declaration
832///
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000833void Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
834 SourceLocation atLoc) {
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000835 assert(Tok.is(tok::l_brace) && "expected {");
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000836 llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
Chris Lattner3dd8d392008-04-10 06:46:29 +0000837 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
838
Steve Naroffc4474992007-08-21 21:17:12 +0000839 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Steve Naroffc4474992007-08-21 21:17:12 +0000840
Fariborz Jahanian7c420a72008-04-29 23:03:51 +0000841 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffc4474992007-08-21 21:17:12 +0000842 // While we still have something to read, read the instance variables.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000843 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000844 // Each iteration of this loop reads one objc-instance-variable-decl.
845
846 // Check for extraneous top-level semicolon.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000847 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000848 Diag(Tok, diag::ext_extra_struct_semi);
849 ConsumeToken();
850 continue;
851 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000852
Steve Naroffc4474992007-08-21 21:17:12 +0000853 // Set the default visibility to private.
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000854 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffc4474992007-08-21 21:17:12 +0000855 ConsumeToken(); // eat the @ sign
Steve Naroff87c329f2007-08-23 18:16:40 +0000856 switch (Tok.getObjCKeywordID()) {
Steve Naroffc4474992007-08-21 21:17:12 +0000857 case tok::objc_private:
858 case tok::objc_public:
859 case tok::objc_protected:
860 case tok::objc_package:
Steve Naroff87c329f2007-08-23 18:16:40 +0000861 visibility = Tok.getObjCKeywordID();
Steve Naroffc4474992007-08-21 21:17:12 +0000862 ConsumeToken();
863 continue;
864 default:
865 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffc4474992007-08-21 21:17:12 +0000866 continue;
867 }
868 }
Chris Lattner3dd8d392008-04-10 06:46:29 +0000869
870 // Parse all the comma separated declarators.
871 DeclSpec DS;
872 FieldDeclarators.clear();
873 ParseStructDeclaration(DS, FieldDeclarators);
874
875 // Convert them all to fields.
876 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
877 FieldDeclarator &FD = FieldDeclarators[i];
878 // Install the declarator into interfaceDecl.
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000879 DeclTy *Field = Actions.ActOnIvar(CurScope,
Chris Lattner3dd8d392008-04-10 06:46:29 +0000880 DS.getSourceRange().getBegin(),
Fariborz Jahanian751c6172008-04-10 23:32:45 +0000881 FD.D, FD.BitfieldSize, visibility);
Chris Lattner3dd8d392008-04-10 06:46:29 +0000882 AllIvarDecls.push_back(Field);
Fariborz Jahanian3957dae2007-09-13 20:56:13 +0000883 }
Steve Naroff81f1bba2007-09-06 21:24:23 +0000884
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000885 if (Tok.is(tok::semi)) {
Steve Naroffc4474992007-08-21 21:17:12 +0000886 ConsumeToken();
Steve Naroffc4474992007-08-21 21:17:12 +0000887 } else {
888 Diag(Tok, diag::err_expected_semi_decl_list);
889 // Skip to end of block or statement
890 SkipUntil(tok::r_brace, true, true);
891 }
892 }
Steve Naroff1a7fa7b2007-10-29 21:38:07 +0000893 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff809b4f02007-10-31 22:11:35 +0000894 // Call ActOnFields() even if we don't have any decls. This is useful
895 // for code rewriting tools that need to be aware of the empty list.
896 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
897 &AllIvarDecls[0], AllIvarDecls.size(),
Daniel Dunbarf3944442008-10-03 02:03:53 +0000898 LBraceLoc, RBraceLoc, 0);
Steve Naroffc4474992007-08-21 21:17:12 +0000899 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000900}
Steve Narofffb367882007-08-20 21:31:48 +0000901
902/// objc-protocol-declaration:
903/// objc-protocol-definition
904/// objc-protocol-forward-reference
905///
906/// objc-protocol-definition:
907/// @protocol identifier
908/// objc-protocol-refs[opt]
Steve Naroff81f1bba2007-09-06 21:24:23 +0000909/// objc-interface-decl-list
Steve Narofffb367882007-08-20 21:31:48 +0000910/// @end
911///
912/// objc-protocol-forward-reference:
913/// @protocol identifier-list ';'
914///
915/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff81f1bba2007-09-06 21:24:23 +0000916/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Narofffb367882007-08-20 21:31:48 +0000917/// semicolon in the first alternative if objc-protocol-refs are omitted.
Daniel Dunbar28680d12008-09-26 04:48:09 +0000918Parser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
919 AttributeList *attrList) {
Steve Naroff87c329f2007-08-23 18:16:40 +0000920 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff72f17fb2007-08-22 22:17:26 +0000921 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
922 ConsumeToken(); // the "protocol" identifier
923
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000924 if (Tok.isNot(tok::identifier)) {
Steve Naroff72f17fb2007-08-22 22:17:26 +0000925 Diag(Tok, diag::err_expected_ident); // missing protocol name.
926 return 0;
927 }
928 // Save the protocol name, then consume it.
929 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
930 SourceLocation nameLoc = ConsumeToken();
931
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000932 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000933 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000934 ConsumeToken();
Chris Lattnere705e5e2008-07-21 22:17:28 +0000935 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
Steve Naroff72f17fb2007-08-22 22:17:26 +0000936 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000937
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000938 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnere705e5e2008-07-21 22:17:28 +0000939 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
940 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
941
Steve Naroff72f17fb2007-08-22 22:17:26 +0000942 // Parse the list of forward declarations.
Steve Naroff72f17fb2007-08-22 22:17:26 +0000943 while (1) {
944 ConsumeToken(); // the ','
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);
947 SkipUntil(tok::semi);
948 return 0;
949 }
Chris Lattnere705e5e2008-07-21 22:17:28 +0000950 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
951 Tok.getLocation()));
Steve Naroff72f17fb2007-08-22 22:17:26 +0000952 ConsumeToken(); // the identifier
953
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000954 if (Tok.isNot(tok::comma))
Steve Naroff72f17fb2007-08-22 22:17:26 +0000955 break;
956 }
957 // Consume the ';'.
958 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
959 return 0;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000960
Steve Naroff415c1832007-10-10 17:32:04 +0000961 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Steve Naroffb4dfe362007-10-02 22:39:18 +0000962 &ProtocolRefs[0],
963 ProtocolRefs.size());
Chris Lattnere705e5e2008-07-21 22:17:28 +0000964 }
965
Steve Naroff72f17fb2007-08-22 22:17:26 +0000966 // Last, and definitely not least, parse a protocol declaration.
Chris Lattner2bdedd62008-07-26 04:03:38 +0000967 SourceLocation EndProtoLoc;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000968
Chris Lattner2bdedd62008-07-26 04:03:38 +0000969 llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
Chris Lattnere705e5e2008-07-21 22:17:28 +0000970 if (Tok.is(tok::less) &&
Chris Lattner2bdedd62008-07-26 04:03:38 +0000971 ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
Chris Lattnere705e5e2008-07-21 22:17:28 +0000972 return 0;
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000973
Chris Lattner2bdedd62008-07-26 04:03:38 +0000974 DeclTy *ProtoType =
975 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
976 &ProtocolRefs[0], ProtocolRefs.size(),
Daniel Dunbar28680d12008-09-26 04:48:09 +0000977 EndProtoLoc, attrList);
Fariborz Jahanian63ca8ae2007-09-17 21:07:36 +0000978 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnera40577e2008-10-20 06:10:06 +0000979 return ProtoType;
Chris Lattner4b009652007-07-25 00:24:17 +0000980}
Steve Narofffb367882007-08-20 21:31:48 +0000981
982/// objc-implementation:
983/// objc-class-implementation-prologue
984/// objc-category-implementation-prologue
985///
986/// objc-class-implementation-prologue:
987/// @implementation identifier objc-superclass[opt]
988/// objc-class-instance-variables[opt]
989///
990/// objc-category-implementation-prologue:
991/// @implementation identifier ( identifier )
992
Fariborz Jahanian027c23b2007-09-01 00:26:16 +0000993Parser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
994 SourceLocation atLoc) {
995 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
996 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
997 ConsumeToken(); // the "implementation" identifier
998
Chris Lattnera1d2bb72007-10-09 17:51:17 +0000999 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001000 Diag(Tok, diag::err_expected_ident); // missing class or category name.
1001 return 0;
1002 }
1003 // We have a class or category name - consume it.
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001004 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001005 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1006
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001007 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001008 // we have a category implementation.
1009 SourceLocation lparenLoc = ConsumeParen();
1010 SourceLocation categoryLoc, rparenLoc;
1011 IdentifierInfo *categoryId = 0;
1012
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001013 if (Tok.is(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001014 categoryId = Tok.getIdentifierInfo();
1015 categoryLoc = ConsumeToken();
1016 } else {
1017 Diag(Tok, diag::err_expected_ident); // missing category name.
1018 return 0;
1019 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001020 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001021 Diag(Tok, diag::err_expected_rparen);
1022 SkipUntil(tok::r_paren, false); // don't stop at ';'
1023 return 0;
1024 }
1025 rparenLoc = ConsumeParen();
Steve Naroff415c1832007-10-10 17:32:04 +00001026 DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
Fariborz Jahaniana91aa322007-10-02 16:38:50 +00001027 atLoc, nameId, nameLoc, categoryId,
1028 categoryLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001029 ObjCImpDecl = ImplCatType;
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001030 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001031 }
1032 // We have a class implementation
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001033 SourceLocation superClassLoc;
1034 IdentifierInfo *superClassId = 0;
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001035 if (Tok.is(tok::colon)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001036 // We have a super class
1037 ConsumeToken();
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001038 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001039 Diag(Tok, diag::err_expected_ident); // missing super class name.
1040 return 0;
1041 }
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001042 superClassId = Tok.getIdentifierInfo();
1043 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001044 }
Steve Naroff415c1832007-10-10 17:32:04 +00001045 DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner847f5c12007-12-27 19:57:00 +00001046 atLoc, nameId, nameLoc,
Fariborz Jahanianc091b5d2007-09-25 18:38:09 +00001047 superClassId, superClassLoc);
1048
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001049 if (Tok.is(tok::l_brace)) // we have ivars
1050 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek42730c52008-01-07 19:49:32 +00001051 ObjCImpDecl = ImplClsType;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001052
Fariborz Jahanian83ddf822007-11-10 20:59:13 +00001053 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001054}
Steve Naroff1a7fa7b2007-10-29 21:38:07 +00001055
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001056Parser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1057 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1058 "ParseObjCAtEndDeclaration(): Expected @end");
1059 ConsumeToken(); // the "end" identifier
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001060 if (ObjCImpDecl)
Ted Kremenek42730c52008-01-07 19:49:32 +00001061 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Fariborz Jahanian1a8dcaf2008-01-10 17:58:07 +00001062 else
1063 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Ted Kremenek42730c52008-01-07 19:49:32 +00001064 return ObjCImpDecl;
Steve Narofffb367882007-08-20 21:31:48 +00001065}
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001066
1067/// compatibility-alias-decl:
1068/// @compatibility_alias alias-name class-name ';'
1069///
1070Parser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1071 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1072 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1073 ConsumeToken(); // consume compatibility_alias
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001074 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001075 Diag(Tok, diag::err_expected_ident);
1076 return 0;
1077 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001078 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1079 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001080 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianb62aff32007-09-04 19:26:51 +00001081 Diag(Tok, diag::err_expected_ident);
1082 return 0;
1083 }
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001084 IdentifierInfo *classId = Tok.getIdentifierInfo();
1085 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1086 if (Tok.isNot(tok::semi)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001087 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Fariborz Jahanian05d212a2007-10-11 23:42:27 +00001088 return 0;
1089 }
1090 DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1091 aliasId, aliasLoc,
1092 classId, classLoc);
1093 return ClsType;
Chris Lattner4b009652007-07-25 00:24:17 +00001094}
1095
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001096/// property-synthesis:
1097/// @synthesize property-ivar-list ';'
1098///
1099/// property-ivar-list:
1100/// property-ivar
1101/// property-ivar-list ',' property-ivar
1102///
1103/// property-ivar:
1104/// identifier
1105/// identifier '=' identifier
1106///
1107Parser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1108 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1109 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001110 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001111 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001112 Diag(Tok, diag::err_expected_ident);
1113 return 0;
1114 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001115 while (Tok.is(tok::identifier)) {
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001116 IdentifierInfo *propertyIvar = 0;
1117 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1118 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001119 if (Tok.is(tok::equal)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001120 // property '=' ivar-name
1121 ConsumeToken(); // consume '='
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001122 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001123 Diag(Tok, diag::err_expected_ident);
1124 break;
1125 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001126 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001127 ConsumeToken(); // consume ivar-name
1128 }
Fariborz Jahanian78f7e312008-04-18 00:19:30 +00001129 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1130 propertyId, propertyIvar);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001131 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001132 break;
1133 ConsumeToken(); // consume ','
1134 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001135 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001136 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001137 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001138}
1139
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001140/// property-dynamic:
1141/// @dynamic property-list
1142///
1143/// property-list:
1144/// identifier
1145/// property-list ',' identifier
1146///
1147Parser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1148 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1149 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1150 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001152 Diag(Tok, diag::err_expected_ident);
1153 return 0;
1154 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001155 while (Tok.is(tok::identifier)) {
Fariborz Jahanian900e3dc2008-04-21 21:05:54 +00001156 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1157 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1158 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1159 propertyId, 0);
1160
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001161 if (Tok.isNot(tok::comma))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001162 break;
1163 ConsumeToken(); // consume ','
1164 }
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001165 if (Tok.isNot(tok::semi))
Chris Lattnerf006a222008-11-18 07:48:38 +00001166 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001167 return 0;
1168}
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001169
1170/// objc-throw-statement:
1171/// throw expression[opt];
1172///
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001173Parser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1174 ExprResult Res;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001175 ConsumeToken(); // consume throw
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001176 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001177 Res = ParseExpression();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001178 if (Res.isInvalid) {
1179 SkipUntil(tok::semi);
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001180 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001181 }
1182 }
Fariborz Jahanian08df2c62007-11-07 02:00:49 +00001183 ConsumeToken(); // consume ';'
Ted Kremenek42730c52008-01-07 19:49:32 +00001184 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001185}
1186
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001187/// objc-synchronized-statement:
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001188/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001189///
1190Parser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001191 ConsumeToken(); // consume synchronized
1192 if (Tok.isNot(tok::l_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001193 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001194 return true;
1195 }
1196 ConsumeParen(); // '('
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001197 ExprResult Res = ParseExpression();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001198 if (Res.isInvalid) {
1199 SkipUntil(tok::semi);
1200 return true;
1201 }
1202 if (Tok.isNot(tok::r_paren)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001203 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001204 return true;
1205 }
1206 ConsumeParen(); // ')'
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001207 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001208 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian5f5d6222008-01-30 17:38:29 +00001209 return true;
1210 }
Steve Naroff70337ac2008-06-04 20:36:13 +00001211 // Enter a scope to hold everything within the compound stmt. Compound
1212 // statements can always hold declarations.
1213 EnterScope(Scope::DeclScope);
1214
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001215 StmtResult SynchBody = ParseCompoundStatementBody();
Steve Naroff70337ac2008-06-04 20:36:13 +00001216
1217 ExitScope();
Fariborz Jahanianc9fd4d12008-01-29 19:14:59 +00001218 if (SynchBody.isInvalid)
1219 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1220 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
Fariborz Jahanian993360a2008-01-29 18:21:32 +00001221}
1222
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001223/// objc-try-catch-statement:
1224/// @try compound-statement objc-catch-list[opt]
1225/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1226///
1227/// objc-catch-list:
1228/// @catch ( parameter-declaration ) compound-statement
1229/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1230/// catch-parameter-declaration:
1231/// parameter-declaration
1232/// '...' [OBJC2]
1233///
Chris Lattner80712392008-03-10 06:06:04 +00001234Parser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001235 bool catch_or_finally_seen = false;
Steve Naroffc949a462008-02-05 21:27:35 +00001236
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001237 ConsumeToken(); // consume try
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001238 if (Tok.isNot(tok::l_brace)) {
Chris Lattnerf006a222008-11-18 07:48:38 +00001239 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian70952482007-11-01 21:12:44 +00001240 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001241 }
Fariborz Jahanian06798362007-11-01 23:59:59 +00001242 StmtResult CatchStmts;
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001243 StmtResult FinallyStmt;
Ted Kremenekba849be2008-09-26 17:32:47 +00001244 EnterScope(Scope::DeclScope);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001245 StmtResult TryBody = ParseCompoundStatementBody();
Ted Kremenekba849be2008-09-26 17:32:47 +00001246 ExitScope();
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001247 if (TryBody.isInvalid)
1248 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Chris Lattner80712392008-03-10 06:06:04 +00001249
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001250 while (Tok.is(tok::at)) {
Chris Lattner80712392008-03-10 06:06:04 +00001251 // At this point, we need to lookahead to determine if this @ is the start
1252 // of an @catch or @finally. We don't want to consume the @ token if this
1253 // is an @try or @encode or something else.
1254 Token AfterAt = GetLookAheadToken(1);
1255 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1256 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1257 break;
1258
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001259 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner847f5c12007-12-27 19:57:00 +00001260 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Fariborz Jahanian06798362007-11-01 23:59:59 +00001261 StmtTy *FirstPart = 0;
1262 ConsumeToken(); // consume catch
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001263 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001264 ConsumeParen();
Fariborz Jahanian06798362007-11-01 23:59:59 +00001265 EnterScope(Scope::DeclScope);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001266 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001267 DeclSpec DS;
1268 ParseDeclarationSpecifiers(DS);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001269 // For some odd reason, the name of the exception variable is
1270 // optional. As a result, we need to use PrototypeContext.
1271 Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001272 ParseDeclarator(DeclaratorInfo);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001273 if (DeclaratorInfo.getIdentifier()) {
1274 DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
Daniel Dunbar72eaf8a2008-08-05 16:28:08 +00001275 DeclaratorInfo, 0);
Steve Narofffd8f76c2008-06-03 05:36:54 +00001276 StmtResult stmtResult =
1277 Actions.ActOnDeclStmt(aBlockVarDecl,
1278 DS.getSourceRange().getBegin(),
1279 DeclaratorInfo.getSourceRange().getEnd());
1280 FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1281 }
Steve Naroffc949a462008-02-05 21:27:35 +00001282 } else
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001283 ConsumeToken(); // consume '...'
Fariborz Jahanian06798362007-11-01 23:59:59 +00001284 SourceLocation RParenLoc = ConsumeParen();
Chris Lattner8027be62008-02-14 19:27:54 +00001285
1286 StmtResult CatchBody(true);
1287 if (Tok.is(tok::l_brace))
1288 CatchBody = ParseCompoundStatementBody();
1289 else
1290 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahanian06798362007-11-01 23:59:59 +00001291 if (CatchBody.isInvalid)
1292 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001293 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
Fariborz Jahanian06798362007-11-01 23:59:59 +00001294 FirstPart, CatchBody.Val, CatchStmts.Val);
1295 ExitScope();
Steve Naroffc949a462008-02-05 21:27:35 +00001296 } else {
Chris Lattnerf006a222008-11-18 07:48:38 +00001297 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1298 << "@catch clause";
Fariborz Jahanian70952482007-11-01 21:12:44 +00001299 return true;
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001300 }
1301 catch_or_finally_seen = true;
Chris Lattner80712392008-03-10 06:06:04 +00001302 } else {
1303 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffc949a462008-02-05 21:27:35 +00001304 ConsumeToken(); // consume finally
Ted Kremenek3637b892008-09-26 00:31:16 +00001305 EnterScope(Scope::DeclScope);
1306
Chris Lattner8027be62008-02-14 19:27:54 +00001307
1308 StmtResult FinallyBody(true);
1309 if (Tok.is(tok::l_brace))
1310 FinallyBody = ParseCompoundStatementBody();
1311 else
1312 Diag(Tok, diag::err_expected_lbrace);
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001313 if (FinallyBody.isInvalid)
1314 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Ted Kremenek42730c52008-01-07 19:49:32 +00001315 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Fariborz Jahaniande3abf82007-11-02 00:18:53 +00001316 FinallyBody.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001317 catch_or_finally_seen = true;
Ted Kremenek3637b892008-09-26 00:31:16 +00001318 ExitScope();
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001319 break;
1320 }
1321 }
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001322 if (!catch_or_finally_seen) {
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001323 Diag(atLoc, diag::err_missing_catch_finally);
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001324 return true;
1325 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001326 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
Fariborz Jahanianb8bf6072007-11-02 15:39:31 +00001327 FinallyStmt.Val);
Fariborz Jahanian64b864e2007-09-19 19:14:32 +00001328}
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001329
Steve Naroff81f1bba2007-09-06 21:24:23 +00001330/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001331///
Steve Naroff18c83382007-11-13 23:01:27 +00001332Parser::DeclTy *Parser::ParseObjCMethodDefinition() {
Ted Kremenek42730c52008-01-07 19:49:32 +00001333 DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001334 // parse optional ';'
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001335 if (Tok.is(tok::semi))
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001336 ConsumeToken();
1337
Steve Naroff9191a9e82007-11-11 19:54:21 +00001338 // We should have an opening brace now.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001339 if (Tok.isNot(tok::l_brace)) {
Steve Naroff70f16242008-02-29 21:48:07 +00001340 Diag(Tok, diag::err_expected_method_body);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001341
1342 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1343 SkipUntil(tok::l_brace, true, true);
1344
1345 // If we didn't find the '{', bail out.
1346 if (Tok.isNot(tok::l_brace))
Steve Naroff18c83382007-11-13 23:01:27 +00001347 return 0;
Fariborz Jahanian027c23b2007-09-01 00:26:16 +00001348 }
Steve Naroff9191a9e82007-11-11 19:54:21 +00001349 SourceLocation BraceLoc = Tok.getLocation();
1350
1351 // Enter a scope for the method body.
1352 EnterScope(Scope::FnScope|Scope::DeclScope);
1353
1354 // Tell the actions module that we have entered a method definition with the
Steve Naroff3ac43f92008-07-25 17:57:26 +00001355 // specified Declarator for the method.
1356 Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
Steve Naroff9191a9e82007-11-11 19:54:21 +00001357
1358 StmtResult FnBody = ParseCompoundStatementBody();
1359
1360 // If the function body could not be parsed, make a bogus compoundstmt.
1361 if (FnBody.isInvalid)
1362 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1363
1364 // Leave the function body scope.
1365 ExitScope();
1366
1367 // TODO: Pass argument information.
Steve Naroff3ac43f92008-07-25 17:57:26 +00001368 Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
Steve Naroff18c83382007-11-13 23:01:27 +00001369 return MDecl;
Chris Lattner4b009652007-07-25 00:24:17 +00001370}
Anders Carlssona66cad42007-08-21 17:43:55 +00001371
Steve Naroffc949a462008-02-05 21:27:35 +00001372Parser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1373 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner80712392008-03-10 06:06:04 +00001374 return ParseObjCTryStmt(AtLoc);
Steve Naroffc949a462008-02-05 21:27:35 +00001375 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1376 return ParseObjCThrowStmt(AtLoc);
1377 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1378 return ParseObjCSynchronizedStmt(AtLoc);
1379 ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
1380 if (Res.isInvalid) {
1381 // If the expression is invalid, skip ahead to the next semicolon. Not
1382 // doing this opens us up to the possibility of infinite loops if
1383 // ParseExpression does not consume any tokens.
1384 SkipUntil(tok::semi);
1385 return true;
1386 }
1387 // Otherwise, eat the semicolon.
1388 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1389 return Actions.ActOnExprStmt(Res.Val);
1390}
1391
Steve Narofffb9dd752007-10-15 20:55:58 +00001392Parser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001393 switch (Tok.getKind()) {
Chris Lattnerddd3e632007-12-12 01:04:12 +00001394 case tok::string_literal: // primary-expression: string-literal
1395 case tok::wide_string_literal:
1396 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1397 default:
Chris Lattnerf9311a92008-08-05 06:19:09 +00001398 if (Tok.getIdentifierInfo() == 0)
1399 return Diag(AtLoc, diag::err_unexpected_at);
1400
1401 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1402 case tok::objc_encode:
1403 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1404 case tok::objc_protocol:
1405 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1406 case tok::objc_selector:
1407 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1408 default:
Chris Lattnerf006a222008-11-18 07:48:38 +00001409 return Diag(AtLoc, diag::err_unexpected_at);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001410 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001411 }
Anders Carlssona66cad42007-08-21 17:43:55 +00001412}
1413
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001414/// objc-message-expr:
1415/// '[' objc-receiver objc-message-args ']'
1416///
1417/// objc-receiver:
1418/// expression
1419/// class-name
1420/// type-name
Chris Lattnered27a532008-01-25 18:59:06 +00001421Parser::ExprResult Parser::ParseObjCMessageExpression() {
1422 assert(Tok.is(tok::l_square) && "'[' expected");
1423 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1424
1425 // Parse receiver
Chris Lattnerc0587e12008-01-25 19:25:00 +00001426 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattnered27a532008-01-25 18:59:06 +00001427 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1428 ConsumeToken();
1429 return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1430 }
1431
Chris Lattnerc185e1a2008-09-19 17:44:00 +00001432 ExprResult Res = ParseExpression();
Chris Lattnered27a532008-01-25 18:59:06 +00001433 if (Res.isInvalid) {
Chris Lattnere69015d2008-01-25 19:43:26 +00001434 SkipUntil(tok::r_square);
Chris Lattnered27a532008-01-25 18:59:06 +00001435 return Res;
1436 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001437
Chris Lattnered27a532008-01-25 18:59:06 +00001438 return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1439}
1440
1441/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1442/// the rest of a message expression.
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001443///
1444/// objc-message-args:
1445/// objc-selector
1446/// objc-keywordarg-list
1447///
1448/// objc-keywordarg-list:
1449/// objc-keywordarg
1450/// objc-keywordarg-list objc-keywordarg
1451///
1452/// objc-keywordarg:
1453/// selector-name[opt] ':' objc-keywordexpr
1454///
1455/// objc-keywordexpr:
1456/// nonempty-expr-list
1457///
1458/// nonempty-expr-list:
1459/// assignment-expression
1460/// nonempty-expr-list , assignment-expression
1461///
Chris Lattnered27a532008-01-25 18:59:06 +00001462Parser::ExprResult
1463Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1464 IdentifierInfo *ReceiverName,
1465 ExprTy *ReceiverExpr) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001466 // Parse objc-selector
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001467 SourceLocation Loc;
1468 IdentifierInfo *selIdent = ParseObjCSelector(Loc);
Steve Naroff4ed9d662007-09-27 14:38:14 +00001469
1470 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1471 llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
1472
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001473 if (Tok.is(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001474 while (1) {
1475 // Each iteration parses a single keyword argument.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001476 KeyIdents.push_back(selIdent);
Steve Naroff253118b2007-09-17 20:25:27 +00001477
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001478 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001479 Diag(Tok, diag::err_expected_colon);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001480 // We must manually skip to a ']', otherwise the expression skipper will
1481 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1482 // the enclosing expression.
1483 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001484 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001485 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001486
Steve Naroff4ed9d662007-09-27 14:38:14 +00001487 ConsumeToken(); // Eat the ':'.
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001488 /// Parse the expression after ':'
Steve Naroff253118b2007-09-17 20:25:27 +00001489 ExprResult Res = ParseAssignmentExpression();
1490 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001491 // We must manually skip to a ']', otherwise the expression skipper will
1492 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1493 // the enclosing expression.
1494 SkipUntil(tok::r_square);
Steve Naroff253118b2007-09-17 20:25:27 +00001495 return Res;
1496 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001497
Steve Naroff253118b2007-09-17 20:25:27 +00001498 // We have a valid expression.
Steve Naroff4ed9d662007-09-27 14:38:14 +00001499 KeyExprs.push_back(Res.Val);
Steve Naroff253118b2007-09-17 20:25:27 +00001500
1501 // Check for another keyword selector.
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001502 selIdent = ParseObjCSelector(Loc);
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001503 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001504 break;
1505 // We have a selector or a colon, continue parsing.
1506 }
1507 // Parse the, optional, argument list, comma separated.
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001508 while (Tok.is(tok::comma)) {
Steve Naroff9f176d12007-11-15 13:05:42 +00001509 ConsumeToken(); // Eat the ','.
1510 /// Parse the expression after ','
1511 ExprResult Res = ParseAssignmentExpression();
1512 if (Res.isInvalid) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001513 // We must manually skip to a ']', otherwise the expression skipper will
1514 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1515 // the enclosing expression.
1516 SkipUntil(tok::r_square);
Steve Naroff9f176d12007-11-15 13:05:42 +00001517 return Res;
1518 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001519
Steve Naroff9f176d12007-11-15 13:05:42 +00001520 // We have a valid expression.
1521 KeyExprs.push_back(Res.Val);
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001522 }
1523 } else if (!selIdent) {
1524 Diag(Tok, diag::err_expected_ident); // missing selector name.
Chris Lattnerf9311a92008-08-05 06:19:09 +00001525
1526 // We must manually skip to a ']', otherwise the expression skipper will
1527 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1528 // the enclosing expression.
1529 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001530 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001531 }
Chris Lattnerd031a452007-10-07 02:00:24 +00001532
Chris Lattnera1d2bb72007-10-09 17:51:17 +00001533 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001534 Diag(Tok, diag::err_expected_rsquare);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001535 // We must manually skip to a ']', otherwise the expression skipper will
1536 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1537 // the enclosing expression.
1538 SkipUntil(tok::r_square);
Fariborz Jahanian1fc82242008-01-02 18:09:46 +00001539 return true;
Fariborz Jahaniand4462f92007-09-05 23:08:20 +00001540 }
Chris Lattnerf9311a92008-08-05 06:19:09 +00001541
Chris Lattnered27a532008-01-25 18:59:06 +00001542 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Steve Naroff253118b2007-09-17 20:25:27 +00001543
Steve Narofff9e80db2007-10-05 18:42:47 +00001544 unsigned nKeys = KeyIdents.size();
Chris Lattnerd031a452007-10-07 02:00:24 +00001545 if (nKeys == 0)
1546 KeyIdents.push_back(selIdent);
1547 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1548
1549 // We've just parsed a keyword message.
Steve Naroff253118b2007-09-17 20:25:27 +00001550 if (ReceiverName)
Fariborz Jahanian2ce5dc52007-11-12 20:13:27 +00001551 return Actions.ActOnClassMessage(CurScope,
Chris Lattnered27a532008-01-25 18:59:06 +00001552 ReceiverName, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001553 &KeyExprs[0], KeyExprs.size());
Chris Lattnered27a532008-01-25 18:59:06 +00001554 return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
Steve Naroff9f176d12007-11-15 13:05:42 +00001555 &KeyExprs[0], KeyExprs.size());
Fariborz Jahanian1e534dc2007-09-05 19:52:07 +00001556}
1557
Steve Naroff0add5d22007-11-03 11:27:19 +00001558Parser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Anders Carlssona66cad42007-08-21 17:43:55 +00001559 ExprResult Res = ParseStringLiteralExpression();
Anders Carlssona66cad42007-08-21 17:43:55 +00001560 if (Res.isInvalid) return Res;
Chris Lattnerddd3e632007-12-12 01:04:12 +00001561
1562 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1563 // expressions. At this point, we know that the only valid thing that starts
1564 // with '@' is an @"".
1565 llvm::SmallVector<SourceLocation, 4> AtLocs;
1566 llvm::SmallVector<ExprTy*, 4> AtStrings;
1567 AtLocs.push_back(AtLoc);
1568 AtStrings.push_back(Res.Val);
1569
1570 while (Tok.is(tok::at)) {
1571 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlssona66cad42007-08-21 17:43:55 +00001572
Chris Lattnerddd3e632007-12-12 01:04:12 +00001573 ExprResult Res(true); // Invalid unless there is a string literal.
1574 if (isTokenStringLiteral())
1575 Res = ParseStringLiteralExpression();
1576 else
1577 Diag(Tok, diag::err_objc_concat_string);
1578
1579 if (Res.isInvalid) {
1580 while (!AtStrings.empty()) {
1581 Actions.DeleteExpr(AtStrings.back());
1582 AtStrings.pop_back();
1583 }
1584 return Res;
1585 }
1586
1587 AtStrings.push_back(Res.Val);
1588 }
Fariborz Jahanian1a442d32007-12-12 23:55:49 +00001589
Chris Lattnerddd3e632007-12-12 01:04:12 +00001590 return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1591 AtStrings.size());
Anders Carlssona66cad42007-08-21 17:43:55 +00001592}
Anders Carlsson8be1d402007-08-22 15:14:15 +00001593
1594/// objc-encode-expression:
1595/// @encode ( type-name )
Chris Lattnercfd61c82007-10-16 22:51:17 +00001596Parser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff87c329f2007-08-23 18:16:40 +00001597 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Anders Carlsson8be1d402007-08-22 15:14:15 +00001598
1599 SourceLocation EncLoc = ConsumeToken();
1600
Chris Lattnerf9311a92008-08-05 06:19:09 +00001601 if (Tok.isNot(tok::l_paren))
Chris Lattnerf006a222008-11-18 07:48:38 +00001602 return Diag(Tok, diag::err_expected_lparen_after) << "@encode";
Anders Carlsson8be1d402007-08-22 15:14:15 +00001603
1604 SourceLocation LParenLoc = ConsumeParen();
1605
1606 TypeTy *Ty = ParseTypeName();
1607
Anders Carlsson92faeb82007-08-23 15:31:37 +00001608 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001609
Chris Lattnercfd61c82007-10-16 22:51:17 +00001610 return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
Anders Carlsson92faeb82007-08-23 15:31:37 +00001611 RParenLoc);
Anders Carlsson8be1d402007-08-22 15:14:15 +00001612}
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001613
1614/// objc-protocol-expression
1615/// @protocol ( protocol-name )
1616
Chris Lattnerf006a222008-11-18 07:48:38 +00001617Parser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001618 SourceLocation ProtoLoc = ConsumeToken();
1619
Chris Lattnerf9311a92008-08-05 06:19:09 +00001620 if (Tok.isNot(tok::l_paren))
Chris Lattnerf006a222008-11-18 07:48:38 +00001621 return Diag(Tok, diag::err_expected_lparen_after) << "@protocol";
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001622
1623 SourceLocation LParenLoc = ConsumeParen();
1624
Chris Lattnerf9311a92008-08-05 06:19:09 +00001625 if (Tok.isNot(tok::identifier))
1626 return Diag(Tok, diag::err_expected_ident);
1627
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001628 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001629 ConsumeToken();
1630
Anders Carlsson92faeb82007-08-23 15:31:37 +00001631 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001632
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001633 return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1634 LParenLoc, RParenLoc);
Anders Carlsson2996b4e2007-08-23 15:25:28 +00001635}
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001636
1637/// objc-selector-expression
1638/// @selector '(' objc-keyword-selector ')'
Chris Lattnerf006a222008-11-18 07:48:38 +00001639Parser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001640 SourceLocation SelectorLoc = ConsumeToken();
1641
Chris Lattnerf9311a92008-08-05 06:19:09 +00001642 if (Tok.isNot(tok::l_paren))
Chris Lattnerf006a222008-11-18 07:48:38 +00001643 return Diag(Tok, diag::err_expected_lparen_after) << "@selector";
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001644
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001645 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001646 SourceLocation LParenLoc = ConsumeParen();
1647 SourceLocation sLoc;
1648 IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
Chris Lattnerf9311a92008-08-05 06:19:09 +00001649 if (!SelIdent && Tok.isNot(tok::colon))
1650 return Diag(Tok, diag::err_expected_ident); // missing selector name.
1651
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001652 KeyIdents.push_back(SelIdent);
Steve Naroff6fd89272007-12-05 22:21:29 +00001653 unsigned nColons = 0;
1654 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001655 while (1) {
Chris Lattnerf9311a92008-08-05 06:19:09 +00001656 if (Tok.isNot(tok::colon))
1657 return Diag(Tok, diag::err_expected_colon);
1658
Chris Lattner847f5c12007-12-27 19:57:00 +00001659 nColons++;
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001660 ConsumeToken(); // Eat the ':'.
1661 if (Tok.is(tok::r_paren))
1662 break;
1663 // Check for another keyword selector.
1664 SourceLocation Loc;
1665 SelIdent = ParseObjCSelector(Loc);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001666 KeyIdents.push_back(SelIdent);
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001667 if (!SelIdent && Tok.isNot(tok::colon))
1668 break;
1669 }
Steve Naroff6fd89272007-12-05 22:21:29 +00001670 }
Fariborz Jahanian056c6b02007-10-15 23:39:13 +00001671 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff6fd89272007-12-05 22:21:29 +00001672 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Fariborz Jahanian957448a2007-10-16 23:21:02 +00001673 return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001674 RParenLoc);
Gabor Greifa823dd12007-10-19 15:38:32 +00001675 }