blob: a60c89a0f122ccf11c2db1bae43d86c7ba4ad205 [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08: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 Narofff1bc45b2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian9e63b982007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000019using namespace clang;
20
21
Chris Lattner3a907162008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroffe0933392007-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 Lattner83f095c2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000032
Steve Naroff7c348172007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-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 Lattner83f095c2009-03-28 19:18:32 +000053 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000054 }
55}
56
57///
Mike Stump11289f42009-09-09 15:08:12 +000058/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000059/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000060///
Chris Lattner83f095c2009-03-28 19:18:32 +000061Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000062 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000063 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremeneka26da852009-11-17 23:12:20 +000064 llvm::SmallVector<SourceLocation, 8> ClassLocs;
65
Mike Stump11289f42009-09-09 15:08:12 +000066
Chris Lattnerda59c2f2006-11-05 02:08:13 +000067 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000068 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000069 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000070 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000071 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000072 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000073 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000074 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000075 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000076
Chris Lattner0ef13522007-10-09 17:51:17 +000077 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 break;
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 ConsumeToken();
81 }
Mike Stump11289f42009-09-09 15:08:12 +000082
Chris Lattnerda59c2f2006-11-05 02:08:13 +000083 // Consume the ';'.
84 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattner83f095c2009-03-28 19:18:32 +000085 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +000086
Ted Kremeneka26da852009-11-17 23:12:20 +000087 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
88 ClassLocs.data(),
89 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000090}
91
Steve Naroff1eb1ad62007-08-20 21:31:48 +000092///
93/// objc-interface:
94/// objc-class-interface-attributes[opt] objc-class-interface
95/// objc-category-interface
96///
97/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +000098/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +000099/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000100/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000101/// objc-interface-decl-list
102/// @end
103///
104/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000105/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000106/// objc-protocol-refs[opt]
107/// objc-interface-decl-list
108/// @end
109///
110/// objc-superclass:
111/// ':' identifier
112///
113/// objc-class-interface-attributes:
114/// __attribute__((visibility("default")))
115/// __attribute__((visibility("hidden")))
116/// __attribute__((deprecated))
117/// __attribute__((unavailable))
118/// __attribute__((objc_exception)) - used by NSException on 64-bit
119///
Chris Lattner83f095c2009-03-28 19:18:32 +0000120Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000121 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000122 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000123 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
124 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000125
Douglas Gregor49c22a72009-11-18 16:26:39 +0000126 // Code completion after '@interface'.
127 if (Tok.is(tok::code_completion)) {
128 Actions.CodeCompleteObjCInterfaceDecl(CurScope);
129 ConsumeToken();
130 }
131
Chris Lattner0ef13522007-10-09 17:51:17 +0000132 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000133 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000134 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000135 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000136
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000137 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000138 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000139 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000140
Chris Lattner0ef13522007-10-09 17:51:17 +0000141 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000142 SourceLocation lparenLoc = ConsumeParen();
143 SourceLocation categoryLoc, rparenLoc;
144 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000145
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000146 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000147 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000148 categoryId = Tok.getIdentifierInfo();
149 categoryLoc = ConsumeToken();
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000150 } else if (!getLang().ObjC2) {
151 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000152 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000153 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000154 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000155 Diag(Tok, diag::err_expected_rparen);
156 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000157 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000158 }
159 rparenLoc = ConsumeParen();
Mike Stump11289f42009-09-09 15:08:12 +0000160
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000161 // Next, we need to check for any protocol references.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000162 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000163 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000164 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerb1f3c942008-07-26 04:07:02 +0000165 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000166 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
167 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000168 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000169
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000170 if (attrList) // categories don't support attributes.
171 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000172
Jay Foad7d0479f2009-05-21 09:52:38 +0000173 DeclPtrTy CategoryType =
Mike Stump11289f42009-09-09 15:08:12 +0000174 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000175 nameId, nameLoc,
176 categoryId, categoryLoc,
177 ProtocolRefs.data(),
178 ProtocolRefs.size(),
179 EndProtoLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000180
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000181 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000182 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000183 }
184 // Parse a class interface.
185 IdentifierInfo *superClassId = 0;
186 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000187
Chris Lattner0ef13522007-10-09 17:51:17 +0000188 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000189 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000190
191 // Code completion of superclass names.
192 if (Tok.is(tok::code_completion)) {
193 Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
194 ConsumeToken();
195 }
196
Chris Lattner0ef13522007-10-09 17:51:17 +0000197 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000198 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000199 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000200 }
201 superClassId = Tok.getIdentifierInfo();
202 superClassLoc = ConsumeToken();
203 }
204 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000205 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000206 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
207 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000208 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000209 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
210 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000211 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000212
213 DeclPtrTy ClsType =
214 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000215 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000216 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000217 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000218
Chris Lattner0ef13522007-10-09 17:51:17 +0000219 if (Tok.is(tok::l_brace))
Steve Naroff33a1e802007-10-29 21:38:07 +0000220 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000221
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000222 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000223 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000224}
225
226/// objc-interface-decl-list:
227/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000228/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000229/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000230/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000231/// objc-interface-decl-list declaration
232/// objc-interface-decl-list ';'
233///
Steve Naroff99264b42007-08-22 16:35:03 +0000234/// objc-method-requirement: [OBJC2]
235/// @required
236/// @optional
237///
Chris Lattner83f095c2009-03-28 19:18:32 +0000238void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000239 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000240 llvm::SmallVector<DeclPtrTy, 32> allMethods;
241 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000242 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000243 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000244
Chris Lattnerda9fb152008-10-20 06:10:06 +0000245 SourceLocation AtEndLoc;
246
Steve Naroff99264b42007-08-22 16:35:03 +0000247 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000248 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000249 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000250 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000251 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000252 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000253 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
254 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000255 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
256 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000257 continue;
258 }
Mike Stump11289f42009-09-09 15:08:12 +0000259
Chris Lattner038a3e32008-10-20 05:46:22 +0000260 // Ignore excess semicolons.
261 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000262 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000263 continue;
264 }
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattnerda9fb152008-10-20 06:10:06 +0000266 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000267 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000268 break;
Mike Stump11289f42009-09-09 15:08:12 +0000269
Chris Lattner038a3e32008-10-20 05:46:22 +0000270 // If we don't have an @ directive, parse it as a function definition.
271 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000272 // The code below does not consume '}'s because it is afraid of eating the
273 // end of a namespace. Because of the way this code is structured, an
274 // erroneous r_brace would cause an infinite loop if not handled here.
275 if (Tok.is(tok::r_brace))
276 break;
Mike Stump11289f42009-09-09 15:08:12 +0000277
Steve Narofff1bc45b2007-08-22 18:35:33 +0000278 // FIXME: as the name implies, this rule allows function definitions.
279 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000280 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattner038a3e32008-10-20 05:46:22 +0000281 continue;
282 }
Mike Stump11289f42009-09-09 15:08:12 +0000283
Chris Lattner038a3e32008-10-20 05:46:22 +0000284 // Otherwise, we have an @ directive, eat the @.
285 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000286 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000287
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000288 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattner038a3e32008-10-20 05:46:22 +0000289 AtEndLoc = AtLoc;
290 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000291 }
Mike Stump11289f42009-09-09 15:08:12 +0000292
Chris Lattnerda9fb152008-10-20 06:10:06 +0000293 // Eat the identifier.
294 ConsumeToken();
295
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000296 switch (DirectiveKind) {
297 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000298 // FIXME: If someone forgets an @end on a protocol, this loop will
299 // continue to eat up tons of stuff and spew lots of nonsense errors. It
300 // would probably be better to bail out if we saw an @class or @interface
301 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000302 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000303 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000304 SkipUntil(tok::r_brace, tok::at);
305 break;
Mike Stump11289f42009-09-09 15:08:12 +0000306
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000307 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000308 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000309 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000310 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000311 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000312 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000313 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000314 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000315 break;
Mike Stump11289f42009-09-09 15:08:12 +0000316
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000317 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000318 if (!getLang().ObjC2)
319 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
320
Chris Lattner038a3e32008-10-20 05:46:22 +0000321 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000322 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000323 if (Tok.is(tok::l_paren))
Chris Lattner038a3e32008-10-20 05:46:22 +0000324 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000325
John McCallcfefb6d2009-11-03 02:38:08 +0000326 struct ObjCPropertyCallback : FieldCallback {
327 Parser &P;
328 DeclPtrTy IDecl;
329 llvm::SmallVectorImpl<DeclPtrTy> &Props;
330 ObjCDeclSpec &OCDS;
331 SourceLocation AtLoc;
332 tok::ObjCKeywordKind MethodImplKind;
333
334 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
335 llvm::SmallVectorImpl<DeclPtrTy> &Props,
336 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
337 tok::ObjCKeywordKind MethodImplKind) :
338 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
339 MethodImplKind(MethodImplKind) {
340 }
341
342 DeclPtrTy invoke(FieldDeclarator &FD) {
343 if (FD.D.getIdentifier() == 0) {
344 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
345 << FD.D.getSourceRange();
346 return DeclPtrTy();
347 }
348 if (FD.BitfieldSize) {
349 P.Diag(AtLoc, diag::err_objc_property_bitfield)
350 << FD.D.getSourceRange();
351 return DeclPtrTy();
352 }
353
354 // Install the property declarator into interfaceDecl.
355 IdentifierInfo *SelName =
356 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
357
358 Selector GetterSel =
359 P.PP.getSelectorTable().getNullarySelector(SelName);
360 IdentifierInfo *SetterName = OCDS.getSetterName();
361 Selector SetterSel;
362 if (SetterName)
363 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
364 else
365 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
366 P.PP.getSelectorTable(),
367 FD.D.getIdentifier());
368 bool isOverridingProperty = false;
369 DeclPtrTy Property =
370 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
371 GetterSel, SetterSel, IDecl,
372 &isOverridingProperty,
373 MethodImplKind);
374 if (!isOverridingProperty)
375 Props.push_back(Property);
376
377 return Property;
378 }
379 } Callback(*this, interfaceDecl, allProperties,
380 OCDS, AtLoc, MethodImplKind);
381
Chris Lattner038a3e32008-10-20 05:46:22 +0000382 // Parse all the comma separated declarators.
383 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000384 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000385
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000386 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
387 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000388 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000389 }
Steve Naroff99264b42007-08-22 16:35:03 +0000390 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000391
392 // We break out of the big loop in two cases: when we see @end or when we see
393 // EOF. In the former case, eat the @end. In the later case, emit an error.
394 if (Tok.isObjCAtKeyword(tok::objc_end))
395 ConsumeToken(); // the "end" identifier
396 else
397 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000399 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000400 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek09a0d042008-06-06 16:45:15 +0000401 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000402 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000403 allProperties.data(), allProperties.size(),
404 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000405}
406
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000407/// Parse property attribute declarations.
408///
409/// property-attr-decl: '(' property-attrlist ')'
410/// property-attrlist:
411/// property-attribute
412/// property-attrlist ',' property-attribute
413/// property-attribute:
414/// getter '=' identifier
415/// setter '=' identifier ':'
416/// readonly
417/// readwrite
418/// assign
419/// retain
420/// copy
421/// nonatomic
422///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000423void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000424 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000425 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000427 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000428 if (Tok.is(tok::code_completion)) {
429 Actions.CodeCompleteObjCProperty(CurScope, DS);
430 ConsumeToken();
431 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000432 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000433
Chris Lattner76619232008-10-20 07:22:18 +0000434 // If this is not an identifier at all, bail out early.
435 if (II == 0) {
436 MatchRHSPunctuation(tok::r_paren, LHSLoc);
437 return;
438 }
Mike Stump11289f42009-09-09 15:08:12 +0000439
Chris Lattner1db33542008-10-20 07:37:22 +0000440 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000441
Chris Lattner68e48682008-11-20 04:42:34 +0000442 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000443 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000444 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000445 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000446 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000447 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000448 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000449 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000450 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000451 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000452 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000453 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000454 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000455 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000456 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
457 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000458 return;
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattnerbeca7702008-10-20 07:24:39 +0000460 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000461 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000462 SkipUntil(tok::r_paren);
463 return;
464 }
Mike Stump11289f42009-09-09 15:08:12 +0000465
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000466 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000467 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
468 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000469 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000470
Chris Lattner1db33542008-10-20 07:37:22 +0000471 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
472 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000473 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000474 } else {
475 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
476 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000477 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000478 }
Chris Lattner825bca12008-10-20 07:39:53 +0000479 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000480 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000481 SkipUntil(tok::r_paren);
482 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000483 }
Mike Stump11289f42009-09-09 15:08:12 +0000484
Chris Lattner1db33542008-10-20 07:37:22 +0000485 if (Tok.isNot(tok::comma))
486 break;
Mike Stump11289f42009-09-09 15:08:12 +0000487
Chris Lattner1db33542008-10-20 07:37:22 +0000488 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000489 }
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattner1db33542008-10-20 07:37:22 +0000491 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000492}
493
Steve Naroff09bf8152007-09-06 21:24:23 +0000494/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000495/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000496/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000497///
498/// objc-instance-method: '-'
499/// objc-class-method: '+'
500///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000501/// objc-method-attributes: [OBJC2]
502/// __attribute__((deprecated))
503///
Mike Stump11289f42009-09-09 15:08:12 +0000504Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000505 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000506 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000507
Mike Stump11289f42009-09-09 15:08:12 +0000508 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000509 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000510
Chris Lattner83f095c2009-03-28 19:18:32 +0000511 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000512 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000513 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000514 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000515}
516
517/// objc-selector:
518/// identifier
519/// one of
520/// enum struct union if else while do for switch case default
521/// break continue return goto asm sizeof typeof __alignof
522/// unsigned long const short volatile signed restrict _Complex
523/// in out inout bycopy byref oneway int char float double void _Bool
524///
Chris Lattner4f472a32009-04-11 18:13:45 +0000525IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000526 switch (Tok.getKind()) {
527 default:
528 return 0;
529 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000530 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000531 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000532 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000533 case tok::kw_break:
534 case tok::kw_case:
535 case tok::kw_catch:
536 case tok::kw_char:
537 case tok::kw_class:
538 case tok::kw_const:
539 case tok::kw_const_cast:
540 case tok::kw_continue:
541 case tok::kw_default:
542 case tok::kw_delete:
543 case tok::kw_do:
544 case tok::kw_double:
545 case tok::kw_dynamic_cast:
546 case tok::kw_else:
547 case tok::kw_enum:
548 case tok::kw_explicit:
549 case tok::kw_export:
550 case tok::kw_extern:
551 case tok::kw_false:
552 case tok::kw_float:
553 case tok::kw_for:
554 case tok::kw_friend:
555 case tok::kw_goto:
556 case tok::kw_if:
557 case tok::kw_inline:
558 case tok::kw_int:
559 case tok::kw_long:
560 case tok::kw_mutable:
561 case tok::kw_namespace:
562 case tok::kw_new:
563 case tok::kw_operator:
564 case tok::kw_private:
565 case tok::kw_protected:
566 case tok::kw_public:
567 case tok::kw_register:
568 case tok::kw_reinterpret_cast:
569 case tok::kw_restrict:
570 case tok::kw_return:
571 case tok::kw_short:
572 case tok::kw_signed:
573 case tok::kw_sizeof:
574 case tok::kw_static:
575 case tok::kw_static_cast:
576 case tok::kw_struct:
577 case tok::kw_switch:
578 case tok::kw_template:
579 case tok::kw_this:
580 case tok::kw_throw:
581 case tok::kw_true:
582 case tok::kw_try:
583 case tok::kw_typedef:
584 case tok::kw_typeid:
585 case tok::kw_typename:
586 case tok::kw_typeof:
587 case tok::kw_union:
588 case tok::kw_unsigned:
589 case tok::kw_using:
590 case tok::kw_virtual:
591 case tok::kw_void:
592 case tok::kw_volatile:
593 case tok::kw_wchar_t:
594 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000595 case tok::kw__Bool:
596 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000597 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000598 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000599 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000600 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000601 }
Steve Naroff99264b42007-08-22 16:35:03 +0000602}
603
Fariborz Jahanian83615522008-01-02 22:54:34 +0000604/// objc-for-collection-in: 'in'
605///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000606bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000607 // FIXME: May have to do additional look-ahead to only allow for
608 // valid tokens following an 'in'; such as an identifier, unary operators,
609 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000610 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000611 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000612}
613
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000614/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000615/// qualifier list and builds their bitmask representation in the input
616/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000617///
618/// objc-type-qualifiers:
619/// objc-type-qualifier
620/// objc-type-qualifiers objc-type-qualifier
621///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000622void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000623 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000624 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000625 return;
Mike Stump11289f42009-09-09 15:08:12 +0000626
Chris Lattner61511e12007-12-12 06:56:32 +0000627 const IdentifierInfo *II = Tok.getIdentifierInfo();
628 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000629 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000630 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000631
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000632 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000633 switch (i) {
634 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000635 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
636 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
637 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
638 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
639 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
640 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000641 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000642 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000643 ConsumeToken();
644 II = 0;
645 break;
646 }
Mike Stump11289f42009-09-09 15:08:12 +0000647
Chris Lattner61511e12007-12-12 06:56:32 +0000648 // If this wasn't a recognized qualifier, bail out.
649 if (II) return;
650 }
651}
652
653/// objc-type-name:
654/// '(' objc-type-qualifiers[opt] type-name ')'
655/// '(' objc-type-qualifiers[opt] ')'
656///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000657Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000658 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000659
Chris Lattnerb7954432008-10-22 03:52:06 +0000660 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000661 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000662
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000663 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000664 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000665
Chris Lattnerb7954432008-10-22 03:52:06 +0000666 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000667 if (isTypeSpecifierQualifier()) {
668 TypeResult TypeSpec = ParseTypeName();
669 if (!TypeSpec.isInvalid())
670 Ty = TypeSpec.get();
671 }
Mike Stump11289f42009-09-09 15:08:12 +0000672
Steve Naroff90255b42008-10-21 14:15:04 +0000673 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000674 ConsumeParen();
675 else if (Tok.getLocation() == TypeStartLoc) {
676 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000677 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000678 SkipUntil(tok::r_paren);
679 } else {
680 // Otherwise, we found *something*, but didn't get a ')' in the right
681 // place. Emit an error then return what we have as the type.
682 MatchRHSPunctuation(tok::r_paren, LParenLoc);
683 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000684 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000685}
686
687/// objc-method-decl:
688/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000689/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000690/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000691/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000692///
693/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000694/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000695/// objc-keyword-selector objc-keyword-decl
696///
697/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000698/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
699/// objc-selector ':' objc-keyword-attributes[opt] identifier
700/// ':' objc-type-name objc-keyword-attributes[opt] identifier
701/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000702///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000703/// objc-parmlist:
704/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000705///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000706/// objc-parms:
707/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000708///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000709/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000710/// , ...
711///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000712/// objc-keyword-attributes: [OBJC2]
713/// __attribute__((unused))
714///
Chris Lattner83f095c2009-03-28 19:18:32 +0000715Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
716 tok::TokenKind mType,
717 DeclPtrTy IDecl,
718 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000719 ParsingDeclRAIIObject PD(*this);
720
Chris Lattner2ebb1782008-08-23 01:48:03 +0000721 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000722 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000723 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000724 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000725 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000726
Steve Naroff161a92b2007-10-26 20:53:56 +0000727 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000728 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000729
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000730 // An unnamed colon is valid.
731 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000732 Diag(Tok, diag::err_expected_selector_for_method)
733 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000734 // Skip until we get a ; or {}.
735 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000736 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000737 }
Mike Stump11289f42009-09-09 15:08:12 +0000738
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000739 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000740 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000741 // If attributes exist after the method, parse them.
742 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000743 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000744 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattner5700fab2007-10-07 02:00:24 +0000746 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000747 DeclPtrTy Result
748 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000749 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000750 0, CargNames, MethodAttrs,
751 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000752 PD.complete(Result);
753 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000754 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000755
Steve Narofff73590d2007-09-27 14:38:14 +0000756 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000757 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000758
Chris Lattner5700fab2007-10-07 02:00:24 +0000759 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000760 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattner5700fab2007-10-07 02:00:24 +0000762 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000763 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000764 Diag(Tok, diag::err_expected_colon);
765 break;
766 }
767 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000769 ArgInfo.Type = 0;
770 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
771 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
772
Chris Lattner5700fab2007-10-07 02:00:24 +0000773 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000774 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000775 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000776 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000777
Chris Lattner0ef13522007-10-09 17:51:17 +0000778 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000779 Diag(Tok, diag::err_expected_ident); // missing argument name.
780 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000781 }
Mike Stump11289f42009-09-09 15:08:12 +0000782
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000783 ArgInfo.Name = Tok.getIdentifierInfo();
784 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000785 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000786
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000787 ArgInfos.push_back(ArgInfo);
788 KeyIdents.push_back(SelIdent);
789
Chris Lattner5700fab2007-10-07 02:00:24 +0000790 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000791 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000792 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000793 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000794 break;
795 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000796 }
Mike Stump11289f42009-09-09 15:08:12 +0000797
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000798 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000799
Chris Lattner5700fab2007-10-07 02:00:24 +0000800 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000801 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000802 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000803 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000804 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000805 ConsumeToken();
806 break;
807 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000808 DeclSpec DS;
809 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000810 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000811 Declarator ParmDecl(DS, Declarator::PrototypeContext);
812 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000813 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000814 }
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattner5700fab2007-10-07 02:00:24 +0000816 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000817 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000818 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000819 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000820 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000821
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000822 if (KeyIdents.size() == 0)
823 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000824 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
825 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000826 DeclPtrTy Result
827 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000828 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000829 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000830 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000831 PD.complete(Result);
832 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000833}
834
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000835/// objc-protocol-refs:
836/// '<' identifier-list '>'
837///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000838bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000839ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000840 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
841 bool WarnOnDeclarations,
842 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000843 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000844
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000845 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner3bbae002008-07-26 04:03:38 +0000847 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000848
Chris Lattner3bbae002008-07-26 04:03:38 +0000849 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +0000850 if (Tok.is(tok::code_completion)) {
851 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
852 ProtocolIdents.size());
853 ConsumeToken();
854 }
855
Chris Lattner3bbae002008-07-26 04:03:38 +0000856 if (Tok.isNot(tok::identifier)) {
857 Diag(Tok, diag::err_expected_ident);
858 SkipUntil(tok::greater);
859 return true;
860 }
861 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
862 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000863 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000864 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000865
Chris Lattner3bbae002008-07-26 04:03:38 +0000866 if (Tok.isNot(tok::comma))
867 break;
868 ConsumeToken();
869 }
Mike Stump11289f42009-09-09 15:08:12 +0000870
Chris Lattner3bbae002008-07-26 04:03:38 +0000871 // Consume the '>'.
872 if (Tok.isNot(tok::greater)) {
873 Diag(Tok, diag::err_expected_greater);
874 return true;
875 }
Mike Stump11289f42009-09-09 15:08:12 +0000876
Chris Lattner3bbae002008-07-26 04:03:38 +0000877 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000878
Chris Lattner3bbae002008-07-26 04:03:38 +0000879 // Convert the list of protocols identifiers into a list of protocol decls.
880 Actions.FindProtocolDeclaration(WarnOnDeclarations,
881 &ProtocolIdents[0], ProtocolIdents.size(),
882 Protocols);
883 return false;
884}
885
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000886/// objc-class-instance-variables:
887/// '{' objc-instance-variable-decl-list[opt] '}'
888///
889/// objc-instance-variable-decl-list:
890/// objc-visibility-spec
891/// objc-instance-variable-decl ';'
892/// ';'
893/// objc-instance-variable-decl-list objc-visibility-spec
894/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
895/// objc-instance-variable-decl-list ';'
896///
897/// objc-visibility-spec:
898/// @private
899/// @protected
900/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000901/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000902///
903/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000904/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000905///
Chris Lattner83f095c2009-03-28 19:18:32 +0000906void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000907 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000908 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000909 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000910
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000911 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000912
Steve Naroff00433d32007-08-21 21:17:12 +0000913 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000914
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000915 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000916 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000917 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000918 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000919
Steve Naroff00433d32007-08-21 21:17:12 +0000920 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000921 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000922 Diag(Tok, diag::ext_extra_struct_semi)
923 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroff00433d32007-08-21 21:17:12 +0000924 ConsumeToken();
925 continue;
926 }
Mike Stump11289f42009-09-09 15:08:12 +0000927
Steve Naroff00433d32007-08-21 21:17:12 +0000928 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000929 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000930 ConsumeToken(); // eat the @ sign
Steve Naroff7c348172007-08-23 18:16:40 +0000931 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000932 case tok::objc_private:
933 case tok::objc_public:
934 case tok::objc_protected:
935 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000936 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000937 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000938 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000939 default:
940 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000941 continue;
942 }
943 }
Mike Stump11289f42009-09-09 15:08:12 +0000944
John McCallcfefb6d2009-11-03 02:38:08 +0000945 struct ObjCIvarCallback : FieldCallback {
946 Parser &P;
947 DeclPtrTy IDecl;
948 tok::ObjCKeywordKind visibility;
949 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
950
951 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
952 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
953 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
954 }
955
956 DeclPtrTy invoke(FieldDeclarator &FD) {
957 // Install the declarator into the interface decl.
958 DeclPtrTy Field
959 = P.Actions.ActOnIvar(P.CurScope,
960 FD.D.getDeclSpec().getSourceRange().getBegin(),
961 IDecl, FD.D, FD.BitfieldSize, visibility);
962 AllIvarDecls.push_back(Field);
963 return Field;
964 }
965 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
966
Chris Lattnera12405b2008-04-10 06:46:29 +0000967 // Parse all the comma separated declarators.
968 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000969 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000970
Chris Lattner0ef13522007-10-09 17:51:17 +0000971 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000972 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +0000973 } else {
974 Diag(Tok, diag::err_expected_semi_decl_list);
975 // Skip to end of block or statement
976 SkipUntil(tok::r_brace, true, true);
977 }
978 }
Steve Naroff33a1e802007-10-29 21:38:07 +0000979 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +0000980 // Call ActOnFields() even if we don't have any decls. This is useful
981 // for code rewriting tools that need to be aware of the empty list.
982 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +0000983 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +0000984 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +0000985 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000986}
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000987
988/// objc-protocol-declaration:
989/// objc-protocol-definition
990/// objc-protocol-forward-reference
991///
992/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +0000993/// @protocol identifier
994/// objc-protocol-refs[opt]
995/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000996/// @end
997///
998/// objc-protocol-forward-reference:
999/// @protocol identifier-list ';'
1000///
1001/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001002/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001003/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +00001004Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1005 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +00001006 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001007 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1008 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001009
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001010 if (Tok.is(tok::code_completion)) {
1011 Actions.CodeCompleteObjCProtocolDecl(CurScope);
1012 ConsumeToken();
1013 }
1014
Chris Lattner0ef13522007-10-09 17:51:17 +00001015 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001016 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001017 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001018 }
1019 // Save the protocol name, then consume it.
1020 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1021 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001022
Chris Lattner0ef13522007-10-09 17:51:17 +00001023 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001024 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001025 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001026 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001027 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattner0ef13522007-10-09 17:51:17 +00001030 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001031 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1032 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1033
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001034 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001035 while (1) {
1036 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001037 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001038 Diag(Tok, diag::err_expected_ident);
1039 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001040 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001041 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001042 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1043 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001044 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001045
Chris Lattner0ef13522007-10-09 17:51:17 +00001046 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001047 break;
1048 }
1049 // Consume the ';'.
1050 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001051 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001052
Steve Naroff93eb5f12007-10-10 17:32:04 +00001053 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001054 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001055 ProtocolRefs.size(),
1056 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001057 }
Mike Stump11289f42009-09-09 15:08:12 +00001058
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001059 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001060 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001061
Chris Lattner83f095c2009-03-28 19:18:32 +00001062 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001063 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001064 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001065 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1066 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001067 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001068
Chris Lattner83f095c2009-03-28 19:18:32 +00001069 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001070 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001071 ProtocolRefs.data(),
1072 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001073 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001074 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001075 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001076}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001077
1078/// objc-implementation:
1079/// objc-class-implementation-prologue
1080/// objc-category-implementation-prologue
1081///
1082/// objc-class-implementation-prologue:
1083/// @implementation identifier objc-superclass[opt]
1084/// objc-class-instance-variables[opt]
1085///
1086/// objc-category-implementation-prologue:
1087/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001088Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001089 SourceLocation atLoc) {
1090 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1091 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1092 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001093
Douglas Gregor49c22a72009-11-18 16:26:39 +00001094 // Code completion after '@implementation'.
1095 if (Tok.is(tok::code_completion)) {
1096 Actions.CodeCompleteObjCImplementationDecl(CurScope);
1097 ConsumeToken();
1098 }
1099
Chris Lattner0ef13522007-10-09 17:51:17 +00001100 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001101 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001102 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001103 }
1104 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001105 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001106 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001107
1108 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001109 // we have a category implementation.
1110 SourceLocation lparenLoc = ConsumeParen();
1111 SourceLocation categoryLoc, rparenLoc;
1112 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001113
Chris Lattner0ef13522007-10-09 17:51:17 +00001114 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001115 categoryId = Tok.getIdentifierInfo();
1116 categoryLoc = ConsumeToken();
1117 } else {
1118 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001119 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001120 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001121 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001122 Diag(Tok, diag::err_expected_rparen);
1123 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001124 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001125 }
1126 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001127 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001128 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001129 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001130 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001131 PendingObjCImpDecl.push_back(ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001132 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001133 }
1134 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001135 SourceLocation superClassLoc;
1136 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001137 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001138 // We have a super class
1139 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001140 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001141 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001142 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001143 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001144 superClassId = Tok.getIdentifierInfo();
1145 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001146 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001147 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001148 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001149 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001150
Steve Naroff33a1e802007-10-29 21:38:07 +00001151 if (Tok.is(tok::l_brace)) // we have ivars
1152 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001153 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001154 PendingObjCImpDecl.push_back(ObjCImpDecl);
1155
Chris Lattner83f095c2009-03-28 19:18:32 +00001156 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001157}
Steve Naroff33a1e802007-10-29 21:38:07 +00001158
Chris Lattner83f095c2009-03-28 19:18:32 +00001159Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001160 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1161 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001162 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001163 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001164 if (ObjCImpDecl) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001165 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001166 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001167 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001168 }
Fariborz Jahanianc644ee42008-01-10 17:58:07 +00001169 else
1170 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001171 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001172}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001173
Fariborz Jahanianbc02a102009-11-17 17:15:16 +00001174Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001175 if (PendingObjCImpDecl.empty())
1176 return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1177 DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1178 Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
1179 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1180}
1181
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001182/// compatibility-alias-decl:
1183/// @compatibility_alias alias-name class-name ';'
1184///
Chris Lattner83f095c2009-03-28 19:18:32 +00001185Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001186 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1187 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1188 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001189 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001190 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001191 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001192 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001193 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1194 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001195 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001196 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001197 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001198 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001199 IdentifierInfo *classId = Tok.getIdentifierInfo();
1200 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1201 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001202 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001203 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001204 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001205 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1206 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001207}
1208
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001209/// property-synthesis:
1210/// @synthesize property-ivar-list ';'
1211///
1212/// property-ivar-list:
1213/// property-ivar
1214/// property-ivar-list ',' property-ivar
1215///
1216/// property-ivar:
1217/// identifier
1218/// identifier '=' identifier
1219///
Chris Lattner83f095c2009-03-28 19:18:32 +00001220Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001221 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1222 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001223 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattner0ef13522007-10-09 17:51:17 +00001224 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001225 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001226 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001227 }
Mike Stump11289f42009-09-09 15:08:12 +00001228
Chris Lattner0ef13522007-10-09 17:51:17 +00001229 while (Tok.is(tok::identifier)) {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001230 IdentifierInfo *propertyIvar = 0;
1231 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1232 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001233 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001234 // property '=' ivar-name
1235 ConsumeToken(); // consume '='
Chris Lattner0ef13522007-10-09 17:51:17 +00001236 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001237 Diag(Tok, diag::err_expected_ident);
1238 break;
1239 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001240 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001241 ConsumeToken(); // consume ivar-name
1242 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001243 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1244 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001245 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001246 break;
1247 ConsumeToken(); // consume ','
1248 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001249 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001250 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanian95239112009-11-06 21:48:47 +00001251 else
1252 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001253 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001254}
1255
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001256/// property-dynamic:
1257/// @dynamic property-list
1258///
1259/// property-list:
1260/// identifier
1261/// property-list ',' identifier
1262///
Chris Lattner83f095c2009-03-28 19:18:32 +00001263Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001264 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1265 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1266 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattner0ef13522007-10-09 17:51:17 +00001267 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001268 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001269 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001270 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001271 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001272 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1273 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1274 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1275 propertyId, 0);
1276
Chris Lattner0ef13522007-10-09 17:51:17 +00001277 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001278 break;
1279 ConsumeToken(); // consume ','
1280 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001281 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001282 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001283 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001284}
Mike Stump11289f42009-09-09 15:08:12 +00001285
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001286/// objc-throw-statement:
1287/// throw expression[opt];
1288///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001289Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001290 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001291 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001292 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001293 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001294 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001295 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001296 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001297 }
1298 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001299 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001300 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001301}
1302
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001303/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001304/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001305///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001306Parser::OwningStmtResult
1307Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001308 ConsumeToken(); // consume synchronized
1309 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001310 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001311 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001312 }
1313 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001314 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001315 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001316 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001317 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001318 }
1319 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001320 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001321 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001322 }
1323 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001324 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001325 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001326 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001327 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001328 // Enter a scope to hold everything within the compound stmt. Compound
1329 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001330 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001331
Sebastian Redl042ad952008-12-11 19:30:53 +00001332 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001333
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001334 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001335 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001336 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001337 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001338}
1339
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001340/// objc-try-catch-statement:
1341/// @try compound-statement objc-catch-list[opt]
1342/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1343///
1344/// objc-catch-list:
1345/// @catch ( parameter-declaration ) compound-statement
1346/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1347/// catch-parameter-declaration:
1348/// parameter-declaration
1349/// '...' [OBJC2]
1350///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001351Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001352 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001353
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001354 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001355 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001356 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001357 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001358 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001359 OwningStmtResult CatchStmts(Actions);
1360 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001361 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001362 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001363 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001364 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001365 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001366
Chris Lattner0ef13522007-10-09 17:51:17 +00001367 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001368 // At this point, we need to lookahead to determine if this @ is the start
1369 // of an @catch or @finally. We don't want to consume the @ token if this
1370 // is an @try or @encode or something else.
1371 Token AfterAt = GetLookAheadToken(1);
1372 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1373 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1374 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001375
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001376 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001377 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001378 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001379 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001380 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001381 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001382 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001383 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001384 DeclSpec DS;
1385 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001386 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001387 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001388 // we must accept either 'declarator' or 'abstract-declarator' here.
1389 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1390 ParseDeclarator(ParmDecl);
1391
1392 // Inform the actions module about the parameter declarator, so it
1393 // gets added to the current scope.
1394 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001395 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001396 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001397
Steve Naroff65a00892009-04-07 22:56:58 +00001398 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001399
Steve Naroff65a00892009-04-07 22:56:58 +00001400 if (Tok.is(tok::r_paren))
1401 RParenLoc = ConsumeParen();
1402 else // Skip over garbage, until we get to ')'. Eat the ')'.
1403 SkipUntil(tok::r_paren, true, false);
1404
Sebastian Redlc13f2682008-12-09 20:22:58 +00001405 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001406 if (Tok.is(tok::l_brace))
1407 CatchBody = ParseCompoundStatementBody();
1408 else
1409 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001410 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001411 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001412 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001413 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001414 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001415 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001416 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1417 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001418 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001419 }
1420 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001421 } else {
1422 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001423 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001424 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001425
Sebastian Redlc13f2682008-12-09 20:22:58 +00001426 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001427 if (Tok.is(tok::l_brace))
1428 FinallyBody = ParseCompoundStatementBody();
1429 else
1430 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001431 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001432 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001433 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001434 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001435 catch_or_finally_seen = true;
1436 break;
1437 }
1438 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001439 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001440 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001441 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001442 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001443 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1444 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001445}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001446
Steve Naroff09bf8152007-09-06 21:24:23 +00001447/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001448///
Chris Lattner83f095c2009-03-28 19:18:32 +00001449Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1450 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001451
Chris Lattnereae6cb62009-03-05 08:00:35 +00001452 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1453 PP.getSourceManager(),
1454 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001455
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001456 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001457 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001458 if (ObjCImpDecl) {
1459 Diag(Tok, diag::warn_semicolon_before_method_body)
1460 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1461 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001462 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001463 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001464
Steve Naroffbb875722007-11-11 19:54:21 +00001465 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001466 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001467 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001468
Steve Naroffbb875722007-11-11 19:54:21 +00001469 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1470 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001471
Steve Naroffbb875722007-11-11 19:54:21 +00001472 // If we didn't find the '{', bail out.
1473 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001474 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001475 }
Steve Naroffbb875722007-11-11 19:54:21 +00001476 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001477
Steve Naroffbb875722007-11-11 19:54:21 +00001478 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001479 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001480
Steve Naroffbb875722007-11-11 19:54:21 +00001481 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001482 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001483 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001484
1485 OwningStmtResult FnBody(ParseCompoundStatementBody());
1486
Steve Naroffbb875722007-11-11 19:54:21 +00001487 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001488 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001489 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1490 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001491
Steve Naroffb94d7f62009-03-02 22:00:56 +00001492 // TODO: Pass argument information.
1493 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001494
Steve Naroffbb875722007-11-11 19:54:21 +00001495 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001496 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001497
Steve Naroff7b8fa472007-11-13 23:01:27 +00001498 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001499}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001500
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001501Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffe6016792008-02-05 21:27:35 +00001502 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001503 return ParseObjCTryStmt(AtLoc);
Steve Naroffe6016792008-02-05 21:27:35 +00001504 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1505 return ParseObjCThrowStmt(AtLoc);
1506 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1507 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001508 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001509 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001510 // If the expression is invalid, skip ahead to the next semicolon. Not
1511 // doing this opens us up to the possibility of infinite loops if
1512 // ParseExpression does not consume any tokens.
1513 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001514 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001515 }
1516 // Otherwise, eat the semicolon.
1517 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +00001518 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001519}
1520
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001521Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001522 switch (Tok.getKind()) {
Chris Lattnere002fbe2007-12-12 01:04:12 +00001523 case tok::string_literal: // primary-expression: string-literal
1524 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001525 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001526 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001527 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001528 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001529
Chris Lattner197a3012008-08-05 06:19:09 +00001530 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1531 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001532 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001533 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001534 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001535 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001536 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001537 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001538 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001539 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001540 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001541}
1542
Mike Stump11289f42009-09-09 15:08:12 +00001543/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001544/// '[' objc-receiver objc-message-args ']'
1545///
1546/// objc-receiver:
1547/// expression
1548/// class-name
1549/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001550Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001551 assert(Tok.is(tok::l_square) && "'[' expected");
1552 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1553
1554 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001555 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001556 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001557 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1558 SourceLocation NameLoc = ConsumeToken();
1559 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1560 ExprArg(Actions));
1561 }
Chris Lattner8f697062008-01-25 18:59:06 +00001562 }
1563
Sebastian Redl59b5e512008-12-11 21:36:32 +00001564 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001565 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001566 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001567 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001568 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001569
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001570 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001571 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001572}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001573
Chris Lattner8f697062008-01-25 18:59:06 +00001574/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1575/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001576///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001577/// objc-message-args:
1578/// objc-selector
1579/// objc-keywordarg-list
1580///
1581/// objc-keywordarg-list:
1582/// objc-keywordarg
1583/// objc-keywordarg-list objc-keywordarg
1584///
Mike Stump11289f42009-09-09 15:08:12 +00001585/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001586/// selector-name[opt] ':' objc-keywordexpr
1587///
1588/// objc-keywordexpr:
1589/// nonempty-expr-list
1590///
1591/// nonempty-expr-list:
1592/// assignment-expression
1593/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001594///
1595Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001596Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001597 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001598 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001599 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001600 if (Tok.is(tok::code_completion)) {
1601 if (ReceiverName)
Douglas Gregor090dd182009-11-17 23:31:36 +00001602 Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc);
Steve Naroffeae65032009-11-07 02:08:14 +00001603 else
Douglas Gregor090dd182009-11-17 23:31:36 +00001604 Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get());
Steve Naroffeae65032009-11-07 02:08:14 +00001605 ConsumeToken();
1606 }
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001607 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001608 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001609 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001610
Anders Carlsson978f08d2009-02-14 18:21:46 +00001611 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001612
Steve Narofff73590d2007-09-27 14:38:14 +00001613 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001614 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001615
Chris Lattner0ef13522007-10-09 17:51:17 +00001616 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001617 while (1) {
1618 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001619 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001620
Chris Lattner0ef13522007-10-09 17:51:17 +00001621 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001622 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-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 Redlcb6e2c62008-12-13 15:32:12 +00001627 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001628 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001629
Steve Narofff73590d2007-09-27 14:38:14 +00001630 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001631 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001632 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001633 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001634 // We must manually skip to a ']', otherwise the expression skipper will
1635 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1636 // the enclosing expression.
1637 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001638 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001639 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001640
Steve Naroff486760a2007-09-17 20:25:27 +00001641 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001642 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001643
Steve Naroff486760a2007-09-17 20:25:27 +00001644 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001645 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001646 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001647 break;
1648 // We have a selector or a colon, continue parsing.
1649 }
1650 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001651 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001652 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001653 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001654 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001655 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001656 // We must manually skip to a ']', otherwise the expression skipper will
1657 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1658 // the enclosing expression.
1659 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001660 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001661 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001662
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001663 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001664 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001665 }
1666 } else if (!selIdent) {
1667 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001668
Chris Lattner197a3012008-08-05 06:19:09 +00001669 // We must manually skip to a ']', otherwise the expression skipper will
1670 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1671 // the enclosing expression.
1672 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001673 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001674 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001675
Chris Lattner0ef13522007-10-09 17:51:17 +00001676 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001677 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001678 // We must manually skip to a ']', otherwise the expression skipper will
1679 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1680 // the enclosing expression.
1681 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001682 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001683 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001684
Chris Lattner8f697062008-01-25 18:59:06 +00001685 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001686
Steve Naroffe61bfa82007-10-05 18:42:47 +00001687 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001688 if (nKeys == 0)
1689 KeyIdents.push_back(selIdent);
1690 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001691
Chris Lattner5700fab2007-10-07 02:00:24 +00001692 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001693 if (ReceiverName)
1694 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001695 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001696 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001697 KeyExprs.take(), KeyExprs.size()));
1698 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001699 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001700 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001701}
1702
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001703Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001704 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001705 if (Res.isInvalid()) return move(Res);
1706
Chris Lattnere002fbe2007-12-12 01:04:12 +00001707 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1708 // expressions. At this point, we know that the only valid thing that starts
1709 // with '@' is an @"".
1710 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001711 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001712 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001713 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001714
Chris Lattnere002fbe2007-12-12 01:04:12 +00001715 while (Tok.is(tok::at)) {
1716 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001717
Sebastian Redlc13f2682008-12-09 20:22:58 +00001718 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001719 if (!isTokenStringLiteral())
1720 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001721
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001722 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001723 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001724 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001725
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001726 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001727 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001728
1729 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1730 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001731}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001732
1733/// objc-encode-expression:
1734/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001735Parser::OwningExprResult
1736Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001737 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001738
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001739 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001740
Chris Lattner197a3012008-08-05 06:19:09 +00001741 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001742 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1743
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001744 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001745
Douglas Gregor220cac52009-02-18 17:45:20 +00001746 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001747
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001748 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001749
Douglas Gregor220cac52009-02-18 17:45:20 +00001750 if (Ty.isInvalid())
1751 return ExprError();
1752
Mike Stump11289f42009-09-09 15:08:12 +00001753 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001754 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001755}
Anders Carlssone01493d2007-08-23 15:25:28 +00001756
1757/// objc-protocol-expression
1758/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001759Parser::OwningExprResult
1760Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001761 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001762
Chris Lattner197a3012008-08-05 06:19:09 +00001763 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001764 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1765
Anders Carlssone01493d2007-08-23 15:25:28 +00001766 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001767
Chris Lattner197a3012008-08-05 06:19:09 +00001768 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001769 return ExprError(Diag(Tok, diag::err_expected_ident));
1770
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001771 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001772 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001773
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001774 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001775
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001776 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1777 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001778}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001779
1780/// objc-selector-expression
1781/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001782Parser::OwningExprResult
1783Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001784 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001785
Chris Lattner197a3012008-08-05 06:19:09 +00001786 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001787 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1788
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001789 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001790 SourceLocation LParenLoc = ConsumeParen();
1791 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001792 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001793 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1794 return ExprError(Diag(Tok, diag::err_expected_ident));
1795
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001796 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001797 unsigned nColons = 0;
1798 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001799 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001800 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001801 return ExprError(Diag(Tok, diag::err_expected_colon));
1802
Chris Lattner5e530bc2007-12-27 19:57:00 +00001803 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001804 ConsumeToken(); // Eat the ':'.
1805 if (Tok.is(tok::r_paren))
1806 break;
1807 // Check for another keyword selector.
1808 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001809 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001810 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001811 if (!SelIdent && Tok.isNot(tok::colon))
1812 break;
1813 }
Steve Naroff152dd812007-12-05 22:21:29 +00001814 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001815 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001816 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001817 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1818 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001819 }