blob: d06d8b6e9c72de8d7c2995fdb1170558e13ba564 [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) {
John McCall28a6aea2009-11-04 02:18:39 +0000701 ParsingDeclRAIIObject PD(*this);
702
Chris Lattner2ebb1782008-08-23 01:48:03 +0000703 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000704 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000705 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000706 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000707 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000708
Steve Naroff161a92b2007-10-26 20:53:56 +0000709 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000710 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000711
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000712 // An unnamed colon is valid.
713 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000714 Diag(Tok, diag::err_expected_selector_for_method)
715 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000716 // Skip until we get a ; or {}.
717 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000718 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000719 }
Mike Stump11289f42009-09-09 15:08:12 +0000720
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000721 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000722 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000723 // If attributes exist after the method, parse them.
724 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000725 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000726 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000727
Chris Lattner5700fab2007-10-07 02:00:24 +0000728 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000729 DeclPtrTy Result
730 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000731 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000732 0, CargNames, MethodAttrs,
733 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000734 PD.complete(Result);
735 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000736 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000737
Steve Narofff73590d2007-09-27 14:38:14 +0000738 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000739 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000740
Chris Lattner5700fab2007-10-07 02:00:24 +0000741 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000742 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000743
Chris Lattner5700fab2007-10-07 02:00:24 +0000744 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000745 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000746 Diag(Tok, diag::err_expected_colon);
747 break;
748 }
749 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000751 ArgInfo.Type = 0;
752 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
753 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
754
Chris Lattner5700fab2007-10-07 02:00:24 +0000755 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000756 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000757 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000758 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000759
Chris Lattner0ef13522007-10-09 17:51:17 +0000760 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000761 Diag(Tok, diag::err_expected_ident); // missing argument name.
762 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000763 }
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000765 ArgInfo.Name = Tok.getIdentifierInfo();
766 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000767 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000769 ArgInfos.push_back(ArgInfo);
770 KeyIdents.push_back(SelIdent);
771
Chris Lattner5700fab2007-10-07 02:00:24 +0000772 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000773 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000774 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000775 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000776 break;
777 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000778 }
Mike Stump11289f42009-09-09 15:08:12 +0000779
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000780 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000781
Chris Lattner5700fab2007-10-07 02:00:24 +0000782 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000783 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000784 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000785 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000786 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000787 ConsumeToken();
788 break;
789 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000790 DeclSpec DS;
791 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000792 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000793 Declarator ParmDecl(DS, Declarator::PrototypeContext);
794 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000795 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000796 }
Mike Stump11289f42009-09-09 15:08:12 +0000797
Chris Lattner5700fab2007-10-07 02:00:24 +0000798 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000799 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000800 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000801 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000802 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000803
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000804 if (KeyIdents.size() == 0)
805 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000806 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
807 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000808 DeclPtrTy Result
809 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000810 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000811 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000812 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000813 PD.complete(Result);
814 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000815}
816
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000817/// objc-protocol-refs:
818/// '<' identifier-list '>'
819///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000820bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000821ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000822 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
823 bool WarnOnDeclarations,
824 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000825 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000826
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000827 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000828
Chris Lattner3bbae002008-07-26 04:03:38 +0000829 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner3bbae002008-07-26 04:03:38 +0000831 while (1) {
832 if (Tok.isNot(tok::identifier)) {
833 Diag(Tok, diag::err_expected_ident);
834 SkipUntil(tok::greater);
835 return true;
836 }
837 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
838 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000839 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000840 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattner3bbae002008-07-26 04:03:38 +0000842 if (Tok.isNot(tok::comma))
843 break;
844 ConsumeToken();
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner3bbae002008-07-26 04:03:38 +0000847 // Consume the '>'.
848 if (Tok.isNot(tok::greater)) {
849 Diag(Tok, diag::err_expected_greater);
850 return true;
851 }
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner3bbae002008-07-26 04:03:38 +0000853 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000854
Chris Lattner3bbae002008-07-26 04:03:38 +0000855 // Convert the list of protocols identifiers into a list of protocol decls.
856 Actions.FindProtocolDeclaration(WarnOnDeclarations,
857 &ProtocolIdents[0], ProtocolIdents.size(),
858 Protocols);
859 return false;
860}
861
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000862/// objc-class-instance-variables:
863/// '{' objc-instance-variable-decl-list[opt] '}'
864///
865/// objc-instance-variable-decl-list:
866/// objc-visibility-spec
867/// objc-instance-variable-decl ';'
868/// ';'
869/// objc-instance-variable-decl-list objc-visibility-spec
870/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
871/// objc-instance-variable-decl-list ';'
872///
873/// objc-visibility-spec:
874/// @private
875/// @protected
876/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000877/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000878///
879/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000880/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000881///
Chris Lattner83f095c2009-03-28 19:18:32 +0000882void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000883 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000884 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000885 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000886
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000887 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000888
Steve Naroff00433d32007-08-21 21:17:12 +0000889 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000890
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000891 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000892 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000893 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000894 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000895
Steve Naroff00433d32007-08-21 21:17:12 +0000896 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000897 if (Tok.is(tok::semi)) {
Chris Lattner3e4fac72009-11-06 06:40:12 +0000898 Diag(Tok, diag::ext_extra_struct_semi)
899 << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
Steve Naroff00433d32007-08-21 21:17:12 +0000900 ConsumeToken();
901 continue;
902 }
Mike Stump11289f42009-09-09 15:08:12 +0000903
Steve Naroff00433d32007-08-21 21:17:12 +0000904 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000905 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000906 ConsumeToken(); // eat the @ sign
Steve Naroff7c348172007-08-23 18:16:40 +0000907 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000908 case tok::objc_private:
909 case tok::objc_public:
910 case tok::objc_protected:
911 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000912 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000913 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000914 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000915 default:
916 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000917 continue;
918 }
919 }
Mike Stump11289f42009-09-09 15:08:12 +0000920
John McCallcfefb6d2009-11-03 02:38:08 +0000921 struct ObjCIvarCallback : FieldCallback {
922 Parser &P;
923 DeclPtrTy IDecl;
924 tok::ObjCKeywordKind visibility;
925 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
926
927 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
928 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
929 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
930 }
931
932 DeclPtrTy invoke(FieldDeclarator &FD) {
933 // Install the declarator into the interface decl.
934 DeclPtrTy Field
935 = P.Actions.ActOnIvar(P.CurScope,
936 FD.D.getDeclSpec().getSourceRange().getBegin(),
937 IDecl, FD.D, FD.BitfieldSize, visibility);
938 AllIvarDecls.push_back(Field);
939 return Field;
940 }
941 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
942
Chris Lattnera12405b2008-04-10 06:46:29 +0000943 // Parse all the comma separated declarators.
944 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000945 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000946
Chris Lattner0ef13522007-10-09 17:51:17 +0000947 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000948 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +0000949 } else {
950 Diag(Tok, diag::err_expected_semi_decl_list);
951 // Skip to end of block or statement
952 SkipUntil(tok::r_brace, true, true);
953 }
954 }
Steve Naroff33a1e802007-10-29 21:38:07 +0000955 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +0000956 // Call ActOnFields() even if we don't have any decls. This is useful
957 // for code rewriting tools that need to be aware of the empty list.
958 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +0000959 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +0000960 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +0000961 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000962}
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000963
964/// objc-protocol-declaration:
965/// objc-protocol-definition
966/// objc-protocol-forward-reference
967///
968/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +0000969/// @protocol identifier
970/// objc-protocol-refs[opt]
971/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000972/// @end
973///
974/// objc-protocol-forward-reference:
975/// @protocol identifier-list ';'
976///
977/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +0000978/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000979/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +0000980Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
981 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000982 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000983 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
984 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000985
Chris Lattner0ef13522007-10-09 17:51:17 +0000986 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000987 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000988 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000989 }
990 // Save the protocol name, then consume it.
991 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
992 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000993
Chris Lattner0ef13522007-10-09 17:51:17 +0000994 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +0000995 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000996 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000997 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000998 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000999 }
Mike Stump11289f42009-09-09 15:08:12 +00001000
Chris Lattner0ef13522007-10-09 17:51:17 +00001001 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001002 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1003 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1004
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001005 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001006 while (1) {
1007 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001008 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001009 Diag(Tok, diag::err_expected_ident);
1010 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001011 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001012 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001013 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1014 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001015 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001016
Chris Lattner0ef13522007-10-09 17:51:17 +00001017 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001018 break;
1019 }
1020 // Consume the ';'.
1021 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001022 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001023
Steve Naroff93eb5f12007-10-10 17:32:04 +00001024 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001025 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001026 ProtocolRefs.size(),
1027 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001028 }
Mike Stump11289f42009-09-09 15:08:12 +00001029
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001030 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001031 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001032
Chris Lattner83f095c2009-03-28 19:18:32 +00001033 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001034 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001035 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001036 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1037 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001038 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001039
Chris Lattner83f095c2009-03-28 19:18:32 +00001040 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001041 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001042 ProtocolRefs.data(),
1043 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001044 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001045 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001046 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001047}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001048
1049/// objc-implementation:
1050/// objc-class-implementation-prologue
1051/// objc-category-implementation-prologue
1052///
1053/// objc-class-implementation-prologue:
1054/// @implementation identifier objc-superclass[opt]
1055/// objc-class-instance-variables[opt]
1056///
1057/// objc-category-implementation-prologue:
1058/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001059Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001060 SourceLocation atLoc) {
1061 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1062 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1063 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattner0ef13522007-10-09 17:51:17 +00001065 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001066 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001067 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001068 }
1069 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001070 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001071 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001072
1073 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001074 // we have a category implementation.
1075 SourceLocation lparenLoc = ConsumeParen();
1076 SourceLocation categoryLoc, rparenLoc;
1077 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattner0ef13522007-10-09 17:51:17 +00001079 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001080 categoryId = Tok.getIdentifierInfo();
1081 categoryLoc = ConsumeToken();
1082 } else {
1083 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001084 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001085 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001086 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001087 Diag(Tok, diag::err_expected_rparen);
1088 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001089 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001090 }
1091 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001092 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001093 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001094 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001095 ObjCImpDecl = ImplCatType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001096 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001097 }
1098 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001099 SourceLocation superClassLoc;
1100 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001101 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001102 // We have a super class
1103 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001104 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001105 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001106 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001107 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001108 superClassId = Tok.getIdentifierInfo();
1109 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001110 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001111 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001112 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001113 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001114
Steve Naroff33a1e802007-10-29 21:38:07 +00001115 if (Tok.is(tok::l_brace)) // we have ivars
1116 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001117 ObjCImpDecl = ImplClsType;
Mike Stump11289f42009-09-09 15:08:12 +00001118
Chris Lattner83f095c2009-03-28 19:18:32 +00001119 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001120}
Steve Naroff33a1e802007-10-29 21:38:07 +00001121
Chris Lattner83f095c2009-03-28 19:18:32 +00001122Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001123 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1124 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001125 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001126 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001127 if (ObjCImpDecl) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001128 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001129 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001130 }
Fariborz Jahanianc644ee42008-01-10 17:58:07 +00001131 else
1132 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001133 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001134}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001135
1136/// compatibility-alias-decl:
1137/// @compatibility_alias alias-name class-name ';'
1138///
Chris Lattner83f095c2009-03-28 19:18:32 +00001139Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001140 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1141 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1142 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001143 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001144 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001145 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001146 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001147 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1148 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001149 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001150 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001151 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001152 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001153 IdentifierInfo *classId = Tok.getIdentifierInfo();
1154 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1155 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001156 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001157 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001158 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001159 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1160 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001161}
1162
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001163/// property-synthesis:
1164/// @synthesize property-ivar-list ';'
1165///
1166/// property-ivar-list:
1167/// property-ivar
1168/// property-ivar-list ',' property-ivar
1169///
1170/// property-ivar:
1171/// identifier
1172/// identifier '=' identifier
1173///
Chris Lattner83f095c2009-03-28 19:18:32 +00001174Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001175 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1176 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001177 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattner0ef13522007-10-09 17:51:17 +00001178 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001179 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001180 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001181 }
Mike Stump11289f42009-09-09 15:08:12 +00001182
Chris Lattner0ef13522007-10-09 17:51:17 +00001183 while (Tok.is(tok::identifier)) {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001184 IdentifierInfo *propertyIvar = 0;
1185 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1186 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001187 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001188 // property '=' ivar-name
1189 ConsumeToken(); // consume '='
Chris Lattner0ef13522007-10-09 17:51:17 +00001190 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001191 Diag(Tok, diag::err_expected_ident);
1192 break;
1193 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001194 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001195 ConsumeToken(); // consume ivar-name
1196 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001197 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1198 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001199 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001200 break;
1201 ConsumeToken(); // consume ','
1202 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001203 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001204 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Fariborz Jahanian95239112009-11-06 21:48:47 +00001205 else
1206 ConsumeToken(); // consume ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001207 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001208}
1209
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001210/// property-dynamic:
1211/// @dynamic property-list
1212///
1213/// property-list:
1214/// identifier
1215/// property-list ',' identifier
1216///
Chris Lattner83f095c2009-03-28 19:18:32 +00001217Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001218 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1219 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1220 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattner0ef13522007-10-09 17:51:17 +00001221 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001222 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001223 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001224 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001225 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001226 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1227 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1228 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1229 propertyId, 0);
1230
Chris Lattner0ef13522007-10-09 17:51:17 +00001231 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001232 break;
1233 ConsumeToken(); // consume ','
1234 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001235 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001236 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001237 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001238}
Mike Stump11289f42009-09-09 15:08:12 +00001239
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001240/// objc-throw-statement:
1241/// throw expression[opt];
1242///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001243Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001244 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001245 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001246 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001247 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001248 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001249 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001250 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001251 }
1252 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001253 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001254 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001255}
1256
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001257/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001258/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001259///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001260Parser::OwningStmtResult
1261Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001262 ConsumeToken(); // consume synchronized
1263 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001264 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001265 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001266 }
1267 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001268 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001269 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001270 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001271 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001272 }
1273 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001274 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001275 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001276 }
1277 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001278 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001279 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001280 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001281 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001282 // Enter a scope to hold everything within the compound stmt. Compound
1283 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001284 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001285
Sebastian Redl042ad952008-12-11 19:30:53 +00001286 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001287
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001288 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001289 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001290 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001291 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001292}
1293
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001294/// objc-try-catch-statement:
1295/// @try compound-statement objc-catch-list[opt]
1296/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1297///
1298/// objc-catch-list:
1299/// @catch ( parameter-declaration ) compound-statement
1300/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1301/// catch-parameter-declaration:
1302/// parameter-declaration
1303/// '...' [OBJC2]
1304///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001305Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001306 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001307
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001308 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001309 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001310 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001311 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001312 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001313 OwningStmtResult CatchStmts(Actions);
1314 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001315 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001316 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001317 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001318 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001319 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001320
Chris Lattner0ef13522007-10-09 17:51:17 +00001321 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001322 // At this point, we need to lookahead to determine if this @ is the start
1323 // of an @catch or @finally. We don't want to consume the @ token if this
1324 // is an @try or @encode or something else.
1325 Token AfterAt = GetLookAheadToken(1);
1326 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1327 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1328 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001329
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001330 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001331 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001332 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001333 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001334 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001335 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001336 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001337 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001338 DeclSpec DS;
1339 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001340 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001341 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001342 // we must accept either 'declarator' or 'abstract-declarator' here.
1343 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1344 ParseDeclarator(ParmDecl);
1345
1346 // Inform the actions module about the parameter declarator, so it
1347 // gets added to the current scope.
1348 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001349 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001350 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001351
Steve Naroff65a00892009-04-07 22:56:58 +00001352 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001353
Steve Naroff65a00892009-04-07 22:56:58 +00001354 if (Tok.is(tok::r_paren))
1355 RParenLoc = ConsumeParen();
1356 else // Skip over garbage, until we get to ')'. Eat the ')'.
1357 SkipUntil(tok::r_paren, true, false);
1358
Sebastian Redlc13f2682008-12-09 20:22:58 +00001359 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001360 if (Tok.is(tok::l_brace))
1361 CatchBody = ParseCompoundStatementBody();
1362 else
1363 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001364 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001365 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001366 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001367 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001368 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001369 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001370 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1371 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001372 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001373 }
1374 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001375 } else {
1376 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001377 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001378 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001379
Sebastian Redlc13f2682008-12-09 20:22:58 +00001380 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001381 if (Tok.is(tok::l_brace))
1382 FinallyBody = ParseCompoundStatementBody();
1383 else
1384 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001385 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001386 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001387 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001388 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001389 catch_or_finally_seen = true;
1390 break;
1391 }
1392 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001393 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001394 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001395 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001396 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001397 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1398 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001399}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001400
Steve Naroff09bf8152007-09-06 21:24:23 +00001401/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001402///
Chris Lattner83f095c2009-03-28 19:18:32 +00001403Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1404 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001405
Chris Lattnereae6cb62009-03-05 08:00:35 +00001406 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1407 PP.getSourceManager(),
1408 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001409
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001410 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001411 if (Tok.is(tok::semi)) {
1412 if (ObjCImpDecl)
1413 Diag(Tok, diag::warn_semicolon_before_method_nody);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001414 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001415 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001416
Steve Naroffbb875722007-11-11 19:54:21 +00001417 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001418 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001419 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001420
Steve Naroffbb875722007-11-11 19:54:21 +00001421 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1422 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001423
Steve Naroffbb875722007-11-11 19:54:21 +00001424 // If we didn't find the '{', bail out.
1425 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001426 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001427 }
Steve Naroffbb875722007-11-11 19:54:21 +00001428 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001429
Steve Naroffbb875722007-11-11 19:54:21 +00001430 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001431 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001432
Steve Naroffbb875722007-11-11 19:54:21 +00001433 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001434 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001435 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001436
1437 OwningStmtResult FnBody(ParseCompoundStatementBody());
1438
Steve Naroffbb875722007-11-11 19:54:21 +00001439 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001440 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001441 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1442 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001443
Steve Naroffb94d7f62009-03-02 22:00:56 +00001444 // TODO: Pass argument information.
1445 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001446
Steve Naroffbb875722007-11-11 19:54:21 +00001447 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001448 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001449
Steve Naroff7b8fa472007-11-13 23:01:27 +00001450 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001451}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001452
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001453Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffe6016792008-02-05 21:27:35 +00001454 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001455 return ParseObjCTryStmt(AtLoc);
Steve Naroffe6016792008-02-05 21:27:35 +00001456 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1457 return ParseObjCThrowStmt(AtLoc);
1458 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1459 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001460 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001461 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001462 // If the expression is invalid, skip ahead to the next semicolon. Not
1463 // doing this opens us up to the possibility of infinite loops if
1464 // ParseExpression does not consume any tokens.
1465 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001466 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001467 }
1468 // Otherwise, eat the semicolon.
1469 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +00001470 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001471}
1472
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001473Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001474 switch (Tok.getKind()) {
Chris Lattnere002fbe2007-12-12 01:04:12 +00001475 case tok::string_literal: // primary-expression: string-literal
1476 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001477 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001478 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001479 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001480 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001481
Chris Lattner197a3012008-08-05 06:19:09 +00001482 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1483 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001484 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001485 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001486 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001487 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001488 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001489 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001490 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001491 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001492 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001493}
1494
Mike Stump11289f42009-09-09 15:08:12 +00001495/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001496/// '[' objc-receiver objc-message-args ']'
1497///
1498/// objc-receiver:
1499/// expression
1500/// class-name
1501/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001502Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001503 assert(Tok.is(tok::l_square) && "'[' expected");
1504 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1505
1506 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001507 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001508 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001509 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1510 SourceLocation NameLoc = ConsumeToken();
1511 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1512 ExprArg(Actions));
1513 }
Chris Lattner8f697062008-01-25 18:59:06 +00001514 }
1515
Sebastian Redl59b5e512008-12-11 21:36:32 +00001516 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001517 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001518 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001519 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001520 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001521
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001522 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001523 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001524}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001525
Chris Lattner8f697062008-01-25 18:59:06 +00001526/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1527/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001528///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001529/// objc-message-args:
1530/// objc-selector
1531/// objc-keywordarg-list
1532///
1533/// objc-keywordarg-list:
1534/// objc-keywordarg
1535/// objc-keywordarg-list objc-keywordarg
1536///
Mike Stump11289f42009-09-09 15:08:12 +00001537/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001538/// selector-name[opt] ':' objc-keywordexpr
1539///
1540/// objc-keywordexpr:
1541/// nonempty-expr-list
1542///
1543/// nonempty-expr-list:
1544/// assignment-expression
1545/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001546///
1547Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001548Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001549 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001550 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001551 ExprArg ReceiverExpr) {
Steve Naroffeae65032009-11-07 02:08:14 +00001552 if (Tok.is(tok::code_completion)) {
1553 if (ReceiverName)
1554 Actions.CodeCompleteObjCFactoryMethod(CurScope, ReceiverName);
1555 else
1556 Actions.CodeCompleteObjCInstanceMethod(CurScope, ReceiverExpr.release());
1557 ConsumeToken();
1558 }
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001559 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001560 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001561 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001562
Anders Carlsson978f08d2009-02-14 18:21:46 +00001563 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001564
Steve Narofff73590d2007-09-27 14:38:14 +00001565 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001566 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001567
Chris Lattner0ef13522007-10-09 17:51:17 +00001568 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001569 while (1) {
1570 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001571 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001572
Chris Lattner0ef13522007-10-09 17:51:17 +00001573 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001574 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001575 // We must manually skip to a ']', otherwise the expression skipper will
1576 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1577 // the enclosing expression.
1578 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001579 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001580 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001581
Steve Narofff73590d2007-09-27 14:38:14 +00001582 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001583 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001584 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001585 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001586 // We must manually skip to a ']', otherwise the expression skipper will
1587 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1588 // the enclosing expression.
1589 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001590 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001591 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001592
Steve Naroff486760a2007-09-17 20:25:27 +00001593 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001594 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001595
Steve Naroff486760a2007-09-17 20:25:27 +00001596 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001597 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001598 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001599 break;
1600 // We have a selector or a colon, continue parsing.
1601 }
1602 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001603 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001604 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001605 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001606 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001607 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001608 // We must manually skip to a ']', otherwise the expression skipper will
1609 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1610 // the enclosing expression.
1611 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001612 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001613 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001614
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001615 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001616 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001617 }
1618 } else if (!selIdent) {
1619 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001620
Chris Lattner197a3012008-08-05 06:19:09 +00001621 // We must manually skip to a ']', otherwise the expression skipper will
1622 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1623 // the enclosing expression.
1624 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001625 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001626 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001627
Chris Lattner0ef13522007-10-09 17:51:17 +00001628 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001629 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001630 // We must manually skip to a ']', otherwise the expression skipper will
1631 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1632 // the enclosing expression.
1633 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001634 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001635 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001636
Chris Lattner8f697062008-01-25 18:59:06 +00001637 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001638
Steve Naroffe61bfa82007-10-05 18:42:47 +00001639 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001640 if (nKeys == 0)
1641 KeyIdents.push_back(selIdent);
1642 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001643
Chris Lattner5700fab2007-10-07 02:00:24 +00001644 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001645 if (ReceiverName)
1646 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001647 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001648 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001649 KeyExprs.take(), KeyExprs.size()));
1650 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001651 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001652 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001653}
1654
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001655Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001656 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001657 if (Res.isInvalid()) return move(Res);
1658
Chris Lattnere002fbe2007-12-12 01:04:12 +00001659 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1660 // expressions. At this point, we know that the only valid thing that starts
1661 // with '@' is an @"".
1662 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001663 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001664 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001665 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001666
Chris Lattnere002fbe2007-12-12 01:04:12 +00001667 while (Tok.is(tok::at)) {
1668 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001669
Sebastian Redlc13f2682008-12-09 20:22:58 +00001670 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001671 if (!isTokenStringLiteral())
1672 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001673
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001674 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001675 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001676 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001677
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001678 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001679 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001680
1681 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1682 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001683}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001684
1685/// objc-encode-expression:
1686/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001687Parser::OwningExprResult
1688Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001689 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001690
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001691 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001692
Chris Lattner197a3012008-08-05 06:19:09 +00001693 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001694 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1695
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001696 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001697
Douglas Gregor220cac52009-02-18 17:45:20 +00001698 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001699
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001700 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001701
Douglas Gregor220cac52009-02-18 17:45:20 +00001702 if (Ty.isInvalid())
1703 return ExprError();
1704
Mike Stump11289f42009-09-09 15:08:12 +00001705 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001706 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001707}
Anders Carlssone01493d2007-08-23 15:25:28 +00001708
1709/// objc-protocol-expression
1710/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001711Parser::OwningExprResult
1712Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001713 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001714
Chris Lattner197a3012008-08-05 06:19:09 +00001715 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001716 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1717
Anders Carlssone01493d2007-08-23 15:25:28 +00001718 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001719
Chris Lattner197a3012008-08-05 06:19:09 +00001720 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001721 return ExprError(Diag(Tok, diag::err_expected_ident));
1722
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001723 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001724 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001725
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001726 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001727
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001728 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1729 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001730}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001731
1732/// objc-selector-expression
1733/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001734Parser::OwningExprResult
1735Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001736 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001737
Chris Lattner197a3012008-08-05 06:19:09 +00001738 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001739 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1740
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001741 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001742 SourceLocation LParenLoc = ConsumeParen();
1743 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001744 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001745 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1746 return ExprError(Diag(Tok, diag::err_expected_ident));
1747
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001748 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001749 unsigned nColons = 0;
1750 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001751 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001752 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001753 return ExprError(Diag(Tok, diag::err_expected_colon));
1754
Chris Lattner5e530bc2007-12-27 19:57:00 +00001755 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001756 ConsumeToken(); // Eat the ':'.
1757 if (Tok.is(tok::r_paren))
1758 break;
1759 // Check for another keyword selector.
1760 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001761 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001762 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001763 if (!SelIdent && Tok.isNot(tok::colon))
1764 break;
1765 }
Steve Naroff152dd812007-12-05 22:21:29 +00001766 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001767 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001768 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001769 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1770 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001771 }