blob: 65d71ae481a94de45aee0a16d866d0d7ff6a97e7 [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;
Mike Stump11289f42009-09-09 15:08:12 +000064
Chris Lattnerda59c2f2006-11-05 02:08:13 +000065 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000067 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000068 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000069 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000070 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000073
Chris Lattner0ef13522007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000075 break;
Mike Stump11289f42009-09-09 15:08:12 +000076
Chris Lattnerda59c2f2006-11-05 02:08:13 +000077 ConsumeToken();
78 }
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattner83f095c2009-03-28 19:18:32 +000082 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +000083
Steve Naroff93eb5f12007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff09bf8152007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000086}
87
Steve Naroff1eb1ad62007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +000094/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +000095/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +000096/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +000097/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000101/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
Chris Lattner83f095c2009-03-28 19:18:32 +0000116Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattner0ef13522007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000124 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000125 }
126 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000129
Chris Lattner0ef13522007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000134
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000142 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000146 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000147 }
148 rparenLoc = ConsumeParen();
Mike Stump11289f42009-09-09 15:08:12 +0000149
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000151 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000152 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000153 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerb1f3c942008-07-26 04:07:02 +0000154 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000155 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
156 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000157 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000158
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000159 if (attrList) // categories don't support attributes.
160 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000161
Jay Foad7d0479f2009-05-21 09:52:38 +0000162 DeclPtrTy CategoryType =
Mike Stump11289f42009-09-09 15:08:12 +0000163 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000164 nameId, nameLoc,
165 categoryId, categoryLoc,
166 ProtocolRefs.data(),
167 ProtocolRefs.size(),
168 EndProtoLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000169
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000170 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000171 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000176
Chris Lattner0ef13522007-10-09 17:51:17 +0000177 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000178 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000181 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000187 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000188 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
189 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000190 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000191 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
192 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000193 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000194
195 DeclPtrTy ClsType =
196 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000197 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000198 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000199 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000200
Chris Lattner0ef13522007-10-09 17:51:17 +0000201 if (Tok.is(tok::l_brace))
Steve Naroff33a1e802007-10-29 21:38:07 +0000202 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000204 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000205 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000206}
207
208/// objc-interface-decl-list:
209/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000211/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000212/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000213/// objc-interface-decl-list declaration
214/// objc-interface-decl-list ';'
215///
Steve Naroff99264b42007-08-22 16:35:03 +0000216/// objc-method-requirement: [OBJC2]
217/// @required
218/// @optional
219///
Chris Lattner83f095c2009-03-28 19:18:32 +0000220void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000221 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000222 llvm::SmallVector<DeclPtrTy, 32> allMethods;
223 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000224 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000225 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000226
Chris Lattnerda9fb152008-10-20 06:10:06 +0000227 SourceLocation AtEndLoc;
228
Steve Naroff99264b42007-08-22 16:35:03 +0000229 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000230 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000231 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000232 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000233 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000234 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000235 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
236 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000237 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
238 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000239 continue;
240 }
Mike Stump11289f42009-09-09 15:08:12 +0000241
Chris Lattner038a3e32008-10-20 05:46:22 +0000242 // Ignore excess semicolons.
243 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000244 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000245 continue;
246 }
Mike Stump11289f42009-09-09 15:08:12 +0000247
Chris Lattnerda9fb152008-10-20 06:10:06 +0000248 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000249 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000250 break;
Mike Stump11289f42009-09-09 15:08:12 +0000251
Chris Lattner038a3e32008-10-20 05:46:22 +0000252 // If we don't have an @ directive, parse it as a function definition.
253 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000254 // The code below does not consume '}'s because it is afraid of eating the
255 // end of a namespace. Because of the way this code is structured, an
256 // erroneous r_brace would cause an infinite loop if not handled here.
257 if (Tok.is(tok::r_brace))
258 break;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Steve Narofff1bc45b2007-08-22 18:35:33 +0000260 // FIXME: as the name implies, this rule allows function definitions.
261 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000262 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattner038a3e32008-10-20 05:46:22 +0000263 continue;
264 }
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattner038a3e32008-10-20 05:46:22 +0000266 // Otherwise, we have an @ directive, eat the @.
267 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000268 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000269
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000270 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattner038a3e32008-10-20 05:46:22 +0000271 AtEndLoc = AtLoc;
272 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattnerda9fb152008-10-20 06:10:06 +0000275 // Eat the identifier.
276 ConsumeToken();
277
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000278 switch (DirectiveKind) {
279 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000280 // FIXME: If someone forgets an @end on a protocol, this loop will
281 // continue to eat up tons of stuff and spew lots of nonsense errors. It
282 // would probably be better to bail out if we saw an @class or @interface
283 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000284 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000285 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000286 SkipUntil(tok::r_brace, tok::at);
287 break;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000289 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000290 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000291 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000292 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000293 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000294 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000295 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000296 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000297 break;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000299 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000300 if (!getLang().ObjC2)
301 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
302
Chris Lattner038a3e32008-10-20 05:46:22 +0000303 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000304 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000305 if (Tok.is(tok::l_paren))
Chris Lattner038a3e32008-10-20 05:46:22 +0000306 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000307
John McCallcfefb6d2009-11-03 02:38:08 +0000308 struct ObjCPropertyCallback : FieldCallback {
309 Parser &P;
310 DeclPtrTy IDecl;
311 llvm::SmallVectorImpl<DeclPtrTy> &Props;
312 ObjCDeclSpec &OCDS;
313 SourceLocation AtLoc;
314 tok::ObjCKeywordKind MethodImplKind;
315
316 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
317 llvm::SmallVectorImpl<DeclPtrTy> &Props,
318 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
319 tok::ObjCKeywordKind MethodImplKind) :
320 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
321 MethodImplKind(MethodImplKind) {
322 }
323
324 DeclPtrTy invoke(FieldDeclarator &FD) {
325 if (FD.D.getIdentifier() == 0) {
326 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
327 << FD.D.getSourceRange();
328 return DeclPtrTy();
329 }
330 if (FD.BitfieldSize) {
331 P.Diag(AtLoc, diag::err_objc_property_bitfield)
332 << FD.D.getSourceRange();
333 return DeclPtrTy();
334 }
335
336 // Install the property declarator into interfaceDecl.
337 IdentifierInfo *SelName =
338 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
339
340 Selector GetterSel =
341 P.PP.getSelectorTable().getNullarySelector(SelName);
342 IdentifierInfo *SetterName = OCDS.getSetterName();
343 Selector SetterSel;
344 if (SetterName)
345 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
346 else
347 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
348 P.PP.getSelectorTable(),
349 FD.D.getIdentifier());
350 bool isOverridingProperty = false;
351 DeclPtrTy Property =
352 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
353 GetterSel, SetterSel, IDecl,
354 &isOverridingProperty,
355 MethodImplKind);
356 if (!isOverridingProperty)
357 Props.push_back(Property);
358
359 return Property;
360 }
361 } Callback(*this, interfaceDecl, allProperties,
362 OCDS, AtLoc, MethodImplKind);
363
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 // Parse all the comma separated declarators.
365 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000366 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000368 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
369 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000370 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000371 }
Steve Naroff99264b42007-08-22 16:35:03 +0000372 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000373
374 // We break out of the big loop in two cases: when we see @end or when we see
375 // EOF. In the former case, eat the @end. In the later case, emit an error.
376 if (Tok.isObjCAtKeyword(tok::objc_end))
377 ConsumeToken(); // the "end" identifier
378 else
379 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000380
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000381 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000382 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek09a0d042008-06-06 16:45:15 +0000383 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000384 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000385 allProperties.data(), allProperties.size(),
386 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000387}
388
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000389/// Parse property attribute declarations.
390///
391/// property-attr-decl: '(' property-attrlist ')'
392/// property-attrlist:
393/// property-attribute
394/// property-attrlist ',' property-attribute
395/// property-attribute:
396/// getter '=' identifier
397/// setter '=' identifier ':'
398/// readonly
399/// readwrite
400/// assign
401/// retain
402/// copy
403/// nonatomic
404///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000405void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000406 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000407 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000409 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000410 if (Tok.is(tok::code_completion)) {
411 Actions.CodeCompleteObjCProperty(CurScope, DS);
412 ConsumeToken();
413 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000414 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattner76619232008-10-20 07:22:18 +0000416 // If this is not an identifier at all, bail out early.
417 if (II == 0) {
418 MatchRHSPunctuation(tok::r_paren, LHSLoc);
419 return;
420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattner1db33542008-10-20 07:37:22 +0000422 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattner68e48682008-11-20 04:42:34 +0000424 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000425 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000426 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000427 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000428 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000429 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000430 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000431 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000432 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000433 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000434 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000435 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000436 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000437 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000438 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
439 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000440 return;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Chris Lattnerbeca7702008-10-20 07:24:39 +0000442 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000443 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000444 SkipUntil(tok::r_paren);
445 return;
446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000448 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000449 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
450 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000451 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattner1db33542008-10-20 07:37:22 +0000453 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
454 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000455 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000456 } else {
457 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
458 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000459 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000460 }
Chris Lattner825bca12008-10-20 07:39:53 +0000461 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000462 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000463 SkipUntil(tok::r_paren);
464 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Chris Lattner1db33542008-10-20 07:37:22 +0000467 if (Tok.isNot(tok::comma))
468 break;
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chris Lattner1db33542008-10-20 07:37:22 +0000470 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000471 }
Mike Stump11289f42009-09-09 15:08:12 +0000472
Chris Lattner1db33542008-10-20 07:37:22 +0000473 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000474}
475
Steve Naroff09bf8152007-09-06 21:24:23 +0000476/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000477/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000478/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000479///
480/// objc-instance-method: '-'
481/// objc-class-method: '+'
482///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000483/// objc-method-attributes: [OBJC2]
484/// __attribute__((deprecated))
485///
Mike Stump11289f42009-09-09 15:08:12 +0000486Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000487 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000488 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000489
Mike Stump11289f42009-09-09 15:08:12 +0000490 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000491 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattner83f095c2009-03-28 19:18:32 +0000493 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000494 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000495 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000496 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000497}
498
499/// objc-selector:
500/// identifier
501/// one of
502/// enum struct union if else while do for switch case default
503/// break continue return goto asm sizeof typeof __alignof
504/// unsigned long const short volatile signed restrict _Complex
505/// in out inout bycopy byref oneway int char float double void _Bool
506///
Chris Lattner4f472a32009-04-11 18:13:45 +0000507IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000508 switch (Tok.getKind()) {
509 default:
510 return 0;
511 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000512 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000513 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000514 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000515 case tok::kw_break:
516 case tok::kw_case:
517 case tok::kw_catch:
518 case tok::kw_char:
519 case tok::kw_class:
520 case tok::kw_const:
521 case tok::kw_const_cast:
522 case tok::kw_continue:
523 case tok::kw_default:
524 case tok::kw_delete:
525 case tok::kw_do:
526 case tok::kw_double:
527 case tok::kw_dynamic_cast:
528 case tok::kw_else:
529 case tok::kw_enum:
530 case tok::kw_explicit:
531 case tok::kw_export:
532 case tok::kw_extern:
533 case tok::kw_false:
534 case tok::kw_float:
535 case tok::kw_for:
536 case tok::kw_friend:
537 case tok::kw_goto:
538 case tok::kw_if:
539 case tok::kw_inline:
540 case tok::kw_int:
541 case tok::kw_long:
542 case tok::kw_mutable:
543 case tok::kw_namespace:
544 case tok::kw_new:
545 case tok::kw_operator:
546 case tok::kw_private:
547 case tok::kw_protected:
548 case tok::kw_public:
549 case tok::kw_register:
550 case tok::kw_reinterpret_cast:
551 case tok::kw_restrict:
552 case tok::kw_return:
553 case tok::kw_short:
554 case tok::kw_signed:
555 case tok::kw_sizeof:
556 case tok::kw_static:
557 case tok::kw_static_cast:
558 case tok::kw_struct:
559 case tok::kw_switch:
560 case tok::kw_template:
561 case tok::kw_this:
562 case tok::kw_throw:
563 case tok::kw_true:
564 case tok::kw_try:
565 case tok::kw_typedef:
566 case tok::kw_typeid:
567 case tok::kw_typename:
568 case tok::kw_typeof:
569 case tok::kw_union:
570 case tok::kw_unsigned:
571 case tok::kw_using:
572 case tok::kw_virtual:
573 case tok::kw_void:
574 case tok::kw_volatile:
575 case tok::kw_wchar_t:
576 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000577 case tok::kw__Bool:
578 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000579 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000580 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000581 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000582 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000583 }
Steve Naroff99264b42007-08-22 16:35:03 +0000584}
585
Fariborz Jahanian83615522008-01-02 22:54:34 +0000586/// objc-for-collection-in: 'in'
587///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000588bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000589 // FIXME: May have to do additional look-ahead to only allow for
590 // valid tokens following an 'in'; such as an identifier, unary operators,
591 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000592 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000593 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000594}
595
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000596/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000597/// qualifier list and builds their bitmask representation in the input
598/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000599///
600/// objc-type-qualifiers:
601/// objc-type-qualifier
602/// objc-type-qualifiers objc-type-qualifier
603///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000604void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000605 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000606 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000607 return;
Mike Stump11289f42009-09-09 15:08:12 +0000608
Chris Lattner61511e12007-12-12 06:56:32 +0000609 const IdentifierInfo *II = Tok.getIdentifierInfo();
610 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000611 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000612 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000613
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000614 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000615 switch (i) {
616 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000617 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
618 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
619 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
620 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
621 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
622 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000623 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000624 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000625 ConsumeToken();
626 II = 0;
627 break;
628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
Chris Lattner61511e12007-12-12 06:56:32 +0000630 // If this wasn't a recognized qualifier, bail out.
631 if (II) return;
632 }
633}
634
635/// objc-type-name:
636/// '(' objc-type-qualifiers[opt] type-name ')'
637/// '(' objc-type-qualifiers[opt] ')'
638///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000639Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000640 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattnerb7954432008-10-22 03:52:06 +0000642 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000643 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000644
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000645 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000646 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000647
Chris Lattnerb7954432008-10-22 03:52:06 +0000648 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000649 if (isTypeSpecifierQualifier()) {
650 TypeResult TypeSpec = ParseTypeName();
651 if (!TypeSpec.isInvalid())
652 Ty = TypeSpec.get();
653 }
Mike Stump11289f42009-09-09 15:08:12 +0000654
Steve Naroff90255b42008-10-21 14:15:04 +0000655 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000656 ConsumeParen();
657 else if (Tok.getLocation() == TypeStartLoc) {
658 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000659 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000660 SkipUntil(tok::r_paren);
661 } else {
662 // Otherwise, we found *something*, but didn't get a ')' in the right
663 // place. Emit an error then return what we have as the type.
664 MatchRHSPunctuation(tok::r_paren, LParenLoc);
665 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000666 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000667}
668
669/// objc-method-decl:
670/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000671/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000672/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000673/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000674///
675/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000676/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000677/// objc-keyword-selector objc-keyword-decl
678///
679/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000680/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
681/// objc-selector ':' objc-keyword-attributes[opt] identifier
682/// ':' objc-type-name objc-keyword-attributes[opt] identifier
683/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000684///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000685/// objc-parmlist:
686/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000687///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000688/// objc-parms:
689/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000690///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000691/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000692/// , ...
693///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000694/// objc-keyword-attributes: [OBJC2]
695/// __attribute__((unused))
696///
Chris Lattner83f095c2009-03-28 19:18:32 +0000697Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
698 tok::TokenKind mType,
699 DeclPtrTy IDecl,
700 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner2ebb1782008-08-23 01:48:03 +0000701 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000702 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000703 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000704 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000705 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000706
Steve Naroff161a92b2007-10-26 20:53:56 +0000707 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000708 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000709
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000710 // An unnamed colon is valid.
711 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000712 Diag(Tok, diag::err_expected_selector_for_method)
713 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000714 // Skip until we get a ; or {}.
715 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000716 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000717 }
Mike Stump11289f42009-09-09 15:08:12 +0000718
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000719 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000720 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000721 // If attributes exist after the method, parse them.
722 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000723 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000724 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000725
Chris Lattner5700fab2007-10-07 02:00:24 +0000726 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff161a92b2007-10-26 20:53:56 +0000727 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000728 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000729 0, CargNames, MethodAttrs,
730 MethodImplKind);
Chris Lattner5700fab2007-10-07 02:00:24 +0000731 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000732
Steve Narofff73590d2007-09-27 14:38:14 +0000733 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000734 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000735
Chris Lattner5700fab2007-10-07 02:00:24 +0000736 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000737 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000738
Chris Lattner5700fab2007-10-07 02:00:24 +0000739 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000740 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000741 Diag(Tok, diag::err_expected_colon);
742 break;
743 }
744 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000746 ArgInfo.Type = 0;
747 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
748 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
749
Chris Lattner5700fab2007-10-07 02:00:24 +0000750 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000751 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000752 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000753 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000754
Chris Lattner0ef13522007-10-09 17:51:17 +0000755 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000756 Diag(Tok, diag::err_expected_ident); // missing argument name.
757 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000758 }
Mike Stump11289f42009-09-09 15:08:12 +0000759
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000760 ArgInfo.Name = Tok.getIdentifierInfo();
761 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000762 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000763
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000764 ArgInfos.push_back(ArgInfo);
765 KeyIdents.push_back(SelIdent);
766
Chris Lattner5700fab2007-10-07 02:00:24 +0000767 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000768 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000769 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000770 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000771 break;
772 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000773 }
Mike Stump11289f42009-09-09 15:08:12 +0000774
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000775 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000776
Chris Lattner5700fab2007-10-07 02:00:24 +0000777 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000778 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000779 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000780 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000781 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000782 ConsumeToken();
783 break;
784 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000785 DeclSpec DS;
786 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000787 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000788 Declarator ParmDecl(DS, Declarator::PrototypeContext);
789 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000790 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000791 }
Mike Stump11289f42009-09-09 15:08:12 +0000792
Chris Lattner5700fab2007-10-07 02:00:24 +0000793 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000794 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000795 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000796 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000797 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000798
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000799 if (KeyIdents.size() == 0)
800 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000801 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
802 &KeyIdents[0]);
Steve Naroff161a92b2007-10-26 20:53:56 +0000803 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000804 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000805 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000806 MethodImplKind, isVariadic);
Steve Naroff99264b42007-08-22 16:35:03 +0000807}
808
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000809/// objc-protocol-refs:
810/// '<' identifier-list '>'
811///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000812bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000813ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000814 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
815 bool WarnOnDeclarations,
816 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000817 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000818
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000819 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000820
Chris Lattner3bbae002008-07-26 04:03:38 +0000821 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattner3bbae002008-07-26 04:03:38 +0000823 while (1) {
824 if (Tok.isNot(tok::identifier)) {
825 Diag(Tok, diag::err_expected_ident);
826 SkipUntil(tok::greater);
827 return true;
828 }
829 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
830 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000831 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000832 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000833
Chris Lattner3bbae002008-07-26 04:03:38 +0000834 if (Tok.isNot(tok::comma))
835 break;
836 ConsumeToken();
837 }
Mike Stump11289f42009-09-09 15:08:12 +0000838
Chris Lattner3bbae002008-07-26 04:03:38 +0000839 // Consume the '>'.
840 if (Tok.isNot(tok::greater)) {
841 Diag(Tok, diag::err_expected_greater);
842 return true;
843 }
Mike Stump11289f42009-09-09 15:08:12 +0000844
Chris Lattner3bbae002008-07-26 04:03:38 +0000845 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner3bbae002008-07-26 04:03:38 +0000847 // Convert the list of protocols identifiers into a list of protocol decls.
848 Actions.FindProtocolDeclaration(WarnOnDeclarations,
849 &ProtocolIdents[0], ProtocolIdents.size(),
850 Protocols);
851 return false;
852}
853
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000854/// objc-class-instance-variables:
855/// '{' objc-instance-variable-decl-list[opt] '}'
856///
857/// objc-instance-variable-decl-list:
858/// objc-visibility-spec
859/// objc-instance-variable-decl ';'
860/// ';'
861/// objc-instance-variable-decl-list objc-visibility-spec
862/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
863/// objc-instance-variable-decl-list ';'
864///
865/// objc-visibility-spec:
866/// @private
867/// @protected
868/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000869/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000870///
871/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000872/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000873///
Chris Lattner83f095c2009-03-28 19:18:32 +0000874void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000875 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000876 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000877 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000878
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000879 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000880
Steve Naroff00433d32007-08-21 21:17:12 +0000881 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000882
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000883 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000884 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000885 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000886 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000887
Steve Naroff00433d32007-08-21 21:17:12 +0000888 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000889 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000890 Diag(Tok, diag::ext_extra_struct_semi);
891 ConsumeToken();
892 continue;
893 }
Mike Stump11289f42009-09-09 15:08:12 +0000894
Steve Naroff00433d32007-08-21 21:17:12 +0000895 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000896 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000897 ConsumeToken(); // eat the @ sign
Steve Naroff7c348172007-08-23 18:16:40 +0000898 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000899 case tok::objc_private:
900 case tok::objc_public:
901 case tok::objc_protected:
902 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000903 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000904 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000905 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000906 default:
907 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000908 continue;
909 }
910 }
Mike Stump11289f42009-09-09 15:08:12 +0000911
John McCallcfefb6d2009-11-03 02:38:08 +0000912 struct ObjCIvarCallback : FieldCallback {
913 Parser &P;
914 DeclPtrTy IDecl;
915 tok::ObjCKeywordKind visibility;
916 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
917
918 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
919 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
920 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
921 }
922
923 DeclPtrTy invoke(FieldDeclarator &FD) {
924 // Install the declarator into the interface decl.
925 DeclPtrTy Field
926 = P.Actions.ActOnIvar(P.CurScope,
927 FD.D.getDeclSpec().getSourceRange().getBegin(),
928 IDecl, FD.D, FD.BitfieldSize, visibility);
929 AllIvarDecls.push_back(Field);
930 return Field;
931 }
932 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
933
Chris Lattnera12405b2008-04-10 06:46:29 +0000934 // Parse all the comma separated declarators.
935 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000936 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000937
Chris Lattner0ef13522007-10-09 17:51:17 +0000938 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000939 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +0000940 } else {
941 Diag(Tok, diag::err_expected_semi_decl_list);
942 // Skip to end of block or statement
943 SkipUntil(tok::r_brace, true, true);
944 }
945 }
Steve Naroff33a1e802007-10-29 21:38:07 +0000946 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +0000947 // Call ActOnFields() even if we don't have any decls. This is useful
948 // for code rewriting tools that need to be aware of the empty list.
949 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +0000950 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +0000951 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +0000952 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000953}
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000954
955/// objc-protocol-declaration:
956/// objc-protocol-definition
957/// objc-protocol-forward-reference
958///
959/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +0000960/// @protocol identifier
961/// objc-protocol-refs[opt]
962/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000963/// @end
964///
965/// objc-protocol-forward-reference:
966/// @protocol identifier-list ';'
967///
968/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +0000969/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000970/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +0000971Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
972 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000973 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000974 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
975 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000976
Chris Lattner0ef13522007-10-09 17:51:17 +0000977 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000978 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000979 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000980 }
981 // Save the protocol name, then consume it.
982 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
983 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattner0ef13522007-10-09 17:51:17 +0000985 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +0000986 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000987 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000988 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000989 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000990 }
Mike Stump11289f42009-09-09 15:08:12 +0000991
Chris Lattner0ef13522007-10-09 17:51:17 +0000992 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +0000993 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
994 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
995
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000996 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000997 while (1) {
998 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +0000999 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001000 Diag(Tok, diag::err_expected_ident);
1001 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001002 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001003 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001004 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1005 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001006 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001007
Chris Lattner0ef13522007-10-09 17:51:17 +00001008 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001009 break;
1010 }
1011 // Consume the ';'.
1012 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001013 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001014
Steve Naroff93eb5f12007-10-10 17:32:04 +00001015 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001016 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001017 ProtocolRefs.size(),
1018 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001019 }
Mike Stump11289f42009-09-09 15:08:12 +00001020
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001021 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001022 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001023
Chris Lattner83f095c2009-03-28 19:18:32 +00001024 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001025 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001026 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001027 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1028 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001029 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001030
Chris Lattner83f095c2009-03-28 19:18:32 +00001031 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001032 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001033 ProtocolRefs.data(),
1034 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001035 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001036 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001037 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001038}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001039
1040/// objc-implementation:
1041/// objc-class-implementation-prologue
1042/// objc-category-implementation-prologue
1043///
1044/// objc-class-implementation-prologue:
1045/// @implementation identifier objc-superclass[opt]
1046/// objc-class-instance-variables[opt]
1047///
1048/// objc-category-implementation-prologue:
1049/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001050Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001051 SourceLocation atLoc) {
1052 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1053 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1054 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattner0ef13522007-10-09 17:51:17 +00001056 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001057 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001058 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001059 }
1060 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001061 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001062 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001063
1064 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001065 // we have a category implementation.
1066 SourceLocation lparenLoc = ConsumeParen();
1067 SourceLocation categoryLoc, rparenLoc;
1068 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001069
Chris Lattner0ef13522007-10-09 17:51:17 +00001070 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001071 categoryId = Tok.getIdentifierInfo();
1072 categoryLoc = ConsumeToken();
1073 } else {
1074 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001075 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001076 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001077 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001078 Diag(Tok, diag::err_expected_rparen);
1079 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001080 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001081 }
1082 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001083 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001084 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001085 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001086 ObjCImpDecl = ImplCatType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001087 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001088 }
1089 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001090 SourceLocation superClassLoc;
1091 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001092 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001093 // We have a super class
1094 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001095 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001096 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001097 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001098 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001099 superClassId = Tok.getIdentifierInfo();
1100 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001101 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001102 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001103 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001104 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001105
Steve Naroff33a1e802007-10-29 21:38:07 +00001106 if (Tok.is(tok::l_brace)) // we have ivars
1107 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001108 ObjCImpDecl = ImplClsType;
Mike Stump11289f42009-09-09 15:08:12 +00001109
Chris Lattner83f095c2009-03-28 19:18:32 +00001110 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001111}
Steve Naroff33a1e802007-10-29 21:38:07 +00001112
Chris Lattner83f095c2009-03-28 19:18:32 +00001113Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001114 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1115 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001116 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001117 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001118 if (ObjCImpDecl) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001119 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001120 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001121 }
Fariborz Jahanianc644ee42008-01-10 17:58:07 +00001122 else
1123 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001124 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001125}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001126
1127/// compatibility-alias-decl:
1128/// @compatibility_alias alias-name class-name ';'
1129///
Chris Lattner83f095c2009-03-28 19:18:32 +00001130Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001131 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1132 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1133 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001134 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001135 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001136 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001137 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001138 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1139 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001140 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001141 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001142 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001143 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001144 IdentifierInfo *classId = Tok.getIdentifierInfo();
1145 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1146 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001147 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001148 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001149 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001150 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1151 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001152}
1153
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001154/// property-synthesis:
1155/// @synthesize property-ivar-list ';'
1156///
1157/// property-ivar-list:
1158/// property-ivar
1159/// property-ivar-list ',' property-ivar
1160///
1161/// property-ivar:
1162/// identifier
1163/// identifier '=' identifier
1164///
Chris Lattner83f095c2009-03-28 19:18:32 +00001165Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001166 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1167 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001168 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattner0ef13522007-10-09 17:51:17 +00001169 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001170 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001171 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001172 }
Mike Stump11289f42009-09-09 15:08:12 +00001173
Chris Lattner0ef13522007-10-09 17:51:17 +00001174 while (Tok.is(tok::identifier)) {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001175 IdentifierInfo *propertyIvar = 0;
1176 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1177 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001178 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001179 // property '=' ivar-name
1180 ConsumeToken(); // consume '='
Chris Lattner0ef13522007-10-09 17:51:17 +00001181 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001182 Diag(Tok, diag::err_expected_ident);
1183 break;
1184 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001185 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001186 ConsumeToken(); // consume ivar-name
1187 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001188 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1189 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001190 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001191 break;
1192 ConsumeToken(); // consume ','
1193 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001194 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001195 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattner83f095c2009-03-28 19:18:32 +00001196 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001197}
1198
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001199/// property-dynamic:
1200/// @dynamic property-list
1201///
1202/// property-list:
1203/// identifier
1204/// property-list ',' identifier
1205///
Chris Lattner83f095c2009-03-28 19:18:32 +00001206Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001207 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1208 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1209 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattner0ef13522007-10-09 17:51:17 +00001210 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001211 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001212 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001213 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001214 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001215 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1216 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1217 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1218 propertyId, 0);
1219
Chris Lattner0ef13522007-10-09 17:51:17 +00001220 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001221 break;
1222 ConsumeToken(); // consume ','
1223 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001224 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001225 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001226 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001227}
Mike Stump11289f42009-09-09 15:08:12 +00001228
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001229/// objc-throw-statement:
1230/// throw expression[opt];
1231///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001232Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001233 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001234 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001235 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001236 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001237 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001238 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001239 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001240 }
1241 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001242 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001243 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001244}
1245
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001246/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001247/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001248///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001249Parser::OwningStmtResult
1250Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001251 ConsumeToken(); // consume synchronized
1252 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001253 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001254 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001255 }
1256 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001257 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001258 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001259 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001260 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001261 }
1262 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001263 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001264 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001265 }
1266 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001267 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001268 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001269 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001270 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001271 // Enter a scope to hold everything within the compound stmt. Compound
1272 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001273 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001274
Sebastian Redl042ad952008-12-11 19:30:53 +00001275 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001276
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001277 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001278 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001279 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001280 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001281}
1282
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001283/// objc-try-catch-statement:
1284/// @try compound-statement objc-catch-list[opt]
1285/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1286///
1287/// objc-catch-list:
1288/// @catch ( parameter-declaration ) compound-statement
1289/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1290/// catch-parameter-declaration:
1291/// parameter-declaration
1292/// '...' [OBJC2]
1293///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001294Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001295 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001296
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001297 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001298 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001299 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001300 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001301 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001302 OwningStmtResult CatchStmts(Actions);
1303 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001304 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001305 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001306 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001307 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001308 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001309
Chris Lattner0ef13522007-10-09 17:51:17 +00001310 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001311 // At this point, we need to lookahead to determine if this @ is the start
1312 // of an @catch or @finally. We don't want to consume the @ token if this
1313 // is an @try or @encode or something else.
1314 Token AfterAt = GetLookAheadToken(1);
1315 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1316 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1317 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001318
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001319 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001320 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001321 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001322 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001323 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001324 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001325 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001326 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001327 DeclSpec DS;
1328 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001329 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001330 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001331 // we must accept either 'declarator' or 'abstract-declarator' here.
1332 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1333 ParseDeclarator(ParmDecl);
1334
1335 // Inform the actions module about the parameter declarator, so it
1336 // gets added to the current scope.
1337 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001338 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001339 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001340
Steve Naroff65a00892009-04-07 22:56:58 +00001341 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001342
Steve Naroff65a00892009-04-07 22:56:58 +00001343 if (Tok.is(tok::r_paren))
1344 RParenLoc = ConsumeParen();
1345 else // Skip over garbage, until we get to ')'. Eat the ')'.
1346 SkipUntil(tok::r_paren, true, false);
1347
Sebastian Redlc13f2682008-12-09 20:22:58 +00001348 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001349 if (Tok.is(tok::l_brace))
1350 CatchBody = ParseCompoundStatementBody();
1351 else
1352 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001353 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001354 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001355 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001356 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001357 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001358 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001359 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1360 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001361 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001362 }
1363 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001364 } else {
1365 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001366 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001367 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001368
Sebastian Redlc13f2682008-12-09 20:22:58 +00001369 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001370 if (Tok.is(tok::l_brace))
1371 FinallyBody = ParseCompoundStatementBody();
1372 else
1373 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001374 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001375 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001376 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001377 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001378 catch_or_finally_seen = true;
1379 break;
1380 }
1381 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001382 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001383 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001384 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001385 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001386 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1387 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001388}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001389
Steve Naroff09bf8152007-09-06 21:24:23 +00001390/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001391///
Chris Lattner83f095c2009-03-28 19:18:32 +00001392Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1393 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001394
Chris Lattnereae6cb62009-03-05 08:00:35 +00001395 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1396 PP.getSourceManager(),
1397 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001398
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001399 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001400 if (Tok.is(tok::semi)) {
1401 if (ObjCImpDecl)
1402 Diag(Tok, diag::warn_semicolon_before_method_nody);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001403 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001404 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001405
Steve Naroffbb875722007-11-11 19:54:21 +00001406 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001407 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001408 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001409
Steve Naroffbb875722007-11-11 19:54:21 +00001410 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1411 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001412
Steve Naroffbb875722007-11-11 19:54:21 +00001413 // If we didn't find the '{', bail out.
1414 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001415 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001416 }
Steve Naroffbb875722007-11-11 19:54:21 +00001417 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001418
Steve Naroffbb875722007-11-11 19:54:21 +00001419 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001420 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001421
Steve Naroffbb875722007-11-11 19:54:21 +00001422 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001423 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001424 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001425
1426 OwningStmtResult FnBody(ParseCompoundStatementBody());
1427
Steve Naroffbb875722007-11-11 19:54:21 +00001428 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001429 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001430 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1431 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001432
Steve Naroffb94d7f62009-03-02 22:00:56 +00001433 // TODO: Pass argument information.
1434 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001435
Steve Naroffbb875722007-11-11 19:54:21 +00001436 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001437 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001438
Steve Naroff7b8fa472007-11-13 23:01:27 +00001439 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001440}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001441
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001442Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffe6016792008-02-05 21:27:35 +00001443 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001444 return ParseObjCTryStmt(AtLoc);
Steve Naroffe6016792008-02-05 21:27:35 +00001445 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1446 return ParseObjCThrowStmt(AtLoc);
1447 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1448 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001449 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001450 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001451 // If the expression is invalid, skip ahead to the next semicolon. Not
1452 // doing this opens us up to the possibility of infinite loops if
1453 // ParseExpression does not consume any tokens.
1454 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001455 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001456 }
1457 // Otherwise, eat the semicolon.
1458 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +00001459 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001460}
1461
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001462Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001463 switch (Tok.getKind()) {
Chris Lattnere002fbe2007-12-12 01:04:12 +00001464 case tok::string_literal: // primary-expression: string-literal
1465 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001466 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001467 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001468 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001469 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001470
Chris Lattner197a3012008-08-05 06:19:09 +00001471 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1472 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001473 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001474 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001475 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001476 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001477 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001478 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001479 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001480 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001481 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001482}
1483
Mike Stump11289f42009-09-09 15:08:12 +00001484/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001485/// '[' objc-receiver objc-message-args ']'
1486///
1487/// objc-receiver:
1488/// expression
1489/// class-name
1490/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001491Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001492 assert(Tok.is(tok::l_square) && "'[' expected");
1493 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1494
1495 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001496 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001497 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001498 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1499 SourceLocation NameLoc = ConsumeToken();
1500 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1501 ExprArg(Actions));
1502 }
Chris Lattner8f697062008-01-25 18:59:06 +00001503 }
1504
Sebastian Redl59b5e512008-12-11 21:36:32 +00001505 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001506 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001507 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001508 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001509 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001510
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001511 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001512 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001513}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001514
Chris Lattner8f697062008-01-25 18:59:06 +00001515/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1516/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001517///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001518/// objc-message-args:
1519/// objc-selector
1520/// objc-keywordarg-list
1521///
1522/// objc-keywordarg-list:
1523/// objc-keywordarg
1524/// objc-keywordarg-list objc-keywordarg
1525///
Mike Stump11289f42009-09-09 15:08:12 +00001526/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001527/// selector-name[opt] ':' objc-keywordexpr
1528///
1529/// objc-keywordexpr:
1530/// nonempty-expr-list
1531///
1532/// nonempty-expr-list:
1533/// assignment-expression
1534/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001535///
1536Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001537Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001538 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001539 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001540 ExprArg ReceiverExpr) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001541 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001542 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001543 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001544
Anders Carlsson978f08d2009-02-14 18:21:46 +00001545 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001546
Steve Narofff73590d2007-09-27 14:38:14 +00001547 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001548 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001549
Chris Lattner0ef13522007-10-09 17:51:17 +00001550 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001551 while (1) {
1552 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001553 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001554
Chris Lattner0ef13522007-10-09 17:51:17 +00001555 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001556 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001557 // We must manually skip to a ']', otherwise the expression skipper will
1558 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1559 // the enclosing expression.
1560 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001561 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001562 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001563
Steve Narofff73590d2007-09-27 14:38:14 +00001564 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001565 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001566 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001567 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001568 // We must manually skip to a ']', otherwise the expression skipper will
1569 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1570 // the enclosing expression.
1571 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001572 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001573 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001574
Steve Naroff486760a2007-09-17 20:25:27 +00001575 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001576 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001577
Steve Naroff486760a2007-09-17 20:25:27 +00001578 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001579 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001580 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001581 break;
1582 // We have a selector or a colon, continue parsing.
1583 }
1584 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001585 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001586 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001587 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001588 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001589 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001590 // We must manually skip to a ']', otherwise the expression skipper will
1591 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1592 // the enclosing expression.
1593 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001594 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001595 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001596
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001597 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001598 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001599 }
1600 } else if (!selIdent) {
1601 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001602
Chris Lattner197a3012008-08-05 06:19:09 +00001603 // We must manually skip to a ']', otherwise the expression skipper will
1604 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1605 // the enclosing expression.
1606 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001607 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001608 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001609
Chris Lattner0ef13522007-10-09 17:51:17 +00001610 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001611 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001612 // We must manually skip to a ']', otherwise the expression skipper will
1613 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1614 // the enclosing expression.
1615 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001616 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001617 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001618
Chris Lattner8f697062008-01-25 18:59:06 +00001619 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001620
Steve Naroffe61bfa82007-10-05 18:42:47 +00001621 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001622 if (nKeys == 0)
1623 KeyIdents.push_back(selIdent);
1624 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001625
Chris Lattner5700fab2007-10-07 02:00:24 +00001626 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001627 if (ReceiverName)
1628 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001629 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001630 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001631 KeyExprs.take(), KeyExprs.size()));
1632 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001633 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001634 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001635}
1636
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001637Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001638 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001639 if (Res.isInvalid()) return move(Res);
1640
Chris Lattnere002fbe2007-12-12 01:04:12 +00001641 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1642 // expressions. At this point, we know that the only valid thing that starts
1643 // with '@' is an @"".
1644 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001645 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001646 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001647 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001648
Chris Lattnere002fbe2007-12-12 01:04:12 +00001649 while (Tok.is(tok::at)) {
1650 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001651
Sebastian Redlc13f2682008-12-09 20:22:58 +00001652 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001653 if (!isTokenStringLiteral())
1654 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001655
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001656 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001657 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001658 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001659
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001660 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001661 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001662
1663 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1664 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001665}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001666
1667/// objc-encode-expression:
1668/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001669Parser::OwningExprResult
1670Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001671 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001672
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001673 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001674
Chris Lattner197a3012008-08-05 06:19:09 +00001675 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001676 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1677
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001678 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001679
Douglas Gregor220cac52009-02-18 17:45:20 +00001680 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001681
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001682 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001683
Douglas Gregor220cac52009-02-18 17:45:20 +00001684 if (Ty.isInvalid())
1685 return ExprError();
1686
Mike Stump11289f42009-09-09 15:08:12 +00001687 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001688 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001689}
Anders Carlssone01493d2007-08-23 15:25:28 +00001690
1691/// objc-protocol-expression
1692/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001693Parser::OwningExprResult
1694Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001695 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001696
Chris Lattner197a3012008-08-05 06:19:09 +00001697 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001698 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1699
Anders Carlssone01493d2007-08-23 15:25:28 +00001700 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001701
Chris Lattner197a3012008-08-05 06:19:09 +00001702 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001703 return ExprError(Diag(Tok, diag::err_expected_ident));
1704
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001705 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001706 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001707
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001708 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001709
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001710 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1711 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001712}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001713
1714/// objc-selector-expression
1715/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001716Parser::OwningExprResult
1717Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001718 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001719
Chris Lattner197a3012008-08-05 06:19:09 +00001720 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001721 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1722
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001723 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001724 SourceLocation LParenLoc = ConsumeParen();
1725 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001726 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001727 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1728 return ExprError(Diag(Tok, diag::err_expected_ident));
1729
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001730 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001731 unsigned nColons = 0;
1732 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001733 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001734 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001735 return ExprError(Diag(Tok, diag::err_expected_colon));
1736
Chris Lattner5e530bc2007-12-27 19:57:00 +00001737 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001738 ConsumeToken(); // Eat the ':'.
1739 if (Tok.is(tok::r_paren))
1740 break;
1741 // Check for another keyword selector.
1742 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001743 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001744 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001745 if (!SelIdent && Tok.isNot(tok::colon))
1746 break;
1747 }
Steve Naroff152dd812007-12-05 22:21:29 +00001748 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001749 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001750 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001751 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1752 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001753 }