blob: 014f10edd40f1433f383ebeb99243be147ae163d [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
Chris Lattner038a3e32008-10-20 05:46:22 +0000308 // Parse all the comma separated declarators.
309 DeclSpec DS;
310 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
311 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump11289f42009-09-09 15:08:12 +0000312
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000313 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
314 tok::at);
Mike Stump11289f42009-09-09 15:08:12 +0000315
Chris Lattner038a3e32008-10-20 05:46:22 +0000316 // Convert them all to property declarations.
317 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
318 FieldDeclarator &FD = FieldDeclarators[i];
Chris Lattner93429b02008-10-20 06:33:53 +0000319 if (FD.D.getIdentifier() == 0) {
Chris Lattner3d31c6c2008-11-18 07:50:21 +0000320 Diag(AtLoc, diag::err_objc_property_requires_field_name)
321 << FD.D.getSourceRange();
Chris Lattner93429b02008-10-20 06:33:53 +0000322 continue;
323 }
Fariborz Jahanian18789e1c2009-01-17 23:21:10 +0000324 if (FD.BitfieldSize) {
325 Diag(AtLoc, diag::err_objc_property_bitfield)
326 << FD.D.getSourceRange();
327 continue;
328 }
Mike Stump11289f42009-09-09 15:08:12 +0000329
Chris Lattner038a3e32008-10-20 05:46:22 +0000330 // Install the property declarator into interfaceDecl.
Chris Lattner93429b02008-10-20 06:33:53 +0000331 IdentifierInfo *SelName =
332 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +0000333
334 Selector GetterSel =
Chris Lattner93429b02008-10-20 06:33:53 +0000335 PP.getSelectorTable().getNullarySelector(SelName);
Chris Lattner038a3e32008-10-20 05:46:22 +0000336 IdentifierInfo *SetterName = OCDS.getSetterName();
Fariborz Jahanian36ce7e12009-03-12 22:34:11 +0000337 Selector SetterSel;
338 if (SetterName)
339 SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
340 else
341 SetterSel = SelectorTable::constructSetterName(PP.getIdentifierTable(),
342 PP.getSelectorTable(),
343 FD.D.getIdentifier());
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +0000344 bool isOverridingProperty = false;
Chris Lattner83f095c2009-03-28 19:18:32 +0000345 DeclPtrTy Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
346 GetterSel, SetterSel,
Mike Stump11289f42009-09-09 15:08:12 +0000347 interfaceDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000348 &isOverridingProperty,
349 MethodImplKind);
Fariborz Jahanianf8ef9f32008-11-26 20:01:34 +0000350 if (!isOverridingProperty)
351 allProperties.push_back(Property);
Chris Lattner038a3e32008-10-20 05:46:22 +0000352 }
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000353 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000354 }
Steve Naroff99264b42007-08-22 16:35:03 +0000355 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000356
357 // We break out of the big loop in two cases: when we see @end or when we see
358 // EOF. In the former case, eat the @end. In the later case, emit an error.
359 if (Tok.isObjCAtKeyword(tok::objc_end))
360 ConsumeToken(); // the "end" identifier
361 else
362 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000363
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000364 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000365 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek09a0d042008-06-06 16:45:15 +0000366 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000367 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000368 allProperties.data(), allProperties.size(),
369 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000370}
371
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000372/// Parse property attribute declarations.
373///
374/// property-attr-decl: '(' property-attrlist ')'
375/// property-attrlist:
376/// property-attribute
377/// property-attrlist ',' property-attribute
378/// property-attribute:
379/// getter '=' identifier
380/// setter '=' identifier ':'
381/// readonly
382/// readwrite
383/// assign
384/// retain
385/// copy
386/// nonatomic
387///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000388void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000389 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000390 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000392 while (1) {
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000393 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000394
Chris Lattner76619232008-10-20 07:22:18 +0000395 // If this is not an identifier at all, bail out early.
396 if (II == 0) {
397 MatchRHSPunctuation(tok::r_paren, LHSLoc);
398 return;
399 }
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattner1db33542008-10-20 07:37:22 +0000401 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000402
Chris Lattner68e48682008-11-20 04:42:34 +0000403 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000404 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000405 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000406 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000407 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000408 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000409 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000410 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000411 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000412 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000413 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000414 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000415 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000416 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000417 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
418 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000419 return;
Mike Stump11289f42009-09-09 15:08:12 +0000420
Chris Lattnerbeca7702008-10-20 07:24:39 +0000421 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000422 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000423 SkipUntil(tok::r_paren);
424 return;
425 }
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattner2dfde912008-10-20 07:43:01 +0000427 if (II->getName()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000428 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
429 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000430 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000431
Chris Lattner1db33542008-10-20 07:37:22 +0000432 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
433 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000434 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000435 } else {
436 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
437 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000438 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000439 }
Chris Lattner825bca12008-10-20 07:39:53 +0000440 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000441 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000442 SkipUntil(tok::r_paren);
443 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000444 }
Mike Stump11289f42009-09-09 15:08:12 +0000445
Chris Lattner1db33542008-10-20 07:37:22 +0000446 if (Tok.isNot(tok::comma))
447 break;
Mike Stump11289f42009-09-09 15:08:12 +0000448
Chris Lattner1db33542008-10-20 07:37:22 +0000449 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000450 }
Mike Stump11289f42009-09-09 15:08:12 +0000451
Chris Lattner1db33542008-10-20 07:37:22 +0000452 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000453}
454
Steve Naroff09bf8152007-09-06 21:24:23 +0000455/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000456/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000457/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000458///
459/// objc-instance-method: '-'
460/// objc-class-method: '+'
461///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000462/// objc-method-attributes: [OBJC2]
463/// __attribute__((deprecated))
464///
Mike Stump11289f42009-09-09 15:08:12 +0000465Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000466 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000467 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000468
Mike Stump11289f42009-09-09 15:08:12 +0000469 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000470 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000471
Chris Lattner83f095c2009-03-28 19:18:32 +0000472 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000473 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000474 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000475 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000476}
477
478/// objc-selector:
479/// identifier
480/// one of
481/// enum struct union if else while do for switch case default
482/// break continue return goto asm sizeof typeof __alignof
483/// unsigned long const short volatile signed restrict _Complex
484/// in out inout bycopy byref oneway int char float double void _Bool
485///
Chris Lattner4f472a32009-04-11 18:13:45 +0000486IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000487 switch (Tok.getKind()) {
488 default:
489 return 0;
490 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000491 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000492 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000493 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000494 case tok::kw_break:
495 case tok::kw_case:
496 case tok::kw_catch:
497 case tok::kw_char:
498 case tok::kw_class:
499 case tok::kw_const:
500 case tok::kw_const_cast:
501 case tok::kw_continue:
502 case tok::kw_default:
503 case tok::kw_delete:
504 case tok::kw_do:
505 case tok::kw_double:
506 case tok::kw_dynamic_cast:
507 case tok::kw_else:
508 case tok::kw_enum:
509 case tok::kw_explicit:
510 case tok::kw_export:
511 case tok::kw_extern:
512 case tok::kw_false:
513 case tok::kw_float:
514 case tok::kw_for:
515 case tok::kw_friend:
516 case tok::kw_goto:
517 case tok::kw_if:
518 case tok::kw_inline:
519 case tok::kw_int:
520 case tok::kw_long:
521 case tok::kw_mutable:
522 case tok::kw_namespace:
523 case tok::kw_new:
524 case tok::kw_operator:
525 case tok::kw_private:
526 case tok::kw_protected:
527 case tok::kw_public:
528 case tok::kw_register:
529 case tok::kw_reinterpret_cast:
530 case tok::kw_restrict:
531 case tok::kw_return:
532 case tok::kw_short:
533 case tok::kw_signed:
534 case tok::kw_sizeof:
535 case tok::kw_static:
536 case tok::kw_static_cast:
537 case tok::kw_struct:
538 case tok::kw_switch:
539 case tok::kw_template:
540 case tok::kw_this:
541 case tok::kw_throw:
542 case tok::kw_true:
543 case tok::kw_try:
544 case tok::kw_typedef:
545 case tok::kw_typeid:
546 case tok::kw_typename:
547 case tok::kw_typeof:
548 case tok::kw_union:
549 case tok::kw_unsigned:
550 case tok::kw_using:
551 case tok::kw_virtual:
552 case tok::kw_void:
553 case tok::kw_volatile:
554 case tok::kw_wchar_t:
555 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000556 case tok::kw__Bool:
557 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000558 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000559 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000560 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000561 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000562 }
Steve Naroff99264b42007-08-22 16:35:03 +0000563}
564
Fariborz Jahanian83615522008-01-02 22:54:34 +0000565/// objc-for-collection-in: 'in'
566///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000567bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000568 // FIXME: May have to do additional look-ahead to only allow for
569 // valid tokens following an 'in'; such as an identifier, unary operators,
570 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000571 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000572 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000573}
574
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000575/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000576/// qualifier list and builds their bitmask representation in the input
577/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000578///
579/// objc-type-qualifiers:
580/// objc-type-qualifier
581/// objc-type-qualifiers objc-type-qualifier
582///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000583void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000584 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000585 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000586 return;
Mike Stump11289f42009-09-09 15:08:12 +0000587
Chris Lattner61511e12007-12-12 06:56:32 +0000588 const IdentifierInfo *II = Tok.getIdentifierInfo();
589 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000590 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000591 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000592
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000593 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000594 switch (i) {
595 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000596 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
597 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
598 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
599 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
600 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
601 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000602 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000603 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000604 ConsumeToken();
605 II = 0;
606 break;
607 }
Mike Stump11289f42009-09-09 15:08:12 +0000608
Chris Lattner61511e12007-12-12 06:56:32 +0000609 // If this wasn't a recognized qualifier, bail out.
610 if (II) return;
611 }
612}
613
614/// objc-type-name:
615/// '(' objc-type-qualifiers[opt] type-name ')'
616/// '(' objc-type-qualifiers[opt] ')'
617///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000618Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000619 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattnerb7954432008-10-22 03:52:06 +0000621 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000622 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000623
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000624 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000625 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000626
Chris Lattnerb7954432008-10-22 03:52:06 +0000627 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000628 if (isTypeSpecifierQualifier()) {
629 TypeResult TypeSpec = ParseTypeName();
630 if (!TypeSpec.isInvalid())
631 Ty = TypeSpec.get();
632 }
Mike Stump11289f42009-09-09 15:08:12 +0000633
Steve Naroff90255b42008-10-21 14:15:04 +0000634 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000635 ConsumeParen();
636 else if (Tok.getLocation() == TypeStartLoc) {
637 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000638 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000639 SkipUntil(tok::r_paren);
640 } else {
641 // Otherwise, we found *something*, but didn't get a ')' in the right
642 // place. Emit an error then return what we have as the type.
643 MatchRHSPunctuation(tok::r_paren, LParenLoc);
644 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000645 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000646}
647
648/// objc-method-decl:
649/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000650/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000651/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000652/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000653///
654/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000655/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000656/// objc-keyword-selector objc-keyword-decl
657///
658/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000659/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
660/// objc-selector ':' objc-keyword-attributes[opt] identifier
661/// ':' objc-type-name objc-keyword-attributes[opt] identifier
662/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000663///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000664/// objc-parmlist:
665/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000666///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000667/// objc-parms:
668/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000669///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000670/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000671/// , ...
672///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000673/// objc-keyword-attributes: [OBJC2]
674/// __attribute__((unused))
675///
Chris Lattner83f095c2009-03-28 19:18:32 +0000676Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
677 tok::TokenKind mType,
678 DeclPtrTy IDecl,
679 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner2ebb1782008-08-23 01:48:03 +0000680 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000681 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000682 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000683 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000684 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000685
Steve Naroff161a92b2007-10-26 20:53:56 +0000686 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000687 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000688
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000689 // An unnamed colon is valid.
690 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000691 Diag(Tok, diag::err_expected_selector_for_method)
692 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000693 // Skip until we get a ; or {}.
694 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000695 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000696 }
Mike Stump11289f42009-09-09 15:08:12 +0000697
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000698 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000699 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000700 // If attributes exist after the method, parse them.
701 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000702 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000703 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000704
Chris Lattner5700fab2007-10-07 02:00:24 +0000705 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
Steve Naroff161a92b2007-10-26 20:53:56 +0000706 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000707 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000708 0, CargNames, MethodAttrs,
709 MethodImplKind);
Chris Lattner5700fab2007-10-07 02:00:24 +0000710 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000711
Steve Narofff73590d2007-09-27 14:38:14 +0000712 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000713 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000714
Chris Lattner5700fab2007-10-07 02:00:24 +0000715 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000716 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattner5700fab2007-10-07 02:00:24 +0000718 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000719 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000720 Diag(Tok, diag::err_expected_colon);
721 break;
722 }
723 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000724
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000725 ArgInfo.Type = 0;
726 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
727 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
728
Chris Lattner5700fab2007-10-07 02:00:24 +0000729 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000730 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000731 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000732 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000733
Chris Lattner0ef13522007-10-09 17:51:17 +0000734 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000735 Diag(Tok, diag::err_expected_ident); // missing argument name.
736 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000737 }
Mike Stump11289f42009-09-09 15:08:12 +0000738
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000739 ArgInfo.Name = Tok.getIdentifierInfo();
740 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000741 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000742
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000743 ArgInfos.push_back(ArgInfo);
744 KeyIdents.push_back(SelIdent);
745
Chris Lattner5700fab2007-10-07 02:00:24 +0000746 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000747 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000748 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000749 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000750 break;
751 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000752 }
Mike Stump11289f42009-09-09 15:08:12 +0000753
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000754 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattner5700fab2007-10-07 02:00:24 +0000756 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000757 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000758 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000759 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000760 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000761 ConsumeToken();
762 break;
763 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000764 DeclSpec DS;
765 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000766 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000767 Declarator ParmDecl(DS, Declarator::PrototypeContext);
768 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000769 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000770 }
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner5700fab2007-10-07 02:00:24 +0000772 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000773 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000774 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000775 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000776 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000777
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000778 if (KeyIdents.size() == 0)
779 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000780 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
781 &KeyIdents[0]);
Steve Naroff161a92b2007-10-26 20:53:56 +0000782 return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000783 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000784 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000785 MethodImplKind, isVariadic);
Steve Naroff99264b42007-08-22 16:35:03 +0000786}
787
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000788/// objc-protocol-refs:
789/// '<' identifier-list '>'
790///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000791bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000792ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000793 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
794 bool WarnOnDeclarations,
795 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000796 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000797
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000798 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000799
Chris Lattner3bbae002008-07-26 04:03:38 +0000800 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000801
Chris Lattner3bbae002008-07-26 04:03:38 +0000802 while (1) {
803 if (Tok.isNot(tok::identifier)) {
804 Diag(Tok, diag::err_expected_ident);
805 SkipUntil(tok::greater);
806 return true;
807 }
808 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
809 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000810 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000811 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000812
Chris Lattner3bbae002008-07-26 04:03:38 +0000813 if (Tok.isNot(tok::comma))
814 break;
815 ConsumeToken();
816 }
Mike Stump11289f42009-09-09 15:08:12 +0000817
Chris Lattner3bbae002008-07-26 04:03:38 +0000818 // Consume the '>'.
819 if (Tok.isNot(tok::greater)) {
820 Diag(Tok, diag::err_expected_greater);
821 return true;
822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattner3bbae002008-07-26 04:03:38 +0000824 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000825
Chris Lattner3bbae002008-07-26 04:03:38 +0000826 // Convert the list of protocols identifiers into a list of protocol decls.
827 Actions.FindProtocolDeclaration(WarnOnDeclarations,
828 &ProtocolIdents[0], ProtocolIdents.size(),
829 Protocols);
830 return false;
831}
832
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000833/// objc-class-instance-variables:
834/// '{' objc-instance-variable-decl-list[opt] '}'
835///
836/// objc-instance-variable-decl-list:
837/// objc-visibility-spec
838/// objc-instance-variable-decl ';'
839/// ';'
840/// objc-instance-variable-decl-list objc-visibility-spec
841/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
842/// objc-instance-variable-decl-list ';'
843///
844/// objc-visibility-spec:
845/// @private
846/// @protected
847/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000848/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000849///
850/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000851/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000852///
Chris Lattner83f095c2009-03-28 19:18:32 +0000853void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000854 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000855 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000856 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000857 llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
858
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000859 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000860
Steve Naroff00433d32007-08-21 21:17:12 +0000861 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000862
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000863 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000864 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000865 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000866 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000867
Steve Naroff00433d32007-08-21 21:17:12 +0000868 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000869 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000870 Diag(Tok, diag::ext_extra_struct_semi);
871 ConsumeToken();
872 continue;
873 }
Mike Stump11289f42009-09-09 15:08:12 +0000874
Steve Naroff00433d32007-08-21 21:17:12 +0000875 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000876 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000877 ConsumeToken(); // eat the @ sign
Steve Naroff7c348172007-08-23 18:16:40 +0000878 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000879 case tok::objc_private:
880 case tok::objc_public:
881 case tok::objc_protected:
882 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000883 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000884 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000885 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000886 default:
887 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000888 continue;
889 }
890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Chris Lattnera12405b2008-04-10 06:46:29 +0000892 // Parse all the comma separated declarators.
893 DeclSpec DS;
894 FieldDeclarators.clear();
895 ParseStructDeclaration(DS, FieldDeclarators);
Mike Stump11289f42009-09-09 15:08:12 +0000896
Chris Lattnera12405b2008-04-10 06:46:29 +0000897 // Convert them all to fields.
898 for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
899 FieldDeclarator &FD = FieldDeclarators[i];
900 // Install the declarator into interfaceDecl.
Chris Lattner83f095c2009-03-28 19:18:32 +0000901 DeclPtrTy Field = Actions.ActOnIvar(CurScope,
902 DS.getSourceRange().getBegin(),
Fariborz Jahanian68453832009-06-05 18:16:35 +0000903 interfaceDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000904 FD.D, FD.BitfieldSize, visibility);
Chris Lattnera12405b2008-04-10 06:46:29 +0000905 AllIvarDecls.push_back(Field);
Fariborz Jahanian6812cbb2007-09-13 20:56:13 +0000906 }
Mike Stump11289f42009-09-09 15:08:12 +0000907
Chris Lattner0ef13522007-10-09 17:51:17 +0000908 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000909 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +0000910 } else {
911 Diag(Tok, diag::err_expected_semi_decl_list);
912 // Skip to end of block or statement
913 SkipUntil(tok::r_brace, true, true);
914 }
915 }
Steve Naroff33a1e802007-10-29 21:38:07 +0000916 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +0000917 // Call ActOnFields() even if we don't have any decls. This is useful
918 // for code rewriting tools that need to be aware of the empty list.
919 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +0000920 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +0000921 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +0000922 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000923}
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000924
925/// objc-protocol-declaration:
926/// objc-protocol-definition
927/// objc-protocol-forward-reference
928///
929/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +0000930/// @protocol identifier
931/// objc-protocol-refs[opt]
932/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000933/// @end
934///
935/// objc-protocol-forward-reference:
936/// @protocol identifier-list ';'
937///
938/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +0000939/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000940/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +0000941Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
942 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000943 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000944 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
945 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000946
Chris Lattner0ef13522007-10-09 17:51:17 +0000947 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000948 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000949 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000950 }
951 // Save the protocol name, then consume it.
952 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
953 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000954
Chris Lattner0ef13522007-10-09 17:51:17 +0000955 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +0000956 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000957 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000958 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000959 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000960 }
Mike Stump11289f42009-09-09 15:08:12 +0000961
Chris Lattner0ef13522007-10-09 17:51:17 +0000962 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +0000963 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
964 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
965
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000966 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000967 while (1) {
968 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +0000969 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000970 Diag(Tok, diag::err_expected_ident);
971 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +0000972 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000973 }
Chris Lattnerd7352d62008-07-21 22:17:28 +0000974 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
975 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000976 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +0000977
Chris Lattner0ef13522007-10-09 17:51:17 +0000978 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000979 break;
980 }
981 // Consume the ';'.
982 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +0000983 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000984
Steve Naroff93eb5f12007-10-10 17:32:04 +0000985 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000986 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000987 ProtocolRefs.size(),
988 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +0000989 }
Mike Stump11289f42009-09-09 15:08:12 +0000990
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000991 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000992 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +0000993
Chris Lattner83f095c2009-03-28 19:18:32 +0000994 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000995 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +0000996 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000997 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
998 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000999 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001000
Chris Lattner83f095c2009-03-28 19:18:32 +00001001 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001002 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001003 ProtocolRefs.data(),
1004 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001005 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001006 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001007 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001008}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001009
1010/// objc-implementation:
1011/// objc-class-implementation-prologue
1012/// objc-category-implementation-prologue
1013///
1014/// objc-class-implementation-prologue:
1015/// @implementation identifier objc-superclass[opt]
1016/// objc-class-instance-variables[opt]
1017///
1018/// objc-category-implementation-prologue:
1019/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001020Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001021 SourceLocation atLoc) {
1022 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1023 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1024 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001025
Chris Lattner0ef13522007-10-09 17:51:17 +00001026 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001027 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001028 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001029 }
1030 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001031 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001032 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001033
1034 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001035 // we have a category implementation.
1036 SourceLocation lparenLoc = ConsumeParen();
1037 SourceLocation categoryLoc, rparenLoc;
1038 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001039
Chris Lattner0ef13522007-10-09 17:51:17 +00001040 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001041 categoryId = Tok.getIdentifierInfo();
1042 categoryLoc = ConsumeToken();
1043 } else {
1044 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001045 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001046 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001047 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001048 Diag(Tok, diag::err_expected_rparen);
1049 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001050 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001051 }
1052 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001053 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001054 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001055 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001056 ObjCImpDecl = ImplCatType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001057 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001058 }
1059 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001060 SourceLocation superClassLoc;
1061 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001062 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001063 // We have a super class
1064 ConsumeToken();
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 super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001067 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001068 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001069 superClassId = Tok.getIdentifierInfo();
1070 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001071 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001072 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001073 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001074 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001075
Steve Naroff33a1e802007-10-29 21:38:07 +00001076 if (Tok.is(tok::l_brace)) // we have ivars
1077 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001078 ObjCImpDecl = ImplClsType;
Mike Stump11289f42009-09-09 15:08:12 +00001079
Chris Lattner83f095c2009-03-28 19:18:32 +00001080 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001081}
Steve Naroff33a1e802007-10-29 21:38:07 +00001082
Chris Lattner83f095c2009-03-28 19:18:32 +00001083Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001084 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1085 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001086 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001087 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001088 if (ObjCImpDecl) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001089 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001090 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001091 }
Fariborz Jahanianc644ee42008-01-10 17:58:07 +00001092 else
1093 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001094 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001095}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001096
1097/// compatibility-alias-decl:
1098/// @compatibility_alias alias-name class-name ';'
1099///
Chris Lattner83f095c2009-03-28 19:18:32 +00001100Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001101 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1102 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1103 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001104 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001105 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001106 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001107 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001108 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1109 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001110 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001111 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001112 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001113 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001114 IdentifierInfo *classId = Tok.getIdentifierInfo();
1115 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1116 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001117 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001118 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001119 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001120 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1121 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001122}
1123
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001124/// property-synthesis:
1125/// @synthesize property-ivar-list ';'
1126///
1127/// property-ivar-list:
1128/// property-ivar
1129/// property-ivar-list ',' property-ivar
1130///
1131/// property-ivar:
1132/// identifier
1133/// identifier '=' identifier
1134///
Chris Lattner83f095c2009-03-28 19:18:32 +00001135Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001136 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1137 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001138 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattner0ef13522007-10-09 17:51:17 +00001139 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001140 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001141 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001142 }
Mike Stump11289f42009-09-09 15:08:12 +00001143
Chris Lattner0ef13522007-10-09 17:51:17 +00001144 while (Tok.is(tok::identifier)) {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001145 IdentifierInfo *propertyIvar = 0;
1146 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1147 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001148 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001149 // property '=' ivar-name
1150 ConsumeToken(); // consume '='
Chris Lattner0ef13522007-10-09 17:51:17 +00001151 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001152 Diag(Tok, diag::err_expected_ident);
1153 break;
1154 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001155 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001156 ConsumeToken(); // consume ivar-name
1157 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001158 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1159 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001160 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001161 break;
1162 ConsumeToken(); // consume ','
1163 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001164 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001165 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattner83f095c2009-03-28 19:18:32 +00001166 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001167}
1168
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001169/// property-dynamic:
1170/// @dynamic property-list
1171///
1172/// property-list:
1173/// identifier
1174/// property-list ',' identifier
1175///
Chris Lattner83f095c2009-03-28 19:18:32 +00001176Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001177 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1178 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1179 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattner0ef13522007-10-09 17:51:17 +00001180 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001181 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001182 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001183 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001184 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001185 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1186 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1187 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1188 propertyId, 0);
1189
Chris Lattner0ef13522007-10-09 17:51:17 +00001190 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001191 break;
1192 ConsumeToken(); // consume ','
1193 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001194 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001195 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001196 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001197}
Mike Stump11289f42009-09-09 15:08:12 +00001198
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001199/// objc-throw-statement:
1200/// throw expression[opt];
1201///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001202Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001203 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001204 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001205 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001206 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001207 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001208 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001209 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001210 }
1211 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001212 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001213 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001214}
1215
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001216/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001217/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001218///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001219Parser::OwningStmtResult
1220Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001221 ConsumeToken(); // consume synchronized
1222 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001223 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001224 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001225 }
1226 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001227 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001228 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001229 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001230 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001231 }
1232 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001233 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001234 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001235 }
1236 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001237 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001238 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001239 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001240 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001241 // Enter a scope to hold everything within the compound stmt. Compound
1242 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001243 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001244
Sebastian Redl042ad952008-12-11 19:30:53 +00001245 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001246
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001247 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001248 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001249 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001250 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001251}
1252
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001253/// objc-try-catch-statement:
1254/// @try compound-statement objc-catch-list[opt]
1255/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1256///
1257/// objc-catch-list:
1258/// @catch ( parameter-declaration ) compound-statement
1259/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1260/// catch-parameter-declaration:
1261/// parameter-declaration
1262/// '...' [OBJC2]
1263///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001264Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001265 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001266
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001267 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001268 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001269 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001270 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001271 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001272 OwningStmtResult CatchStmts(Actions);
1273 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001274 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001275 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001276 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001277 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001278 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001279
Chris Lattner0ef13522007-10-09 17:51:17 +00001280 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001281 // At this point, we need to lookahead to determine if this @ is the start
1282 // of an @catch or @finally. We don't want to consume the @ token if this
1283 // is an @try or @encode or something else.
1284 Token AfterAt = GetLookAheadToken(1);
1285 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1286 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1287 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001288
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001289 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001290 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001291 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001292 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001293 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001294 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001295 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001296 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001297 DeclSpec DS;
1298 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001299 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001300 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001301 // we must accept either 'declarator' or 'abstract-declarator' here.
1302 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1303 ParseDeclarator(ParmDecl);
1304
1305 // Inform the actions module about the parameter declarator, so it
1306 // gets added to the current scope.
1307 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001308 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001309 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001310
Steve Naroff65a00892009-04-07 22:56:58 +00001311 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001312
Steve Naroff65a00892009-04-07 22:56:58 +00001313 if (Tok.is(tok::r_paren))
1314 RParenLoc = ConsumeParen();
1315 else // Skip over garbage, until we get to ')'. Eat the ')'.
1316 SkipUntil(tok::r_paren, true, false);
1317
Sebastian Redlc13f2682008-12-09 20:22:58 +00001318 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001319 if (Tok.is(tok::l_brace))
1320 CatchBody = ParseCompoundStatementBody();
1321 else
1322 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001323 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001324 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001325 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001326 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001327 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001328 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001329 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1330 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001331 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001332 }
1333 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001334 } else {
1335 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001336 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001337 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001338
Sebastian Redlc13f2682008-12-09 20:22:58 +00001339 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001340 if (Tok.is(tok::l_brace))
1341 FinallyBody = ParseCompoundStatementBody();
1342 else
1343 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001344 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001345 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001346 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001347 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001348 catch_or_finally_seen = true;
1349 break;
1350 }
1351 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001352 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001353 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001354 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001355 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001356 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1357 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001358}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001359
Steve Naroff09bf8152007-09-06 21:24:23 +00001360/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001361///
Chris Lattner83f095c2009-03-28 19:18:32 +00001362Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1363 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001364
Chris Lattnereae6cb62009-03-05 08:00:35 +00001365 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1366 PP.getSourceManager(),
1367 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001368
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001369 // parse optional ';'
Chris Lattner0ef13522007-10-09 17:51:17 +00001370 if (Tok.is(tok::semi))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001371 ConsumeToken();
1372
Steve Naroffbb875722007-11-11 19:54:21 +00001373 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001374 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001375 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001376
Steve Naroffbb875722007-11-11 19:54:21 +00001377 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1378 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001379
Steve Naroffbb875722007-11-11 19:54:21 +00001380 // If we didn't find the '{', bail out.
1381 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001382 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001383 }
Steve Naroffbb875722007-11-11 19:54:21 +00001384 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001385
Steve Naroffbb875722007-11-11 19:54:21 +00001386 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001387 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001388
Steve Naroffbb875722007-11-11 19:54:21 +00001389 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001390 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001391 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001392
1393 OwningStmtResult FnBody(ParseCompoundStatementBody());
1394
Steve Naroffbb875722007-11-11 19:54:21 +00001395 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001396 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001397 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1398 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001399
Steve Naroffb94d7f62009-03-02 22:00:56 +00001400 // TODO: Pass argument information.
1401 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001402
Steve Naroffbb875722007-11-11 19:54:21 +00001403 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001404 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001405
Steve Naroff7b8fa472007-11-13 23:01:27 +00001406 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001407}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001408
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001409Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffe6016792008-02-05 21:27:35 +00001410 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001411 return ParseObjCTryStmt(AtLoc);
Steve Naroffe6016792008-02-05 21:27:35 +00001412 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1413 return ParseObjCThrowStmt(AtLoc);
1414 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1415 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001416 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001417 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001418 // If the expression is invalid, skip ahead to the next semicolon. Not
1419 // doing this opens us up to the possibility of infinite loops if
1420 // ParseExpression does not consume any tokens.
1421 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001422 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001423 }
1424 // Otherwise, eat the semicolon.
1425 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +00001426 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001427}
1428
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001429Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001430 switch (Tok.getKind()) {
Chris Lattnere002fbe2007-12-12 01:04:12 +00001431 case tok::string_literal: // primary-expression: string-literal
1432 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001433 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001434 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001435 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001436 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001437
Chris Lattner197a3012008-08-05 06:19:09 +00001438 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1439 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001440 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001441 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001442 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001443 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001444 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001445 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001446 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001447 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001448 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001449}
1450
Mike Stump11289f42009-09-09 15:08:12 +00001451/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001452/// '[' objc-receiver objc-message-args ']'
1453///
1454/// objc-receiver:
1455/// expression
1456/// class-name
1457/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001458Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001459 assert(Tok.is(tok::l_square) && "'[' expected");
1460 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1461
1462 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001463 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001464 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001465 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1466 SourceLocation NameLoc = ConsumeToken();
1467 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1468 ExprArg(Actions));
1469 }
Chris Lattner8f697062008-01-25 18:59:06 +00001470 }
1471
Sebastian Redl59b5e512008-12-11 21:36:32 +00001472 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001473 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001474 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001475 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001476 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001477
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001478 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001479 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001480}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001481
Chris Lattner8f697062008-01-25 18:59:06 +00001482/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1483/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001484///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001485/// objc-message-args:
1486/// objc-selector
1487/// objc-keywordarg-list
1488///
1489/// objc-keywordarg-list:
1490/// objc-keywordarg
1491/// objc-keywordarg-list objc-keywordarg
1492///
Mike Stump11289f42009-09-09 15:08:12 +00001493/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001494/// selector-name[opt] ':' objc-keywordexpr
1495///
1496/// objc-keywordexpr:
1497/// nonempty-expr-list
1498///
1499/// nonempty-expr-list:
1500/// assignment-expression
1501/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001502///
1503Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001504Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001505 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001506 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001507 ExprArg ReceiverExpr) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001508 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001509 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001510 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001511
Anders Carlsson978f08d2009-02-14 18:21:46 +00001512 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001513
Steve Narofff73590d2007-09-27 14:38:14 +00001514 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001515 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001516
Chris Lattner0ef13522007-10-09 17:51:17 +00001517 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001518 while (1) {
1519 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001520 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001521
Chris Lattner0ef13522007-10-09 17:51:17 +00001522 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001523 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001524 // We must manually skip to a ']', otherwise the expression skipper will
1525 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1526 // the enclosing expression.
1527 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001528 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001529 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001530
Steve Narofff73590d2007-09-27 14:38:14 +00001531 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001532 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001533 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001534 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001535 // We must manually skip to a ']', otherwise the expression skipper will
1536 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1537 // the enclosing expression.
1538 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001539 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001540 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001541
Steve Naroff486760a2007-09-17 20:25:27 +00001542 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001543 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001544
Steve Naroff486760a2007-09-17 20:25:27 +00001545 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001546 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001547 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001548 break;
1549 // We have a selector or a colon, continue parsing.
1550 }
1551 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001552 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001553 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001554 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001555 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001556 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001557 // We must manually skip to a ']', otherwise the expression skipper will
1558 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1559 // the enclosing expression.
1560 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001561 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001562 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001563
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001564 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001565 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001566 }
1567 } else if (!selIdent) {
1568 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001569
Chris Lattner197a3012008-08-05 06:19:09 +00001570 // We must manually skip to a ']', otherwise the expression skipper will
1571 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1572 // the enclosing expression.
1573 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001574 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001575 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001576
Chris Lattner0ef13522007-10-09 17:51:17 +00001577 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001578 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001579 // We must manually skip to a ']', otherwise the expression skipper will
1580 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1581 // the enclosing expression.
1582 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001583 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001584 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001585
Chris Lattner8f697062008-01-25 18:59:06 +00001586 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001587
Steve Naroffe61bfa82007-10-05 18:42:47 +00001588 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001589 if (nKeys == 0)
1590 KeyIdents.push_back(selIdent);
1591 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001592
Chris Lattner5700fab2007-10-07 02:00:24 +00001593 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001594 if (ReceiverName)
1595 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001596 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001597 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001598 KeyExprs.take(), KeyExprs.size()));
1599 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001600 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001601 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001602}
1603
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001604Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001605 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001606 if (Res.isInvalid()) return move(Res);
1607
Chris Lattnere002fbe2007-12-12 01:04:12 +00001608 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1609 // expressions. At this point, we know that the only valid thing that starts
1610 // with '@' is an @"".
1611 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001612 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001613 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001614 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001615
Chris Lattnere002fbe2007-12-12 01:04:12 +00001616 while (Tok.is(tok::at)) {
1617 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001618
Sebastian Redlc13f2682008-12-09 20:22:58 +00001619 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001620 if (!isTokenStringLiteral())
1621 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001622
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001623 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001624 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001625 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001626
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001627 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001628 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001629
1630 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1631 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001632}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001633
1634/// objc-encode-expression:
1635/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001636Parser::OwningExprResult
1637Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001638 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001639
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001640 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001641
Chris Lattner197a3012008-08-05 06:19:09 +00001642 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001643 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1644
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001645 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001646
Douglas Gregor220cac52009-02-18 17:45:20 +00001647 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001648
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001649 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001650
Douglas Gregor220cac52009-02-18 17:45:20 +00001651 if (Ty.isInvalid())
1652 return ExprError();
1653
Mike Stump11289f42009-09-09 15:08:12 +00001654 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001655 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001656}
Anders Carlssone01493d2007-08-23 15:25:28 +00001657
1658/// objc-protocol-expression
1659/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001660Parser::OwningExprResult
1661Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001662 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001663
Chris Lattner197a3012008-08-05 06:19:09 +00001664 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001665 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1666
Anders Carlssone01493d2007-08-23 15:25:28 +00001667 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001668
Chris Lattner197a3012008-08-05 06:19:09 +00001669 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001670 return ExprError(Diag(Tok, diag::err_expected_ident));
1671
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001672 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001673 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001674
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001675 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001676
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001677 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1678 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001679}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001680
1681/// objc-selector-expression
1682/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001683Parser::OwningExprResult
1684Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001685 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001686
Chris Lattner197a3012008-08-05 06:19:09 +00001687 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001688 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1689
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001690 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001691 SourceLocation LParenLoc = ConsumeParen();
1692 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001693 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001694 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1695 return ExprError(Diag(Tok, diag::err_expected_ident));
1696
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001697 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001698 unsigned nColons = 0;
1699 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001700 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001701 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001702 return ExprError(Diag(Tok, diag::err_expected_colon));
1703
Chris Lattner5e530bc2007-12-27 19:57:00 +00001704 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001705 ConsumeToken(); // Eat the ':'.
1706 if (Tok.is(tok::r_paren))
1707 break;
1708 // Check for another keyword selector.
1709 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001710 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001711 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001712 if (!SelIdent && Tok.isNot(tok::colon))
1713 break;
1714 }
Steve Naroff152dd812007-12-05 22:21:29 +00001715 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001716 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001717 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001718 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1719 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001720 }