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