blob: fa14bb9a52e3df4704c3f3ecef485732403587de [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))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000329 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000330
John McCallbdd563e2009-11-03 02:38:08 +0000331 struct ObjCPropertyCallback : FieldCallback {
332 Parser &P;
333 DeclPtrTy IDecl;
334 llvm::SmallVectorImpl<DeclPtrTy> &Props;
335 ObjCDeclSpec &OCDS;
336 SourceLocation AtLoc;
337 tok::ObjCKeywordKind MethodImplKind;
338
339 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
340 llvm::SmallVectorImpl<DeclPtrTy> &Props,
341 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
342 tok::ObjCKeywordKind MethodImplKind) :
343 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
344 MethodImplKind(MethodImplKind) {
345 }
346
347 DeclPtrTy invoke(FieldDeclarator &FD) {
348 if (FD.D.getIdentifier() == 0) {
349 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
350 << FD.D.getSourceRange();
351 return DeclPtrTy();
352 }
353 if (FD.BitfieldSize) {
354 P.Diag(AtLoc, diag::err_objc_property_bitfield)
355 << FD.D.getSourceRange();
356 return DeclPtrTy();
357 }
358
359 // Install the property declarator into interfaceDecl.
360 IdentifierInfo *SelName =
361 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
362
363 Selector GetterSel =
364 P.PP.getSelectorTable().getNullarySelector(SelName);
365 IdentifierInfo *SetterName = OCDS.getSetterName();
366 Selector SetterSel;
367 if (SetterName)
368 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
369 else
370 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
371 P.PP.getSelectorTable(),
372 FD.D.getIdentifier());
373 bool isOverridingProperty = false;
374 DeclPtrTy Property =
375 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
376 GetterSel, SetterSel, IDecl,
377 &isOverridingProperty,
378 MethodImplKind);
379 if (!isOverridingProperty)
380 Props.push_back(Property);
381
382 return Property;
383 }
384 } Callback(*this, interfaceDecl, allProperties,
385 OCDS, AtLoc, MethodImplKind);
386
Chris Lattnere82a10f2008-10-20 05:46:22 +0000387 // Parse all the comma separated declarators.
388 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000389 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000391 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
392 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000393 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000394 }
Steve Naroff294494e2007-08-22 16:35:03 +0000395 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000396
397 // We break out of the big loop in two cases: when we see @end or when we see
398 // EOF. In the former case, eat the @end. In the later case, emit an error.
399 if (Tok.isObjCAtKeyword(tok::objc_end))
400 ConsumeToken(); // the "end" identifier
401 else
402 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattnera2449b22008-10-20 05:57:40 +0000404 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000405 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000406 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000407 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000408 allProperties.data(), allProperties.size(),
409 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000410}
411
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000412/// Parse property attribute declarations.
413///
414/// property-attr-decl: '(' property-attrlist ')'
415/// property-attrlist:
416/// property-attribute
417/// property-attrlist ',' property-attribute
418/// property-attribute:
419/// getter '=' identifier
420/// setter '=' identifier ':'
421/// readonly
422/// readwrite
423/// assign
424/// retain
425/// copy
426/// nonatomic
427///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000428void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000429 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000430 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000432 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000433 if (Tok.is(tok::code_completion)) {
Douglas Gregora93b1082009-11-18 23:08:07 +0000434 Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
Steve Naroffece8e712009-10-08 21:55:05 +0000435 ConsumeToken();
436 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000437 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000439 // If this is not an identifier at all, bail out early.
440 if (II == 0) {
441 MatchRHSPunctuation(tok::r_paren, LHSLoc);
442 return;
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Chris Lattner156b0612008-10-20 07:37:22 +0000445 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattner92e62b02008-11-20 04:42:34 +0000447 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000448 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000449 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000450 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000451 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000452 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000453 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000454 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000455 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000456 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000457 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000458 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000459 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000460 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000461 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
462 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000463 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000464
Chris Lattner8ca329c2008-10-20 07:24:39 +0000465 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000466 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000467 SkipUntil(tok::r_paren);
468 return;
469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Daniel Dunbare013d682009-10-18 20:26:12 +0000471 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000472 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
473 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000474 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Chris Lattner156b0612008-10-20 07:37:22 +0000476 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
477 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000478 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000479 } else {
480 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
481 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000482 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000483 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000484 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000485 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000486 SkipUntil(tok::r_paren);
487 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000488 }
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattner156b0612008-10-20 07:37:22 +0000490 if (Tok.isNot(tok::comma))
491 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattner156b0612008-10-20 07:37:22 +0000493 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000494 }
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Chris Lattner156b0612008-10-20 07:37:22 +0000496 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000497}
498
Steve Naroff3536b442007-09-06 21:24:23 +0000499/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000500/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000501/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000502///
503/// objc-instance-method: '-'
504/// objc-class-method: '+'
505///
Steve Naroff4985ace2007-08-22 18:35:33 +0000506/// objc-method-attributes: [OBJC2]
507/// __attribute__((deprecated))
508///
Mike Stump1eb44332009-09-09 15:08:12 +0000509Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000510 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000511 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000512
Mike Stump1eb44332009-09-09 15:08:12 +0000513 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000514 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Chris Lattnerb28317a2009-03-28 19:18:32 +0000516 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000517 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000518 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000519 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000520}
521
522/// objc-selector:
523/// identifier
524/// one of
525/// enum struct union if else while do for switch case default
526/// break continue return goto asm sizeof typeof __alignof
527/// unsigned long const short volatile signed restrict _Complex
528/// in out inout bycopy byref oneway int char float double void _Bool
529///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000530IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000531 switch (Tok.getKind()) {
532 default:
533 return 0;
534 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000535 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000536 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000537 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000538 case tok::kw_break:
539 case tok::kw_case:
540 case tok::kw_catch:
541 case tok::kw_char:
542 case tok::kw_class:
543 case tok::kw_const:
544 case tok::kw_const_cast:
545 case tok::kw_continue:
546 case tok::kw_default:
547 case tok::kw_delete:
548 case tok::kw_do:
549 case tok::kw_double:
550 case tok::kw_dynamic_cast:
551 case tok::kw_else:
552 case tok::kw_enum:
553 case tok::kw_explicit:
554 case tok::kw_export:
555 case tok::kw_extern:
556 case tok::kw_false:
557 case tok::kw_float:
558 case tok::kw_for:
559 case tok::kw_friend:
560 case tok::kw_goto:
561 case tok::kw_if:
562 case tok::kw_inline:
563 case tok::kw_int:
564 case tok::kw_long:
565 case tok::kw_mutable:
566 case tok::kw_namespace:
567 case tok::kw_new:
568 case tok::kw_operator:
569 case tok::kw_private:
570 case tok::kw_protected:
571 case tok::kw_public:
572 case tok::kw_register:
573 case tok::kw_reinterpret_cast:
574 case tok::kw_restrict:
575 case tok::kw_return:
576 case tok::kw_short:
577 case tok::kw_signed:
578 case tok::kw_sizeof:
579 case tok::kw_static:
580 case tok::kw_static_cast:
581 case tok::kw_struct:
582 case tok::kw_switch:
583 case tok::kw_template:
584 case tok::kw_this:
585 case tok::kw_throw:
586 case tok::kw_true:
587 case tok::kw_try:
588 case tok::kw_typedef:
589 case tok::kw_typeid:
590 case tok::kw_typename:
591 case tok::kw_typeof:
592 case tok::kw_union:
593 case tok::kw_unsigned:
594 case tok::kw_using:
595 case tok::kw_virtual:
596 case tok::kw_void:
597 case tok::kw_volatile:
598 case tok::kw_wchar_t:
599 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000600 case tok::kw__Bool:
601 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000602 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000603 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000604 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000605 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000606 }
Steve Naroff294494e2007-08-22 16:35:03 +0000607}
608
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000609/// objc-for-collection-in: 'in'
610///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000611bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000612 // FIXME: May have to do additional look-ahead to only allow for
613 // valid tokens following an 'in'; such as an identifier, unary operators,
614 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000615 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000616 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000617}
618
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000619/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000620/// qualifier list and builds their bitmask representation in the input
621/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000622///
623/// objc-type-qualifiers:
624/// objc-type-qualifier
625/// objc-type-qualifiers objc-type-qualifier
626///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000627void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000628 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000629 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000630 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattnere8b724d2007-12-12 06:56:32 +0000632 const IdentifierInfo *II = Tok.getIdentifierInfo();
633 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000634 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000635 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000636
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000637 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000638 switch (i) {
639 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
641 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
642 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
643 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
644 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
645 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000646 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000648 ConsumeToken();
649 II = 0;
650 break;
651 }
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Chris Lattnere8b724d2007-12-12 06:56:32 +0000653 // If this wasn't a recognized qualifier, bail out.
654 if (II) return;
655 }
656}
657
658/// objc-type-name:
659/// '(' objc-type-qualifiers[opt] type-name ')'
660/// '(' objc-type-qualifiers[opt] ')'
661///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000662Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000663 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000664
Chris Lattner4a76b292008-10-22 03:52:06 +0000665 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000666 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000668 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000669 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000670
Chris Lattner4a76b292008-10-22 03:52:06 +0000671 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000672 if (isTypeSpecifierQualifier()) {
673 TypeResult TypeSpec = ParseTypeName();
674 if (!TypeSpec.isInvalid())
675 Ty = TypeSpec.get();
676 }
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Steve Naroffd7333c22008-10-21 14:15:04 +0000678 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000679 ConsumeParen();
680 else if (Tok.getLocation() == TypeStartLoc) {
681 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000682 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000683 SkipUntil(tok::r_paren);
684 } else {
685 // Otherwise, we found *something*, but didn't get a ')' in the right
686 // place. Emit an error then return what we have as the type.
687 MatchRHSPunctuation(tok::r_paren, LParenLoc);
688 }
Steve Narofff28b2642007-09-05 23:30:30 +0000689 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000690}
691
692/// objc-method-decl:
693/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000694/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000695/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000696/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000697///
698/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000699/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000700/// objc-keyword-selector objc-keyword-decl
701///
702/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000703/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
704/// objc-selector ':' objc-keyword-attributes[opt] identifier
705/// ':' objc-type-name objc-keyword-attributes[opt] identifier
706/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000707///
Steve Naroff4985ace2007-08-22 18:35:33 +0000708/// objc-parmlist:
709/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000710///
Steve Naroff4985ace2007-08-22 18:35:33 +0000711/// objc-parms:
712/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000713///
Steve Naroff4985ace2007-08-22 18:35:33 +0000714/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000715/// , ...
716///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000717/// objc-keyword-attributes: [OBJC2]
718/// __attribute__((unused))
719///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000720Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
721 tok::TokenKind mType,
722 DeclPtrTy IDecl,
723 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000724 ParsingDeclRAIIObject PD(*this);
725
Chris Lattnere8904e92008-08-23 01:48:03 +0000726 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000727 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000728 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000729 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000730 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000731
Steve Naroffbef11852007-10-26 20:53:56 +0000732 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000733 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000734
Steve Naroff84c43102009-02-11 20:43:13 +0000735 // An unnamed colon is valid.
736 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000737 Diag(Tok, diag::err_expected_selector_for_method)
738 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000739 // Skip until we get a ; or {}.
740 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000741 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000744 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000745 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000746 // If attributes exist after the method, parse them.
747 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000748 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000749 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Chris Lattnerff384912007-10-07 02:00:24 +0000751 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000752 DeclPtrTy Result
753 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000754 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000755 0, CargNames, MethodAttrs,
756 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000757 PD.complete(Result);
758 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000759 }
Steve Narofff28b2642007-09-05 23:30:30 +0000760
Steve Naroff68d331a2007-09-27 14:38:14 +0000761 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000762 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000763
Chris Lattnerff384912007-10-07 02:00:24 +0000764 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000765 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Chris Lattnerff384912007-10-07 02:00:24 +0000767 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000768 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000769 Diag(Tok, diag::err_expected_colon);
770 break;
771 }
772 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Chris Lattnere294d3f2009-04-11 18:57:04 +0000774 ArgInfo.Type = 0;
775 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
776 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
777
Chris Lattnerff384912007-10-07 02:00:24 +0000778 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000779 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000780 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnere294d3f2009-04-11 18:57:04 +0000781 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000782
Chris Lattnerdf195262007-10-09 17:51:17 +0000783 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000784 Diag(Tok, diag::err_expected_ident); // missing argument name.
785 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000786 }
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Chris Lattnere294d3f2009-04-11 18:57:04 +0000788 ArgInfo.Name = Tok.getIdentifierInfo();
789 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000790 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Chris Lattnere294d3f2009-04-11 18:57:04 +0000792 ArgInfos.push_back(ArgInfo);
793 KeyIdents.push_back(SelIdent);
794
Chris Lattnerff384912007-10-07 02:00:24 +0000795 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000796 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000797 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000798 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000799 break;
800 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000801 }
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Steve Naroff335eafa2007-11-15 12:35:21 +0000803 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Chris Lattnerff384912007-10-07 02:00:24 +0000805 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000806 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000807 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000808 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000809 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000810 ConsumeToken();
811 break;
812 }
Chris Lattnerff384912007-10-07 02:00:24 +0000813 DeclSpec DS;
814 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000815 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000816 Declarator ParmDecl(DS, Declarator::PrototypeContext);
817 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000818 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Chris Lattnerff384912007-10-07 02:00:24 +0000821 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000822 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000823 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000824 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000825 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000827 if (KeyIdents.size() == 0)
828 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000829 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
830 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000831 DeclPtrTy Result
832 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000833 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000834 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000835 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000836 PD.complete(Result);
837 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000838}
839
Steve Naroffdac269b2007-08-20 21:31:48 +0000840/// objc-protocol-refs:
841/// '<' identifier-list '>'
842///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000843bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000844ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000845 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
846 bool WarnOnDeclarations,
847 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000848 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000850 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Chris Lattnere13b9592008-07-26 04:03:38 +0000852 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattnere13b9592008-07-26 04:03:38 +0000854 while (1) {
Douglas Gregor55385fe2009-11-18 04:19:12 +0000855 if (Tok.is(tok::code_completion)) {
856 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
857 ProtocolIdents.size());
858 ConsumeToken();
859 }
860
Chris Lattnere13b9592008-07-26 04:03:38 +0000861 if (Tok.isNot(tok::identifier)) {
862 Diag(Tok, diag::err_expected_ident);
863 SkipUntil(tok::greater);
864 return true;
865 }
866 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
867 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000868 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000869 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Chris Lattnere13b9592008-07-26 04:03:38 +0000871 if (Tok.isNot(tok::comma))
872 break;
873 ConsumeToken();
874 }
Mike Stump1eb44332009-09-09 15:08:12 +0000875
Chris Lattnere13b9592008-07-26 04:03:38 +0000876 // Consume the '>'.
877 if (Tok.isNot(tok::greater)) {
878 Diag(Tok, diag::err_expected_greater);
879 return true;
880 }
Mike Stump1eb44332009-09-09 15:08:12 +0000881
Chris Lattnere13b9592008-07-26 04:03:38 +0000882 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Chris Lattnere13b9592008-07-26 04:03:38 +0000884 // Convert the list of protocols identifiers into a list of protocol decls.
885 Actions.FindProtocolDeclaration(WarnOnDeclarations,
886 &ProtocolIdents[0], ProtocolIdents.size(),
887 Protocols);
888 return false;
889}
890
Steve Naroffdac269b2007-08-20 21:31:48 +0000891/// objc-class-instance-variables:
892/// '{' objc-instance-variable-decl-list[opt] '}'
893///
894/// objc-instance-variable-decl-list:
895/// objc-visibility-spec
896/// objc-instance-variable-decl ';'
897/// ';'
898/// objc-instance-variable-decl-list objc-visibility-spec
899/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
900/// objc-instance-variable-decl-list ';'
901///
902/// objc-visibility-spec:
903/// @private
904/// @protected
905/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000906/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000907///
908/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000909/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000910///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000911void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000912 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000913 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000914 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000915
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000916 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000917
Steve Naroffddbff782007-08-21 21:17:12 +0000918 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000920 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000921 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000922 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000923 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Steve Naroffddbff782007-08-21 21:17:12 +0000925 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000926 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000927 Diag(Tok, diag::ext_extra_struct_semi)
928 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroffddbff782007-08-21 21:17:12 +0000929 ConsumeToken();
930 continue;
931 }
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Steve Naroffddbff782007-08-21 21:17:12 +0000933 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000934 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000935 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000936 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000937 case tok::objc_private:
938 case tok::objc_public:
939 case tok::objc_protected:
940 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000941 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000942 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000943 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000944 default:
945 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000946 continue;
947 }
948 }
Mike Stump1eb44332009-09-09 15:08:12 +0000949
John McCallbdd563e2009-11-03 02:38:08 +0000950 struct ObjCIvarCallback : FieldCallback {
951 Parser &P;
952 DeclPtrTy IDecl;
953 tok::ObjCKeywordKind visibility;
954 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
955
956 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
957 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
958 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
959 }
960
961 DeclPtrTy invoke(FieldDeclarator &FD) {
962 // Install the declarator into the interface decl.
963 DeclPtrTy Field
964 = P.Actions.ActOnIvar(P.CurScope,
965 FD.D.getDeclSpec().getSourceRange().getBegin(),
966 IDecl, FD.D, FD.BitfieldSize, visibility);
967 AllIvarDecls.push_back(Field);
968 return Field;
969 }
970 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
971
Chris Lattnere1359422008-04-10 06:46:29 +0000972 // Parse all the comma separated declarators.
973 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000974 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattnerdf195262007-10-09 17:51:17 +0000976 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000977 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000978 } else {
979 Diag(Tok, diag::err_expected_semi_decl_list);
980 // Skip to end of block or statement
981 SkipUntil(tok::r_brace, true, true);
982 }
983 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000984 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000985 // Call ActOnFields() even if we don't have any decls. This is useful
986 // for code rewriting tools that need to be aware of the empty list.
987 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000988 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000989 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000990 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000991}
Steve Naroffdac269b2007-08-20 21:31:48 +0000992
993/// objc-protocol-declaration:
994/// objc-protocol-definition
995/// objc-protocol-forward-reference
996///
997/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +0000998/// @protocol identifier
999/// objc-protocol-refs[opt]
1000/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +00001001/// @end
1002///
1003/// objc-protocol-forward-reference:
1004/// @protocol identifier-list ';'
1005///
1006/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +00001007/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +00001008/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001009Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1010 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001011 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001012 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1013 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Douglas Gregor083128f2009-11-18 04:49:41 +00001015 if (Tok.is(tok::code_completion)) {
1016 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1017 ConsumeToken();
1018 }
1019
Chris Lattnerdf195262007-10-09 17:51:17 +00001020 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001021 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001022 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001023 }
1024 // Save the protocol name, then consume it.
1025 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1026 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattnerdf195262007-10-09 17:51:17 +00001028 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001029 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001030 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001031 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001032 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001033 }
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Chris Lattnerdf195262007-10-09 17:51:17 +00001035 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001036 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1037 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1038
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001039 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001040 while (1) {
1041 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001042 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001043 Diag(Tok, diag::err_expected_ident);
1044 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001045 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001046 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001047 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1048 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001049 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001050
Chris Lattnerdf195262007-10-09 17:51:17 +00001051 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001052 break;
1053 }
1054 // Consume the ';'.
1055 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001056 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Steve Naroffe440eb82007-10-10 17:32:04 +00001058 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001059 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001060 ProtocolRefs.size(),
1061 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001064 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001065 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001066
Chris Lattnerb28317a2009-03-28 19:18:32 +00001067 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001068 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001069 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001070 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1071 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001072 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001073
Chris Lattnerb28317a2009-03-28 19:18:32 +00001074 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001075 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001076 ProtocolRefs.data(),
1077 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001078 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001079 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001080 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001081}
Steve Naroffdac269b2007-08-20 21:31:48 +00001082
1083/// objc-implementation:
1084/// objc-class-implementation-prologue
1085/// objc-category-implementation-prologue
1086///
1087/// objc-class-implementation-prologue:
1088/// @implementation identifier objc-superclass[opt]
1089/// objc-class-instance-variables[opt]
1090///
1091/// objc-category-implementation-prologue:
1092/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001093Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001094 SourceLocation atLoc) {
1095 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1096 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1097 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Douglas Gregor3b49aca2009-11-18 16:26:39 +00001099 // Code completion after '@implementation'.
1100 if (Tok.is(tok::code_completion)) {
1101 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1102 ConsumeToken();
1103 }
1104
Chris Lattnerdf195262007-10-09 17:51:17 +00001105 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001106 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001107 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001108 }
1109 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001110 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001111 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001112
1113 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001114 // we have a category implementation.
1115 SourceLocation lparenLoc = ConsumeParen();
1116 SourceLocation categoryLoc, rparenLoc;
1117 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Douglas Gregor33ced0b2009-11-18 19:08:43 +00001119 if (Tok.is(tok::code_completion)) {
1120 Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
1121 ConsumeToken();
1122 }
1123
Chris Lattnerdf195262007-10-09 17:51:17 +00001124 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001125 categoryId = Tok.getIdentifierInfo();
1126 categoryLoc = ConsumeToken();
1127 } else {
1128 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001129 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001130 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001131 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001132 Diag(Tok, diag::err_expected_rparen);
1133 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001134 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001135 }
1136 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001137 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001138 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001139 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001140 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001141 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001142 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001143 }
1144 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001145 SourceLocation superClassLoc;
1146 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001147 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001148 // We have a super class
1149 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001150 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001151 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001152 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001153 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001154 superClassId = Tok.getIdentifierInfo();
1155 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001156 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001157 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001158 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001159 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001160
Steve Naroff60fccee2007-10-29 21:38:07 +00001161 if (Tok.is(tok::l_brace)) // we have ivars
1162 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001163 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001164 PendingObjCImpDecl.push_back(ObjCImpDecl);
1165
Chris Lattnerb28317a2009-03-28 19:18:32 +00001166 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001167}
Steve Naroff60fccee2007-10-29 21:38:07 +00001168
Chris Lattnerb28317a2009-03-28 19:18:32 +00001169Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001170 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1171 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001172 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001173 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001174 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001175 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001176 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001177 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001178 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001179 else
1180 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001181 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001182}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001183
Fariborz Jahanian1ac71042009-11-17 17:15:16 +00001184Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001185 if (PendingObjCImpDecl.empty())
1186 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1187 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1188 Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
1189 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1190}
1191
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001192/// compatibility-alias-decl:
1193/// @compatibility_alias alias-name class-name ';'
1194///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001195Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001196 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1197 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1198 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001199 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001200 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001201 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001202 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001203 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1204 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001205 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001206 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001207 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001208 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001209 IdentifierInfo *classId = Tok.getIdentifierInfo();
1210 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1211 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001212 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001213 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001214 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001215 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1216 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001217}
1218
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001219/// property-synthesis:
1220/// @synthesize property-ivar-list ';'
1221///
1222/// property-ivar-list:
1223/// property-ivar
1224/// property-ivar-list ',' property-ivar
1225///
1226/// property-ivar:
1227/// identifier
1228/// identifier '=' identifier
1229///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001230Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001231 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1232 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001233 SourceLocation loc = ConsumeToken(); // consume synthesize
Mike Stump1eb44332009-09-09 15:08:12 +00001234
Douglas Gregorb328c422009-11-18 19:45:45 +00001235 while (true) {
Douglas Gregor322328b2009-11-18 22:32:06 +00001236 if (Tok.is(tok::code_completion)) {
Douglas Gregor424b2a52009-11-18 22:56:13 +00001237 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
Douglas Gregor322328b2009-11-18 22:32:06 +00001238 ConsumeToken();
1239 }
1240
Douglas Gregorb328c422009-11-18 19:45:45 +00001241 if (Tok.isNot(tok::identifier)) {
1242 Diag(Tok, diag::err_synthesized_property_name);
1243 SkipUntil(tok::semi);
1244 return DeclPtrTy();
1245 }
1246
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001247 IdentifierInfo *propertyIvar = 0;
1248 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1249 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001250 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001251 // property '=' ivar-name
1252 ConsumeToken(); // consume '='
Douglas Gregor322328b2009-11-18 22:32:06 +00001253
1254 if (Tok.is(tok::code_completion)) {
1255 Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1256 ObjCImpDecl);
1257 ConsumeToken();
1258 }
1259
Chris Lattnerdf195262007-10-09 17:51:17 +00001260 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001261 Diag(Tok, diag::err_expected_ident);
1262 break;
1263 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001264 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001265 ConsumeToken(); // consume ivar-name
1266 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001267 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1268 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001269 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001270 break;
1271 ConsumeToken(); // consume ','
1272 }
Douglas Gregorb328c422009-11-18 19:45:45 +00001273 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001274 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Douglas Gregorb328c422009-11-18 19:45:45 +00001275 SkipUntil(tok::semi);
1276 }
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001277 else
1278 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001279 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001280}
1281
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001282/// property-dynamic:
1283/// @dynamic property-list
1284///
1285/// property-list:
1286/// identifier
1287/// property-list ',' identifier
1288///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001289Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001290 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1291 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1292 SourceLocation loc = ConsumeToken(); // consume dynamic
Douglas Gregor424b2a52009-11-18 22:56:13 +00001293 while (true) {
1294 if (Tok.is(tok::code_completion)) {
1295 Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1296 ConsumeToken();
1297 }
1298
1299 if (Tok.isNot(tok::identifier)) {
1300 Diag(Tok, diag::err_expected_ident);
1301 SkipUntil(tok::semi);
1302 return DeclPtrTy();
1303 }
1304
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001305 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1306 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1307 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1308 propertyId, 0);
1309
Chris Lattnerdf195262007-10-09 17:51:17 +00001310 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001311 break;
1312 ConsumeToken(); // consume ','
1313 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001314 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001315 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001316 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001317}
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001319/// objc-throw-statement:
1320/// throw expression[opt];
1321///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001322Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001323 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001324 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001325 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001326 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001327 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001328 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001329 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001330 }
1331 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001332 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001333 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001334}
1335
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001336/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001337/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001338///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001339Parser::OwningStmtResult
1340Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001341 ConsumeToken(); // consume synchronized
1342 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001343 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001344 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001345 }
1346 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001347 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001348 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001349 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001350 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001351 }
1352 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001353 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001354 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001355 }
1356 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001357 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001358 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001359 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001360 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001361 // Enter a scope to hold everything within the compound stmt. Compound
1362 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001363 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001364
Sebastian Redl61364dd2008-12-11 19:30:53 +00001365 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001366
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001367 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001368 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001369 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001370 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001371}
1372
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001373/// objc-try-catch-statement:
1374/// @try compound-statement objc-catch-list[opt]
1375/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1376///
1377/// objc-catch-list:
1378/// @catch ( parameter-declaration ) compound-statement
1379/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1380/// catch-parameter-declaration:
1381/// parameter-declaration
1382/// '...' [OBJC2]
1383///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001384Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001385 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001386
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001387 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001388 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001389 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001390 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001391 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001392 OwningStmtResult CatchStmts(Actions);
1393 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001394 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001395 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001396 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001397 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001398 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001399
Chris Lattnerdf195262007-10-09 17:51:17 +00001400 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001401 // At this point, we need to lookahead to determine if this @ is the start
1402 // of an @catch or @finally. We don't want to consume the @ token if this
1403 // is an @try or @encode or something else.
1404 Token AfterAt = GetLookAheadToken(1);
1405 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1406 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1407 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001408
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001409 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001410 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001411 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001412 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001413 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001414 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001415 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001416 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001417 DeclSpec DS;
1418 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001419 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001420 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001421 // we must accept either 'declarator' or 'abstract-declarator' here.
1422 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1423 ParseDeclarator(ParmDecl);
1424
1425 // Inform the actions module about the parameter declarator, so it
1426 // gets added to the current scope.
1427 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001428 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001429 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Steve Naroff93a25952009-04-07 22:56:58 +00001431 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001432
Steve Naroff93a25952009-04-07 22:56:58 +00001433 if (Tok.is(tok::r_paren))
1434 RParenLoc = ConsumeParen();
1435 else // Skip over garbage, until we get to ')'. Eat the ')'.
1436 SkipUntil(tok::r_paren, true, false);
1437
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001438 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001439 if (Tok.is(tok::l_brace))
1440 CatchBody = ParseCompoundStatementBody();
1441 else
1442 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001443 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001444 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001445 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001446 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001447 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001448 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001449 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1450 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001451 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001452 }
1453 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001454 } else {
1455 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001456 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001457 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001458
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001459 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001460 if (Tok.is(tok::l_brace))
1461 FinallyBody = ParseCompoundStatementBody();
1462 else
1463 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001464 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001465 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001466 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001467 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001468 catch_or_finally_seen = true;
1469 break;
1470 }
1471 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001472 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001473 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001474 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001475 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001476 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1477 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001478}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001479
Steve Naroff3536b442007-09-06 21:24:23 +00001480/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001481///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001482Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1483 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Chris Lattner49f28ca2009-03-05 08:00:35 +00001485 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1486 PP.getSourceManager(),
1487 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001489 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001490 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001491 if (ObjCImpDecl) {
1492 Diag(Tok, diag::warn_semicolon_before_method_body)
1493 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1494 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001495 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001496 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001497
Steve Naroff409be832007-11-11 19:54:21 +00001498 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001499 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001500 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001501
Steve Naroff409be832007-11-11 19:54:21 +00001502 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1503 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001504
Steve Naroff409be832007-11-11 19:54:21 +00001505 // If we didn't find the '{', bail out.
1506 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001507 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001508 }
Steve Naroff409be832007-11-11 19:54:21 +00001509 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Steve Naroff409be832007-11-11 19:54:21 +00001511 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001512 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Steve Naroff409be832007-11-11 19:54:21 +00001514 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001515 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001516 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001517
1518 OwningStmtResult FnBody(ParseCompoundStatementBody());
1519
Steve Naroff409be832007-11-11 19:54:21 +00001520 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001521 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001522 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1523 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001524
Steve Naroff32ce8372009-03-02 22:00:56 +00001525 // TODO: Pass argument information.
1526 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001527
Steve Naroff409be832007-11-11 19:54:21 +00001528 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001529 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001530
Steve Naroff71c0a952007-11-13 23:01:27 +00001531 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001532}
Anders Carlsson55085182007-08-21 17:43:55 +00001533
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001534Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001535 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001536 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001537 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1538 return ParseObjCThrowStmt(AtLoc);
1539 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1540 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001541 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001542 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001543 // If the expression is invalid, skip ahead to the next semicolon. Not
1544 // doing this opens us up to the possibility of infinite loops if
1545 // ParseExpression does not consume any tokens.
1546 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001547 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001548 }
1549 // Otherwise, eat the semicolon.
1550 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001551 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001552}
1553
Sebastian Redl1d922962008-12-13 15:32:12 +00001554Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001555 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001556 case tok::string_literal: // primary-expression: string-literal
1557 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001558 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001559 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001560 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001561 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001562
Chris Lattner4fef81d2008-08-05 06:19:09 +00001563 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1564 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001565 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001566 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001567 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001568 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001569 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001570 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001571 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001572 }
Anders Carlsson55085182007-08-21 17:43:55 +00001573 }
Anders Carlsson55085182007-08-21 17:43:55 +00001574}
1575
Mike Stump1eb44332009-09-09 15:08:12 +00001576/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001577/// '[' objc-receiver objc-message-args ']'
1578///
1579/// objc-receiver:
1580/// expression
1581/// class-name
1582/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001583Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001584 assert(Tok.is(tok::l_square) && "'[' expected");
1585 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1586
1587 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001588 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001589 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001590 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1591 SourceLocation NameLoc = ConsumeToken();
1592 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1593 ExprArg(Actions));
1594 }
Chris Lattner699b6612008-01-25 18:59:06 +00001595 }
1596
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001597 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001598 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001599 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001600 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001601 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001602
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001603 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001604 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001605}
Sebastian Redl1d922962008-12-13 15:32:12 +00001606
Chris Lattner699b6612008-01-25 18:59:06 +00001607/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1608/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001609///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001610/// objc-message-args:
1611/// objc-selector
1612/// objc-keywordarg-list
1613///
1614/// objc-keywordarg-list:
1615/// objc-keywordarg
1616/// objc-keywordarg-list objc-keywordarg
1617///
Mike Stump1eb44332009-09-09 15:08:12 +00001618/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001619/// selector-name[opt] ':' objc-keywordexpr
1620///
1621/// objc-keywordexpr:
1622/// nonempty-expr-list
1623///
1624/// nonempty-expr-list:
1625/// assignment-expression
1626/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001627///
1628Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001629Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001630 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001631 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001632 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001633 if (Tok.is(tok::code_completion)) {
1634 if (ReceiverName)
Douglas Gregor60b01cc2009-11-17 23:31:36 +00001635 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc);
Steve Naroffc4df6d22009-11-07 02:08:14 +00001636 else
Douglas Gregor60b01cc2009-11-17 23:31:36 +00001637 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get());
Steve Naroffc4df6d22009-11-07 02:08:14 +00001638 ConsumeToken();
1639 }
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001640 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001641 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001642 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001643
Anders Carlssonff975cf2009-02-14 18:21:46 +00001644 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001645
Steve Naroff68d331a2007-09-27 14:38:14 +00001646 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001647 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001648
Chris Lattnerdf195262007-10-09 17:51:17 +00001649 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001650 while (1) {
1651 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001652 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001653
Chris Lattnerdf195262007-10-09 17:51:17 +00001654 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001655 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001656 // We must manually skip to a ']', otherwise the expression skipper will
1657 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1658 // the enclosing expression.
1659 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001660 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001661 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001662
Steve Naroff68d331a2007-09-27 14:38:14 +00001663 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001664 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001665 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001666 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001667 // We must manually skip to a ']', otherwise the expression skipper will
1668 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1669 // the enclosing expression.
1670 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001671 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001672 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001673
Steve Naroff37387c92007-09-17 20:25:27 +00001674 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001675 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001676
Steve Naroff37387c92007-09-17 20:25:27 +00001677 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001678 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001679 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001680 break;
1681 // We have a selector or a colon, continue parsing.
1682 }
1683 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001684 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001685 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001686 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001687 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001688 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001689 // We must manually skip to a ']', otherwise the expression skipper will
1690 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1691 // the enclosing expression.
1692 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001693 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001694 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001695
Steve Naroff49f109c2007-11-15 13:05:42 +00001696 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001697 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001698 }
1699 } else if (!selIdent) {
1700 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001701
Chris Lattner4fef81d2008-08-05 06:19:09 +00001702 // We must manually skip to a ']', otherwise the expression skipper will
1703 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1704 // the enclosing expression.
1705 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001706 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001707 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001708
Chris Lattnerdf195262007-10-09 17:51:17 +00001709 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001710 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001711 // We must manually skip to a ']', otherwise the expression skipper will
1712 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1713 // the enclosing expression.
1714 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001715 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001716 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001717
Chris Lattner699b6612008-01-25 18:59:06 +00001718 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001719
Steve Naroff29238a02007-10-05 18:42:47 +00001720 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001721 if (nKeys == 0)
1722 KeyIdents.push_back(selIdent);
1723 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001724
Chris Lattnerff384912007-10-07 02:00:24 +00001725 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001726 if (ReceiverName)
1727 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001728 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001729 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001730 KeyExprs.take(), KeyExprs.size()));
1731 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001732 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001733 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001734}
1735
Sebastian Redl1d922962008-12-13 15:32:12 +00001736Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001737 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001738 if (Res.isInvalid()) return move(Res);
1739
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001740 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1741 // expressions. At this point, we know that the only valid thing that starts
1742 // with '@' is an @"".
1743 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001744 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001745 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001746 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001747
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001748 while (Tok.is(tok::at)) {
1749 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001750
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001751 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001752 if (!isTokenStringLiteral())
1753 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001754
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001755 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001756 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001757 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001758
Sebastian Redleffa8d12008-12-10 00:02:53 +00001759 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001760 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001761
1762 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1763 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001764}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001765
1766/// objc-encode-expression:
1767/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001768Parser::OwningExprResult
1769Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001770 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001771
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001772 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001773
Chris Lattner4fef81d2008-08-05 06:19:09 +00001774 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001775 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1776
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001777 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001778
Douglas Gregor809070a2009-02-18 17:45:20 +00001779 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001780
Anders Carlsson4988ae32007-08-23 15:31:37 +00001781 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001782
Douglas Gregor809070a2009-02-18 17:45:20 +00001783 if (Ty.isInvalid())
1784 return ExprError();
1785
Mike Stump1eb44332009-09-09 15:08:12 +00001786 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001787 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001788}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001789
1790/// objc-protocol-expression
1791/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001792Parser::OwningExprResult
1793Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001794 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001795
Chris Lattner4fef81d2008-08-05 06:19:09 +00001796 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001797 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1798
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001799 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001800
Chris Lattner4fef81d2008-08-05 06:19:09 +00001801 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001802 return ExprError(Diag(Tok, diag::err_expected_ident));
1803
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001804 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001805 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001806
Anders Carlsson4988ae32007-08-23 15:31:37 +00001807 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001808
Sebastian Redl1d922962008-12-13 15:32:12 +00001809 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1810 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001811}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001812
1813/// objc-selector-expression
1814/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001815Parser::OwningExprResult
1816Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001817 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001818
Chris Lattner4fef81d2008-08-05 06:19:09 +00001819 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001820 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1821
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001822 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001823 SourceLocation LParenLoc = ConsumeParen();
1824 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001825 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001826 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1827 return ExprError(Diag(Tok, diag::err_expected_ident));
1828
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001829 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001830 unsigned nColons = 0;
1831 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001832 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001833 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001834 return ExprError(Diag(Tok, diag::err_expected_colon));
1835
Chris Lattnercb53b362007-12-27 19:57:00 +00001836 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001837 ConsumeToken(); // Eat the ':'.
1838 if (Tok.is(tok::r_paren))
1839 break;
1840 // Check for another keyword selector.
1841 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001842 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001843 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001844 if (!SelIdent && Tok.isNot(tok::colon))
1845 break;
1846 }
Steve Naroff887407e2007-12-05 22:21:29 +00001847 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001848 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001849 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001850 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1851 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001852 }