blob: 5ba1dd17bdf89b5a4d1f62b5df0651a44a1e9a2b [file] [log] [blame]
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Reid Spencer5f016e22007-07-11 17:01:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Naroff4985ace2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21
Chris Lattner891dca62008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Reid Spencer5f016e22007-07-11 17:01:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroff91fa0b72007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Chris Lattnerb28317a2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Reid Spencer5f016e22007-07-11 17:01:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump1eb44332009-09-09 15:08:12 +000032
Steve Naroff861cf3e2007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattner5ffb14b2008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000053 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000054 }
55}
56
57///
Mike Stump1eb44332009-09-09 15:08:12 +000058/// objc-class-declaration:
Reid Spencer5f016e22007-07-11 17:01:13 +000059/// '@' 'class' identifier-list ';'
Mike Stump1eb44332009-09-09 15:08:12 +000060///
Chris Lattnerb28317a2009-03-28 19:18:32 +000061Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Reid Spencer5f016e22007-07-11 17:01:13 +000062 ConsumeToken(); // the identifier "class"
63 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremenekc09cba62009-11-17 23:12:20 +000064 llvm::SmallVector<SourceLocation, 8> ClassLocs;
65
Mike Stump1eb44332009-09-09 15:08:12 +000066
Reid Spencer5f016e22007-07-11 17:01:13 +000067 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000068 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000069 Diag(Tok, diag::err_expected_ident);
70 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000071 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000072 }
Reid Spencer5f016e22007-07-11 17:01:13 +000073 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremenekc09cba62009-11-17 23:12:20 +000074 ClassLocs.push_back(Tok.getLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +000075 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chris Lattnerdf195262007-10-09 17:51:17 +000077 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000078 break;
Mike Stump1eb44332009-09-09 15:08:12 +000079
Reid Spencer5f016e22007-07-11 17:01:13 +000080 ConsumeToken();
81 }
Mike Stump1eb44332009-09-09 15:08:12 +000082
Reid Spencer5f016e22007-07-11 17:01:13 +000083 // Consume the ';'.
84 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000085 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremenekc09cba62009-11-17 23:12:20 +000087 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
88 ClassLocs.data(),
89 ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000090}
91
Steve Naroffdac269b2007-08-20 21:31:48 +000092///
93/// objc-interface:
94/// objc-class-interface-attributes[opt] objc-class-interface
95/// objc-category-interface
96///
97/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +000098/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +000099/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +0000100/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +0000101/// objc-interface-decl-list
102/// @end
103///
104/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000105/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
107/// objc-interface-decl-list
108/// @end
109///
110/// objc-superclass:
111/// ':' identifier
112///
113/// objc-class-interface-attributes:
114/// __attribute__((visibility("default")))
115/// __attribute__((visibility("hidden")))
116/// __attribute__((deprecated))
117/// __attribute__((unavailable))
118/// __attribute__((objc_exception)) - used by NSException on 64-bit
119///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000120Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000121 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000122 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000123 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
124 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000125
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000126 // Code completion after '@interface'.
127 if (Tok.is(tok::code_completion)) {
128 Actions.CodeCompleteObjCInterfaceDecl(CurScope);
129 ConsumeToken();
130 }
131
Chris Lattnerdf195262007-10-09 17:51:17 +0000132 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000133 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000134 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000135 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000136
Steve Naroffdac269b2007-08-20 21:31:48 +0000137 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000138 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000139 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Chris Lattnerdf195262007-10-09 17:51:17 +0000141 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 SourceLocation lparenLoc = ConsumeParen();
143 SourceLocation categoryLoc, rparenLoc;
144 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Douglas Gregor33ced0b2009-11-18 19:08:43 +0000146 if (Tok.is(tok::code_completion)) {
147 Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
148 ConsumeToken();
149 }
150
Steve Naroff527fe232007-08-23 19:56:30 +0000151 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000152 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000153 categoryId = Tok.getIdentifierInfo();
154 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000155 } else if (!getLang().ObjC2) {
156 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000157 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000158 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000159 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000160 Diag(Tok, diag::err_expected_rparen);
161 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000162 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000163 }
164 rparenLoc = ConsumeParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000165
Steve Naroffdac269b2007-08-20 21:31:48 +0000166 // Next, we need to check for any protocol references.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000167 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000168 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000169 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000170 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000171 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
172 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000173 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Steve Naroffdac269b2007-08-20 21:31:48 +0000175 if (attrList) // categories don't support attributes.
176 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Jay Foadbeaaccd2009-05-21 09:52:38 +0000178 DeclPtrTy CategoryType =
Mike Stump1eb44332009-09-09 15:08:12 +0000179 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000180 nameId, nameLoc,
181 categoryId, categoryLoc,
182 ProtocolRefs.data(),
183 ProtocolRefs.size(),
184 EndProtoLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000186 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000187 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000188 }
189 // Parse a class interface.
190 IdentifierInfo *superClassId = 0;
191 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000192
Chris Lattnerdf195262007-10-09 17:51:17 +0000193 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000194 ConsumeToken();
Douglas Gregor3b49aca2009-11-18 16:26:39 +0000195
196 // Code completion of superclass names.
197 if (Tok.is(tok::code_completion)) {
198 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
199 ConsumeToken();
200 }
201
Chris Lattnerdf195262007-10-09 17:51:17 +0000202 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000203 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000204 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000205 }
206 superClassId = Tok.getIdentifierInfo();
207 superClassLoc = ConsumeToken();
208 }
209 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000210 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000211 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
212 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000213 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000214 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
215 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000216 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000217
218 DeclPtrTy ClsType =
219 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000220 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000221 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattner06036d32008-07-26 04:13:19 +0000222 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattnerdf195262007-10-09 17:51:17 +0000224 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000225 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000226
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000227 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000228 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000229}
230
231/// objc-interface-decl-list:
232/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000233/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000234/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000235/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000236/// objc-interface-decl-list declaration
237/// objc-interface-decl-list ';'
238///
Steve Naroff294494e2007-08-22 16:35:03 +0000239/// objc-method-requirement: [OBJC2]
240/// @required
241/// @optional
242///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000243void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000244 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000245 llvm::SmallVector<DeclPtrTy, 32> allMethods;
246 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000247 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000248 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000249
Chris Lattnerbc662af2008-10-20 06:10:06 +0000250 SourceLocation AtEndLoc;
251
Steve Naroff294494e2007-08-22 16:35:03 +0000252 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000253 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000254 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000255 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000256 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000257 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000258 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
259 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000260 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
261 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000262 continue;
263 }
Mike Stump1eb44332009-09-09 15:08:12 +0000264
Chris Lattnere82a10f2008-10-20 05:46:22 +0000265 // Ignore excess semicolons.
266 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000267 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000268 continue;
269 }
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattnerbc662af2008-10-20 06:10:06 +0000271 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000272 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000273 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnere82a10f2008-10-20 05:46:22 +0000275 // If we don't have an @ directive, parse it as a function definition.
276 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000277 // The code below does not consume '}'s because it is afraid of eating the
278 // end of a namespace. Because of the way this code is structured, an
279 // erroneous r_brace would cause an infinite loop if not handled here.
280 if (Tok.is(tok::r_brace))
281 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000282
Steve Naroff4985ace2007-08-22 18:35:33 +0000283 // FIXME: as the name implies, this rule allows function definitions.
284 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner682bf922009-03-29 16:50:03 +0000285 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000286 continue;
287 }
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattnere82a10f2008-10-20 05:46:22 +0000289 // Otherwise, we have an @ directive, eat the @.
290 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000291 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chris Lattnera2449b22008-10-20 05:57:40 +0000293 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000294 AtEndLoc = AtLoc;
295 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000296 }
Mike Stump1eb44332009-09-09 15:08:12 +0000297
Chris Lattnerbc662af2008-10-20 06:10:06 +0000298 // Eat the identifier.
299 ConsumeToken();
300
Chris Lattnera2449b22008-10-20 05:57:40 +0000301 switch (DirectiveKind) {
302 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000303 // FIXME: If someone forgets an @end on a protocol, this loop will
304 // continue to eat up tons of stuff and spew lots of nonsense errors. It
305 // would probably be better to bail out if we saw an @class or @interface
306 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000307 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000308 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000309 SkipUntil(tok::r_brace, tok::at);
310 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000311
Chris Lattnera2449b22008-10-20 05:57:40 +0000312 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000313 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000314 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000315 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000316 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000317 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000318 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000319 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000320 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattnera2449b22008-10-20 05:57:40 +0000322 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000323 if (!getLang().ObjC2)
324 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
325
Chris Lattnere82a10f2008-10-20 05:46:22 +0000326 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000327 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000328 if (Tok.is(tok::l_paren))
Douglas Gregor4ad96852009-11-19 07:41:15 +0000329 ParseObjCPropertyAttribute(OCDS, interfaceDecl,
330 allMethods.data(), allMethods.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000331
John McCallbdd563e2009-11-03 02:38:08 +0000332 struct ObjCPropertyCallback : FieldCallback {
333 Parser &P;
334 DeclPtrTy IDecl;
335 llvm::SmallVectorImpl<DeclPtrTy> &Props;
336 ObjCDeclSpec &OCDS;
337 SourceLocation AtLoc;
338 tok::ObjCKeywordKind MethodImplKind;
339
340 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
341 llvm::SmallVectorImpl<DeclPtrTy> &Props,
342 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
343 tok::ObjCKeywordKind MethodImplKind) :
344 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
345 MethodImplKind(MethodImplKind) {
346 }
347
348 DeclPtrTy invoke(FieldDeclarator &FD) {
349 if (FD.D.getIdentifier() == 0) {
350 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
351 << FD.D.getSourceRange();
352 return DeclPtrTy();
353 }
354 if (FD.BitfieldSize) {
355 P.Diag(AtLoc, diag::err_objc_property_bitfield)
356 << FD.D.getSourceRange();
357 return DeclPtrTy();
358 }
359
360 // Install the property declarator into interfaceDecl.
361 IdentifierInfo *SelName =
362 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
363
364 Selector GetterSel =
365 P.PP.getSelectorTable().getNullarySelector(SelName);
366 IdentifierInfo *SetterName = OCDS.getSetterName();
367 Selector SetterSel;
368 if (SetterName)
369 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
370 else
371 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
372 P.PP.getSelectorTable(),
373 FD.D.getIdentifier());
374 bool isOverridingProperty = false;
375 DeclPtrTy Property =
376 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
377 GetterSel, SetterSel, IDecl,
378 &isOverridingProperty,
379 MethodImplKind);
380 if (!isOverridingProperty)
381 Props.push_back(Property);
382
383 return Property;
384 }
385 } Callback(*this, interfaceDecl, allProperties,
386 OCDS, AtLoc, MethodImplKind);
387
Chris Lattnere82a10f2008-10-20 05:46:22 +0000388 // Parse all the comma separated declarators.
389 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000390 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000392 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
393 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000394 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000395 }
Steve Naroff294494e2007-08-22 16:35:03 +0000396 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000397
398 // We break out of the big loop in two cases: when we see @end or when we see
399 // EOF. In the former case, eat the @end. In the later case, emit an error.
400 if (Tok.isObjCAtKeyword(tok::objc_end))
401 ConsumeToken(); // the "end" identifier
402 else
403 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattnera2449b22008-10-20 05:57:40 +0000405 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000406 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000407 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000408 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000409 allProperties.data(), allProperties.size(),
410 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000411}
412
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000413/// Parse property attribute declarations.
414///
415/// property-attr-decl: '(' property-attrlist ')'
416/// property-attrlist:
417/// property-attribute
418/// property-attrlist ',' property-attribute
419/// property-attribute:
420/// getter '=' identifier
421/// setter '=' identifier ':'
422/// readonly
423/// readwrite
424/// assign
425/// retain
426/// copy
427/// nonatomic
428///
Douglas Gregor4ad96852009-11-19 07:41:15 +0000429void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
430 DeclPtrTy *Methods,
431 unsigned NumMethods) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000432 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000433 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000435 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000436 if (Tok.is(tok::code_completion)) {
Douglas Gregora93b1082009-11-18 23:08:07 +0000437 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroffece8e712009-10-08 21:55:05 +0000438 ConsumeToken();
439 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000440 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000442 // If this is not an identifier at all, bail out early.
443 if (II == 0) {
444 MatchRHSPunctuation(tok::r_paren, LHSLoc);
445 return;
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Chris Lattner156b0612008-10-20 07:37:22 +0000448 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner92e62b02008-11-20 04:42:34 +0000450 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000451 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000452 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000453 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000454 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000455 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000456 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000457 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000458 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000459 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000460 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000461 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000462 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000463 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000464 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
465 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000466 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Douglas Gregor4ad96852009-11-19 07:41:15 +0000468 if (Tok.is(tok::code_completion)) {
469 if (II->getNameStart()[0] == 's')
470 Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
471 Methods, NumMethods);
472 else
473 Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
474 Methods, NumMethods);
475 ConsumeToken();
476 }
477
Chris Lattner8ca329c2008-10-20 07:24:39 +0000478 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000479 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000480 SkipUntil(tok::r_paren);
481 return;
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Daniel Dunbare013d682009-10-18 20:26:12 +0000484 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000485 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
486 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000487 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Chris Lattner156b0612008-10-20 07:37:22 +0000489 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
490 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000491 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000492 } else {
493 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
494 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000495 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000496 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000497 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000498 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000499 SkipUntil(tok::r_paren);
500 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000501 }
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattner156b0612008-10-20 07:37:22 +0000503 if (Tok.isNot(tok::comma))
504 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000505
Chris Lattner156b0612008-10-20 07:37:22 +0000506 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000507 }
Mike Stump1eb44332009-09-09 15:08:12 +0000508
Chris Lattner156b0612008-10-20 07:37:22 +0000509 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000510}
511
Steve Naroff3536b442007-09-06 21:24:23 +0000512/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000513/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000514/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000515///
516/// objc-instance-method: '-'
517/// objc-class-method: '+'
518///
Steve Naroff4985ace2007-08-22 18:35:33 +0000519/// objc-method-attributes: [OBJC2]
520/// __attribute__((deprecated))
521///
Mike Stump1eb44332009-09-09 15:08:12 +0000522Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000523 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000524 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000525
Mike Stump1eb44332009-09-09 15:08:12 +0000526 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000527 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattnerb28317a2009-03-28 19:18:32 +0000529 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000530 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000531 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000532 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000533}
534
535/// objc-selector:
536/// identifier
537/// one of
538/// enum struct union if else while do for switch case default
539/// break continue return goto asm sizeof typeof __alignof
540/// unsigned long const short volatile signed restrict _Complex
541/// in out inout bycopy byref oneway int char float double void _Bool
542///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000543IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000544 switch (Tok.getKind()) {
545 default:
546 return 0;
547 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000548 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000549 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000550 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000551 case tok::kw_break:
552 case tok::kw_case:
553 case tok::kw_catch:
554 case tok::kw_char:
555 case tok::kw_class:
556 case tok::kw_const:
557 case tok::kw_const_cast:
558 case tok::kw_continue:
559 case tok::kw_default:
560 case tok::kw_delete:
561 case tok::kw_do:
562 case tok::kw_double:
563 case tok::kw_dynamic_cast:
564 case tok::kw_else:
565 case tok::kw_enum:
566 case tok::kw_explicit:
567 case tok::kw_export:
568 case tok::kw_extern:
569 case tok::kw_false:
570 case tok::kw_float:
571 case tok::kw_for:
572 case tok::kw_friend:
573 case tok::kw_goto:
574 case tok::kw_if:
575 case tok::kw_inline:
576 case tok::kw_int:
577 case tok::kw_long:
578 case tok::kw_mutable:
579 case tok::kw_namespace:
580 case tok::kw_new:
581 case tok::kw_operator:
582 case tok::kw_private:
583 case tok::kw_protected:
584 case tok::kw_public:
585 case tok::kw_register:
586 case tok::kw_reinterpret_cast:
587 case tok::kw_restrict:
588 case tok::kw_return:
589 case tok::kw_short:
590 case tok::kw_signed:
591 case tok::kw_sizeof:
592 case tok::kw_static:
593 case tok::kw_static_cast:
594 case tok::kw_struct:
595 case tok::kw_switch:
596 case tok::kw_template:
597 case tok::kw_this:
598 case tok::kw_throw:
599 case tok::kw_true:
600 case tok::kw_try:
601 case tok::kw_typedef:
602 case tok::kw_typeid:
603 case tok::kw_typename:
604 case tok::kw_typeof:
605 case tok::kw_union:
606 case tok::kw_unsigned:
607 case tok::kw_using:
608 case tok::kw_virtual:
609 case tok::kw_void:
610 case tok::kw_volatile:
611 case tok::kw_wchar_t:
612 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000613 case tok::kw__Bool:
614 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000615 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000616 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000617 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000618 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000619 }
Steve Naroff294494e2007-08-22 16:35:03 +0000620}
621
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000622/// objc-for-collection-in: 'in'
623///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000624bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000625 // FIXME: May have to do additional look-ahead to only allow for
626 // valid tokens following an 'in'; such as an identifier, unary operators,
627 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000628 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000629 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000630}
631
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000632/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000633/// qualifier list and builds their bitmask representation in the input
634/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000635///
636/// objc-type-qualifiers:
637/// objc-type-qualifier
638/// objc-type-qualifiers objc-type-qualifier
639///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000641 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000642 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000643 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Chris Lattnere8b724d2007-12-12 06:56:32 +0000645 const IdentifierInfo *II = Tok.getIdentifierInfo();
646 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000648 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000650 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000651 switch (i) {
652 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000653 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
654 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
655 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
656 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
657 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
658 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000659 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000660 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000661 ConsumeToken();
662 II = 0;
663 break;
664 }
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Chris Lattnere8b724d2007-12-12 06:56:32 +0000666 // If this wasn't a recognized qualifier, bail out.
667 if (II) return;
668 }
669}
670
671/// objc-type-name:
672/// '(' objc-type-qualifiers[opt] type-name ')'
673/// '(' objc-type-qualifiers[opt] ')'
674///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000675Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000676 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Chris Lattner4a76b292008-10-22 03:52:06 +0000678 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000679 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000681 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000682 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000683
Chris Lattner4a76b292008-10-22 03:52:06 +0000684 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000685 if (isTypeSpecifierQualifier()) {
686 TypeResult TypeSpec = ParseTypeName();
687 if (!TypeSpec.isInvalid())
688 Ty = TypeSpec.get();
689 }
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Steve Naroffd7333c22008-10-21 14:15:04 +0000691 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000692 ConsumeParen();
693 else if (Tok.getLocation() == TypeStartLoc) {
694 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000695 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000696 SkipUntil(tok::r_paren);
697 } else {
698 // Otherwise, we found *something*, but didn't get a ')' in the right
699 // place. Emit an error then return what we have as the type.
700 MatchRHSPunctuation(tok::r_paren, LParenLoc);
701 }
Steve Narofff28b2642007-09-05 23:30:30 +0000702 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000703}
704
705/// objc-method-decl:
706/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000707/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000708/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000709/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000710///
711/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000712/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000713/// objc-keyword-selector objc-keyword-decl
714///
715/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000716/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
717/// objc-selector ':' objc-keyword-attributes[opt] identifier
718/// ':' objc-type-name objc-keyword-attributes[opt] identifier
719/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000720///
Steve Naroff4985ace2007-08-22 18:35:33 +0000721/// objc-parmlist:
722/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000723///
Steve Naroff4985ace2007-08-22 18:35:33 +0000724/// objc-parms:
725/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000726///
Steve Naroff4985ace2007-08-22 18:35:33 +0000727/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000728/// , ...
729///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000730/// objc-keyword-attributes: [OBJC2]
731/// __attribute__((unused))
732///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000733Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
734 tok::TokenKind mType,
735 DeclPtrTy IDecl,
736 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000737 ParsingDeclRAIIObject PD(*this);
738
Chris Lattnere8904e92008-08-23 01:48:03 +0000739 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000740 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000741 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000742 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000743 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Steve Naroffbef11852007-10-26 20:53:56 +0000745 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000746 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000747
Steve Naroff84c43102009-02-11 20:43:13 +0000748 // An unnamed colon is valid.
749 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000750 Diag(Tok, diag::err_expected_selector_for_method)
751 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000752 // Skip until we get a ; or {}.
753 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000754 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000755 }
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000757 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000758 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000759 // If attributes exist after the method, parse them.
760 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000761 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000762 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Chris Lattnerff384912007-10-07 02:00:24 +0000764 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000765 DeclPtrTy Result
766 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000767 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000768 0, CargNames, MethodAttrs,
769 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000770 PD.complete(Result);
771 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000772 }
Steve Narofff28b2642007-09-05 23:30:30 +0000773
Steve Naroff68d331a2007-09-27 14:38:14 +0000774 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000775 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Chris Lattnerff384912007-10-07 02:00:24 +0000777 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000778 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Chris Lattnerff384912007-10-07 02:00:24 +0000780 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000781 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000782 Diag(Tok, diag::err_expected_colon);
783 break;
784 }
785 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Chris Lattnere294d3f2009-04-11 18:57:04 +0000787 ArgInfo.Type = 0;
788 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
789 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
790
Chris Lattnerff384912007-10-07 02:00:24 +0000791 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000792 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000793 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnere294d3f2009-04-11 18:57:04 +0000794 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000795
Chris Lattnerdf195262007-10-09 17:51:17 +0000796 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000797 Diag(Tok, diag::err_expected_ident); // missing argument name.
798 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000799 }
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattnere294d3f2009-04-11 18:57:04 +0000801 ArgInfo.Name = Tok.getIdentifierInfo();
802 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000803 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Chris Lattnere294d3f2009-04-11 18:57:04 +0000805 ArgInfos.push_back(ArgInfo);
806 KeyIdents.push_back(SelIdent);
807
Chris Lattnerff384912007-10-07 02:00:24 +0000808 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000809 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000810 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000811 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000812 break;
813 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000814 }
Mike Stump1eb44332009-09-09 15:08:12 +0000815
Steve Naroff335eafa2007-11-15 12:35:21 +0000816 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Chris Lattnerff384912007-10-07 02:00:24 +0000818 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000819 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000820 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000821 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000822 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000823 ConsumeToken();
824 break;
825 }
Chris Lattnerff384912007-10-07 02:00:24 +0000826 DeclSpec DS;
827 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000828 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000829 Declarator ParmDecl(DS, Declarator::PrototypeContext);
830 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000831 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000832 }
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattnerff384912007-10-07 02:00:24 +0000834 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000835 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000836 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000837 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000838 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000840 if (KeyIdents.size() == 0)
841 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000842 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
843 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000844 DeclPtrTy Result
845 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000846 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000847 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000848 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000849 PD.complete(Result);
850 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000851}
852
Steve Naroffdac269b2007-08-20 21:31:48 +0000853/// objc-protocol-refs:
854/// '<' identifier-list '>'
855///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000856bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000857ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000858 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
859 bool WarnOnDeclarations,
860 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000861 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000863 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattnere13b9592008-07-26 04:03:38 +0000865 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattnere13b9592008-07-26 04:03:38 +0000867 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000868 if (Tok.is(tok::code_completion)) {
869 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
870 ProtocolIdents.size());
871 ConsumeToken();
872 }
873
Chris Lattnere13b9592008-07-26 04:03:38 +0000874 if (Tok.isNot(tok::identifier)) {
875 Diag(Tok, diag::err_expected_ident);
876 SkipUntil(tok::greater);
877 return true;
878 }
879 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
880 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000881 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000882 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Chris Lattnere13b9592008-07-26 04:03:38 +0000884 if (Tok.isNot(tok::comma))
885 break;
886 ConsumeToken();
887 }
Mike Stump1eb44332009-09-09 15:08:12 +0000888
Chris Lattnere13b9592008-07-26 04:03:38 +0000889 // Consume the '>'.
890 if (Tok.isNot(tok::greater)) {
891 Diag(Tok, diag::err_expected_greater);
892 return true;
893 }
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Chris Lattnere13b9592008-07-26 04:03:38 +0000895 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chris Lattnere13b9592008-07-26 04:03:38 +0000897 // Convert the list of protocols identifiers into a list of protocol decls.
898 Actions.FindProtocolDeclaration(WarnOnDeclarations,
899 &ProtocolIdents[0], ProtocolIdents.size(),
900 Protocols);
901 return false;
902}
903
Steve Naroffdac269b2007-08-20 21:31:48 +0000904/// objc-class-instance-variables:
905/// '{' objc-instance-variable-decl-list[opt] '}'
906///
907/// objc-instance-variable-decl-list:
908/// objc-visibility-spec
909/// objc-instance-variable-decl ';'
910/// ';'
911/// objc-instance-variable-decl-list objc-visibility-spec
912/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
913/// objc-instance-variable-decl-list ';'
914///
915/// objc-visibility-spec:
916/// @private
917/// @protected
918/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000919/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000920///
921/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000922/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000923///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000924void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000925 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000926 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000927 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000928
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000929 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000930
Steve Naroffddbff782007-08-21 21:17:12 +0000931 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000933 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000934 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000935 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000936 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Steve Naroffddbff782007-08-21 21:17:12 +0000938 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000939 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000940 Diag(Tok, diag::ext_extra_struct_semi)
941 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroffddbff782007-08-21 21:17:12 +0000942 ConsumeToken();
943 continue;
944 }
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Steve Naroffddbff782007-08-21 21:17:12 +0000946 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000947 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000948 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000949 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000950 case tok::objc_private:
951 case tok::objc_public:
952 case tok::objc_protected:
953 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000954 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000955 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000956 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000957 default:
958 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000959 continue;
960 }
961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
John McCallbdd563e2009-11-03 02:38:08 +0000963 struct ObjCIvarCallback : FieldCallback {
964 Parser &P;
965 DeclPtrTy IDecl;
966 tok::ObjCKeywordKind visibility;
967 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
968
969 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
970 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
971 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
972 }
973
974 DeclPtrTy invoke(FieldDeclarator &FD) {
975 // Install the declarator into the interface decl.
976 DeclPtrTy Field
977 = P.Actions.ActOnIvar(P.CurScope,
978 FD.D.getDeclSpec().getSourceRange().getBegin(),
979 IDecl, FD.D, FD.BitfieldSize, visibility);
980 AllIvarDecls.push_back(Field);
981 return Field;
982 }
983 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
984
Chris Lattnere1359422008-04-10 06:46:29 +0000985 // Parse all the comma separated declarators.
986 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000987 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattnerdf195262007-10-09 17:51:17 +0000989 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000990 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000991 } else {
992 Diag(Tok, diag::err_expected_semi_decl_list);
993 // Skip to end of block or statement
994 SkipUntil(tok::r_brace, true, true);
995 }
996 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000997 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000998 // Call ActOnFields() even if we don't have any decls. This is useful
999 // for code rewriting tools that need to be aware of the empty list.
1000 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001001 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00001002 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +00001003 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00001004}
Steve Naroffdac269b2007-08-20 21:31:48 +00001005
1006/// objc-protocol-declaration:
1007/// objc-protocol-definition
1008/// objc-protocol-forward-reference
1009///
1010/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +00001011/// @protocol identifier
1012/// objc-protocol-refs[opt]
1013/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001014/// @end
1015///
1016/// objc-protocol-forward-reference:
1017/// @protocol identifier-list ';'
1018///
1019/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001020/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001021/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001022Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1023 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001024 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001025 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1026 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Douglas Gregor083128f2009-11-18 04:49:41 +00001028 if (Tok.is(tok::code_completion)) {
1029 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1030 ConsumeToken();
1031 }
1032
Chris Lattnerdf195262007-10-09 17:51:17 +00001033 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001034 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001035 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001036 }
1037 // Save the protocol name, then consume it.
1038 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1039 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Chris Lattnerdf195262007-10-09 17:51:17 +00001041 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001042 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001043 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001044 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001045 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001046 }
Mike Stump1eb44332009-09-09 15:08:12 +00001047
Chris Lattnerdf195262007-10-09 17:51:17 +00001048 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001049 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1050 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1051
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001052 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001053 while (1) {
1054 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001055 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001056 Diag(Tok, diag::err_expected_ident);
1057 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001058 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001059 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001060 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1061 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001062 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Chris Lattnerdf195262007-10-09 17:51:17 +00001064 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001065 break;
1066 }
1067 // Consume the ';'.
1068 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001069 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Steve Naroffe440eb82007-10-10 17:32:04 +00001071 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001072 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001073 ProtocolRefs.size(),
1074 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001075 }
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001077 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001078 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001079
Chris Lattnerb28317a2009-03-28 19:18:32 +00001080 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001081 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001082 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001083 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1084 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001085 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001086
Chris Lattnerb28317a2009-03-28 19:18:32 +00001087 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001088 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001089 ProtocolRefs.data(),
1090 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001091 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001092 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001093 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001094}
Steve Naroffdac269b2007-08-20 21:31:48 +00001095
1096/// objc-implementation:
1097/// objc-class-implementation-prologue
1098/// objc-category-implementation-prologue
1099///
1100/// objc-class-implementation-prologue:
1101/// @implementation identifier objc-superclass[opt]
1102/// objc-class-instance-variables[opt]
1103///
1104/// objc-category-implementation-prologue:
1105/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001106Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001107 SourceLocation atLoc) {
1108 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1109 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1110 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001112 // Code completion after '@implementation'.
1113 if (Tok.is(tok::code_completion)) {
1114 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1115 ConsumeToken();
1116 }
1117
Chris Lattnerdf195262007-10-09 17:51:17 +00001118 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001119 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001120 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001121 }
1122 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001123 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001124 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001125
1126 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001127 // we have a category implementation.
1128 SourceLocation lparenLoc = ConsumeParen();
1129 SourceLocation categoryLoc, rparenLoc;
1130 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001132 if (Tok.is(tok::code_completion)) {
1133 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1134 ConsumeToken();
1135 }
1136
Chris Lattnerdf195262007-10-09 17:51:17 +00001137 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001138 categoryId = Tok.getIdentifierInfo();
1139 categoryLoc = ConsumeToken();
1140 } else {
1141 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001142 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001143 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001144 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001145 Diag(Tok, diag::err_expected_rparen);
1146 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001147 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001148 }
1149 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001150 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001151 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001152 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001153 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001154 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001155 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001156 }
1157 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001158 SourceLocation superClassLoc;
1159 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001160 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001161 // We have a super class
1162 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001163 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001164 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001165 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001166 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001167 superClassId = Tok.getIdentifierInfo();
1168 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001169 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001170 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001171 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001172 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001173
Steve Naroff60fccee2007-10-29 21:38:07 +00001174 if (Tok.is(tok::l_brace)) // we have ivars
1175 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001176 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001177 PendingObjCImpDecl.push_back(ObjCImpDecl);
1178
Chris Lattnerb28317a2009-03-28 19:18:32 +00001179 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001180}
Steve Naroff60fccee2007-10-29 21:38:07 +00001181
Chris Lattnerb28317a2009-03-28 19:18:32 +00001182Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001183 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1184 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001185 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001186 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001187 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001188 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001189 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001190 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001191 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001192 else
1193 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001194 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001195}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001196
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001197Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001198 if (PendingObjCImpDecl.empty())
1199 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1200 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1201 Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
1202 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1203}
1204
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001205/// compatibility-alias-decl:
1206/// @compatibility_alias alias-name class-name ';'
1207///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001208Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001209 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1210 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1211 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001212 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001213 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001214 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001215 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001216 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1217 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001218 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001219 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001220 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001221 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001222 IdentifierInfo *classId = Tok.getIdentifierInfo();
1223 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1224 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001225 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001226 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001227 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001228 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1229 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230}
1231
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001232/// property-synthesis:
1233/// @synthesize property-ivar-list ';'
1234///
1235/// property-ivar-list:
1236/// property-ivar
1237/// property-ivar-list ',' property-ivar
1238///
1239/// property-ivar:
1240/// identifier
1241/// identifier '=' identifier
1242///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001243Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001244 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1245 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001246 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001247
Douglas Gregorb328c422009-11-18 19:45:45 +00001248 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001249 if (Tok.is(tok::code_completion)) {
Douglas Gregor424b2a52009-11-18 22:56:13 +00001250 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor322328b2009-11-18 22:32:06 +00001251 ConsumeToken();
1252 }
1253
Douglas Gregorb328c422009-11-18 19:45:45 +00001254 if (Tok.isNot(tok::identifier)) {
1255 Diag(Tok, diag::err_synthesized_property_name);
1256 SkipUntil(tok::semi);
1257 return DeclPtrTy();
1258 }
1259
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001260 IdentifierInfo *propertyIvar = 0;
1261 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1262 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001263 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001264 // property '=' ivar-name
1265 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001266
1267 if (Tok.is(tok::code_completion)) {
1268 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1269 ObjCImpDecl);
1270 ConsumeToken();
1271 }
1272
Chris Lattnerdf195262007-10-09 17:51:17 +00001273 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001274 Diag(Tok, diag::err_expected_ident);
1275 break;
1276 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001277 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001278 ConsumeToken(); // consume ivar-name
1279 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001280 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1281 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001282 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001283 break;
1284 ConsumeToken(); // consume ','
1285 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001286 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001287 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001288 SkipUntil(tok::semi);
1289 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001290 else
1291 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001292 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001293}
1294
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001295/// property-dynamic:
1296/// @dynamic property-list
1297///
1298/// property-list:
1299/// identifier
1300/// property-list ',' identifier
1301///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001302Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001303 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1304 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1305 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001306 while (true) {
1307 if (Tok.is(tok::code_completion)) {
1308 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1309 ConsumeToken();
1310 }
1311
1312 if (Tok.isNot(tok::identifier)) {
1313 Diag(Tok, diag::err_expected_ident);
1314 SkipUntil(tok::semi);
1315 return DeclPtrTy();
1316 }
1317
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001318 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1319 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1320 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1321 propertyId, 0);
1322
Chris Lattnerdf195262007-10-09 17:51:17 +00001323 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001324 break;
1325 ConsumeToken(); // consume ','
1326 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001327 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001328 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001329 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001330}
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001332/// objc-throw-statement:
1333/// throw expression[opt];
1334///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001335Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001336 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001337 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001338 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001339 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001340 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001341 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001342 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001343 }
1344 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001345 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001346 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001347}
1348
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001349/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001350/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001351///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001352Parser::OwningStmtResult
1353Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001354 ConsumeToken(); // consume synchronized
1355 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001356 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001357 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001358 }
1359 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001360 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001361 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001362 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001363 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001364 }
1365 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001366 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001367 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001368 }
1369 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001370 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001371 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001372 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001373 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001374 // Enter a scope to hold everything within the compound stmt. Compound
1375 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001376 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001377
Sebastian Redl61364dd2008-12-11 19:30:53 +00001378 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001379
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001380 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001381 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001382 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001383 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001384}
1385
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001386/// objc-try-catch-statement:
1387/// @try compound-statement objc-catch-list[opt]
1388/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1389///
1390/// objc-catch-list:
1391/// @catch ( parameter-declaration ) compound-statement
1392/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1393/// catch-parameter-declaration:
1394/// parameter-declaration
1395/// '...' [OBJC2]
1396///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001397Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001398 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001399
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001400 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001401 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001402 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001403 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001404 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001405 OwningStmtResult CatchStmts(Actions);
1406 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001407 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001408 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001409 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001410 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001411 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001412
Chris Lattnerdf195262007-10-09 17:51:17 +00001413 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001414 // At this point, we need to lookahead to determine if this @ is the start
1415 // of an @catch or @finally. We don't want to consume the @ token if this
1416 // is an @try or @encode or something else.
1417 Token AfterAt = GetLookAheadToken(1);
1418 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1419 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1420 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001421
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001422 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001423 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001424 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001425 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001426 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001427 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001428 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001429 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001430 DeclSpec DS;
1431 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001432 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001433 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001434 // we must accept either 'declarator' or 'abstract-declarator' here.
1435 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1436 ParseDeclarator(ParmDecl);
1437
1438 // Inform the actions module about the parameter declarator, so it
1439 // gets added to the current scope.
1440 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001441 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001442 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Steve Naroff93a25952009-04-07 22:56:58 +00001444 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001445
Steve Naroff93a25952009-04-07 22:56:58 +00001446 if (Tok.is(tok::r_paren))
1447 RParenLoc = ConsumeParen();
1448 else // Skip over garbage, until we get to ')'. Eat the ')'.
1449 SkipUntil(tok::r_paren, true, false);
1450
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001451 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001452 if (Tok.is(tok::l_brace))
1453 CatchBody = ParseCompoundStatementBody();
1454 else
1455 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001456 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001457 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001458 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001459 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001460 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001461 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001462 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1463 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001464 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001465 }
1466 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001467 } else {
1468 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001469 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001470 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001471
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001472 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001473 if (Tok.is(tok::l_brace))
1474 FinallyBody = ParseCompoundStatementBody();
1475 else
1476 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001477 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001478 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001479 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001480 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001481 catch_or_finally_seen = true;
1482 break;
1483 }
1484 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001485 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001486 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001487 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001488 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001489 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1490 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001491}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001492
Steve Naroff3536b442007-09-06 21:24:23 +00001493/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001494///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001495Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1496 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001497
Chris Lattner49f28ca2009-03-05 08:00:35 +00001498 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1499 PP.getSourceManager(),
1500 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001502 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001503 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001504 if (ObjCImpDecl) {
1505 Diag(Tok, diag::warn_semicolon_before_method_body)
1506 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1507 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001508 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001509 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001510
Steve Naroff409be832007-11-11 19:54:21 +00001511 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001512 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001513 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Steve Naroff409be832007-11-11 19:54:21 +00001515 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1516 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Steve Naroff409be832007-11-11 19:54:21 +00001518 // If we didn't find the '{', bail out.
1519 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001520 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001521 }
Steve Naroff409be832007-11-11 19:54:21 +00001522 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Steve Naroff409be832007-11-11 19:54:21 +00001524 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001525 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001526
Steve Naroff409be832007-11-11 19:54:21 +00001527 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001528 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001529 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001530
1531 OwningStmtResult FnBody(ParseCompoundStatementBody());
1532
Steve Naroff409be832007-11-11 19:54:21 +00001533 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001534 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001535 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1536 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001537
Steve Naroff32ce8372009-03-02 22:00:56 +00001538 // TODO: Pass argument information.
1539 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001540
Steve Naroff409be832007-11-11 19:54:21 +00001541 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001542 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001543
Steve Naroff71c0a952007-11-13 23:01:27 +00001544 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001545}
Anders Carlsson55085182007-08-21 17:43:55 +00001546
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001547Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001548 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001549 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001550 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1551 return ParseObjCThrowStmt(AtLoc);
1552 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1553 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001554 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001555 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001556 // If the expression is invalid, skip ahead to the next semicolon. Not
1557 // doing this opens us up to the possibility of infinite loops if
1558 // ParseExpression does not consume any tokens.
1559 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001560 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001561 }
1562 // Otherwise, eat the semicolon.
1563 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001564 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001565}
1566
Sebastian Redl1d922962008-12-13 15:32:12 +00001567Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001568 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001569 case tok::string_literal: // primary-expression: string-literal
1570 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001571 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001572 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001573 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001574 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001575
Chris Lattner4fef81d2008-08-05 06:19:09 +00001576 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1577 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001578 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001579 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001580 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001581 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001582 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001583 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001584 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001585 }
Anders Carlsson55085182007-08-21 17:43:55 +00001586 }
Anders Carlsson55085182007-08-21 17:43:55 +00001587}
1588
Mike Stump1eb44332009-09-09 15:08:12 +00001589/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001590/// '[' objc-receiver objc-message-args ']'
1591///
1592/// objc-receiver:
1593/// expression
1594/// class-name
1595/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001596Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001597 assert(Tok.is(tok::l_square) && "'[' expected");
1598 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1599
1600 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001601 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001602 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001603 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1604 SourceLocation NameLoc = ConsumeToken();
1605 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1606 ExprArg(Actions));
1607 }
Chris Lattner699b6612008-01-25 18:59:06 +00001608 }
1609
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001610 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001611 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001612 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001613 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001614 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001615
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001616 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001617 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001618}
Sebastian Redl1d922962008-12-13 15:32:12 +00001619
Chris Lattner699b6612008-01-25 18:59:06 +00001620/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1621/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001622///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001623/// objc-message-args:
1624/// objc-selector
1625/// objc-keywordarg-list
1626///
1627/// objc-keywordarg-list:
1628/// objc-keywordarg
1629/// objc-keywordarg-list objc-keywordarg
1630///
Mike Stump1eb44332009-09-09 15:08:12 +00001631/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001632/// selector-name[opt] ':' objc-keywordexpr
1633///
1634/// objc-keywordexpr:
1635/// nonempty-expr-list
1636///
1637/// nonempty-expr-list:
1638/// assignment-expression
1639/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001640///
1641Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001642Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001643 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001644 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001645 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001646 if (Tok.is(tok::code_completion)) {
1647 if (ReceiverName)
Douglas Gregord3c68542009-11-19 01:08:35 +00001648 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1649 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001650 else
Douglas Gregord3c68542009-11-19 01:08:35 +00001651 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1652 0, 0);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001653 ConsumeToken();
1654 }
Douglas Gregord3c68542009-11-19 01:08:35 +00001655
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001656 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001657 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001658 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001659
Anders Carlssonff975cf2009-02-14 18:21:46 +00001660 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Steve Naroff68d331a2007-09-27 14:38:14 +00001662 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001663 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001664
Chris Lattnerdf195262007-10-09 17:51:17 +00001665 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001666 while (1) {
1667 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001668 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001669
Chris Lattnerdf195262007-10-09 17:51:17 +00001670 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001671 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001672 // We must manually skip to a ']', otherwise the expression skipper will
1673 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1674 // the enclosing expression.
1675 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001676 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001677 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001678
Steve Naroff68d331a2007-09-27 14:38:14 +00001679 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001680 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001681 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001682 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001683 // We must manually skip to a ']', otherwise the expression skipper will
1684 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1685 // the enclosing expression.
1686 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001687 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001688 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001689
Steve Naroff37387c92007-09-17 20:25:27 +00001690 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001691 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001692
Douglas Gregord3c68542009-11-19 01:08:35 +00001693 // Code completion after each argument.
1694 if (Tok.is(tok::code_completion)) {
1695 if (ReceiverName)
1696 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1697 KeyIdents.data(),
1698 KeyIdents.size());
1699 else
1700 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1701 KeyIdents.data(),
1702 KeyIdents.size());
1703 ConsumeToken();
1704 }
1705
Steve Naroff37387c92007-09-17 20:25:27 +00001706 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001707 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001708 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001709 break;
1710 // We have a selector or a colon, continue parsing.
1711 }
1712 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001713 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001714 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001715 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001716 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001717 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001718 // We must manually skip to a ']', otherwise the expression skipper will
1719 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1720 // the enclosing expression.
1721 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001722 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001723 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001724
Steve Naroff49f109c2007-11-15 13:05:42 +00001725 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001726 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001727 }
1728 } else if (!selIdent) {
1729 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001730
Chris Lattner4fef81d2008-08-05 06:19:09 +00001731 // We must manually skip to a ']', otherwise the expression skipper will
1732 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1733 // the enclosing expression.
1734 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001735 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001736 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001737
Chris Lattnerdf195262007-10-09 17:51:17 +00001738 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001739 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001740 // We must manually skip to a ']', otherwise the expression skipper will
1741 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1742 // the enclosing expression.
1743 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001744 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001745 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001746
Chris Lattner699b6612008-01-25 18:59:06 +00001747 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001748
Steve Naroff29238a02007-10-05 18:42:47 +00001749 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001750 if (nKeys == 0)
1751 KeyIdents.push_back(selIdent);
1752 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001753
Chris Lattnerff384912007-10-07 02:00:24 +00001754 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001755 if (ReceiverName)
1756 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001757 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001758 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001759 KeyExprs.take(), KeyExprs.size()));
1760 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001761 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001762 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001763}
1764
Sebastian Redl1d922962008-12-13 15:32:12 +00001765Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001766 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001767 if (Res.isInvalid()) return move(Res);
1768
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001769 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1770 // expressions. At this point, we know that the only valid thing that starts
1771 // with '@' is an @"".
1772 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001773 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001774 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001775 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001776
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001777 while (Tok.is(tok::at)) {
1778 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001779
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001780 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001781 if (!isTokenStringLiteral())
1782 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001783
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001784 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001785 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001786 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001787
Sebastian Redleffa8d12008-12-10 00:02:53 +00001788 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001789 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001790
1791 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1792 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001793}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001794
1795/// objc-encode-expression:
1796/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001797Parser::OwningExprResult
1798Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001799 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001800
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001801 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001802
Chris Lattner4fef81d2008-08-05 06:19:09 +00001803 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001804 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1805
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001806 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001807
Douglas Gregor809070a2009-02-18 17:45:20 +00001808 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001809
Anders Carlsson4988ae32007-08-23 15:31:37 +00001810 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001811
Douglas Gregor809070a2009-02-18 17:45:20 +00001812 if (Ty.isInvalid())
1813 return ExprError();
1814
Mike Stump1eb44332009-09-09 15:08:12 +00001815 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001816 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001817}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001818
1819/// objc-protocol-expression
1820/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001821Parser::OwningExprResult
1822Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001823 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001824
Chris Lattner4fef81d2008-08-05 06:19:09 +00001825 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001826 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1827
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001828 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001829
Chris Lattner4fef81d2008-08-05 06:19:09 +00001830 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001831 return ExprError(Diag(Tok, diag::err_expected_ident));
1832
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001833 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001834 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001835
Anders Carlsson4988ae32007-08-23 15:31:37 +00001836 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001837
Sebastian Redl1d922962008-12-13 15:32:12 +00001838 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1839 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001840}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001841
1842/// objc-selector-expression
1843/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001844Parser::OwningExprResult
1845Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001846 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001847
Chris Lattner4fef81d2008-08-05 06:19:09 +00001848 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001849 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1850
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001851 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001852 SourceLocation LParenLoc = ConsumeParen();
1853 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001854 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001855 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1856 return ExprError(Diag(Tok, diag::err_expected_ident));
1857
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001858 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001859 unsigned nColons = 0;
1860 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001861 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001862 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001863 return ExprError(Diag(Tok, diag::err_expected_colon));
1864
Chris Lattnercb53b362007-12-27 19:57:00 +00001865 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001866 ConsumeToken(); // Eat the ':'.
1867 if (Tok.is(tok::r_paren))
1868 break;
1869 // Check for another keyword selector.
1870 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001871 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001872 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001873 if (!SelIdent && Tok.isNot(tok::colon))
1874 break;
1875 }
Steve Naroff887407e2007-12-05 22:21:29 +00001876 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001877 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001878 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001879 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1880 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001881 }