blob: 29f3fc0a57661fb0c71c01e3fdcc10ec7bfed338 [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 }
126 // We have a class or category name - consume it.
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroffdac269b2007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Chris Lattnerdf195262007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroffdac269b2007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Steve Naroff527fe232007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattnerdf195262007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroff527fe232007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000142 }
Chris Lattnerdf195262007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +0000146 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000147 }
148 rparenLoc = ConsumeParen();
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Steve Naroffdac269b2007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000151 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerb28317a2009-03-28 19:18:32 +0000152 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000153 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner6bd6d0b2008-07-26 04:07:02 +0000154 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000155 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
156 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000157 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Steve Naroffdac269b2007-08-20 21:31:48 +0000159 if (attrList) // categories don't support attributes.
160 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Jay Foadbeaaccd2009-05-21 09:52:38 +0000162 DeclPtrTy CategoryType =
Mike Stump1eb44332009-09-09 15:08:12 +0000163 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000164 nameId, nameLoc,
165 categoryId, categoryLoc,
166 ProtocolRefs.data(),
167 ProtocolRefs.size(),
168 EndProtoLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000169
Fariborz Jahanianfd225cc2007-09-18 20:26:58 +0000170 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000171 return CategoryType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000176
Chris Lattnerdf195262007-10-09 17:51:17 +0000177 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroffdac269b2007-08-20 21:31:48 +0000178 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroffdac269b2007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000181 return DeclPtrTy();
Steve Naroffdac269b2007-08-20 21:31:48 +0000182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000187 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000188 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
189 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner06036d32008-07-26 04:13:19 +0000190 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000191 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
192 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +0000193 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +0000194
195 DeclPtrTy ClsType =
196 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattner06036d32008-07-26 04:13:19 +0000197 superClassId, superClassLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000198 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattner06036d32008-07-26 04:13:19 +0000199 EndProtoLoc, attrList);
Mike Stump1eb44332009-09-09 15:08:12 +0000200
Chris Lattnerdf195262007-10-09 17:51:17 +0000201 if (Tok.is(tok::l_brace))
Steve Naroff60fccee2007-10-29 21:38:07 +0000202 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroffdac269b2007-08-20 21:31:48 +0000203
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000204 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000205 return ClsType;
Steve Naroffdac269b2007-08-20 21:31:48 +0000206}
207
208/// objc-interface-decl-list:
209/// empty
Steve Naroffdac269b2007-08-20 21:31:48 +0000210/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff294494e2007-08-22 16:35:03 +0000211/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff3536b442007-09-06 21:24:23 +0000212/// objc-interface-decl-list objc-method-proto ';'
Steve Naroffdac269b2007-08-20 21:31:48 +0000213/// objc-interface-decl-list declaration
214/// objc-interface-decl-list ';'
215///
Steve Naroff294494e2007-08-22 16:35:03 +0000216/// objc-method-requirement: [OBJC2]
217/// @required
218/// @optional
219///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000220void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattnercb53b362007-12-27 19:57:00 +0000221 tok::ObjCKeywordKind contextKey) {
Chris Lattnerb28317a2009-03-28 19:18:32 +0000222 llvm::SmallVector<DeclPtrTy, 32> allMethods;
223 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner682bf922009-03-29 16:50:03 +0000224 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian00933592007-09-18 00:25:23 +0000225 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattnerbc662af2008-10-20 06:10:06 +0000227 SourceLocation AtEndLoc;
228
Steve Naroff294494e2007-08-22 16:35:03 +0000229 while (1) {
Chris Lattnere82a10f2008-10-20 05:46:22 +0000230 // If this is a method prototype, parse it.
Chris Lattnerdf195262007-10-09 17:51:17 +0000231 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000232 DeclPtrTy methodPrototype =
Chris Lattnerdf195262007-10-09 17:51:17 +0000233 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +0000234 allMethods.push_back(methodPrototype);
Steve Naroff3536b442007-09-06 21:24:23 +0000235 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
236 // method definitions.
Chris Lattnerb6d74a12009-02-15 22:24:30 +0000237 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
238 "", tok::semi);
Steve Naroff294494e2007-08-22 16:35:03 +0000239 continue;
240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattnere82a10f2008-10-20 05:46:22 +0000242 // Ignore excess semicolons.
243 if (Tok.is(tok::semi)) {
Steve Naroff294494e2007-08-22 16:35:03 +0000244 ConsumeToken();
Chris Lattnere82a10f2008-10-20 05:46:22 +0000245 continue;
246 }
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Chris Lattnerbc662af2008-10-20 06:10:06 +0000248 // If we got to the end of the file, exit the loop.
Chris Lattnere82a10f2008-10-20 05:46:22 +0000249 if (Tok.is(tok::eof))
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000250 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Chris Lattnere82a10f2008-10-20 05:46:22 +0000252 // If we don't have an @ directive, parse it as a function definition.
253 if (Tok.isNot(tok::at)) {
Chris Lattner1fd80112009-01-09 04:34:13 +0000254 // The code below does not consume '}'s because it is afraid of eating the
255 // end of a namespace. Because of the way this code is structured, an
256 // erroneous r_brace would cause an infinite loop if not handled here.
257 if (Tok.is(tok::r_brace))
258 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000259
Steve Naroff4985ace2007-08-22 18:35:33 +0000260 // FIXME: as the name implies, this rule allows function definitions.
261 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner682bf922009-03-29 16:50:03 +0000262 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattnere82a10f2008-10-20 05:46:22 +0000263 continue;
264 }
Mike Stump1eb44332009-09-09 15:08:12 +0000265
Chris Lattnere82a10f2008-10-20 05:46:22 +0000266 // Otherwise, we have an @ directive, eat the @.
267 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnera2449b22008-10-20 05:57:40 +0000268 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Chris Lattnera2449b22008-10-20 05:57:40 +0000270 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattnere82a10f2008-10-20 05:46:22 +0000271 AtEndLoc = AtLoc;
272 break;
Chris Lattnerbc662af2008-10-20 06:10:06 +0000273 }
Mike Stump1eb44332009-09-09 15:08:12 +0000274
Chris Lattnerbc662af2008-10-20 06:10:06 +0000275 // Eat the identifier.
276 ConsumeToken();
277
Chris Lattnera2449b22008-10-20 05:57:40 +0000278 switch (DirectiveKind) {
279 default:
Chris Lattnerbc662af2008-10-20 06:10:06 +0000280 // FIXME: If someone forgets an @end on a protocol, this loop will
281 // continue to eat up tons of stuff and spew lots of nonsense errors. It
282 // would probably be better to bail out if we saw an @class or @interface
283 // or something like that.
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000284 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerbc662af2008-10-20 06:10:06 +0000285 // Skip until we see an '@' or '}' or ';'.
Chris Lattnera2449b22008-10-20 05:57:40 +0000286 SkipUntil(tok::r_brace, tok::at);
287 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000288
Chris Lattnera2449b22008-10-20 05:57:40 +0000289 case tok::objc_required:
Chris Lattnera2449b22008-10-20 05:57:40 +0000290 case tok::objc_optional:
Chris Lattnera2449b22008-10-20 05:57:40 +0000291 // This is only valid on protocols.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000292 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattnere82a10f2008-10-20 05:46:22 +0000293 if (contextKey != tok::objc_protocol)
Chris Lattnerbc662af2008-10-20 06:10:06 +0000294 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnera2449b22008-10-20 05:57:40 +0000295 else
Chris Lattnerbc662af2008-10-20 06:10:06 +0000296 MethodImplKind = DirectiveKind;
Chris Lattnera2449b22008-10-20 05:57:40 +0000297 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000298
Chris Lattnera2449b22008-10-20 05:57:40 +0000299 case tok::objc_property:
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000300 if (!getLang().ObjC2)
301 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
302
Chris Lattnere82a10f2008-10-20 05:46:22 +0000303 ObjCDeclSpec OCDS;
Mike Stump1eb44332009-09-09 15:08:12 +0000304 // Parse property attribute list, if any.
Chris Lattner8ca329c2008-10-20 07:24:39 +0000305 if (Tok.is(tok::l_paren))
Chris Lattnere82a10f2008-10-20 05:46:22 +0000306 ParseObjCPropertyAttribute(OCDS);
Mike Stump1eb44332009-09-09 15:08:12 +0000307
John McCallbdd563e2009-11-03 02:38:08 +0000308 struct ObjCPropertyCallback : FieldCallback {
309 Parser &P;
310 DeclPtrTy IDecl;
311 llvm::SmallVectorImpl<DeclPtrTy> &Props;
312 ObjCDeclSpec &OCDS;
313 SourceLocation AtLoc;
314 tok::ObjCKeywordKind MethodImplKind;
315
316 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
317 llvm::SmallVectorImpl<DeclPtrTy> &Props,
318 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
319 tok::ObjCKeywordKind MethodImplKind) :
320 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
321 MethodImplKind(MethodImplKind) {
322 }
323
324 DeclPtrTy invoke(FieldDeclarator &FD) {
325 if (FD.D.getIdentifier() == 0) {
326 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
327 << FD.D.getSourceRange();
328 return DeclPtrTy();
329 }
330 if (FD.BitfieldSize) {
331 P.Diag(AtLoc, diag::err_objc_property_bitfield)
332 << FD.D.getSourceRange();
333 return DeclPtrTy();
334 }
335
336 // Install the property declarator into interfaceDecl.
337 IdentifierInfo *SelName =
338 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
339
340 Selector GetterSel =
341 P.PP.getSelectorTable().getNullarySelector(SelName);
342 IdentifierInfo *SetterName = OCDS.getSetterName();
343 Selector SetterSel;
344 if (SetterName)
345 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
346 else
347 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
348 P.PP.getSelectorTable(),
349 FD.D.getIdentifier());
350 bool isOverridingProperty = false;
351 DeclPtrTy Property =
352 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
353 GetterSel, SetterSel, IDecl,
354 &isOverridingProperty,
355 MethodImplKind);
356 if (!isOverridingProperty)
357 Props.push_back(Property);
358
359 return Property;
360 }
361 } Callback(*this, interfaceDecl, allProperties,
362 OCDS, AtLoc, MethodImplKind);
363
Chris Lattnere82a10f2008-10-20 05:46:22 +0000364 // Parse all the comma separated declarators.
365 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000366 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Chris Lattnera1fed7e2008-10-20 06:15:13 +0000368 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
369 tok::at);
Chris Lattnera2449b22008-10-20 05:57:40 +0000370 break;
Steve Narofff28b2642007-09-05 23:30:30 +0000371 }
Steve Naroff294494e2007-08-22 16:35:03 +0000372 }
Chris Lattnerbc662af2008-10-20 06:10:06 +0000373
374 // We break out of the big loop in two cases: when we see @end or when we see
375 // EOF. In the former case, eat the @end. In the later case, emit an error.
376 if (Tok.isObjCAtKeyword(tok::objc_end))
377 ConsumeToken(); // the "end" identifier
378 else
379 Diag(Tok, diag::err_objc_missing_end);
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Chris Lattnera2449b22008-10-20 05:57:40 +0000381 // Insert collected methods declarations into the @interface object.
Chris Lattnerbc662af2008-10-20 06:10:06 +0000382 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek8a779312008-06-06 16:45:15 +0000383 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000384 allMethods.data(), allMethods.size(),
Jay Foadbeaaccd2009-05-21 09:52:38 +0000385 allProperties.data(), allProperties.size(),
386 allTUVariables.data(), allTUVariables.size());
Steve Naroff294494e2007-08-22 16:35:03 +0000387}
388
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000389/// Parse property attribute declarations.
390///
391/// property-attr-decl: '(' property-attrlist ')'
392/// property-attrlist:
393/// property-attribute
394/// property-attrlist ',' property-attribute
395/// property-attribute:
396/// getter '=' identifier
397/// setter '=' identifier ':'
398/// readonly
399/// readwrite
400/// assign
401/// retain
402/// copy
403/// nonatomic
404///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000405void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000406 assert(Tok.getKind() == tok::l_paren);
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000407 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump1eb44332009-09-09 15:08:12 +0000408
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000409 while (1) {
Steve Naroffece8e712009-10-08 21:55:05 +0000410 if (Tok.is(tok::code_completion)) {
411 Actions.CodeCompleteObjCProperty(CurScope, DS);
412 ConsumeToken();
413 }
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000414 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump1eb44332009-09-09 15:08:12 +0000415
Chris Lattnerf6ed8552008-10-20 07:22:18 +0000416 // If this is not an identifier at all, bail out early.
417 if (II == 0) {
418 MatchRHSPunctuation(tok::r_paren, LHSLoc);
419 return;
420 }
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner156b0612008-10-20 07:37:22 +0000422 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump1eb44332009-09-09 15:08:12 +0000423
Chris Lattner92e62b02008-11-20 04:42:34 +0000424 if (II->isStr("readonly"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000425 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner92e62b02008-11-20 04:42:34 +0000426 else if (II->isStr("assign"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000427 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner92e62b02008-11-20 04:42:34 +0000428 else if (II->isStr("readwrite"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000429 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner92e62b02008-11-20 04:42:34 +0000430 else if (II->isStr("retain"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000431 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner92e62b02008-11-20 04:42:34 +0000432 else if (II->isStr("copy"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000433 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner92e62b02008-11-20 04:42:34 +0000434 else if (II->isStr("nonatomic"))
Chris Lattnere00da7c2008-10-20 07:39:53 +0000435 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner92e62b02008-11-20 04:42:34 +0000436 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattnere00da7c2008-10-20 07:39:53 +0000437 // getter/setter require extra treatment.
Chris Lattner156b0612008-10-20 07:37:22 +0000438 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
439 tok::r_paren))
Chris Lattnerdd5b5f22008-10-20 07:00:43 +0000440 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Chris Lattner8ca329c2008-10-20 07:24:39 +0000442 if (Tok.isNot(tok::identifier)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +0000443 Diag(Tok, diag::err_expected_ident);
Chris Lattner8ca329c2008-10-20 07:24:39 +0000444 SkipUntil(tok::r_paren);
445 return;
446 }
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Daniel Dunbare013d682009-10-18 20:26:12 +0000448 if (II->getNameStart()[0] == 's') {
Chris Lattner8ca329c2008-10-20 07:24:39 +0000449 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
450 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000451 ConsumeToken(); // consume method name
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner156b0612008-10-20 07:37:22 +0000453 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
454 tok::r_paren))
Chris Lattner8ca329c2008-10-20 07:24:39 +0000455 return;
Chris Lattner8ca329c2008-10-20 07:24:39 +0000456 } else {
457 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
458 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner156b0612008-10-20 07:37:22 +0000459 ConsumeToken(); // consume method name
Chris Lattner8ca329c2008-10-20 07:24:39 +0000460 }
Chris Lattnere00da7c2008-10-20 07:39:53 +0000461 } else {
Chris Lattnera9500f02008-11-19 07:49:38 +0000462 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000463 SkipUntil(tok::r_paren);
464 return;
Chris Lattnercd9f4b32008-10-20 07:15:22 +0000465 }
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattner156b0612008-10-20 07:37:22 +0000467 if (Tok.isNot(tok::comma))
468 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Chris Lattner156b0612008-10-20 07:37:22 +0000470 ConsumeToken();
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattner156b0612008-10-20 07:37:22 +0000473 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahaniand0f97d12007-08-31 16:11:31 +0000474}
475
Steve Naroff3536b442007-09-06 21:24:23 +0000476/// objc-method-proto:
Mike Stump1eb44332009-09-09 15:08:12 +0000477/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff3536b442007-09-06 21:24:23 +0000478/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000479///
480/// objc-instance-method: '-'
481/// objc-class-method: '+'
482///
Steve Naroff4985ace2007-08-22 18:35:33 +0000483/// objc-method-attributes: [OBJC2]
484/// __attribute__((deprecated))
485///
Mike Stump1eb44332009-09-09 15:08:12 +0000486Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattnerb28317a2009-03-28 19:18:32 +0000487 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000488 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff294494e2007-08-22 16:35:03 +0000489
Mike Stump1eb44332009-09-09 15:08:12 +0000490 tok::TokenKind methodType = Tok.getKind();
Steve Naroffbef11852007-10-26 20:53:56 +0000491 SourceLocation mLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Chris Lattnerb28317a2009-03-28 19:18:32 +0000493 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff3536b442007-09-06 21:24:23 +0000494 // Since this rule is used for both method declarations and definitions,
Steve Naroff2bd42fa2007-09-10 20:51:04 +0000495 // the caller is (optionally) responsible for consuming the ';'.
Steve Narofff28b2642007-09-05 23:30:30 +0000496 return MDecl;
Steve Naroff294494e2007-08-22 16:35:03 +0000497}
498
499/// objc-selector:
500/// identifier
501/// one of
502/// enum struct union if else while do for switch case default
503/// break continue return goto asm sizeof typeof __alignof
504/// unsigned long const short volatile signed restrict _Complex
505/// in out inout bycopy byref oneway int char float double void _Bool
506///
Chris Lattner2fc5c242009-04-11 18:13:45 +0000507IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattnerff384912007-10-07 02:00:24 +0000508 switch (Tok.getKind()) {
509 default:
510 return 0;
511 case tok::identifier:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000512 case tok::kw_asm:
Chris Lattnerff384912007-10-07 02:00:24 +0000513 case tok::kw_auto:
Chris Lattner9298d962007-11-15 05:25:19 +0000514 case tok::kw_bool:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000515 case tok::kw_break:
516 case tok::kw_case:
517 case tok::kw_catch:
518 case tok::kw_char:
519 case tok::kw_class:
520 case tok::kw_const:
521 case tok::kw_const_cast:
522 case tok::kw_continue:
523 case tok::kw_default:
524 case tok::kw_delete:
525 case tok::kw_do:
526 case tok::kw_double:
527 case tok::kw_dynamic_cast:
528 case tok::kw_else:
529 case tok::kw_enum:
530 case tok::kw_explicit:
531 case tok::kw_export:
532 case tok::kw_extern:
533 case tok::kw_false:
534 case tok::kw_float:
535 case tok::kw_for:
536 case tok::kw_friend:
537 case tok::kw_goto:
538 case tok::kw_if:
539 case tok::kw_inline:
540 case tok::kw_int:
541 case tok::kw_long:
542 case tok::kw_mutable:
543 case tok::kw_namespace:
544 case tok::kw_new:
545 case tok::kw_operator:
546 case tok::kw_private:
547 case tok::kw_protected:
548 case tok::kw_public:
549 case tok::kw_register:
550 case tok::kw_reinterpret_cast:
551 case tok::kw_restrict:
552 case tok::kw_return:
553 case tok::kw_short:
554 case tok::kw_signed:
555 case tok::kw_sizeof:
556 case tok::kw_static:
557 case tok::kw_static_cast:
558 case tok::kw_struct:
559 case tok::kw_switch:
560 case tok::kw_template:
561 case tok::kw_this:
562 case tok::kw_throw:
563 case tok::kw_true:
564 case tok::kw_try:
565 case tok::kw_typedef:
566 case tok::kw_typeid:
567 case tok::kw_typename:
568 case tok::kw_typeof:
569 case tok::kw_union:
570 case tok::kw_unsigned:
571 case tok::kw_using:
572 case tok::kw_virtual:
573 case tok::kw_void:
574 case tok::kw_volatile:
575 case tok::kw_wchar_t:
576 case tok::kw_while:
Chris Lattnerff384912007-10-07 02:00:24 +0000577 case tok::kw__Bool:
578 case tok::kw__Complex:
Anders Carlssonef048ef2008-08-23 21:00:01 +0000579 case tok::kw___alignof:
Chris Lattnerff384912007-10-07 02:00:24 +0000580 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000581 SelectorLoc = ConsumeToken();
Chris Lattnerff384912007-10-07 02:00:24 +0000582 return II;
Fariborz Jahaniand0649512007-09-27 19:52:15 +0000583 }
Steve Naroff294494e2007-08-22 16:35:03 +0000584}
585
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000586/// objc-for-collection-in: 'in'
587///
Fariborz Jahanian335a2d42008-01-04 23:04:08 +0000588bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian3ba5a0f2008-01-03 17:55:25 +0000589 // FIXME: May have to do additional look-ahead to only allow for
590 // valid tokens following an 'in'; such as an identifier, unary operators,
591 // '[' etc.
Mike Stump1eb44332009-09-09 15:08:12 +0000592 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattner5ffb14b2008-08-23 02:02:23 +0000593 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian0196cab2008-01-02 22:54:34 +0000594}
595
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000596/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattnere8b724d2007-12-12 06:56:32 +0000597/// qualifier list and builds their bitmask representation in the input
598/// argument.
Steve Naroff294494e2007-08-22 16:35:03 +0000599///
600/// objc-type-qualifiers:
601/// objc-type-qualifier
602/// objc-type-qualifiers objc-type-qualifier
603///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000604void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattnere8b724d2007-12-12 06:56:32 +0000605 while (1) {
Chris Lattnercb53b362007-12-27 19:57:00 +0000606 if (Tok.isNot(tok::identifier))
Chris Lattnere8b724d2007-12-12 06:56:32 +0000607 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000608
Chris Lattnere8b724d2007-12-12 06:56:32 +0000609 const IdentifierInfo *II = Tok.getIdentifierInfo();
610 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000611 if (II != ObjCTypeQuals[i])
Chris Lattnere8b724d2007-12-12 06:56:32 +0000612 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000614 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000615 switch (i) {
616 default: assert(0 && "Unknown decl qualifier");
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000617 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
618 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
619 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
620 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
621 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
622 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattnere8b724d2007-12-12 06:56:32 +0000623 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000624 DS.setObjCDeclQualifier(Qual);
Chris Lattnere8b724d2007-12-12 06:56:32 +0000625 ConsumeToken();
626 II = 0;
627 break;
628 }
Mike Stump1eb44332009-09-09 15:08:12 +0000629
Chris Lattnere8b724d2007-12-12 06:56:32 +0000630 // If this wasn't a recognized qualifier, bail out.
631 if (II) return;
632 }
633}
634
635/// objc-type-name:
636/// '(' objc-type-qualifiers[opt] type-name ')'
637/// '(' objc-type-qualifiers[opt] ')'
638///
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000639Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000640 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump1eb44332009-09-09 15:08:12 +0000641
Chris Lattner4a76b292008-10-22 03:52:06 +0000642 SourceLocation LParenLoc = ConsumeParen();
Chris Lattnere8904e92008-08-23 01:48:03 +0000643 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Fariborz Jahanian19d74e12007-10-31 21:59:43 +0000645 // Parse type qualifiers, in, inout, etc.
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000646 ParseObjCTypeQualifierList(DS);
Steve Naroff4fa7afd2007-08-22 23:18:22 +0000647
Chris Lattner4a76b292008-10-22 03:52:06 +0000648 TypeTy *Ty = 0;
Douglas Gregor809070a2009-02-18 17:45:20 +0000649 if (isTypeSpecifierQualifier()) {
650 TypeResult TypeSpec = ParseTypeName();
651 if (!TypeSpec.isInvalid())
652 Ty = TypeSpec.get();
653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Steve Naroffd7333c22008-10-21 14:15:04 +0000655 if (Tok.is(tok::r_paren))
Chris Lattner4a76b292008-10-22 03:52:06 +0000656 ConsumeParen();
657 else if (Tok.getLocation() == TypeStartLoc) {
658 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000659 Diag(Tok, diag::err_expected_type);
Chris Lattner4a76b292008-10-22 03:52:06 +0000660 SkipUntil(tok::r_paren);
661 } else {
662 // Otherwise, we found *something*, but didn't get a ')' in the right
663 // place. Emit an error then return what we have as the type.
664 MatchRHSPunctuation(tok::r_paren, LParenLoc);
665 }
Steve Narofff28b2642007-09-05 23:30:30 +0000666 return Ty;
Steve Naroff294494e2007-08-22 16:35:03 +0000667}
668
669/// objc-method-decl:
670/// objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000671/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000672/// objc-type-name objc-selector
Steve Naroff4985ace2007-08-22 18:35:33 +0000673/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000674///
675/// objc-keyword-selector:
Mike Stump1eb44332009-09-09 15:08:12 +0000676/// objc-keyword-decl
Steve Naroff294494e2007-08-22 16:35:03 +0000677/// objc-keyword-selector objc-keyword-decl
678///
679/// objc-keyword-decl:
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000680/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
681/// objc-selector ':' objc-keyword-attributes[opt] identifier
682/// ':' objc-type-name objc-keyword-attributes[opt] identifier
683/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff294494e2007-08-22 16:35:03 +0000684///
Steve Naroff4985ace2007-08-22 18:35:33 +0000685/// objc-parmlist:
686/// objc-parms objc-ellipsis[opt]
Steve Naroff294494e2007-08-22 16:35:03 +0000687///
Steve Naroff4985ace2007-08-22 18:35:33 +0000688/// objc-parms:
689/// objc-parms , parameter-declaration
Steve Naroff294494e2007-08-22 16:35:03 +0000690///
Steve Naroff4985ace2007-08-22 18:35:33 +0000691/// objc-ellipsis:
Steve Naroff294494e2007-08-22 16:35:03 +0000692/// , ...
693///
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000694/// objc-keyword-attributes: [OBJC2]
695/// __attribute__((unused))
696///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000697Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
698 tok::TokenKind mType,
699 DeclPtrTy IDecl,
700 tok::ObjCKeywordKind MethodImplKind) {
John McCall54abf7d2009-11-04 02:18:39 +0000701 ParsingDeclRAIIObject PD(*this);
702
Chris Lattnere8904e92008-08-23 01:48:03 +0000703 // Parse the return type if present.
Chris Lattnerff384912007-10-07 02:00:24 +0000704 TypeTy *ReturnType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000705 ObjCDeclSpec DSRet;
Chris Lattnerdf195262007-10-09 17:51:17 +0000706 if (Tok.is(tok::l_paren))
Fariborz Jahanianf1de0ca2007-10-31 23:53:01 +0000707 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Steve Naroffbef11852007-10-26 20:53:56 +0000709 SourceLocation selLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000710 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattnere8904e92008-08-23 01:48:03 +0000711
Steve Naroff84c43102009-02-11 20:43:13 +0000712 // An unnamed colon is valid.
713 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner1ab3b962008-11-18 07:48:38 +0000714 Diag(Tok, diag::err_expected_selector_for_method)
715 << SourceRange(mLoc, Tok.getLocation());
Chris Lattnere8904e92008-08-23 01:48:03 +0000716 // Skip until we get a ; or {}.
717 SkipUntil(tok::r_brace);
Chris Lattnerb28317a2009-03-28 19:18:32 +0000718 return DeclPtrTy();
Chris Lattnere8904e92008-08-23 01:48:03 +0000719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000721 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattnerdf195262007-10-09 17:51:17 +0000722 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000723 // If attributes exist after the method, parse them.
724 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000725 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000726 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000727
Chris Lattnerff384912007-10-07 02:00:24 +0000728 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall54abf7d2009-11-04 02:18:39 +0000729 DeclPtrTy Result
730 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian1f7b6f82007-11-09 19:52:12 +0000731 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000732 0, CargNames, MethodAttrs,
733 MethodImplKind);
John McCall54abf7d2009-11-04 02:18:39 +0000734 PD.complete(Result);
735 return Result;
Chris Lattnerff384912007-10-07 02:00:24 +0000736 }
Steve Narofff28b2642007-09-05 23:30:30 +0000737
Steve Naroff68d331a2007-09-27 14:38:14 +0000738 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnere294d3f2009-04-11 18:57:04 +0000739 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000740
Chris Lattnerff384912007-10-07 02:00:24 +0000741 while (1) {
Chris Lattnere294d3f2009-04-11 18:57:04 +0000742 Action::ObjCArgInfo ArgInfo;
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Chris Lattnerff384912007-10-07 02:00:24 +0000744 // Each iteration parses a single keyword argument.
Chris Lattnerdf195262007-10-09 17:51:17 +0000745 if (Tok.isNot(tok::colon)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000746 Diag(Tok, diag::err_expected_colon);
747 break;
748 }
749 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Chris Lattnere294d3f2009-04-11 18:57:04 +0000751 ArgInfo.Type = 0;
752 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
753 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
754
Chris Lattnerff384912007-10-07 02:00:24 +0000755 // If attributes exist before the argument name, parse them.
Chris Lattnere294d3f2009-04-11 18:57:04 +0000756 ArgInfo.ArgAttrs = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +0000757 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnere294d3f2009-04-11 18:57:04 +0000758 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000759
Chris Lattnerdf195262007-10-09 17:51:17 +0000760 if (Tok.isNot(tok::identifier)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000761 Diag(Tok, diag::err_expected_ident); // missing argument name.
762 break;
Steve Naroff4985ace2007-08-22 18:35:33 +0000763 }
Mike Stump1eb44332009-09-09 15:08:12 +0000764
Chris Lattnere294d3f2009-04-11 18:57:04 +0000765 ArgInfo.Name = Tok.getIdentifierInfo();
766 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattnerff384912007-10-07 02:00:24 +0000767 ConsumeToken(); // Eat the identifier.
Mike Stump1eb44332009-09-09 15:08:12 +0000768
Chris Lattnere294d3f2009-04-11 18:57:04 +0000769 ArgInfos.push_back(ArgInfo);
770 KeyIdents.push_back(SelIdent);
771
Chris Lattnerff384912007-10-07 02:00:24 +0000772 // Check for another keyword selector.
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000773 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +0000774 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +0000775 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattnerff384912007-10-07 02:00:24 +0000776 break;
777 // We have a selector or a colon, continue parsing.
Steve Naroff4985ace2007-08-22 18:35:33 +0000778 }
Mike Stump1eb44332009-09-09 15:08:12 +0000779
Steve Naroff335eafa2007-11-15 12:35:21 +0000780 bool isVariadic = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Chris Lattnerff384912007-10-07 02:00:24 +0000782 // Parse the (optional) parameter list.
Chris Lattnerdf195262007-10-09 17:51:17 +0000783 while (Tok.is(tok::comma)) {
Chris Lattnerff384912007-10-07 02:00:24 +0000784 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +0000785 if (Tok.is(tok::ellipsis)) {
Steve Naroff335eafa2007-11-15 12:35:21 +0000786 isVariadic = true;
Chris Lattnerff384912007-10-07 02:00:24 +0000787 ConsumeToken();
788 break;
789 }
Chris Lattnerff384912007-10-07 02:00:24 +0000790 DeclSpec DS;
791 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +0000792 // Parse the declarator.
Chris Lattnerff384912007-10-07 02:00:24 +0000793 Declarator ParmDecl(DS, Declarator::PrototypeContext);
794 ParseDeclarator(ParmDecl);
Fariborz Jahanian439c6582009-01-09 00:38:19 +0000795 CargNames.push_back(ParmDecl);
Chris Lattnerff384912007-10-07 02:00:24 +0000796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Chris Lattnerff384912007-10-07 02:00:24 +0000798 // FIXME: Add support for optional parmameter list...
Fariborz Jahaniane3a2ca72007-09-10 20:33:04 +0000799 // If attributes exist after the method, parse them.
Chris Lattnerff384912007-10-07 02:00:24 +0000800 AttributeList *MethodAttrs = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000801 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerff384912007-10-07 02:00:24 +0000802 MethodAttrs = ParseAttributes();
Mike Stump1eb44332009-09-09 15:08:12 +0000803
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000804 if (KeyIdents.size() == 0)
805 return DeclPtrTy();
Chris Lattnerff384912007-10-07 02:00:24 +0000806 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
807 &KeyIdents[0]);
John McCall54abf7d2009-11-04 02:18:39 +0000808 DeclPtrTy Result
809 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian3688fc62009-06-24 17:00:18 +0000810 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenek1c6a3cc2009-05-04 17:04:30 +0000811 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroff335eafa2007-11-15 12:35:21 +0000812 MethodImplKind, isVariadic);
John McCall54abf7d2009-11-04 02:18:39 +0000813 PD.complete(Result);
814 return Result;
Steve Naroff294494e2007-08-22 16:35:03 +0000815}
816
Steve Naroffdac269b2007-08-20 21:31:48 +0000817/// objc-protocol-refs:
818/// '<' identifier-list '>'
819///
Chris Lattner7caeabd2008-07-21 22:17:28 +0000820bool Parser::
Chris Lattnerb28317a2009-03-28 19:18:32 +0000821ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000822 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
823 bool WarnOnDeclarations,
824 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattnere13b9592008-07-26 04:03:38 +0000825 assert(Tok.is(tok::less) && "expected <");
Mike Stump1eb44332009-09-09 15:08:12 +0000826
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000827 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Chris Lattnere13b9592008-07-26 04:03:38 +0000829 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump1eb44332009-09-09 15:08:12 +0000830
Chris Lattnere13b9592008-07-26 04:03:38 +0000831 while (1) {
832 if (Tok.isNot(tok::identifier)) {
833 Diag(Tok, diag::err_expected_ident);
834 SkipUntil(tok::greater);
835 return true;
836 }
837 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
838 Tok.getLocation()));
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +0000839 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattnere13b9592008-07-26 04:03:38 +0000840 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Chris Lattnere13b9592008-07-26 04:03:38 +0000842 if (Tok.isNot(tok::comma))
843 break;
844 ConsumeToken();
845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattnere13b9592008-07-26 04:03:38 +0000847 // Consume the '>'.
848 if (Tok.isNot(tok::greater)) {
849 Diag(Tok, diag::err_expected_greater);
850 return true;
851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Chris Lattnere13b9592008-07-26 04:03:38 +0000853 EndLoc = ConsumeAnyToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Chris Lattnere13b9592008-07-26 04:03:38 +0000855 // Convert the list of protocols identifiers into a list of protocol decls.
856 Actions.FindProtocolDeclaration(WarnOnDeclarations,
857 &ProtocolIdents[0], ProtocolIdents.size(),
858 Protocols);
859 return false;
860}
861
Steve Naroffdac269b2007-08-20 21:31:48 +0000862/// objc-class-instance-variables:
863/// '{' objc-instance-variable-decl-list[opt] '}'
864///
865/// objc-instance-variable-decl-list:
866/// objc-visibility-spec
867/// objc-instance-variable-decl ';'
868/// ';'
869/// objc-instance-variable-decl-list objc-visibility-spec
870/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
871/// objc-instance-variable-decl-list ';'
872///
873/// objc-visibility-spec:
874/// @private
875/// @protected
876/// @public
Steve Naroffddbff782007-08-21 21:17:12 +0000877/// @package [OBJC2]
Steve Naroffdac269b2007-08-20 21:31:48 +0000878///
879/// objc-instance-variable-decl:
Mike Stump1eb44332009-09-09 15:08:12 +0000880/// struct-declaration
Steve Naroffdac269b2007-08-20 21:31:48 +0000881///
Chris Lattnerb28317a2009-03-28 19:18:32 +0000882void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff60fccee2007-10-29 21:38:07 +0000883 SourceLocation atLoc) {
Chris Lattnerdf195262007-10-09 17:51:17 +0000884 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattnerb28317a2009-03-28 19:18:32 +0000885 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnere1359422008-04-10 06:46:29 +0000886
Douglas Gregor1a0d31a2009-01-12 18:45:55 +0000887 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor72de6672009-01-08 20:45:30 +0000888
Steve Naroffddbff782007-08-21 21:17:12 +0000889 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Fariborz Jahanianaa847fe2008-04-29 23:03:51 +0000891 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroffddbff782007-08-21 21:17:12 +0000892 // While we still have something to read, read the instance variables.
Chris Lattnerdf195262007-10-09 17:51:17 +0000893 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000894 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Steve Naroffddbff782007-08-21 21:17:12 +0000896 // Check for extraneous top-level semicolon.
Chris Lattnerdf195262007-10-09 17:51:17 +0000897 if (Tok.is(tok::semi)) {
Chris Lattnerc2253f52009-11-06 06:40:12 +0000898 Diag(Tok, diag::ext_extra_struct_semi)
899 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroffddbff782007-08-21 21:17:12 +0000900 ConsumeToken();
901 continue;
902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Steve Naroffddbff782007-08-21 21:17:12 +0000904 // Set the default visibility to private.
Chris Lattnerdf195262007-10-09 17:51:17 +0000905 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroffddbff782007-08-21 21:17:12 +0000906 ConsumeToken(); // eat the @ sign
Steve Naroff861cf3e2007-08-23 18:16:40 +0000907 switch (Tok.getObjCKeywordID()) {
Steve Naroffddbff782007-08-21 21:17:12 +0000908 case tok::objc_private:
909 case tok::objc_public:
910 case tok::objc_protected:
911 case tok::objc_package:
Steve Naroff861cf3e2007-08-23 18:16:40 +0000912 visibility = Tok.getObjCKeywordID();
Steve Naroffddbff782007-08-21 21:17:12 +0000913 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000914 continue;
Steve Naroffddbff782007-08-21 21:17:12 +0000915 default:
916 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroffddbff782007-08-21 21:17:12 +0000917 continue;
918 }
919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
John McCallbdd563e2009-11-03 02:38:08 +0000921 struct ObjCIvarCallback : FieldCallback {
922 Parser &P;
923 DeclPtrTy IDecl;
924 tok::ObjCKeywordKind visibility;
925 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
926
927 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
928 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
929 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
930 }
931
932 DeclPtrTy invoke(FieldDeclarator &FD) {
933 // Install the declarator into the interface decl.
934 DeclPtrTy Field
935 = P.Actions.ActOnIvar(P.CurScope,
936 FD.D.getDeclSpec().getSourceRange().getBegin(),
937 IDecl, FD.D, FD.BitfieldSize, visibility);
938 AllIvarDecls.push_back(Field);
939 return Field;
940 }
941 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
942
Chris Lattnere1359422008-04-10 06:46:29 +0000943 // Parse all the comma separated declarators.
944 DeclSpec DS;
John McCallbdd563e2009-11-03 02:38:08 +0000945 ParseStructDeclaration(DS, Callback);
Mike Stump1eb44332009-09-09 15:08:12 +0000946
Chris Lattnerdf195262007-10-09 17:51:17 +0000947 if (Tok.is(tok::semi)) {
Steve Naroffddbff782007-08-21 21:17:12 +0000948 ConsumeToken();
Steve Naroffddbff782007-08-21 21:17:12 +0000949 } else {
950 Diag(Tok, diag::err_expected_semi_decl_list);
951 // Skip to end of block or statement
952 SkipUntil(tok::r_brace, true, true);
953 }
954 }
Steve Naroff60fccee2007-10-29 21:38:07 +0000955 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff8749be52007-10-31 22:11:35 +0000956 // Call ActOnFields() even if we don't have any decls. This is useful
957 // for code rewriting tools that need to be aware of the empty list.
958 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foadbeaaccd2009-05-21 09:52:38 +0000959 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +0000960 LBraceLoc, RBraceLoc, 0);
Steve Naroffddbff782007-08-21 21:17:12 +0000961 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000962}
Steve Naroffdac269b2007-08-20 21:31:48 +0000963
964/// objc-protocol-declaration:
965/// objc-protocol-definition
966/// objc-protocol-forward-reference
967///
968/// objc-protocol-definition:
Mike Stump1eb44332009-09-09 15:08:12 +0000969/// @protocol identifier
970/// objc-protocol-refs[opt]
971/// objc-interface-decl-list
Steve Naroffdac269b2007-08-20 21:31:48 +0000972/// @end
973///
974/// objc-protocol-forward-reference:
975/// @protocol identifier-list ';'
976///
977/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff3536b442007-09-06 21:24:23 +0000978/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroffdac269b2007-08-20 21:31:48 +0000979/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000980Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
981 AttributeList *attrList) {
Steve Naroff861cf3e2007-08-23 18:16:40 +0000982 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000983 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
984 ConsumeToken(); // the "protocol" identifier
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Chris Lattnerdf195262007-10-09 17:51:17 +0000986 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000987 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattnerb28317a2009-03-28 19:18:32 +0000988 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000989 }
990 // Save the protocol name, then consume it.
991 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
992 SourceLocation nameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Chris Lattnerdf195262007-10-09 17:51:17 +0000994 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattner7caeabd2008-07-21 22:17:28 +0000995 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000996 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000997 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000998 attrList);
Steve Naroff7ef58fd2007-08-22 22:17:26 +0000999 }
Mike Stump1eb44332009-09-09 15:08:12 +00001000
Chris Lattnerdf195262007-10-09 17:51:17 +00001001 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattner7caeabd2008-07-21 22:17:28 +00001002 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1003 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1004
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001005 // Parse the list of forward declarations.
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001006 while (1) {
1007 ConsumeToken(); // the ','
Chris Lattnerdf195262007-10-09 17:51:17 +00001008 if (Tok.isNot(tok::identifier)) {
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001009 Diag(Tok, diag::err_expected_ident);
1010 SkipUntil(tok::semi);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001011 return DeclPtrTy();
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001012 }
Chris Lattner7caeabd2008-07-21 22:17:28 +00001013 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1014 Tok.getLocation()));
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001015 ConsumeToken(); // the identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Chris Lattnerdf195262007-10-09 17:51:17 +00001017 if (Tok.isNot(tok::comma))
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001018 break;
1019 }
1020 // Consume the ';'.
1021 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001022 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Steve Naroffe440eb82007-10-10 17:32:04 +00001024 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump1eb44332009-09-09 15:08:12 +00001025 &ProtocolRefs[0],
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001026 ProtocolRefs.size(),
1027 attrList);
Chris Lattner7caeabd2008-07-21 22:17:28 +00001028 }
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Steve Naroff7ef58fd2007-08-22 22:17:26 +00001030 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001031 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001032
Chris Lattnerb28317a2009-03-28 19:18:32 +00001033 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001034 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattner7caeabd2008-07-21 22:17:28 +00001035 if (Tok.is(tok::less) &&
Argyrios Kyrtzidis71b0add2009-09-29 19:41:44 +00001036 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1037 LAngleLoc, EndProtoLoc))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001038 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattnerb28317a2009-03-28 19:18:32 +00001040 DeclPtrTy ProtoType =
Chris Lattnere13b9592008-07-26 04:03:38 +00001041 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foadbeaaccd2009-05-21 09:52:38 +00001042 ProtocolRefs.data(),
1043 ProtocolRefs.size(),
Daniel Dunbar246e70f2008-09-26 04:48:09 +00001044 EndProtoLoc, attrList);
Fariborz Jahanian25e077d2007-09-17 21:07:36 +00001045 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerbc662af2008-10-20 06:10:06 +00001046 return ProtoType;
Reid Spencer5f016e22007-07-11 17:01:13 +00001047}
Steve Naroffdac269b2007-08-20 21:31:48 +00001048
1049/// objc-implementation:
1050/// objc-class-implementation-prologue
1051/// objc-category-implementation-prologue
1052///
1053/// objc-class-implementation-prologue:
1054/// @implementation identifier objc-superclass[opt]
1055/// objc-class-instance-variables[opt]
1056///
1057/// objc-category-implementation-prologue:
1058/// @implementation identifier ( identifier )
Chris Lattnerb28317a2009-03-28 19:18:32 +00001059Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001060 SourceLocation atLoc) {
1061 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1062 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1063 ConsumeToken(); // the "implementation" identifier
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattnerdf195262007-10-09 17:51:17 +00001065 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001066 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001067 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001068 }
1069 // We have a class or category name - consume it.
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001070 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001071 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump1eb44332009-09-09 15:08:12 +00001072
1073 if (Tok.is(tok::l_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001074 // we have a category implementation.
1075 SourceLocation lparenLoc = ConsumeParen();
1076 SourceLocation categoryLoc, rparenLoc;
1077 IdentifierInfo *categoryId = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001078
Chris Lattnerdf195262007-10-09 17:51:17 +00001079 if (Tok.is(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001080 categoryId = Tok.getIdentifierInfo();
1081 categoryLoc = ConsumeToken();
1082 } else {
1083 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001084 return DeclPtrTy();
Mike Stump1eb44332009-09-09 15:08:12 +00001085 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001086 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001087 Diag(Tok, diag::err_expected_rparen);
1088 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001089 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001090 }
1091 rparenLoc = ConsumeParen();
Chris Lattnerb28317a2009-03-28 19:18:32 +00001092 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump1eb44332009-09-09 15:08:12 +00001093 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian8f3fde02007-10-02 16:38:50 +00001094 categoryLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001095 ObjCImpDecl = ImplCatType;
Chris Lattnerb28317a2009-03-28 19:18:32 +00001096 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001097 }
1098 // We have a class implementation
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001099 SourceLocation superClassLoc;
1100 IdentifierInfo *superClassId = 0;
Chris Lattnerdf195262007-10-09 17:51:17 +00001101 if (Tok.is(tok::colon)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001102 // We have a super class
1103 ConsumeToken();
Chris Lattnerdf195262007-10-09 17:51:17 +00001104 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001105 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattnerb28317a2009-03-28 19:18:32 +00001106 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001107 }
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001108 superClassId = Tok.getIdentifierInfo();
1109 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001110 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001111 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattnercb53b362007-12-27 19:57:00 +00001112 atLoc, nameId, nameLoc,
Fariborz Jahanianccb4f312007-09-25 18:38:09 +00001113 superClassId, superClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Steve Naroff60fccee2007-10-29 21:38:07 +00001115 if (Tok.is(tok::l_brace)) // we have ivars
1116 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001117 ObjCImpDecl = ImplClsType;
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Chris Lattnerb28317a2009-03-28 19:18:32 +00001119 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001120}
Steve Naroff60fccee2007-10-29 21:38:07 +00001121
Chris Lattnerb28317a2009-03-28 19:18:32 +00001122Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001123 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1124 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattnerb28317a2009-03-28 19:18:32 +00001125 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001126 ConsumeToken(); // the "end" identifier
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001127 if (ObjCImpDecl) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001128 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001129 ObjCImpDecl = DeclPtrTy();
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001130 }
Fariborz Jahanian94cdb252008-01-10 17:58:07 +00001131 else
1132 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahaniana6e3ac52009-03-04 22:30:12 +00001133 return Result;
Steve Naroffdac269b2007-08-20 21:31:48 +00001134}
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001135
1136/// compatibility-alias-decl:
1137/// @compatibility_alias alias-name class-name ';'
1138///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001139Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001140 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1141 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1142 ConsumeToken(); // consume compatibility_alias
Chris Lattnerdf195262007-10-09 17:51:17 +00001143 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001144 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001145 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001146 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001147 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1148 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattnerdf195262007-10-09 17:51:17 +00001149 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001150 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001151 return DeclPtrTy();
Fariborz Jahaniane992af02007-09-04 19:26:51 +00001152 }
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001153 IdentifierInfo *classId = Tok.getIdentifierInfo();
1154 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1155 if (Tok.isNot(tok::semi)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001156 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001157 return DeclPtrTy();
Fariborz Jahanian243b64b2007-10-11 23:42:27 +00001158 }
Chris Lattnerb28317a2009-03-28 19:18:32 +00001159 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1160 classId, classLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +00001161}
1162
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001163/// property-synthesis:
1164/// @synthesize property-ivar-list ';'
1165///
1166/// property-ivar-list:
1167/// property-ivar
1168/// property-ivar-list ',' property-ivar
1169///
1170/// property-ivar:
1171/// identifier
1172/// identifier '=' identifier
1173///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001174Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001175 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1176 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001177 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattnerdf195262007-10-09 17:51:17 +00001178 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001179 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001180 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001181 }
Mike Stump1eb44332009-09-09 15:08:12 +00001182
Chris Lattnerdf195262007-10-09 17:51:17 +00001183 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001184 IdentifierInfo *propertyIvar = 0;
1185 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1186 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattnerdf195262007-10-09 17:51:17 +00001187 if (Tok.is(tok::equal)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001188 // property '=' ivar-name
1189 ConsumeToken(); // consume '='
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);
1192 break;
1193 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001194 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001195 ConsumeToken(); // consume ivar-name
1196 }
Fariborz Jahanianf624f812008-04-18 00:19:30 +00001197 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1198 propertyId, propertyIvar);
Chris Lattnerdf195262007-10-09 17:51:17 +00001199 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001200 break;
1201 ConsumeToken(); // consume ','
1202 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001203 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001204 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahaniand3fdcb52009-11-06 21:48:47 +00001205 else
1206 ConsumeToken(); // consume ';'
Chris Lattnerb28317a2009-03-28 19:18:32 +00001207 return DeclPtrTy();
Reid Spencer5f016e22007-07-11 17:01:13 +00001208}
1209
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001210/// property-dynamic:
1211/// @dynamic property-list
1212///
1213/// property-list:
1214/// identifier
1215/// property-list ',' identifier
1216///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001217Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001218 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1219 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1220 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattnerdf195262007-10-09 17:51:17 +00001221 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001222 Diag(Tok, diag::err_expected_ident);
Chris Lattnerb28317a2009-03-28 19:18:32 +00001223 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001224 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001225 while (Tok.is(tok::identifier)) {
Fariborz Jahanianc35b9e42008-04-21 21:05:54 +00001226 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1227 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1228 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1229 propertyId, 0);
1230
Chris Lattnerdf195262007-10-09 17:51:17 +00001231 if (Tok.isNot(tok::comma))
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001232 break;
1233 ConsumeToken(); // consume ','
1234 }
Chris Lattnerdf195262007-10-09 17:51:17 +00001235 if (Tok.isNot(tok::semi))
Chris Lattner1ab3b962008-11-18 07:48:38 +00001236 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattnerb28317a2009-03-28 19:18:32 +00001237 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001238}
Mike Stump1eb44332009-09-09 15:08:12 +00001239
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001240/// objc-throw-statement:
1241/// throw expression[opt];
1242///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001243Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001244 OwningExprResult Res(Actions);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001245 ConsumeToken(); // consume throw
Chris Lattnerdf195262007-10-09 17:51:17 +00001246 if (Tok.isNot(tok::semi)) {
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001247 Res = ParseExpression();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001248 if (Res.isInvalid()) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001249 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001250 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001251 }
1252 }
Fariborz Jahanian39f8f152007-11-07 02:00:49 +00001253 ConsumeToken(); // consume ';'
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001254 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001255}
1256
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001257/// objc-synchronized-statement:
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001258/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001259///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001260Parser::OwningStmtResult
1261Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001262 ConsumeToken(); // consume synchronized
1263 if (Tok.isNot(tok::l_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001264 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001265 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001266 }
1267 ConsumeParen(); // '('
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001268 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001269 if (Res.isInvalid()) {
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001270 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001271 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001272 }
1273 if (Tok.isNot(tok::r_paren)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001274 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001275 return StmtError();
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001276 }
1277 ConsumeParen(); // ')'
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001278 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001279 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001280 return StmtError();
Fariborz Jahanian78a677b2008-01-30 17:38:29 +00001281 }
Steve Naroff3ac438c2008-06-04 20:36:13 +00001282 // Enter a scope to hold everything within the compound stmt. Compound
1283 // statements can always hold declarations.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001284 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroff3ac438c2008-06-04 20:36:13 +00001285
Sebastian Redl61364dd2008-12-11 19:30:53 +00001286 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001287
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001288 BodyScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001289 if (SynchBody.isInvalid())
Fariborz Jahanianfa3ee8e2008-01-29 19:14:59 +00001290 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001291 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianc385c902008-01-29 18:21:32 +00001292}
1293
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001294/// objc-try-catch-statement:
1295/// @try compound-statement objc-catch-list[opt]
1296/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1297///
1298/// objc-catch-list:
1299/// @catch ( parameter-declaration ) compound-statement
1300/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1301/// catch-parameter-declaration:
1302/// parameter-declaration
1303/// '...' [OBJC2]
1304///
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001305Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001306 bool catch_or_finally_seen = false;
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001307
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001308 ConsumeToken(); // consume try
Chris Lattnerdf195262007-10-09 17:51:17 +00001309 if (Tok.isNot(tok::l_brace)) {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001310 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001311 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001312 }
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001313 OwningStmtResult CatchStmts(Actions);
1314 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001315 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001316 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001317 TryScope.Exit();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001318 if (TryBody.isInvalid())
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001319 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redla55e52c2008-11-25 22:21:31 +00001320
Chris Lattnerdf195262007-10-09 17:51:17 +00001321 while (Tok.is(tok::at)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001322 // At this point, we need to lookahead to determine if this @ is the start
1323 // of an @catch or @finally. We don't want to consume the @ token if this
1324 // is an @try or @encode or something else.
1325 Token AfterAt = GetLookAheadToken(1);
1326 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1327 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1328 break;
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001329
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001330 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattnercb53b362007-12-27 19:57:00 +00001331 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattnerb28317a2009-03-28 19:18:32 +00001332 DeclPtrTy FirstPart;
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001333 ConsumeToken(); // consume catch
Chris Lattnerdf195262007-10-09 17:51:17 +00001334 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001335 ConsumeParen();
Steve Naroffe21dd6f2009-02-11 20:05:44 +00001336 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattnerdf195262007-10-09 17:51:17 +00001337 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001338 DeclSpec DS;
1339 ParseDeclarationSpecifiers(DS);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001340 // For some odd reason, the name of the exception variable is
Mike Stump1eb44332009-09-09 15:08:12 +00001341 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff7ba138a2009-03-03 19:52:17 +00001342 // we must accept either 'declarator' or 'abstract-declarator' here.
1343 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1344 ParseDeclarator(ParmDecl);
1345
1346 // Inform the actions module about the parameter declarator, so it
1347 // gets added to the current scope.
1348 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroff64515f32008-02-05 21:27:35 +00001349 } else
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001350 ConsumeToken(); // consume '...'
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Steve Naroff93a25952009-04-07 22:56:58 +00001352 SourceLocation RParenLoc;
Mike Stump1eb44332009-09-09 15:08:12 +00001353
Steve Naroff93a25952009-04-07 22:56:58 +00001354 if (Tok.is(tok::r_paren))
1355 RParenLoc = ConsumeParen();
1356 else // Skip over garbage, until we get to ')'. Eat the ')'.
1357 SkipUntil(tok::r_paren, true, false);
1358
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001359 OwningStmtResult CatchBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001360 if (Tok.is(tok::l_brace))
1361 CatchBody = ParseCompoundStatementBody();
1362 else
1363 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001364 if (CatchBody.isInvalid())
Fariborz Jahanian3b1191d2007-11-01 23:59:59 +00001365 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001366 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff7ba138a2009-03-03 19:52:17 +00001367 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001368 move(CatchStmts));
Steve Naroff64515f32008-02-05 21:27:35 +00001369 } else {
Chris Lattner1ab3b962008-11-18 07:48:38 +00001370 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1371 << "@catch clause";
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001372 return StmtError();
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001373 }
1374 catch_or_finally_seen = true;
Chris Lattner6b884502008-03-10 06:06:04 +00001375 } else {
1376 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroff64515f32008-02-05 21:27:35 +00001377 ConsumeToken(); // consume finally
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001378 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001379
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001380 OwningStmtResult FinallyBody(Actions, true);
Chris Lattnerc1b3ba52008-02-14 19:27:54 +00001381 if (Tok.is(tok::l_brace))
1382 FinallyBody = ParseCompoundStatementBody();
1383 else
1384 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001385 if (FinallyBody.isInvalid())
Fariborz Jahanian161a9c52007-11-02 00:18:53 +00001386 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001387 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001388 move(FinallyBody));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001389 catch_or_finally_seen = true;
1390 break;
1391 }
1392 }
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001393 if (!catch_or_finally_seen) {
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001394 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001395 return StmtError();
Fariborz Jahanianbd49a642007-11-02 15:39:31 +00001396 }
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001397 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1398 move(FinallyStmt));
Fariborz Jahanian397fcc12007-09-19 19:14:32 +00001399}
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001400
Steve Naroff3536b442007-09-06 21:24:23 +00001401/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001402///
Chris Lattnerb28317a2009-03-28 19:18:32 +00001403Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1404 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001405
Chris Lattner49f28ca2009-03-05 08:00:35 +00001406 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1407 PP.getSourceManager(),
1408 "parsing Objective-C method");
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001410 // parse optional ';'
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001411 if (Tok.is(tok::semi)) {
Ted Kremenek496e45e2009-11-10 22:55:49 +00001412 if (ObjCImpDecl) {
1413 Diag(Tok, diag::warn_semicolon_before_method_body)
1414 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1415 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001416 ConsumeToken();
Fariborz Jahanian209a8c22009-10-20 16:39:13 +00001417 }
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001418
Steve Naroff409be832007-11-11 19:54:21 +00001419 // We should have an opening brace now.
Chris Lattnerdf195262007-10-09 17:51:17 +00001420 if (Tok.isNot(tok::l_brace)) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001421 Diag(Tok, diag::err_expected_method_body);
Mike Stump1eb44332009-09-09 15:08:12 +00001422
Steve Naroff409be832007-11-11 19:54:21 +00001423 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1424 SkipUntil(tok::l_brace, true, true);
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Steve Naroff409be832007-11-11 19:54:21 +00001426 // If we didn't find the '{', bail out.
1427 if (Tok.isNot(tok::l_brace))
Chris Lattnerb28317a2009-03-28 19:18:32 +00001428 return DeclPtrTy();
Fariborz Jahanianac00b7f2007-09-01 00:26:16 +00001429 }
Steve Naroff409be832007-11-11 19:54:21 +00001430 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump1eb44332009-09-09 15:08:12 +00001431
Steve Naroff409be832007-11-11 19:54:21 +00001432 // Enter a scope for the method body.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001433 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump1eb44332009-09-09 15:08:12 +00001434
Steve Naroff409be832007-11-11 19:54:21 +00001435 // Tell the actions module that we have entered a method definition with the
Steve Naroff394f3f42008-07-25 17:57:26 +00001436 // specified Declarator for the method.
Steve Naroffebf64432009-02-28 16:59:13 +00001437 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl61364dd2008-12-11 19:30:53 +00001438
1439 OwningStmtResult FnBody(ParseCompoundStatementBody());
1440
Steve Naroff409be832007-11-11 19:54:21 +00001441 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001442 if (FnBody.isInvalid())
Sebastian Redla60528c2008-12-21 12:04:03 +00001443 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1444 MultiStmtArg(Actions), false);
Sebastian Redl798d1192008-12-13 16:23:55 +00001445
Steve Naroff32ce8372009-03-02 22:00:56 +00001446 // TODO: Pass argument information.
1447 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Steve Naroff409be832007-11-11 19:54:21 +00001449 // Leave the function body scope.
Douglas Gregor8935b8b2008-12-10 06:34:36 +00001450 BodyScope.Exit();
Sebastian Redl798d1192008-12-13 16:23:55 +00001451
Steve Naroff71c0a952007-11-13 23:01:27 +00001452 return MDecl;
Reid Spencer5f016e22007-07-11 17:01:13 +00001453}
Anders Carlsson55085182007-08-21 17:43:55 +00001454
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001455Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroff64515f32008-02-05 21:27:35 +00001456 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner6b884502008-03-10 06:06:04 +00001457 return ParseObjCTryStmt(AtLoc);
Steve Naroff64515f32008-02-05 21:27:35 +00001458 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1459 return ParseObjCThrowStmt(AtLoc);
1460 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1461 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redld8c4e152008-12-11 22:33:27 +00001462 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001463 if (Res.isInvalid()) {
Steve Naroff64515f32008-02-05 21:27:35 +00001464 // If the expression is invalid, skip ahead to the next semicolon. Not
1465 // doing this opens us up to the possibility of infinite loops if
1466 // ParseExpression does not consume any tokens.
1467 SkipUntil(tok::semi);
Sebastian Redl43bc2a02008-12-11 20:12:42 +00001468 return StmtError();
Steve Naroff64515f32008-02-05 21:27:35 +00001469 }
1470 // Otherwise, eat the semicolon.
1471 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson6b1d2832009-05-17 21:11:30 +00001472 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroff64515f32008-02-05 21:27:35 +00001473}
1474
Sebastian Redl1d922962008-12-13 15:32:12 +00001475Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson55085182007-08-21 17:43:55 +00001476 switch (Tok.getKind()) {
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001477 case tok::string_literal: // primary-expression: string-literal
1478 case tok::wide_string_literal:
Sebastian Redl1d922962008-12-13 15:32:12 +00001479 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001480 default:
Chris Lattner4fef81d2008-08-05 06:19:09 +00001481 if (Tok.getIdentifierInfo() == 0)
Sebastian Redl1d922962008-12-13 15:32:12 +00001482 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001483
Chris Lattner4fef81d2008-08-05 06:19:09 +00001484 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1485 case tok::objc_encode:
Sebastian Redl1d922962008-12-13 15:32:12 +00001486 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001487 case tok::objc_protocol:
Sebastian Redl1d922962008-12-13 15:32:12 +00001488 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001489 case tok::objc_selector:
Sebastian Redl1d922962008-12-13 15:32:12 +00001490 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001491 default:
Sebastian Redl1d922962008-12-13 15:32:12 +00001492 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner4fef81d2008-08-05 06:19:09 +00001493 }
Anders Carlsson55085182007-08-21 17:43:55 +00001494 }
Anders Carlsson55085182007-08-21 17:43:55 +00001495}
1496
Mike Stump1eb44332009-09-09 15:08:12 +00001497/// objc-message-expr:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001498/// '[' objc-receiver objc-message-args ']'
1499///
1500/// objc-receiver:
1501/// expression
1502/// class-name
1503/// type-name
Sebastian Redl1d922962008-12-13 15:32:12 +00001504Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner699b6612008-01-25 18:59:06 +00001505 assert(Tok.is(tok::l_square) && "'[' expected");
1506 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1507
1508 // Parse receiver
Chris Lattner14dd98a2008-01-25 19:25:00 +00001509 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner699b6612008-01-25 18:59:06 +00001510 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahaniand2869922009-04-08 19:50:10 +00001511 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1512 SourceLocation NameLoc = ConsumeToken();
1513 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1514 ExprArg(Actions));
1515 }
Chris Lattner699b6612008-01-25 18:59:06 +00001516 }
1517
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001518 OwningExprResult Res(ParseExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001519 if (Res.isInvalid()) {
Chris Lattner5c749422008-01-25 19:43:26 +00001520 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001521 return move(Res);
Chris Lattner699b6612008-01-25 18:59:06 +00001522 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001523
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001524 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl76ad2e82009-02-05 15:02:23 +00001525 0, move(Res));
Chris Lattner699b6612008-01-25 18:59:06 +00001526}
Sebastian Redl1d922962008-12-13 15:32:12 +00001527
Chris Lattner699b6612008-01-25 18:59:06 +00001528/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1529/// the rest of a message expression.
Mike Stump1eb44332009-09-09 15:08:12 +00001530///
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001531/// objc-message-args:
1532/// objc-selector
1533/// objc-keywordarg-list
1534///
1535/// objc-keywordarg-list:
1536/// objc-keywordarg
1537/// objc-keywordarg-list objc-keywordarg
1538///
Mike Stump1eb44332009-09-09 15:08:12 +00001539/// objc-keywordarg:
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001540/// selector-name[opt] ':' objc-keywordexpr
1541///
1542/// objc-keywordexpr:
1543/// nonempty-expr-list
1544///
1545/// nonempty-expr-list:
1546/// assignment-expression
1547/// nonempty-expr-list , assignment-expression
Sebastian Redl1d922962008-12-13 15:32:12 +00001548///
1549Parser::OwningExprResult
Chris Lattner699b6612008-01-25 18:59:06 +00001550Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff5cb93b82008-11-19 15:54:23 +00001551 SourceLocation NameLoc,
Chris Lattner699b6612008-01-25 18:59:06 +00001552 IdentifierInfo *ReceiverName,
Sebastian Redl1d922962008-12-13 15:32:12 +00001553 ExprArg ReceiverExpr) {
Steve Naroffc4df6d22009-11-07 02:08:14 +00001554 if (Tok.is(tok::code_completion)) {
1555 if (ReceiverName)
1556 Actions.CodeCompleteObjCFactoryMethod(CurScope, ReceiverName);
1557 else
1558 Actions.CodeCompleteObjCInstanceMethod(CurScope, ReceiverExpr.release());
1559 ConsumeToken();
1560 }
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001561 // Parse objc-selector
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001562 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001563 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Naroff68d331a2007-09-27 14:38:14 +00001564
Anders Carlssonff975cf2009-02-14 18:21:46 +00001565 SourceLocation SelectorLoc = Loc;
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Steve Naroff68d331a2007-09-27 14:38:14 +00001567 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001568 ExprVector KeyExprs(Actions);
Steve Naroff68d331a2007-09-27 14:38:14 +00001569
Chris Lattnerdf195262007-10-09 17:51:17 +00001570 if (Tok.is(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001571 while (1) {
1572 // Each iteration parses a single keyword argument.
Steve Naroff68d331a2007-09-27 14:38:14 +00001573 KeyIdents.push_back(selIdent);
Steve Naroff37387c92007-09-17 20:25:27 +00001574
Chris Lattnerdf195262007-10-09 17:51:17 +00001575 if (Tok.isNot(tok::colon)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001576 Diag(Tok, diag::err_expected_colon);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001577 // We must manually skip to a ']', otherwise the expression skipper will
1578 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1579 // the enclosing expression.
1580 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001581 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001582 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001583
Steve Naroff68d331a2007-09-27 14:38:14 +00001584 ConsumeToken(); // Eat the ':'.
Mike Stump1eb44332009-09-09 15:08:12 +00001585 /// Parse the expression after ':'
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001586 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001587 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001588 // We must manually skip to a ']', otherwise the expression skipper will
1589 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1590 // the enclosing expression.
1591 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001592 return move(Res);
Steve Naroff37387c92007-09-17 20:25:27 +00001593 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001594
Steve Naroff37387c92007-09-17 20:25:27 +00001595 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001596 KeyExprs.push_back(Res.release());
Sebastian Redl1d922962008-12-13 15:32:12 +00001597
Steve Naroff37387c92007-09-17 20:25:27 +00001598 // Check for another keyword selector.
Chris Lattner2fc5c242009-04-11 18:13:45 +00001599 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattnerdf195262007-10-09 17:51:17 +00001600 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001601 break;
1602 // We have a selector or a colon, continue parsing.
1603 }
1604 // Parse the, optional, argument list, comma separated.
Chris Lattnerdf195262007-10-09 17:51:17 +00001605 while (Tok.is(tok::comma)) {
Steve Naroff49f109c2007-11-15 13:05:42 +00001606 ConsumeToken(); // Eat the ','.
Mike Stump1eb44332009-09-09 15:08:12 +00001607 /// Parse the expression after ','
Sebastian Redl2f7ece72008-12-11 21:36:32 +00001608 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001609 if (Res.isInvalid()) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001610 // We must manually skip to a ']', otherwise the expression skipper will
1611 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1612 // the enclosing expression.
1613 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001614 return move(Res);
Steve Naroff49f109c2007-11-15 13:05:42 +00001615 }
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001616
Steve Naroff49f109c2007-11-15 13:05:42 +00001617 // We have a valid expression.
Sebastian Redleffa8d12008-12-10 00:02:53 +00001618 KeyExprs.push_back(Res.release());
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001619 }
1620 } else if (!selIdent) {
1621 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redl1d922962008-12-13 15:32:12 +00001622
Chris Lattner4fef81d2008-08-05 06:19:09 +00001623 // We must manually skip to a ']', otherwise the expression skipper will
1624 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1625 // the enclosing expression.
1626 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001627 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001628 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001629
Chris Lattnerdf195262007-10-09 17:51:17 +00001630 if (Tok.isNot(tok::r_square)) {
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001631 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner4fef81d2008-08-05 06:19:09 +00001632 // We must manually skip to a ']', otherwise the expression skipper will
1633 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1634 // the enclosing expression.
1635 SkipUntil(tok::r_square);
Sebastian Redl1d922962008-12-13 15:32:12 +00001636 return ExprError();
Fariborz Jahaniana65ff6c2007-09-05 23:08:20 +00001637 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001638
Chris Lattner699b6612008-01-25 18:59:06 +00001639 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redl1d922962008-12-13 15:32:12 +00001640
Steve Naroff29238a02007-10-05 18:42:47 +00001641 unsigned nKeys = KeyIdents.size();
Chris Lattnerff384912007-10-07 02:00:24 +00001642 if (nKeys == 0)
1643 KeyIdents.push_back(selIdent);
1644 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001645
Chris Lattnerff384912007-10-07 02:00:24 +00001646 // We've just parsed a keyword message.
Sebastian Redl1d922962008-12-13 15:32:12 +00001647 if (ReceiverName)
1648 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump1eb44332009-09-09 15:08:12 +00001649 LBracLoc, NameLoc, SelectorLoc,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001650 RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001651 KeyExprs.take(), KeyExprs.size()));
1652 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlssonff975cf2009-02-14 18:21:46 +00001653 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redl1d922962008-12-13 15:32:12 +00001654 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian0ccb27d2007-09-05 19:52:07 +00001655}
1656
Sebastian Redl1d922962008-12-13 15:32:12 +00001657Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redl20df9b72008-12-11 22:51:44 +00001658 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redl1d922962008-12-13 15:32:12 +00001659 if (Res.isInvalid()) return move(Res);
1660
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001661 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1662 // expressions. At this point, we know that the only valid thing that starts
1663 // with '@' is an @"".
1664 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redla55e52c2008-11-25 22:21:31 +00001665 ExprVector AtStrings(Actions);
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001666 AtLocs.push_back(AtLoc);
Sebastian Redleffa8d12008-12-10 00:02:53 +00001667 AtStrings.push_back(Res.release());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001668
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001669 while (Tok.is(tok::at)) {
1670 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson55085182007-08-21 17:43:55 +00001671
Sebastian Redl15faa7f2008-12-09 20:22:58 +00001672 // Invalid unless there is a string literal.
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001673 if (!isTokenStringLiteral())
1674 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001675
Chris Lattner97cf6eb2009-02-18 05:56:09 +00001676 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001677 if (Lit.isInvalid())
Sebastian Redl1d922962008-12-13 15:32:12 +00001678 return move(Lit);
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00001679
Sebastian Redleffa8d12008-12-10 00:02:53 +00001680 AtStrings.push_back(Lit.release());
Chris Lattnerb3a99cd2007-12-12 01:04:12 +00001681 }
Sebastian Redl1d922962008-12-13 15:32:12 +00001682
1683 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1684 AtStrings.size()));
Anders Carlsson55085182007-08-21 17:43:55 +00001685}
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001686
1687/// objc-encode-expression:
1688/// @encode ( type-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001689Parser::OwningExprResult
1690Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff861cf3e2007-08-23 18:16:40 +00001691 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redl1d922962008-12-13 15:32:12 +00001692
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001693 SourceLocation EncLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001694
Chris Lattner4fef81d2008-08-05 06:19:09 +00001695 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001696 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1697
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001698 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001699
Douglas Gregor809070a2009-02-18 17:45:20 +00001700 TypeResult Ty = ParseTypeName();
Sebastian Redl1d922962008-12-13 15:32:12 +00001701
Anders Carlsson4988ae32007-08-23 15:31:37 +00001702 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001703
Douglas Gregor809070a2009-02-18 17:45:20 +00001704 if (Ty.isInvalid())
1705 return ExprError();
1706
Mike Stump1eb44332009-09-09 15:08:12 +00001707 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor809070a2009-02-18 17:45:20 +00001708 Ty.get(), RParenLoc));
Anders Carlssonf9bcf012007-08-22 15:14:15 +00001709}
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001710
1711/// objc-protocol-expression
1712/// @protocol ( protocol-name )
Sebastian Redl1d922962008-12-13 15:32:12 +00001713Parser::OwningExprResult
1714Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001715 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001716
Chris Lattner4fef81d2008-08-05 06:19:09 +00001717 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001718 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1719
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001720 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redl1d922962008-12-13 15:32:12 +00001721
Chris Lattner4fef81d2008-08-05 06:19:09 +00001722 if (Tok.isNot(tok::identifier))
Sebastian Redl1d922962008-12-13 15:32:12 +00001723 return ExprError(Diag(Tok, diag::err_expected_ident));
1724
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001725 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001726 ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001727
Anders Carlsson4988ae32007-08-23 15:31:37 +00001728 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001729
Sebastian Redl1d922962008-12-13 15:32:12 +00001730 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1731 LParenLoc, RParenLoc));
Anders Carlsson29b2cb12007-08-23 15:25:28 +00001732}
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001733
1734/// objc-selector-expression
1735/// @selector '(' objc-keyword-selector ')'
Sebastian Redl1d922962008-12-13 15:32:12 +00001736Parser::OwningExprResult
1737Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001738 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redl1d922962008-12-13 15:32:12 +00001739
Chris Lattner4fef81d2008-08-05 06:19:09 +00001740 if (Tok.isNot(tok::l_paren))
Sebastian Redl1d922962008-12-13 15:32:12 +00001741 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1742
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001743 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001744 SourceLocation LParenLoc = ConsumeParen();
1745 SourceLocation sLoc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001746 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redl1d922962008-12-13 15:32:12 +00001747 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1748 return ExprError(Diag(Tok, diag::err_expected_ident));
1749
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001750 KeyIdents.push_back(SelIdent);
Steve Naroff887407e2007-12-05 22:21:29 +00001751 unsigned nColons = 0;
1752 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001753 while (1) {
Chris Lattner4fef81d2008-08-05 06:19:09 +00001754 if (Tok.isNot(tok::colon))
Sebastian Redl1d922962008-12-13 15:32:12 +00001755 return ExprError(Diag(Tok, diag::err_expected_colon));
1756
Chris Lattnercb53b362007-12-27 19:57:00 +00001757 nColons++;
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001758 ConsumeToken(); // Eat the ':'.
1759 if (Tok.is(tok::r_paren))
1760 break;
1761 // Check for another keyword selector.
1762 SourceLocation Loc;
Chris Lattner2fc5c242009-04-11 18:13:45 +00001763 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001764 KeyIdents.push_back(SelIdent);
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001765 if (!SelIdent && Tok.isNot(tok::colon))
1766 break;
1767 }
Steve Naroff887407e2007-12-05 22:21:29 +00001768 }
Fariborz Jahaniana0818e32007-10-15 23:39:13 +00001769 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff887407e2007-12-05 22:21:29 +00001770 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redl1d922962008-12-13 15:32:12 +00001771 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1772 LParenLoc, RParenLoc));
Gabor Greif58065b22007-10-19 15:38:32 +00001773 }