blob: 2ae0c690cad15ed71dbd2dc28d9ec2705db6937e [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;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Reid Spencer5f016e22007-07-11 17:01:13 +000065 while (1) {
Chris Lattnerdf195262007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +000067 Diag(Tok, diag::err_expected_ident);
68 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +000069 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +000070 }
Reid Spencer5f016e22007-07-11 17:01:13 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattnerdf195262007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +000075 break;
Mike Stump1eb44332009-09-09 15:08:12 +000076
Reid Spencer5f016e22007-07-11 17:01:13 +000077 ConsumeToken();
78 }
Mike Stump1eb44332009-09-09 15:08:12 +000079
Reid Spencer5f016e22007-07-11 17:01:13 +000080 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattnerb28317a2009-03-28 19:18:32 +000082 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +000083
Steve Naroffe440eb82007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff3536b442007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Steve Naroffdac269b2007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
Mike Stump1eb44332009-09-09 15:08:12 +000094/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +000095/// objc-protocol-refs[opt]
Mike Stump1eb44332009-09-09 15:08:12 +000096/// objc-class-instance-variables[opt]
Steve Naroffdac269b2007-08-20 21:31:48 +000097/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
Mike Stump1eb44332009-09-09 15:08:12 +0000101/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroffdac269b2007-08-20 21:31:48 +0000102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000116Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroffdac269b2007-08-20 21:31:48 +0000117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroffdac269b2007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Chris Lattnerdf195262007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000124 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000125 }
Fariborz Jahanian63e963c2009-11-16 18:57:01 +0000126
Steve Naroffdac269b2007-08-20 21:31:48 +0000127 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000128 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000129 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Chris Lattnerdf195262007-10-09 17:51:17 +0000131 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000132 SourceLocation lparenLoc = ConsumeParen();
133 SourceLocation categoryLoc, rparenLoc;
134 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Steve Naroff527fe232007-08-23 19:56:30 +0000136 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000137 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000138 categoryId = Tok.getIdentifierInfo();
139 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000140 } else if (!getLang().ObjC2) {
141 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000142 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000143 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000144 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000145 Diag(Tok, diag::err_expected_rparen);
146 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000147 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000148 }
149 rparenLoc = ConsumeParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Steve Naroffdac269b2007-08-20 21:31:48 +0000151 // Next, we need to check for any protocol references.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000152 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000153 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000154 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000155 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000156 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
157 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000158 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000159
Steve Naroffdac269b2007-08-20 21:31:48 +0000160 if (attrList) // categories don't support attributes.
161 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Jay Foadbeaaccd2009-05-21 09:52:38 +0000163 DeclPtrTy CategoryType =
Mike Stump1eb44332009-09-09 15:08:12 +0000164 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000165 nameId, nameLoc,
166 categoryId, categoryLoc,
167 ProtocolRefs.data(),
168 ProtocolRefs.size(),
169 EndProtoLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000170
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000171 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000172 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000173 }
174 // Parse a class interface.
175 IdentifierInfo *superClassId = 0;
176 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000177
Chris Lattnerdf195262007-10-09 17:51:17 +0000178 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000179 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000180 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000181 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000182 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000183 }
184 superClassId = Tok.getIdentifierInfo();
185 superClassLoc = ConsumeToken();
186 }
187 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000188 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000189 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
190 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000191 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000192 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
193 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000194 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000195
196 DeclPtrTy ClsType =
197 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000198 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000199 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattner06036d32008-07-26 04:13:19 +0000200 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000201
Chris Lattnerdf195262007-10-09 17:51:17 +0000202 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000203 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000204
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000205 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000206 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000207}
208
209/// objc-interface-decl-list:
210/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000211/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000212/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000213/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000214/// objc-interface-decl-list declaration
215/// objc-interface-decl-list ';'
216///
Steve Naroff294494e2007-08-22 16:35:03 +0000217/// objc-method-requirement: [OBJC2]
218/// @required
219/// @optional
220///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000221void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000222 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000223 llvm::SmallVector<DeclPtrTy, 32> allMethods;
224 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000225 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000226 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000227
Chris Lattnerbc662af2008-10-20 06:10:06 +0000228 SourceLocation AtEndLoc;
229
Steve Naroff294494e2007-08-22 16:35:03 +0000230 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000231 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000232 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000233 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000234 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000235 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000236 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
237 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000238 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
239 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000240 continue;
241 }
Mike Stump1eb44332009-09-09 15:08:12 +0000242
Chris Lattnere82a10f2008-10-20 05:46:22 +0000243 // Ignore excess semicolons.
244 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000245 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000246 continue;
247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattnerbc662af2008-10-20 06:10:06 +0000249 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000250 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000251 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Chris Lattnere82a10f2008-10-20 05:46:22 +0000253 // If we don't have an @ directive, parse it as a function definition.
254 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000255 // The code below does not consume '}'s because it is afraid of eating the
256 // end of a namespace. Because of the way this code is structured, an
257 // erroneous r_brace would cause an infinite loop if not handled here.
258 if (Tok.is(tok::r_brace))
259 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000260
Steve Naroff4985ace2007-08-22 18:35:33 +0000261 // FIXME: as the name implies, this rule allows function definitions.
262 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner682bf922009-03-29 16:50:03 +0000263 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000264 continue;
265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Chris Lattnere82a10f2008-10-20 05:46:22 +0000267 // Otherwise, we have an @ directive, eat the @.
268 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000269 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Chris Lattnera2449b22008-10-20 05:57:40 +0000271 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000272 AtEndLoc = AtLoc;
273 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chris Lattnerbc662af2008-10-20 06:10:06 +0000276 // Eat the identifier.
277 ConsumeToken();
278
Chris Lattnera2449b22008-10-20 05:57:40 +0000279 switch (DirectiveKind) {
280 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000281 // FIXME: If someone forgets an @end on a protocol, this loop will
282 // continue to eat up tons of stuff and spew lots of nonsense errors. It
283 // would probably be better to bail out if we saw an @class or @interface
284 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000285 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000286 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000287 SkipUntil(tok::r_brace, tok::at);
288 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000289
Chris Lattnera2449b22008-10-20 05:57:40 +0000290 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000291 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000292 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000293 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000294 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000295 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000296 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000297 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000298 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000299
Chris Lattnera2449b22008-10-20 05:57:40 +0000300 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000301 if (!getLang().ObjC2)
302 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
303
Chris Lattnere82a10f2008-10-20 05:46:22 +0000304 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000305 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000306 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000307 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000308
John McCallbdd563e2009-11-03 02:38:08 +0000309 struct ObjCPropertyCallback : FieldCallback {
310 Parser &P;
311 DeclPtrTy IDecl;
312 llvm::SmallVectorImpl<DeclPtrTy> &Props;
313 ObjCDeclSpec &OCDS;
314 SourceLocation AtLoc;
315 tok::ObjCKeywordKind MethodImplKind;
316
317 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
318 llvm::SmallVectorImpl<DeclPtrTy> &Props,
319 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
320 tok::ObjCKeywordKind MethodImplKind) :
321 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
322 MethodImplKind(MethodImplKind) {
323 }
324
325 DeclPtrTy invoke(FieldDeclarator &FD) {
326 if (FD.D.getIdentifier() == 0) {
327 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
328 << FD.D.getSourceRange();
329 return DeclPtrTy();
330 }
331 if (FD.BitfieldSize) {
332 P.Diag(AtLoc, diag::err_objc_property_bitfield)
333 << FD.D.getSourceRange();
334 return DeclPtrTy();
335 }
336
337 // Install the property declarator into interfaceDecl.
338 IdentifierInfo *SelName =
339 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
340
341 Selector GetterSel =
342 P.PP.getSelectorTable().getNullarySelector(SelName);
343 IdentifierInfo *SetterName = OCDS.getSetterName();
344 Selector SetterSel;
345 if (SetterName)
346 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
347 else
348 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
349 P.PP.getSelectorTable(),
350 FD.D.getIdentifier());
351 bool isOverridingProperty = false;
352 DeclPtrTy Property =
353 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
354 GetterSel, SetterSel, IDecl,
355 &isOverridingProperty,
356 MethodImplKind);
357 if (!isOverridingProperty)
358 Props.push_back(Property);
359
360 return Property;
361 }
362 } Callback(*this, interfaceDecl, allProperties,
363 OCDS, AtLoc, MethodImplKind);
364
Chris Lattnere82a10f2008-10-20 05:46:22 +0000365 // Parse all the comma separated declarators.
366 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000367 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000369 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
370 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000371 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000372 }
Steve Naroff294494e2007-08-22 16:35:03 +0000373 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000374
375 // We break out of the big loop in two cases: when we see @end or when we see
376 // EOF. In the former case, eat the @end. In the later case, emit an error.
377 if (Tok.isObjCAtKeyword(tok::objc_end))
378 ConsumeToken(); // the "end" identifier
379 else
380 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattnera2449b22008-10-20 05:57:40 +0000382 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000383 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000384 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000385 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000386 allProperties.data(), allProperties.size(),
387 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000388}
389
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000390/// Parse property attribute declarations.
391///
392/// property-attr-decl: '(' property-attrlist ')'
393/// property-attrlist:
394/// property-attribute
395/// property-attrlist ',' property-attribute
396/// property-attribute:
397/// getter '=' identifier
398/// setter '=' identifier ':'
399/// readonly
400/// readwrite
401/// assign
402/// retain
403/// copy
404/// nonatomic
405///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000406void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000407 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000408 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000410 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000411 if (Tok.is(tok::code_completion)) {
412 Actions.CodeCompleteObjCProperty(CurScope, DS);
413 ConsumeToken();
414 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000415 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000417 // If this is not an identifier at all, bail out early.
418 if (II == 0) {
419 MatchRHSPunctuation(tok::r_paren, LHSLoc);
420 return;
421 }
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Chris Lattner156b0612008-10-20 07:37:22 +0000423 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000424
Chris Lattner92e62b02008-11-20 04:42:34 +0000425 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000426 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000427 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000428 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000429 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000430 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000431 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000432 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000433 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000434 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000435 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000436 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000437 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000438 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000439 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
440 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000441 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Chris Lattner8ca329c2008-10-20 07:24:39 +0000443 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000444 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000445 SkipUntil(tok::r_paren);
446 return;
447 }
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Daniel Dunbare013d682009-10-18 20:26:12 +0000449 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000450 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
451 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000452 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattner156b0612008-10-20 07:37:22 +0000454 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
455 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000456 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000457 } else {
458 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
459 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000460 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000461 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000462 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000463 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000464 SkipUntil(tok::r_paren);
465 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000466 }
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattner156b0612008-10-20 07:37:22 +0000468 if (Tok.isNot(tok::comma))
469 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattner156b0612008-10-20 07:37:22 +0000471 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000472 }
Mike Stump1eb44332009-09-09 15:08:12 +0000473
Chris Lattner156b0612008-10-20 07:37:22 +0000474 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000475}
476
Steve Naroff3536b442007-09-06 21:24:23 +0000477/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000478/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000479/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000480///
481/// objc-instance-method: '-'
482/// objc-class-method: '+'
483///
Steve Naroff4985ace2007-08-22 18:35:33 +0000484/// objc-method-attributes: [OBJC2]
485/// __attribute__((deprecated))
486///
Mike Stump1eb44332009-09-09 15:08:12 +0000487Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000488 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000489 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000490
Mike Stump1eb44332009-09-09 15:08:12 +0000491 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000492 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattnerb28317a2009-03-28 19:18:32 +0000494 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000495 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000496 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000497 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000498}
499
500/// objc-selector:
501/// identifier
502/// one of
503/// enum struct union if else while do for switch case default
504/// break continue return goto asm sizeof typeof __alignof
505/// unsigned long const short volatile signed restrict _Complex
506/// in out inout bycopy byref oneway int char float double void _Bool
507///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000508IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000509 switch (Tok.getKind()) {
510 default:
511 return 0;
512 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000513 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000514 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000515 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000516 case tok::kw_break:
517 case tok::kw_case:
518 case tok::kw_catch:
519 case tok::kw_char:
520 case tok::kw_class:
521 case tok::kw_const:
522 case tok::kw_const_cast:
523 case tok::kw_continue:
524 case tok::kw_default:
525 case tok::kw_delete:
526 case tok::kw_do:
527 case tok::kw_double:
528 case tok::kw_dynamic_cast:
529 case tok::kw_else:
530 case tok::kw_enum:
531 case tok::kw_explicit:
532 case tok::kw_export:
533 case tok::kw_extern:
534 case tok::kw_false:
535 case tok::kw_float:
536 case tok::kw_for:
537 case tok::kw_friend:
538 case tok::kw_goto:
539 case tok::kw_if:
540 case tok::kw_inline:
541 case tok::kw_int:
542 case tok::kw_long:
543 case tok::kw_mutable:
544 case tok::kw_namespace:
545 case tok::kw_new:
546 case tok::kw_operator:
547 case tok::kw_private:
548 case tok::kw_protected:
549 case tok::kw_public:
550 case tok::kw_register:
551 case tok::kw_reinterpret_cast:
552 case tok::kw_restrict:
553 case tok::kw_return:
554 case tok::kw_short:
555 case tok::kw_signed:
556 case tok::kw_sizeof:
557 case tok::kw_static:
558 case tok::kw_static_cast:
559 case tok::kw_struct:
560 case tok::kw_switch:
561 case tok::kw_template:
562 case tok::kw_this:
563 case tok::kw_throw:
564 case tok::kw_true:
565 case tok::kw_try:
566 case tok::kw_typedef:
567 case tok::kw_typeid:
568 case tok::kw_typename:
569 case tok::kw_typeof:
570 case tok::kw_union:
571 case tok::kw_unsigned:
572 case tok::kw_using:
573 case tok::kw_virtual:
574 case tok::kw_void:
575 case tok::kw_volatile:
576 case tok::kw_wchar_t:
577 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000578 case tok::kw__Bool:
579 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000580 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000581 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000582 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000583 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000584 }
Steve Naroff294494e2007-08-22 16:35:03 +0000585}
586
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000587/// objc-for-collection-in: 'in'
588///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000589bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000590 // FIXME: May have to do additional look-ahead to only allow for
591 // valid tokens following an 'in'; such as an identifier, unary operators,
592 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000593 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000594 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000595}
596
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000597/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000598/// qualifier list and builds their bitmask representation in the input
599/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000600///
601/// objc-type-qualifiers:
602/// objc-type-qualifier
603/// objc-type-qualifiers objc-type-qualifier
604///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000605void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000606 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000607 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000608 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Chris Lattnere8b724d2007-12-12 06:56:32 +0000610 const IdentifierInfo *II = Tok.getIdentifierInfo();
611 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000612 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000613 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000615 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000616 switch (i) {
617 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000618 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
619 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
620 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
621 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
622 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
623 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000624 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000625 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000626 ConsumeToken();
627 II = 0;
628 break;
629 }
Mike Stump1eb44332009-09-09 15:08:12 +0000630
Chris Lattnere8b724d2007-12-12 06:56:32 +0000631 // If this wasn't a recognized qualifier, bail out.
632 if (II) return;
633 }
634}
635
636/// objc-type-name:
637/// '(' objc-type-qualifiers[opt] type-name ')'
638/// '(' objc-type-qualifiers[opt] ')'
639///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000640Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000641 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Chris Lattner4a76b292008-10-22 03:52:06 +0000643 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000644 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000646 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000647 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000648
Chris Lattner4a76b292008-10-22 03:52:06 +0000649 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000650 if (isTypeSpecifierQualifier()) {
651 TypeResult TypeSpec = ParseTypeName();
652 if (!TypeSpec.isInvalid())
653 Ty = TypeSpec.get();
654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Steve Naroffd7333c22008-10-21 14:15:04 +0000656 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000657 ConsumeParen();
658 else if (Tok.getLocation() == TypeStartLoc) {
659 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000660 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000661 SkipUntil(tok::r_paren);
662 } else {
663 // Otherwise, we found *something*, but didn't get a ')' in the right
664 // place. Emit an error then return what we have as the type.
665 MatchRHSPunctuation(tok::r_paren, LParenLoc);
666 }
Steve Narofff28b2642007-09-05 23:30:30 +0000667 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000668}
669
670/// objc-method-decl:
671/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000672/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000673/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000674/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000675///
676/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000677/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000678/// objc-keyword-selector objc-keyword-decl
679///
680/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000681/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
682/// objc-selector ':' objc-keyword-attributes[opt] identifier
683/// ':' objc-type-name objc-keyword-attributes[opt] identifier
684/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000685///
Steve Naroff4985ace2007-08-22 18:35:33 +0000686/// objc-parmlist:
687/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000688///
Steve Naroff4985ace2007-08-22 18:35:33 +0000689/// objc-parms:
690/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000691///
Steve Naroff4985ace2007-08-22 18:35:33 +0000692/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000693/// , ...
694///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000695/// objc-keyword-attributes: [OBJC2]
696/// __attribute__((unused))
697///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000698Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
699 tok::TokenKind mType,
700 DeclPtrTy IDecl,
701 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000702 ParsingDeclRAIIObject PD(*this);
703
Chris Lattnere8904e92008-08-23 01:48:03 +0000704 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000705 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000706 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000707 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000708 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000709
Steve Naroffbef11852007-10-26 20:53:56 +0000710 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000711 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000712
Steve Naroff84c43102009-02-11 20:43:13 +0000713 // An unnamed colon is valid.
714 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000715 Diag(Tok, diag::err_expected_selector_for_method)
716 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000717 // Skip until we get a ; or {}.
718 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000719 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000722 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000723 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000724 // If attributes exist after the method, parse them.
725 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000726 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000727 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Chris Lattnerff384912007-10-07 02:00:24 +0000729 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000730 DeclPtrTy Result
731 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000732 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000733 0, CargNames, MethodAttrs,
734 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000735 PD.complete(Result);
736 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000737 }
Steve Narofff28b2642007-09-05 23:30:30 +0000738
Steve Naroff68d331a2007-09-27 14:38:14 +0000739 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000740 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Chris Lattnerff384912007-10-07 02:00:24 +0000742 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000743 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000744
Chris Lattnerff384912007-10-07 02:00:24 +0000745 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000746 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000747 Diag(Tok, diag::err_expected_colon);
748 break;
749 }
750 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattnere294d3f2009-04-11 18:57:04 +0000752 ArgInfo.Type = 0;
753 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
754 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
755
Chris Lattnerff384912007-10-07 02:00:24 +0000756 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000757 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000758 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnere294d3f2009-04-11 18:57:04 +0000759 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000760
Chris Lattnerdf195262007-10-09 17:51:17 +0000761 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000762 Diag(Tok, diag::err_expected_ident); // missing argument name.
763 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000764 }
Mike Stump1eb44332009-09-09 15:08:12 +0000765
Chris Lattnere294d3f2009-04-11 18:57:04 +0000766 ArgInfo.Name = Tok.getIdentifierInfo();
767 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000768 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chris Lattnere294d3f2009-04-11 18:57:04 +0000770 ArgInfos.push_back(ArgInfo);
771 KeyIdents.push_back(SelIdent);
772
Chris Lattnerff384912007-10-07 02:00:24 +0000773 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000774 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000775 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000776 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000777 break;
778 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000779 }
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Steve Naroff335eafa2007-11-15 12:35:21 +0000781 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattnerff384912007-10-07 02:00:24 +0000783 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000784 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000785 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000786 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000787 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000788 ConsumeToken();
789 break;
790 }
Chris Lattnerff384912007-10-07 02:00:24 +0000791 DeclSpec DS;
792 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000793 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000794 Declarator ParmDecl(DS, Declarator::PrototypeContext);
795 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000796 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000797 }
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattnerff384912007-10-07 02:00:24 +0000799 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000800 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000801 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000802 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000803 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000805 if (KeyIdents.size() == 0)
806 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000807 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
808 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000809 DeclPtrTy Result
810 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000811 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000812 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000813 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000814 PD.complete(Result);
815 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000816}
817
Steve Naroffdac269b2007-08-20 21:31:48 +0000818/// objc-protocol-refs:
819/// '<' identifier-list '>'
820///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000821bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000822ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000823 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
824 bool WarnOnDeclarations,
825 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000826 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000828 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Chris Lattnere13b9592008-07-26 04:03:38 +0000830 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattnere13b9592008-07-26 04:03:38 +0000832 while (1) {
833 if (Tok.isNot(tok::identifier)) {
834 Diag(Tok, diag::err_expected_ident);
835 SkipUntil(tok::greater);
836 return true;
837 }
838 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
839 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000840 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000841 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattnere13b9592008-07-26 04:03:38 +0000843 if (Tok.isNot(tok::comma))
844 break;
845 ConsumeToken();
846 }
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattnere13b9592008-07-26 04:03:38 +0000848 // Consume the '>'.
849 if (Tok.isNot(tok::greater)) {
850 Diag(Tok, diag::err_expected_greater);
851 return true;
852 }
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chris Lattnere13b9592008-07-26 04:03:38 +0000854 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Chris Lattnere13b9592008-07-26 04:03:38 +0000856 // Convert the list of protocols identifiers into a list of protocol decls.
857 Actions.FindProtocolDeclaration(WarnOnDeclarations,
858 &ProtocolIdents[0], ProtocolIdents.size(),
859 Protocols);
860 return false;
861}
862
Steve Naroffdac269b2007-08-20 21:31:48 +0000863/// objc-class-instance-variables:
864/// '{' objc-instance-variable-decl-list[opt] '}'
865///
866/// objc-instance-variable-decl-list:
867/// objc-visibility-spec
868/// objc-instance-variable-decl ';'
869/// ';'
870/// objc-instance-variable-decl-list objc-visibility-spec
871/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
872/// objc-instance-variable-decl-list ';'
873///
874/// objc-visibility-spec:
875/// @private
876/// @protected
877/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000878/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000879///
880/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000881/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000882///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000883void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000884 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000885 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000886 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000887
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000888 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000889
Steve Naroffddbff782007-08-21 21:17:12 +0000890 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000892 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000893 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000894 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000895 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Steve Naroffddbff782007-08-21 21:17:12 +0000897 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000898 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000899 Diag(Tok, diag::ext_extra_struct_semi)
900 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroffddbff782007-08-21 21:17:12 +0000901 ConsumeToken();
902 continue;
903 }
Mike Stump1eb44332009-09-09 15:08:12 +0000904
Steve Naroffddbff782007-08-21 21:17:12 +0000905 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000906 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000907 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000908 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000909 case tok::objc_private:
910 case tok::objc_public:
911 case tok::objc_protected:
912 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000913 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000914 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000915 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000916 default:
917 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000918 continue;
919 }
920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
John McCallbdd563e2009-11-03 02:38:08 +0000922 struct ObjCIvarCallback : FieldCallback {
923 Parser &P;
924 DeclPtrTy IDecl;
925 tok::ObjCKeywordKind visibility;
926 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
927
928 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
929 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
930 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
931 }
932
933 DeclPtrTy invoke(FieldDeclarator &FD) {
934 // Install the declarator into the interface decl.
935 DeclPtrTy Field
936 = P.Actions.ActOnIvar(P.CurScope,
937 FD.D.getDeclSpec().getSourceRange().getBegin(),
938 IDecl, FD.D, FD.BitfieldSize, visibility);
939 AllIvarDecls.push_back(Field);
940 return Field;
941 }
942 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
943
Chris Lattnere1359422008-04-10 06:46:29 +0000944 // Parse all the comma separated declarators.
945 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000946 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Chris Lattnerdf195262007-10-09 17:51:17 +0000948 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000949 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000950 } else {
951 Diag(Tok, diag::err_expected_semi_decl_list);
952 // Skip to end of block or statement
953 SkipUntil(tok::r_brace, true, true);
954 }
955 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000956 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000957 // Call ActOnFields() even if we don't have any decls. This is useful
958 // for code rewriting tools that need to be aware of the empty list.
959 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000960 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000961 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000962 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000963}
Steve Naroffdac269b2007-08-20 21:31:48 +0000964
965/// objc-protocol-declaration:
966/// objc-protocol-definition
967/// objc-protocol-forward-reference
968///
969/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +0000970/// @protocol identifier
971/// objc-protocol-refs[opt]
972/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000973/// @end
974///
975/// objc-protocol-forward-reference:
976/// @protocol identifier-list ';'
977///
978/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000979/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000980/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000981Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
982 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000983 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000984 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
985 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Chris Lattnerdf195262007-10-09 17:51:17 +0000987 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000988 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000989 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000990 }
991 // Save the protocol name, then consume it.
992 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
993 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Chris Lattnerdf195262007-10-09 17:51:17 +0000995 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000996 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000997 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000998 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000999 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Chris Lattnerdf195262007-10-09 17:51:17 +00001002 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001003 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1004 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1005
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001006 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001007 while (1) {
1008 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001009 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001010 Diag(Tok, diag::err_expected_ident);
1011 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001012 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001013 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001014 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1015 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001016 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Chris Lattnerdf195262007-10-09 17:51:17 +00001018 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001019 break;
1020 }
1021 // Consume the ';'.
1022 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001023 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Steve Naroffe440eb82007-10-10 17:32:04 +00001025 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001026 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001027 ProtocolRefs.size(),
1028 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001029 }
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001031 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001032 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001033
Chris Lattnerb28317a2009-03-28 19:18:32 +00001034 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001035 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001036 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001037 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1038 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001039 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Chris Lattnerb28317a2009-03-28 19:18:32 +00001041 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001042 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001043 ProtocolRefs.data(),
1044 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001045 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001046 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001047 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001048}
Steve Naroffdac269b2007-08-20 21:31:48 +00001049
1050/// objc-implementation:
1051/// objc-class-implementation-prologue
1052/// objc-category-implementation-prologue
1053///
1054/// objc-class-implementation-prologue:
1055/// @implementation identifier objc-superclass[opt]
1056/// objc-class-instance-variables[opt]
1057///
1058/// objc-category-implementation-prologue:
1059/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001060Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001061 SourceLocation atLoc) {
1062 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1063 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1064 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Chris Lattnerdf195262007-10-09 17:51:17 +00001066 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001067 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001068 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001069 }
1070 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001071 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001072 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001073
1074 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001075 // we have a category implementation.
1076 SourceLocation lparenLoc = ConsumeParen();
1077 SourceLocation categoryLoc, rparenLoc;
1078 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Chris Lattnerdf195262007-10-09 17:51:17 +00001080 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001081 categoryId = Tok.getIdentifierInfo();
1082 categoryLoc = ConsumeToken();
1083 } else {
1084 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001085 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001086 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001087 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001088 Diag(Tok, diag::err_expected_rparen);
1089 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001090 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001091 }
1092 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001093 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001094 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001095 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001096 ObjCImpDecl = ImplCatType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001097 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001098 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001099 }
1100 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001101 SourceLocation superClassLoc;
1102 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001103 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001104 // We have a super class
1105 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001106 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001107 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001108 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001109 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001110 superClassId = Tok.getIdentifierInfo();
1111 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001112 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001113 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001114 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001115 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Steve Naroff60fccee2007-10-29 21:38:07 +00001117 if (Tok.is(tok::l_brace)) // we have ivars
1118 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001119 ObjCImpDecl = ImplClsType;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001120 PendingObjCImpDecl.push_back(ObjCImpDecl);
1121
Chris Lattnerb28317a2009-03-28 19:18:32 +00001122 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001123}
Steve Naroff60fccee2007-10-29 21:38:07 +00001124
Chris Lattnerb28317a2009-03-28 19:18:32 +00001125Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001126 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1127 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001128 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001129 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001130 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001131 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001132 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001133 PendingObjCImpDecl.pop_back();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001134 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001135 else
1136 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001137 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001138}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001139
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001140Parser::DeclGroupPtrTy Parser::RetreivePendingObjCImpDecl() {
1141 if (PendingObjCImpDecl.empty())
1142 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1143 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1144 Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
1145 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1146}
1147
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001148/// compatibility-alias-decl:
1149/// @compatibility_alias alias-name class-name ';'
1150///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001151Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001152 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1153 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1154 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001155 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001156 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001157 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001158 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001159 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1160 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001161 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001162 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001163 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001164 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001165 IdentifierInfo *classId = Tok.getIdentifierInfo();
1166 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1167 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001168 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001169 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001170 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001171 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1172 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001173}
1174
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001175/// property-synthesis:
1176/// @synthesize property-ivar-list ';'
1177///
1178/// property-ivar-list:
1179/// property-ivar
1180/// property-ivar-list ',' property-ivar
1181///
1182/// property-ivar:
1183/// identifier
1184/// identifier '=' identifier
1185///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001186Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001187 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1188 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001189 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001190 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001191 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001192 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Chris Lattnerdf195262007-10-09 17:51:17 +00001195 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001196 IdentifierInfo *propertyIvar = 0;
1197 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1198 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001199 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001200 // property '=' ivar-name
1201 ConsumeToken(); // consume '='
Chris Lattnerdf195262007-10-09 17:51:17 +00001202 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001203 Diag(Tok, diag::err_expected_ident);
1204 break;
1205 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001206 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001207 ConsumeToken(); // consume ivar-name
1208 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001209 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1210 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001211 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001212 break;
1213 ConsumeToken(); // consume ','
1214 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001215 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001216 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001217 else
1218 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001219 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001220}
1221
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001222/// property-dynamic:
1223/// @dynamic property-list
1224///
1225/// property-list:
1226/// identifier
1227/// property-list ',' identifier
1228///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001229Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001230 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1231 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1232 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001233 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001234 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001235 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001236 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001237 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001238 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1239 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1240 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1241 propertyId, 0);
1242
Chris Lattnerdf195262007-10-09 17:51:17 +00001243 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001244 break;
1245 ConsumeToken(); // consume ','
1246 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001247 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001248 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001249 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001250}
Mike Stump1eb44332009-09-09 15:08:12 +00001251
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001252/// objc-throw-statement:
1253/// throw expression[opt];
1254///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001255Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001256 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001257 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001258 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001259 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001260 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001261 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001262 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001263 }
1264 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001265 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001266 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001267}
1268
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001269/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001270/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001271///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001272Parser::OwningStmtResult
1273Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001274 ConsumeToken(); // consume synchronized
1275 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001276 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001277 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001278 }
1279 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001280 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001281 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001282 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001283 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001284 }
1285 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001286 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001287 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001288 }
1289 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001290 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001291 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001292 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001293 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001294 // Enter a scope to hold everything within the compound stmt. Compound
1295 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001296 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001297
Sebastian Redl61364dd2008-12-11 19:30:53 +00001298 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001299
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001300 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001301 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001302 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001303 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001304}
1305
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001306/// objc-try-catch-statement:
1307/// @try compound-statement objc-catch-list[opt]
1308/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1309///
1310/// objc-catch-list:
1311/// @catch ( parameter-declaration ) compound-statement
1312/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1313/// catch-parameter-declaration:
1314/// parameter-declaration
1315/// '...' [OBJC2]
1316///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001317Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001318 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001319
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001320 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001321 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001322 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001323 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001324 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001325 OwningStmtResult CatchStmts(Actions);
1326 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001327 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001328 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001329 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001330 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001331 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001332
Chris Lattnerdf195262007-10-09 17:51:17 +00001333 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001334 // At this point, we need to lookahead to determine if this @ is the start
1335 // of an @catch or @finally. We don't want to consume the @ token if this
1336 // is an @try or @encode or something else.
1337 Token AfterAt = GetLookAheadToken(1);
1338 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1339 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1340 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001341
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001342 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001343 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001344 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001345 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001346 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001347 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001348 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001349 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001350 DeclSpec DS;
1351 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001352 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001353 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001354 // we must accept either 'declarator' or 'abstract-declarator' here.
1355 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1356 ParseDeclarator(ParmDecl);
1357
1358 // Inform the actions module about the parameter declarator, so it
1359 // gets added to the current scope.
1360 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001361 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001362 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Steve Naroff93a25952009-04-07 22:56:58 +00001364 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001365
Steve Naroff93a25952009-04-07 22:56:58 +00001366 if (Tok.is(tok::r_paren))
1367 RParenLoc = ConsumeParen();
1368 else // Skip over garbage, until we get to ')'. Eat the ')'.
1369 SkipUntil(tok::r_paren, true, false);
1370
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001371 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001372 if (Tok.is(tok::l_brace))
1373 CatchBody = ParseCompoundStatementBody();
1374 else
1375 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001376 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001377 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001378 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001379 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001380 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001381 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001382 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1383 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001384 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001385 }
1386 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001387 } else {
1388 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001389 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001390 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001391
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001392 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001393 if (Tok.is(tok::l_brace))
1394 FinallyBody = ParseCompoundStatementBody();
1395 else
1396 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001397 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001398 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001399 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001400 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001401 catch_or_finally_seen = true;
1402 break;
1403 }
1404 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001405 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001406 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001407 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001408 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001409 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1410 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001411}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001412
Steve Naroff3536b442007-09-06 21:24:23 +00001413/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001414///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001415Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1416 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Chris Lattner49f28ca2009-03-05 08:00:35 +00001418 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1419 PP.getSourceManager(),
1420 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001422 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001423 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001424 if (ObjCImpDecl) {
1425 Diag(Tok, diag::warn_semicolon_before_method_body)
1426 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1427 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001428 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001429 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001430
Steve Naroff409be832007-11-11 19:54:21 +00001431 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001432 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001433 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Steve Naroff409be832007-11-11 19:54:21 +00001435 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1436 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001437
Steve Naroff409be832007-11-11 19:54:21 +00001438 // If we didn't find the '{', bail out.
1439 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001440 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001441 }
Steve Naroff409be832007-11-11 19:54:21 +00001442 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Steve Naroff409be832007-11-11 19:54:21 +00001444 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001445 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Steve Naroff409be832007-11-11 19:54:21 +00001447 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001448 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001449 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001450
1451 OwningStmtResult FnBody(ParseCompoundStatementBody());
1452
Steve Naroff409be832007-11-11 19:54:21 +00001453 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001454 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001455 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1456 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001457
Steve Naroff32ce8372009-03-02 22:00:56 +00001458 // TODO: Pass argument information.
1459 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Steve Naroff409be832007-11-11 19:54:21 +00001461 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001462 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001463
Steve Naroff71c0a952007-11-13 23:01:27 +00001464 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001465}
Anders Carlsson55085182007-08-21 17:43:55 +00001466
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001467Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001468 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001469 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001470 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1471 return ParseObjCThrowStmt(AtLoc);
1472 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1473 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001474 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001475 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001476 // If the expression is invalid, skip ahead to the next semicolon. Not
1477 // doing this opens us up to the possibility of infinite loops if
1478 // ParseExpression does not consume any tokens.
1479 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001480 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001481 }
1482 // Otherwise, eat the semicolon.
1483 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001484 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001485}
1486
Sebastian Redl1d922962008-12-13 15:32:12 +00001487Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001488 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001489 case tok::string_literal: // primary-expression: string-literal
1490 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001491 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001492 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001493 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001494 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001495
Chris Lattner4fef81d2008-08-05 06:19:09 +00001496 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1497 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001498 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001499 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001500 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001501 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001502 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001503 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001504 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001505 }
Anders Carlsson55085182007-08-21 17:43:55 +00001506 }
Anders Carlsson55085182007-08-21 17:43:55 +00001507}
1508
Mike Stump1eb44332009-09-09 15:08:12 +00001509/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001510/// '[' objc-receiver objc-message-args ']'
1511///
1512/// objc-receiver:
1513/// expression
1514/// class-name
1515/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001516Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001517 assert(Tok.is(tok::l_square) && "'[' expected");
1518 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1519
1520 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001521 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001522 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001523 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1524 SourceLocation NameLoc = ConsumeToken();
1525 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1526 ExprArg(Actions));
1527 }
Chris Lattner699b6612008-01-25 18:59:06 +00001528 }
1529
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001530 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001531 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001532 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001533 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001534 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001535
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001536 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001537 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001538}
Sebastian Redl1d922962008-12-13 15:32:12 +00001539
Chris Lattner699b6612008-01-25 18:59:06 +00001540/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1541/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001542///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001543/// objc-message-args:
1544/// objc-selector
1545/// objc-keywordarg-list
1546///
1547/// objc-keywordarg-list:
1548/// objc-keywordarg
1549/// objc-keywordarg-list objc-keywordarg
1550///
Mike Stump1eb44332009-09-09 15:08:12 +00001551/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001552/// selector-name[opt] ':' objc-keywordexpr
1553///
1554/// objc-keywordexpr:
1555/// nonempty-expr-list
1556///
1557/// nonempty-expr-list:
1558/// assignment-expression
1559/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001560///
1561Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001562Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001563 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001564 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001565 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001566 if (Tok.is(tok::code_completion)) {
1567 if (ReceiverName)
1568 Actions.CodeCompleteObjCFactoryMethod(CurScope, ReceiverName);
1569 else
1570 Actions.CodeCompleteObjCInstanceMethod(CurScope, ReceiverExpr.release());
1571 ConsumeToken();
1572 }
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001573 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001574 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001575 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001576
Anders Carlssonff975cf2009-02-14 18:21:46 +00001577 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Steve Naroff68d331a2007-09-27 14:38:14 +00001579 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001580 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001581
Chris Lattnerdf195262007-10-09 17:51:17 +00001582 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001583 while (1) {
1584 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001585 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001586
Chris Lattnerdf195262007-10-09 17:51:17 +00001587 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001588 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001589 // We must manually skip to a ']', otherwise the expression skipper will
1590 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1591 // the enclosing expression.
1592 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001593 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001594 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001595
Steve Naroff68d331a2007-09-27 14:38:14 +00001596 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001597 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001598 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001599 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001600 // We must manually skip to a ']', otherwise the expression skipper will
1601 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1602 // the enclosing expression.
1603 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001604 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001605 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001606
Steve Naroff37387c92007-09-17 20:25:27 +00001607 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001608 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001609
Steve Naroff37387c92007-09-17 20:25:27 +00001610 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001611 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001612 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001613 break;
1614 // We have a selector or a colon, continue parsing.
1615 }
1616 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001617 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001618 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001619 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001620 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001621 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001622 // We must manually skip to a ']', otherwise the expression skipper will
1623 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1624 // the enclosing expression.
1625 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001626 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001627 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001628
Steve Naroff49f109c2007-11-15 13:05:42 +00001629 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001630 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001631 }
1632 } else if (!selIdent) {
1633 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001634
Chris Lattner4fef81d2008-08-05 06:19:09 +00001635 // We must manually skip to a ']', otherwise the expression skipper will
1636 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1637 // the enclosing expression.
1638 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001639 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001640 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001641
Chris Lattnerdf195262007-10-09 17:51:17 +00001642 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001643 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001644 // We must manually skip to a ']', otherwise the expression skipper will
1645 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1646 // the enclosing expression.
1647 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001648 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001649 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001650
Chris Lattner699b6612008-01-25 18:59:06 +00001651 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001652
Steve Naroff29238a02007-10-05 18:42:47 +00001653 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001654 if (nKeys == 0)
1655 KeyIdents.push_back(selIdent);
1656 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001657
Chris Lattnerff384912007-10-07 02:00:24 +00001658 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001659 if (ReceiverName)
1660 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001661 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001662 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001663 KeyExprs.take(), KeyExprs.size()));
1664 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001665 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001666 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001667}
1668
Sebastian Redl1d922962008-12-13 15:32:12 +00001669Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001670 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001671 if (Res.isInvalid()) return move(Res);
1672
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001673 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1674 // expressions. At this point, we know that the only valid thing that starts
1675 // with '@' is an @"".
1676 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001677 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001678 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001679 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001680
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001681 while (Tok.is(tok::at)) {
1682 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001683
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001684 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001685 if (!isTokenStringLiteral())
1686 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001687
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001688 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001689 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001690 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001691
Sebastian Redleffa8d12008-12-10 00:02:53 +00001692 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001693 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001694
1695 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1696 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001697}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001698
1699/// objc-encode-expression:
1700/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001701Parser::OwningExprResult
1702Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001703 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001704
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001705 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001706
Chris Lattner4fef81d2008-08-05 06:19:09 +00001707 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001708 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1709
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001710 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001711
Douglas Gregor809070a2009-02-18 17:45:20 +00001712 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001713
Anders Carlsson4988ae32007-08-23 15:31:37 +00001714 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001715
Douglas Gregor809070a2009-02-18 17:45:20 +00001716 if (Ty.isInvalid())
1717 return ExprError();
1718
Mike Stump1eb44332009-09-09 15:08:12 +00001719 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001720 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001721}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001722
1723/// objc-protocol-expression
1724/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001725Parser::OwningExprResult
1726Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001727 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001728
Chris Lattner4fef81d2008-08-05 06:19:09 +00001729 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001730 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1731
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001732 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001733
Chris Lattner4fef81d2008-08-05 06:19:09 +00001734 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001735 return ExprError(Diag(Tok, diag::err_expected_ident));
1736
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001737 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001738 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001739
Anders Carlsson4988ae32007-08-23 15:31:37 +00001740 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001741
Sebastian Redl1d922962008-12-13 15:32:12 +00001742 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1743 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001744}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001745
1746/// objc-selector-expression
1747/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001748Parser::OwningExprResult
1749Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001750 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001751
Chris Lattner4fef81d2008-08-05 06:19:09 +00001752 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001753 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1754
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001755 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001756 SourceLocation LParenLoc = ConsumeParen();
1757 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001758 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001759 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1760 return ExprError(Diag(Tok, diag::err_expected_ident));
1761
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001762 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001763 unsigned nColons = 0;
1764 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001765 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001766 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001767 return ExprError(Diag(Tok, diag::err_expected_colon));
1768
Chris Lattnercb53b362007-12-27 19:57:00 +00001769 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001770 ConsumeToken(); // Eat the ':'.
1771 if (Tok.is(tok::r_paren))
1772 break;
1773 // Check for another keyword selector.
1774 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001775 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001776 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001777 if (!SelIdent && Tok.isNot(tok::colon))
1778 break;
1779 }
Steve Naroff887407e2007-12-05 22:21:29 +00001780 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001781 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001782 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001783 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1784 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001785 }