blob: b043dd99f304e7b706c1b3fbc6b91427b1ab61cf [file] [log] [blame]
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
Chris Lattnerda59c2f2006-11-05 02:08:13 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerda59c2f2006-11-05 02:08:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Objective-C portions of the Parser interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Steve Narofff1bc45b2007-08-22 18:35:33 +000015#include "clang/Parse/DeclSpec.h"
Fariborz Jahanian9e63b982007-11-01 23:59:59 +000016#include "clang/Parse/Scope.h"
Chris Lattner60f36222009-01-29 05:15:15 +000017#include "clang/Parse/ParseDiagnostic.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000018#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000019using namespace clang;
20
21
Chris Lattner3a907162008-12-08 21:53:24 +000022/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000023/// external-declaration: [C99 6.9]
24/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000025/// [OBJC] objc-class-declaration
26/// [OBJC] objc-alias-declaration
27/// [OBJC] objc-protocol-definition
28/// [OBJC] objc-method-definition
29/// [OBJC] '@' 'end'
Chris Lattner83f095c2009-03-28 19:18:32 +000030Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000031 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000032
Steve Naroff7c348172007-08-23 18:16:40 +000033 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000034 case tok::objc_class:
35 return ParseObjCAtClassDeclaration(AtLoc);
36 case tok::objc_interface:
37 return ParseObjCAtInterfaceDeclaration(AtLoc);
38 case tok::objc_protocol:
39 return ParseObjCAtProtocolDeclaration(AtLoc);
40 case tok::objc_implementation:
41 return ParseObjCAtImplementationDeclaration(AtLoc);
42 case tok::objc_end:
43 return ParseObjCAtEndDeclaration(AtLoc);
44 case tok::objc_compatibility_alias:
45 return ParseObjCAtAliasDeclaration(AtLoc);
46 case tok::objc_synthesize:
47 return ParseObjCPropertySynthesize(AtLoc);
48 case tok::objc_dynamic:
49 return ParseObjCPropertyDynamic(AtLoc);
50 default:
51 Diag(AtLoc, diag::err_unexpected_at);
52 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000053 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000054 }
55}
56
57///
Mike Stump11289f42009-09-09 15:08:12 +000058/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000059/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000060///
Chris Lattner83f095c2009-03-28 19:18:32 +000061Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000062 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000063 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Mike Stump11289f42009-09-09 15:08:12 +000064
Chris Lattnerda59c2f2006-11-05 02:08:13 +000065 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000066 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000067 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000068 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +000069 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +000070 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000071 ClassNames.push_back(Tok.getIdentifierInfo());
72 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000073
Chris Lattner0ef13522007-10-09 17:51:17 +000074 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000075 break;
Mike Stump11289f42009-09-09 15:08:12 +000076
Chris Lattnerda59c2f2006-11-05 02:08:13 +000077 ConsumeToken();
78 }
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattnerda59c2f2006-11-05 02:08:13 +000080 // Consume the ';'.
81 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
Chris Lattner83f095c2009-03-28 19:18:32 +000082 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +000083
Steve Naroff93eb5f12007-10-10 17:32:04 +000084 return Actions.ActOnForwardClassDeclaration(atLoc,
Steve Naroff09bf8152007-09-06 21:24:23 +000085 &ClassNames[0], ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000086}
87
Steve Naroff1eb1ad62007-08-20 21:31:48 +000088///
89/// objc-interface:
90/// objc-class-interface-attributes[opt] objc-class-interface
91/// objc-category-interface
92///
93/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +000094/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +000095/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +000096/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +000097/// objc-interface-decl-list
98/// @end
99///
100/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000101/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000102/// objc-protocol-refs[opt]
103/// objc-interface-decl-list
104/// @end
105///
106/// objc-superclass:
107/// ':' identifier
108///
109/// objc-class-interface-attributes:
110/// __attribute__((visibility("default")))
111/// __attribute__((visibility("hidden")))
112/// __attribute__((deprecated))
113/// __attribute__((unavailable))
114/// __attribute__((objc_exception)) - used by NSException on 64-bit
115///
Chris Lattner83f095c2009-03-28 19:18:32 +0000116Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000117 SourceLocation atLoc, AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000118 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000119 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000121
Chris Lattner0ef13522007-10-09 17:51:17 +0000122 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000123 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000124 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000125 }
126 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000127 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000128 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000129
Chris Lattner0ef13522007-10-09 17:51:17 +0000130 if (Tok.is(tok::l_paren)) { // we have a category.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000131 SourceLocation lparenLoc = ConsumeParen();
132 SourceLocation categoryLoc, rparenLoc;
133 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000134
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000135 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000136 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000137 categoryId = Tok.getIdentifierInfo();
138 categoryLoc = ConsumeToken();
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000139 } else if (!getLang().ObjC2) {
140 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000141 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000142 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_rparen);
145 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +0000146 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000147 }
148 rparenLoc = ConsumeParen();
Mike Stump11289f42009-09-09 15:08:12 +0000149
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000150 // Next, we need to check for any protocol references.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000151 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattner83f095c2009-03-28 19:18:32 +0000152 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000153 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerb1f3c942008-07-26 04:07:02 +0000154 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000155 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
156 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000157 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000158
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000159 if (attrList) // categories don't support attributes.
160 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000161
Jay Foad7d0479f2009-05-21 09:52:38 +0000162 DeclPtrTy CategoryType =
Mike Stump11289f42009-09-09 15:08:12 +0000163 Actions.ActOnStartCategoryInterface(atLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000164 nameId, nameLoc,
165 categoryId, categoryLoc,
166 ProtocolRefs.data(),
167 ProtocolRefs.size(),
168 EndProtoLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000169
Fariborz Jahanian867a7eb2007-09-18 20:26:58 +0000170 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000171 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000172 }
173 // Parse a class interface.
174 IdentifierInfo *superClassId = 0;
175 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000176
Chris Lattner0ef13522007-10-09 17:51:17 +0000177 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000178 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000179 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000180 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000181 return DeclPtrTy();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000182 }
183 superClassId = Tok.getIdentifierInfo();
184 superClassLoc = ConsumeToken();
185 }
186 // Next, we need to check for any protocol references.
Chris Lattner83f095c2009-03-28 19:18:32 +0000187 llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000188 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
189 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000190 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000191 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
192 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +0000193 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +0000194
195 DeclPtrTy ClsType =
196 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000197 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000198 ProtocolRefs.data(), ProtocolRefs.size(),
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000199 EndProtoLoc, attrList);
Mike Stump11289f42009-09-09 15:08:12 +0000200
Chris Lattner0ef13522007-10-09 17:51:17 +0000201 if (Tok.is(tok::l_brace))
Steve Naroff33a1e802007-10-29 21:38:07 +0000202 ParseObjCClassInstanceVariables(ClsType, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000203
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000204 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000205 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000206}
207
208/// objc-interface-decl-list:
209/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000211/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000212/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000213/// objc-interface-decl-list declaration
214/// objc-interface-decl-list ';'
215///
Steve Naroff99264b42007-08-22 16:35:03 +0000216/// objc-method-requirement: [OBJC2]
217/// @required
218/// @optional
219///
Chris Lattner83f095c2009-03-28 19:18:32 +0000220void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000221 tok::ObjCKeywordKind contextKey) {
Chris Lattner83f095c2009-03-28 19:18:32 +0000222 llvm::SmallVector<DeclPtrTy, 32> allMethods;
223 llvm::SmallVector<DeclPtrTy, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000224 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000225 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000226
Chris Lattnerda9fb152008-10-20 06:10:06 +0000227 SourceLocation AtEndLoc;
228
Steve Naroff99264b42007-08-22 16:35:03 +0000229 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000230 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000231 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
Mike Stump11289f42009-09-09 15:08:12 +0000232 DeclPtrTy methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000233 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000234 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000235 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
236 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000237 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
238 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000239 continue;
240 }
Mike Stump11289f42009-09-09 15:08:12 +0000241
Chris Lattner038a3e32008-10-20 05:46:22 +0000242 // Ignore excess semicolons.
243 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000244 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000245 continue;
246 }
Mike Stump11289f42009-09-09 15:08:12 +0000247
Chris Lattnerda9fb152008-10-20 06:10:06 +0000248 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000249 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000250 break;
Mike Stump11289f42009-09-09 15:08:12 +0000251
Chris Lattner038a3e32008-10-20 05:46:22 +0000252 // If we don't have an @ directive, parse it as a function definition.
253 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000254 // The code below does not consume '}'s because it is afraid of eating the
255 // end of a namespace. Because of the way this code is structured, an
256 // erroneous r_brace would cause an infinite loop if not handled here.
257 if (Tok.is(tok::r_brace))
258 break;
Mike Stump11289f42009-09-09 15:08:12 +0000259
Steve Narofff1bc45b2007-08-22 18:35:33 +0000260 // FIXME: as the name implies, this rule allows function definitions.
261 // We could pass a flag or check for functions during semantic analysis.
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000262 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
Chris Lattner038a3e32008-10-20 05:46:22 +0000263 continue;
264 }
Mike Stump11289f42009-09-09 15:08:12 +0000265
Chris Lattner038a3e32008-10-20 05:46:22 +0000266 // Otherwise, we have an @ directive, eat the @.
267 SourceLocation AtLoc = ConsumeToken(); // the "@"
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000268 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000269
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000270 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Chris Lattner038a3e32008-10-20 05:46:22 +0000271 AtEndLoc = AtLoc;
272 break;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000273 }
Mike Stump11289f42009-09-09 15:08:12 +0000274
Chris Lattnerda9fb152008-10-20 06:10:06 +0000275 // Eat the identifier.
276 ConsumeToken();
277
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000278 switch (DirectiveKind) {
279 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000280 // FIXME: If someone forgets an @end on a protocol, this loop will
281 // continue to eat up tons of stuff and spew lots of nonsense errors. It
282 // would probably be better to bail out if we saw an @class or @interface
283 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000284 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000285 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000286 SkipUntil(tok::r_brace, tok::at);
287 break;
Mike Stump11289f42009-09-09 15:08:12 +0000288
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000289 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000290 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000291 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000292 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000293 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000294 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000295 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000296 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000297 break;
Mike Stump11289f42009-09-09 15:08:12 +0000298
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000299 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000300 if (!getLang().ObjC2)
301 Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
302
Chris Lattner038a3e32008-10-20 05:46:22 +0000303 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000304 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000305 if (Tok.is(tok::l_paren))
Chris Lattner038a3e32008-10-20 05:46:22 +0000306 ParseObjCPropertyAttribute(OCDS);
Mike Stump11289f42009-09-09 15:08:12 +0000307
John McCallcfefb6d2009-11-03 02:38:08 +0000308 struct ObjCPropertyCallback : FieldCallback {
309 Parser &P;
310 DeclPtrTy IDecl;
311 llvm::SmallVectorImpl<DeclPtrTy> &Props;
312 ObjCDeclSpec &OCDS;
313 SourceLocation AtLoc;
314 tok::ObjCKeywordKind MethodImplKind;
315
316 ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
317 llvm::SmallVectorImpl<DeclPtrTy> &Props,
318 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
319 tok::ObjCKeywordKind MethodImplKind) :
320 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
321 MethodImplKind(MethodImplKind) {
322 }
323
324 DeclPtrTy invoke(FieldDeclarator &FD) {
325 if (FD.D.getIdentifier() == 0) {
326 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
327 << FD.D.getSourceRange();
328 return DeclPtrTy();
329 }
330 if (FD.BitfieldSize) {
331 P.Diag(AtLoc, diag::err_objc_property_bitfield)
332 << FD.D.getSourceRange();
333 return DeclPtrTy();
334 }
335
336 // Install the property declarator into interfaceDecl.
337 IdentifierInfo *SelName =
338 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
339
340 Selector GetterSel =
341 P.PP.getSelectorTable().getNullarySelector(SelName);
342 IdentifierInfo *SetterName = OCDS.getSetterName();
343 Selector SetterSel;
344 if (SetterName)
345 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
346 else
347 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
348 P.PP.getSelectorTable(),
349 FD.D.getIdentifier());
350 bool isOverridingProperty = false;
351 DeclPtrTy Property =
352 P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
353 GetterSel, SetterSel, IDecl,
354 &isOverridingProperty,
355 MethodImplKind);
356 if (!isOverridingProperty)
357 Props.push_back(Property);
358
359 return Property;
360 }
361 } Callback(*this, interfaceDecl, allProperties,
362 OCDS, AtLoc, MethodImplKind);
363
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 // Parse all the comma separated declarators.
365 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000366 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000367
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000368 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
369 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000370 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000371 }
Steve Naroff99264b42007-08-22 16:35:03 +0000372 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000373
374 // We break out of the big loop in two cases: when we see @end or when we see
375 // EOF. In the former case, eat the @end. In the later case, emit an error.
376 if (Tok.isObjCAtKeyword(tok::objc_end))
377 ConsumeToken(); // the "end" identifier
378 else
379 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000380
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000381 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000382 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Ted Kremenek09a0d042008-06-06 16:45:15 +0000383 Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000384 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000385 allProperties.data(), allProperties.size(),
386 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000387}
388
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000389/// Parse property attribute declarations.
390///
391/// property-attr-decl: '(' property-attrlist ')'
392/// property-attrlist:
393/// property-attribute
394/// property-attrlist ',' property-attribute
395/// property-attribute:
396/// getter '=' identifier
397/// setter '=' identifier ':'
398/// readonly
399/// readwrite
400/// assign
401/// retain
402/// copy
403/// nonatomic
404///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000405void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000406 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000407 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000408
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000409 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000410 if (Tok.is(tok::code_completion)) {
411 Actions.CodeCompleteObjCProperty(CurScope, DS);
412 ConsumeToken();
413 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000414 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattner76619232008-10-20 07:22:18 +0000416 // If this is not an identifier at all, bail out early.
417 if (II == 0) {
418 MatchRHSPunctuation(tok::r_paren, LHSLoc);
419 return;
420 }
Mike Stump11289f42009-09-09 15:08:12 +0000421
Chris Lattner1db33542008-10-20 07:37:22 +0000422 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000423
Chris Lattner68e48682008-11-20 04:42:34 +0000424 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000425 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000426 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000427 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000428 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000429 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000430 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000431 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000432 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000433 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000434 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000435 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000436 else if (II->isStr("getter") || II->isStr("setter")) {
Chris Lattner825bca12008-10-20 07:39:53 +0000437 // getter/setter require extra treatment.
Chris Lattner1db33542008-10-20 07:37:22 +0000438 if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
439 tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000440 return;
Mike Stump11289f42009-09-09 15:08:12 +0000441
Chris Lattnerbeca7702008-10-20 07:24:39 +0000442 if (Tok.isNot(tok::identifier)) {
Chris Lattner6d29c102008-11-18 07:48:38 +0000443 Diag(Tok, diag::err_expected_ident);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000444 SkipUntil(tok::r_paren);
445 return;
446 }
Mike Stump11289f42009-09-09 15:08:12 +0000447
Daniel Dunbar2c422dc92009-10-18 20:26:12 +0000448 if (II->getNameStart()[0] == 's') {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000449 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
450 DS.setSetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000451 ConsumeToken(); // consume method name
Mike Stump11289f42009-09-09 15:08:12 +0000452
Chris Lattner1db33542008-10-20 07:37:22 +0000453 if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
454 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000455 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000456 } else {
457 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
458 DS.setGetterName(Tok.getIdentifierInfo());
Chris Lattner1db33542008-10-20 07:37:22 +0000459 ConsumeToken(); // consume method name
Chris Lattnerbeca7702008-10-20 07:24:39 +0000460 }
Chris Lattner825bca12008-10-20 07:39:53 +0000461 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000462 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000463 SkipUntil(tok::r_paren);
464 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000465 }
Mike Stump11289f42009-09-09 15:08:12 +0000466
Chris Lattner1db33542008-10-20 07:37:22 +0000467 if (Tok.isNot(tok::comma))
468 break;
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chris Lattner1db33542008-10-20 07:37:22 +0000470 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000471 }
Mike Stump11289f42009-09-09 15:08:12 +0000472
Chris Lattner1db33542008-10-20 07:37:22 +0000473 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000474}
475
Steve Naroff09bf8152007-09-06 21:24:23 +0000476/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000477/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000478/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000479///
480/// objc-instance-method: '-'
481/// objc-class-method: '+'
482///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000483/// objc-method-attributes: [OBJC2]
484/// __attribute__((deprecated))
485///
Mike Stump11289f42009-09-09 15:08:12 +0000486Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
Chris Lattner83f095c2009-03-28 19:18:32 +0000487 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000488 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000489
Mike Stump11289f42009-09-09 15:08:12 +0000490 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000491 SourceLocation mLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000492
Chris Lattner83f095c2009-03-28 19:18:32 +0000493 DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000494 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000495 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000496 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000497}
498
499/// objc-selector:
500/// identifier
501/// one of
502/// enum struct union if else while do for switch case default
503/// break continue return goto asm sizeof typeof __alignof
504/// unsigned long const short volatile signed restrict _Complex
505/// in out inout bycopy byref oneway int char float double void _Bool
506///
Chris Lattner4f472a32009-04-11 18:13:45 +0000507IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000508 switch (Tok.getKind()) {
509 default:
510 return 0;
511 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000512 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000513 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000514 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000515 case tok::kw_break:
516 case tok::kw_case:
517 case tok::kw_catch:
518 case tok::kw_char:
519 case tok::kw_class:
520 case tok::kw_const:
521 case tok::kw_const_cast:
522 case tok::kw_continue:
523 case tok::kw_default:
524 case tok::kw_delete:
525 case tok::kw_do:
526 case tok::kw_double:
527 case tok::kw_dynamic_cast:
528 case tok::kw_else:
529 case tok::kw_enum:
530 case tok::kw_explicit:
531 case tok::kw_export:
532 case tok::kw_extern:
533 case tok::kw_false:
534 case tok::kw_float:
535 case tok::kw_for:
536 case tok::kw_friend:
537 case tok::kw_goto:
538 case tok::kw_if:
539 case tok::kw_inline:
540 case tok::kw_int:
541 case tok::kw_long:
542 case tok::kw_mutable:
543 case tok::kw_namespace:
544 case tok::kw_new:
545 case tok::kw_operator:
546 case tok::kw_private:
547 case tok::kw_protected:
548 case tok::kw_public:
549 case tok::kw_register:
550 case tok::kw_reinterpret_cast:
551 case tok::kw_restrict:
552 case tok::kw_return:
553 case tok::kw_short:
554 case tok::kw_signed:
555 case tok::kw_sizeof:
556 case tok::kw_static:
557 case tok::kw_static_cast:
558 case tok::kw_struct:
559 case tok::kw_switch:
560 case tok::kw_template:
561 case tok::kw_this:
562 case tok::kw_throw:
563 case tok::kw_true:
564 case tok::kw_try:
565 case tok::kw_typedef:
566 case tok::kw_typeid:
567 case tok::kw_typename:
568 case tok::kw_typeof:
569 case tok::kw_union:
570 case tok::kw_unsigned:
571 case tok::kw_using:
572 case tok::kw_virtual:
573 case tok::kw_void:
574 case tok::kw_volatile:
575 case tok::kw_wchar_t:
576 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000577 case tok::kw__Bool:
578 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000579 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000580 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000581 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000582 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000583 }
Steve Naroff99264b42007-08-22 16:35:03 +0000584}
585
Fariborz Jahanian83615522008-01-02 22:54:34 +0000586/// objc-for-collection-in: 'in'
587///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000588bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000589 // FIXME: May have to do additional look-ahead to only allow for
590 // valid tokens following an 'in'; such as an identifier, unary operators,
591 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000592 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000593 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000594}
595
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000596/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000597/// qualifier list and builds their bitmask representation in the input
598/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000599///
600/// objc-type-qualifiers:
601/// objc-type-qualifier
602/// objc-type-qualifiers objc-type-qualifier
603///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000604void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
Chris Lattner61511e12007-12-12 06:56:32 +0000605 while (1) {
Chris Lattner5e530bc2007-12-27 19:57:00 +0000606 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000607 return;
Mike Stump11289f42009-09-09 15:08:12 +0000608
Chris Lattner61511e12007-12-12 06:56:32 +0000609 const IdentifierInfo *II = Tok.getIdentifierInfo();
610 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000611 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000612 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000613
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000614 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000615 switch (i) {
616 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000617 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
618 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
619 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
620 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
621 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
622 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000623 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000624 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000625 ConsumeToken();
626 II = 0;
627 break;
628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
Chris Lattner61511e12007-12-12 06:56:32 +0000630 // If this wasn't a recognized qualifier, bail out.
631 if (II) return;
632 }
633}
634
635/// objc-type-name:
636/// '(' objc-type-qualifiers[opt] type-name ')'
637/// '(' objc-type-qualifiers[opt] ')'
638///
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000639Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000640 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattnerb7954432008-10-22 03:52:06 +0000642 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000643 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000644
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000645 // Parse type qualifiers, in, inout, etc.
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000646 ParseObjCTypeQualifierList(DS);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000647
Chris Lattnerb7954432008-10-22 03:52:06 +0000648 TypeTy *Ty = 0;
Douglas Gregor220cac52009-02-18 17:45:20 +0000649 if (isTypeSpecifierQualifier()) {
650 TypeResult TypeSpec = ParseTypeName();
651 if (!TypeSpec.isInvalid())
652 Ty = TypeSpec.get();
653 }
Mike Stump11289f42009-09-09 15:08:12 +0000654
Steve Naroff90255b42008-10-21 14:15:04 +0000655 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000656 ConsumeParen();
657 else if (Tok.getLocation() == TypeStartLoc) {
658 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000659 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000660 SkipUntil(tok::r_paren);
661 } else {
662 // Otherwise, we found *something*, but didn't get a ')' in the right
663 // place. Emit an error then return what we have as the type.
664 MatchRHSPunctuation(tok::r_paren, LParenLoc);
665 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000666 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000667}
668
669/// objc-method-decl:
670/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000671/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000672/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000673/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000674///
675/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000676/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000677/// objc-keyword-selector objc-keyword-decl
678///
679/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000680/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
681/// objc-selector ':' objc-keyword-attributes[opt] identifier
682/// ':' objc-type-name objc-keyword-attributes[opt] identifier
683/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000684///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000685/// objc-parmlist:
686/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000687///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000688/// objc-parms:
689/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000690///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000691/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000692/// , ...
693///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000694/// objc-keyword-attributes: [OBJC2]
695/// __attribute__((unused))
696///
Chris Lattner83f095c2009-03-28 19:18:32 +0000697Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
698 tok::TokenKind mType,
699 DeclPtrTy IDecl,
700 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000701 ParsingDeclRAIIObject PD(*this);
702
Chris Lattner2ebb1782008-08-23 01:48:03 +0000703 // Parse the return type if present.
Chris Lattner5700fab2007-10-07 02:00:24 +0000704 TypeTy *ReturnType = 0;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000705 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000706 if (Tok.is(tok::l_paren))
Fariborz Jahanian7a9c4742007-10-31 23:53:01 +0000707 ReturnType = ParseObjCTypeName(DSRet);
Mike Stump11289f42009-09-09 15:08:12 +0000708
Steve Naroff161a92b2007-10-26 20:53:56 +0000709 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000710 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000711
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000712 // An unnamed colon is valid.
713 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000714 Diag(Tok, diag::err_expected_selector_for_method)
715 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000716 // Skip until we get a ; or {}.
717 SkipUntil(tok::r_brace);
Chris Lattner83f095c2009-03-28 19:18:32 +0000718 return DeclPtrTy();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000719 }
Mike Stump11289f42009-09-09 15:08:12 +0000720
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000721 llvm::SmallVector<Declarator, 8> CargNames;
Chris Lattner0ef13522007-10-09 17:51:17 +0000722 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000723 // If attributes exist after the method, parse them.
724 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000725 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000726 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000727
Chris Lattner5700fab2007-10-07 02:00:24 +0000728 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall28a6aea2009-11-04 02:18:39 +0000729 DeclPtrTy Result
730 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000731 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000732 0, CargNames, MethodAttrs,
733 MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000734 PD.complete(Result);
735 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000736 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000737
Steve Narofff73590d2007-09-27 14:38:14 +0000738 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000739 llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
Mike Stump11289f42009-09-09 15:08:12 +0000740
Chris Lattner5700fab2007-10-07 02:00:24 +0000741 while (1) {
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000742 Action::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000743
Chris Lattner5700fab2007-10-07 02:00:24 +0000744 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000745 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000746 Diag(Tok, diag::err_expected_colon);
747 break;
748 }
749 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000750
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000751 ArgInfo.Type = 0;
752 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
753 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
754
Chris Lattner5700fab2007-10-07 02:00:24 +0000755 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000756 ArgInfo.ArgAttrs = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +0000757 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000758 ArgInfo.ArgAttrs = ParseAttributes();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000759
Chris Lattner0ef13522007-10-09 17:51:17 +0000760 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000761 Diag(Tok, diag::err_expected_ident); // missing argument name.
762 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000763 }
Mike Stump11289f42009-09-09 15:08:12 +0000764
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000765 ArgInfo.Name = Tok.getIdentifierInfo();
766 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000767 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000769 ArgInfos.push_back(ArgInfo);
770 KeyIdents.push_back(SelIdent);
771
Chris Lattner5700fab2007-10-07 02:00:24 +0000772 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000773 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000774 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000775 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000776 break;
777 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000778 }
Mike Stump11289f42009-09-09 15:08:12 +0000779
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000780 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000781
Chris Lattner5700fab2007-10-07 02:00:24 +0000782 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000783 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000784 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000785 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000786 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000787 ConsumeToken();
788 break;
789 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000790 DeclSpec DS;
791 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000792 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000793 Declarator ParmDecl(DS, Declarator::PrototypeContext);
794 ParseDeclarator(ParmDecl);
Fariborz Jahaniane84858c2009-01-09 00:38:19 +0000795 CargNames.push_back(ParmDecl);
Chris Lattner5700fab2007-10-07 02:00:24 +0000796 }
Mike Stump11289f42009-09-09 15:08:12 +0000797
Chris Lattner5700fab2007-10-07 02:00:24 +0000798 // FIXME: Add support for optional parmameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000799 // If attributes exist after the method, parse them.
Chris Lattner5700fab2007-10-07 02:00:24 +0000800 AttributeList *MethodAttrs = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000801 if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
Chris Lattner5700fab2007-10-07 02:00:24 +0000802 MethodAttrs = ParseAttributes();
Mike Stump11289f42009-09-09 15:08:12 +0000803
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000804 if (KeyIdents.size() == 0)
805 return DeclPtrTy();
Chris Lattner5700fab2007-10-07 02:00:24 +0000806 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
807 &KeyIdents[0]);
John McCall28a6aea2009-11-04 02:18:39 +0000808 DeclPtrTy Result
809 = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000810 mType, IDecl, DSRet, ReturnType, Sel,
Ted Kremenekbc76c722009-05-04 17:04:30 +0000811 &ArgInfos[0], CargNames, MethodAttrs,
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000812 MethodImplKind, isVariadic);
John McCall28a6aea2009-11-04 02:18:39 +0000813 PD.complete(Result);
814 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +0000815}
816
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000817/// objc-protocol-refs:
818/// '<' identifier-list '>'
819///
Chris Lattnerd7352d62008-07-21 22:17:28 +0000820bool Parser::
Chris Lattner83f095c2009-03-28 19:18:32 +0000821ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000822 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
823 bool WarnOnDeclarations,
824 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +0000825 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +0000826
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000827 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +0000828
Chris Lattner3bbae002008-07-26 04:03:38 +0000829 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +0000830
Chris Lattner3bbae002008-07-26 04:03:38 +0000831 while (1) {
832 if (Tok.isNot(tok::identifier)) {
833 Diag(Tok, diag::err_expected_ident);
834 SkipUntil(tok::greater);
835 return true;
836 }
837 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
838 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000839 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +0000840 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattner3bbae002008-07-26 04:03:38 +0000842 if (Tok.isNot(tok::comma))
843 break;
844 ConsumeToken();
845 }
Mike Stump11289f42009-09-09 15:08:12 +0000846
Chris Lattner3bbae002008-07-26 04:03:38 +0000847 // Consume the '>'.
848 if (Tok.isNot(tok::greater)) {
849 Diag(Tok, diag::err_expected_greater);
850 return true;
851 }
Mike Stump11289f42009-09-09 15:08:12 +0000852
Chris Lattner3bbae002008-07-26 04:03:38 +0000853 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +0000854
Chris Lattner3bbae002008-07-26 04:03:38 +0000855 // Convert the list of protocols identifiers into a list of protocol decls.
856 Actions.FindProtocolDeclaration(WarnOnDeclarations,
857 &ProtocolIdents[0], ProtocolIdents.size(),
858 Protocols);
859 return false;
860}
861
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000862/// objc-class-instance-variables:
863/// '{' objc-instance-variable-decl-list[opt] '}'
864///
865/// objc-instance-variable-decl-list:
866/// objc-visibility-spec
867/// objc-instance-variable-decl ';'
868/// ';'
869/// objc-instance-variable-decl-list objc-visibility-spec
870/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
871/// objc-instance-variable-decl-list ';'
872///
873/// objc-visibility-spec:
874/// @private
875/// @protected
876/// @public
Steve Naroff00433d32007-08-21 21:17:12 +0000877/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000878///
879/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +0000880/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000881///
Chris Lattner83f095c2009-03-28 19:18:32 +0000882void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
Steve Naroff33a1e802007-10-29 21:38:07 +0000883 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000884 assert(Tok.is(tok::l_brace) && "expected {");
Chris Lattner83f095c2009-03-28 19:18:32 +0000885 llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +0000886
Douglas Gregor45a33ec2009-01-12 18:45:55 +0000887 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +0000888
Steve Naroff00433d32007-08-21 21:17:12 +0000889 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +0000890
Fariborz Jahanian2dfcec12008-04-29 23:03:51 +0000891 tok::ObjCKeywordKind visibility = tok::objc_protected;
Steve Naroff00433d32007-08-21 21:17:12 +0000892 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +0000893 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000894 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +0000895
Steve Naroff00433d32007-08-21 21:17:12 +0000896 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +0000897 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000898 Diag(Tok, diag::ext_extra_struct_semi);
899 ConsumeToken();
900 continue;
901 }
Mike Stump11289f42009-09-09 15:08:12 +0000902
Steve Naroff00433d32007-08-21 21:17:12 +0000903 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +0000904 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +0000905 ConsumeToken(); // eat the @ sign
Steve Naroff7c348172007-08-23 18:16:40 +0000906 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +0000907 case tok::objc_private:
908 case tok::objc_public:
909 case tok::objc_protected:
910 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +0000911 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +0000912 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000913 continue;
Steve Naroff00433d32007-08-21 21:17:12 +0000914 default:
915 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +0000916 continue;
917 }
918 }
Mike Stump11289f42009-09-09 15:08:12 +0000919
John McCallcfefb6d2009-11-03 02:38:08 +0000920 struct ObjCIvarCallback : FieldCallback {
921 Parser &P;
922 DeclPtrTy IDecl;
923 tok::ObjCKeywordKind visibility;
924 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
925
926 ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
927 llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
928 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
929 }
930
931 DeclPtrTy invoke(FieldDeclarator &FD) {
932 // Install the declarator into the interface decl.
933 DeclPtrTy Field
934 = P.Actions.ActOnIvar(P.CurScope,
935 FD.D.getDeclSpec().getSourceRange().getBegin(),
936 IDecl, FD.D, FD.BitfieldSize, visibility);
937 AllIvarDecls.push_back(Field);
938 return Field;
939 }
940 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
941
Chris Lattnera12405b2008-04-10 06:46:29 +0000942 // Parse all the comma separated declarators.
943 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000944 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000945
Chris Lattner0ef13522007-10-09 17:51:17 +0000946 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +0000947 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +0000948 } else {
949 Diag(Tok, diag::err_expected_semi_decl_list);
950 // Skip to end of block or statement
951 SkipUntil(tok::r_brace, true, true);
952 }
953 }
Steve Naroff33a1e802007-10-29 21:38:07 +0000954 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Steve Naroff9c4fddd2007-10-31 22:11:35 +0000955 // Call ActOnFields() even if we don't have any decls. This is useful
956 // for code rewriting tools that need to be aware of the empty list.
957 Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +0000958 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +0000959 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +0000960 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000961}
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000962
963/// objc-protocol-declaration:
964/// objc-protocol-definition
965/// objc-protocol-forward-reference
966///
967/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +0000968/// @protocol identifier
969/// objc-protocol-refs[opt]
970/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000971/// @end
972///
973/// objc-protocol-forward-reference:
974/// @protocol identifier-list ';'
975///
976/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +0000977/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000978/// semicolon in the first alternative if objc-protocol-refs are omitted.
Chris Lattner83f095c2009-03-28 19:18:32 +0000979Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
980 AttributeList *attrList) {
Steve Naroff7c348172007-08-23 18:16:40 +0000981 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000982 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
983 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattner0ef13522007-10-09 17:51:17 +0000985 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000986 Diag(Tok, diag::err_expected_ident); // missing protocol name.
Chris Lattner83f095c2009-03-28 19:18:32 +0000987 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000988 }
989 // Save the protocol name, then consume it.
990 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
991 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000992
Chris Lattner0ef13522007-10-09 17:51:17 +0000993 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +0000994 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000995 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000996 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000997 attrList);
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Chris Lattner0ef13522007-10-09 17:51:17 +00001000 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001001 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1002 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1003
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001004 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001005 while (1) {
1006 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001007 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001008 Diag(Tok, diag::err_expected_ident);
1009 SkipUntil(tok::semi);
Chris Lattner83f095c2009-03-28 19:18:32 +00001010 return DeclPtrTy();
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001011 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001012 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1013 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001014 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001015
Chris Lattner0ef13522007-10-09 17:51:17 +00001016 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001017 break;
1018 }
1019 // Consume the ';'.
1020 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
Chris Lattner83f095c2009-03-28 19:18:32 +00001021 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001022
Steve Naroff93eb5f12007-10-10 17:32:04 +00001023 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001024 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001025 ProtocolRefs.size(),
1026 attrList);
Chris Lattnerd7352d62008-07-21 22:17:28 +00001027 }
Mike Stump11289f42009-09-09 15:08:12 +00001028
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001029 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001030 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001031
Chris Lattner83f095c2009-03-28 19:18:32 +00001032 llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001033 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001034 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001035 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1036 LAngleLoc, EndProtoLoc))
Chris Lattner83f095c2009-03-28 19:18:32 +00001037 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001038
Chris Lattner83f095c2009-03-28 19:18:32 +00001039 DeclPtrTy ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001040 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001041 ProtocolRefs.data(),
1042 ProtocolRefs.size(),
Daniel Dunbar26e2ab42008-09-26 04:48:09 +00001043 EndProtoLoc, attrList);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001044 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001045 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001046}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001047
1048/// objc-implementation:
1049/// objc-class-implementation-prologue
1050/// objc-category-implementation-prologue
1051///
1052/// objc-class-implementation-prologue:
1053/// @implementation identifier objc-superclass[opt]
1054/// objc-class-instance-variables[opt]
1055///
1056/// objc-category-implementation-prologue:
1057/// @implementation identifier ( identifier )
Chris Lattner83f095c2009-03-28 19:18:32 +00001058Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001059 SourceLocation atLoc) {
1060 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1061 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1062 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001063
Chris Lattner0ef13522007-10-09 17:51:17 +00001064 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001065 Diag(Tok, diag::err_expected_ident); // missing class or category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001066 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001067 }
1068 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001069 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001070 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001071
1072 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001073 // we have a category implementation.
1074 SourceLocation lparenLoc = ConsumeParen();
1075 SourceLocation categoryLoc, rparenLoc;
1076 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattner0ef13522007-10-09 17:51:17 +00001078 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001079 categoryId = Tok.getIdentifierInfo();
1080 categoryLoc = ConsumeToken();
1081 } else {
1082 Diag(Tok, diag::err_expected_ident); // missing category name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001083 return DeclPtrTy();
Mike Stump11289f42009-09-09 15:08:12 +00001084 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001085 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001086 Diag(Tok, diag::err_expected_rparen);
1087 SkipUntil(tok::r_paren, false); // don't stop at ';'
Chris Lattner83f095c2009-03-28 19:18:32 +00001088 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001089 }
1090 rparenLoc = ConsumeParen();
Chris Lattner83f095c2009-03-28 19:18:32 +00001091 DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001092 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001093 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001094 ObjCImpDecl = ImplCatType;
Chris Lattner83f095c2009-03-28 19:18:32 +00001095 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001096 }
1097 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001098 SourceLocation superClassLoc;
1099 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001100 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001101 // We have a super class
1102 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001103 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001104 Diag(Tok, diag::err_expected_ident); // missing super class name.
Chris Lattner83f095c2009-03-28 19:18:32 +00001105 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001106 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001107 superClassId = Tok.getIdentifierInfo();
1108 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001109 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001110 DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001111 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001112 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001113
Steve Naroff33a1e802007-10-29 21:38:07 +00001114 if (Tok.is(tok::l_brace)) // we have ivars
1115 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001116 ObjCImpDecl = ImplClsType;
Mike Stump11289f42009-09-09 15:08:12 +00001117
Chris Lattner83f095c2009-03-28 19:18:32 +00001118 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001119}
Steve Naroff33a1e802007-10-29 21:38:07 +00001120
Chris Lattner83f095c2009-03-28 19:18:32 +00001121Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001122 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1123 "ParseObjCAtEndDeclaration(): Expected @end");
Chris Lattner83f095c2009-03-28 19:18:32 +00001124 DeclPtrTy Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001125 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001126 if (ObjCImpDecl) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001127 Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
Chris Lattner83f095c2009-03-28 19:18:32 +00001128 ObjCImpDecl = DeclPtrTy();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001129 }
Fariborz Jahanianc644ee42008-01-10 17:58:07 +00001130 else
1131 Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001132 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001133}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001134
1135/// compatibility-alias-decl:
1136/// @compatibility_alias alias-name class-name ';'
1137///
Chris Lattner83f095c2009-03-28 19:18:32 +00001138Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001139 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1140 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1141 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001142 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001143 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001144 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001145 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001146 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1147 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001148 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001149 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001150 return DeclPtrTy();
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001151 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001152 IdentifierInfo *classId = Tok.getIdentifierInfo();
1153 SourceLocation classLoc = ConsumeToken(); // consume class-name;
1154 if (Tok.isNot(tok::semi)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001155 Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
Chris Lattner83f095c2009-03-28 19:18:32 +00001156 return DeclPtrTy();
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001157 }
Chris Lattner83f095c2009-03-28 19:18:32 +00001158 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1159 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001160}
1161
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001162/// property-synthesis:
1163/// @synthesize property-ivar-list ';'
1164///
1165/// property-ivar-list:
1166/// property-ivar
1167/// property-ivar-list ',' property-ivar
1168///
1169/// property-ivar:
1170/// identifier
1171/// identifier '=' identifier
1172///
Chris Lattner83f095c2009-03-28 19:18:32 +00001173Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001174 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1175 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001176 SourceLocation loc = ConsumeToken(); // consume synthesize
Chris Lattner0ef13522007-10-09 17:51:17 +00001177 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001178 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001179 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001180 }
Mike Stump11289f42009-09-09 15:08:12 +00001181
Chris Lattner0ef13522007-10-09 17:51:17 +00001182 while (Tok.is(tok::identifier)) {
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001183 IdentifierInfo *propertyIvar = 0;
1184 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1185 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Chris Lattner0ef13522007-10-09 17:51:17 +00001186 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001187 // property '=' ivar-name
1188 ConsumeToken(); // consume '='
Chris Lattner0ef13522007-10-09 17:51:17 +00001189 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001190 Diag(Tok, diag::err_expected_ident);
1191 break;
1192 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001193 propertyIvar = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001194 ConsumeToken(); // consume ivar-name
1195 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001196 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1197 propertyId, propertyIvar);
Chris Lattner0ef13522007-10-09 17:51:17 +00001198 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001199 break;
1200 ConsumeToken(); // consume ','
1201 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001202 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001203 Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
Chris Lattner83f095c2009-03-28 19:18:32 +00001204 return DeclPtrTy();
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001205}
1206
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001207/// property-dynamic:
1208/// @dynamic property-list
1209///
1210/// property-list:
1211/// identifier
1212/// property-list ',' identifier
1213///
Chris Lattner83f095c2009-03-28 19:18:32 +00001214Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001215 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1216 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1217 SourceLocation loc = ConsumeToken(); // consume dynamic
Chris Lattner0ef13522007-10-09 17:51:17 +00001218 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001219 Diag(Tok, diag::err_expected_ident);
Chris Lattner83f095c2009-03-28 19:18:32 +00001220 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001221 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001222 while (Tok.is(tok::identifier)) {
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001223 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1224 SourceLocation propertyLoc = ConsumeToken(); // consume property name
1225 Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1226 propertyId, 0);
1227
Chris Lattner0ef13522007-10-09 17:51:17 +00001228 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001229 break;
1230 ConsumeToken(); // consume ','
1231 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001232 if (Tok.isNot(tok::semi))
Chris Lattner6d29c102008-11-18 07:48:38 +00001233 Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
Chris Lattner83f095c2009-03-28 19:18:32 +00001234 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001235}
Mike Stump11289f42009-09-09 15:08:12 +00001236
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001237/// objc-throw-statement:
1238/// throw expression[opt];
1239///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001240Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
Sebastian Redlc13f2682008-12-09 20:22:58 +00001241 OwningExprResult Res(Actions);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001242 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001243 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001244 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001245 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001246 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001247 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001248 }
1249 }
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001250 ConsumeToken(); // consume ';'
Steve Naroff5ee2c022009-02-11 20:05:44 +00001251 return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001252}
1253
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001254/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001255/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001256///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001257Parser::OwningStmtResult
1258Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001259 ConsumeToken(); // consume synchronized
1260 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001261 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001262 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001263 }
1264 ConsumeParen(); // '('
Sebastian Redl59b5e512008-12-11 21:36:32 +00001265 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001266 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001267 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001268 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001269 }
1270 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001271 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001272 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001273 }
1274 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001275 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001276 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001277 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001278 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001279 // Enter a scope to hold everything within the compound stmt. Compound
1280 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001281 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001282
Sebastian Redl042ad952008-12-11 19:30:53 +00001283 OwningStmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001284
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001285 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001286 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001287 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl726a0d92009-02-05 15:02:23 +00001288 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001289}
1290
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001291/// objc-try-catch-statement:
1292/// @try compound-statement objc-catch-list[opt]
1293/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1294///
1295/// objc-catch-list:
1296/// @catch ( parameter-declaration ) compound-statement
1297/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1298/// catch-parameter-declaration:
1299/// parameter-declaration
1300/// '...' [OBJC2]
1301///
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001302Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001303 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001304
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001305 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001306 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001307 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001308 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001309 }
Sebastian Redlc13f2682008-12-09 20:22:58 +00001310 OwningStmtResult CatchStmts(Actions);
1311 OwningStmtResult FinallyStmt(Actions);
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001312 ParseScope TryScope(this, Scope::DeclScope);
Sebastian Redl042ad952008-12-11 19:30:53 +00001313 OwningStmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001314 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001315 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001316 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001317
Chris Lattner0ef13522007-10-09 17:51:17 +00001318 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001319 // At this point, we need to lookahead to determine if this @ is the start
1320 // of an @catch or @finally. We don't want to consume the @ token if this
1321 // is an @try or @encode or something else.
1322 Token AfterAt = GetLookAheadToken(1);
1323 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1324 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1325 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001326
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001327 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001328 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
Chris Lattner83f095c2009-03-28 19:18:32 +00001329 DeclPtrTy FirstPart;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001330 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001331 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001332 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001333 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001334 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001335 DeclSpec DS;
1336 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001337 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001338 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001339 // we must accept either 'declarator' or 'abstract-declarator' here.
1340 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1341 ParseDeclarator(ParmDecl);
1342
1343 // Inform the actions module about the parameter declarator, so it
1344 // gets added to the current scope.
1345 FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001346 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001347 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001348
Steve Naroff65a00892009-04-07 22:56:58 +00001349 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001350
Steve Naroff65a00892009-04-07 22:56:58 +00001351 if (Tok.is(tok::r_paren))
1352 RParenLoc = ConsumeParen();
1353 else // Skip over garbage, until we get to ')'. Eat the ')'.
1354 SkipUntil(tok::r_paren, true, false);
1355
Sebastian Redlc13f2682008-12-09 20:22:58 +00001356 OwningStmtResult CatchBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001357 if (Tok.is(tok::l_brace))
1358 CatchBody = ParseCompoundStatementBody();
1359 else
1360 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001361 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001362 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001363 CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Steve Naroff371b8fb2009-03-03 19:52:17 +00001364 RParenLoc, FirstPart, move(CatchBody),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001365 move(CatchStmts));
Steve Naroffe6016792008-02-05 21:27:35 +00001366 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001367 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1368 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001369 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001370 }
1371 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001372 } else {
1373 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001374 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001375 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001376
Sebastian Redlc13f2682008-12-09 20:22:58 +00001377 OwningStmtResult FinallyBody(Actions, true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001378 if (Tok.is(tok::l_brace))
1379 FinallyBody = ParseCompoundStatementBody();
1380 else
1381 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001382 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001383 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001384 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
Sebastian Redl726a0d92009-02-05 15:02:23 +00001385 move(FinallyBody));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001386 catch_or_finally_seen = true;
1387 break;
1388 }
1389 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001390 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001391 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001392 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001393 }
Sebastian Redl726a0d92009-02-05 15:02:23 +00001394 return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
1395 move(FinallyStmt));
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001396}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001397
Steve Naroff09bf8152007-09-06 21:24:23 +00001398/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001399///
Chris Lattner83f095c2009-03-28 19:18:32 +00001400Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1401 DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001402
Chris Lattnereae6cb62009-03-05 08:00:35 +00001403 PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1404 PP.getSourceManager(),
1405 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001406
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001407 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001408 if (Tok.is(tok::semi)) {
1409 if (ObjCImpDecl)
1410 Diag(Tok, diag::warn_semicolon_before_method_nody);
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001411 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001412 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001413
Steve Naroffbb875722007-11-11 19:54:21 +00001414 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001415 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001416 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001417
Steve Naroffbb875722007-11-11 19:54:21 +00001418 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1419 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001420
Steve Naroffbb875722007-11-11 19:54:21 +00001421 // If we didn't find the '{', bail out.
1422 if (Tok.isNot(tok::l_brace))
Chris Lattner83f095c2009-03-28 19:18:32 +00001423 return DeclPtrTy();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001424 }
Steve Naroffbb875722007-11-11 19:54:21 +00001425 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001426
Steve Naroffbb875722007-11-11 19:54:21 +00001427 // Enter a scope for the method body.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001428 ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001429
Steve Naroffbb875722007-11-11 19:54:21 +00001430 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001431 // specified Declarator for the method.
Steve Naroff70f41d62009-02-28 16:59:13 +00001432 Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001433
1434 OwningStmtResult FnBody(ParseCompoundStatementBody());
1435
Steve Naroffbb875722007-11-11 19:54:21 +00001436 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001437 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001438 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1439 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001440
Steve Naroffb94d7f62009-03-02 22:00:56 +00001441 // TODO: Pass argument information.
1442 Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
Mike Stump11289f42009-09-09 15:08:12 +00001443
Steve Naroffbb875722007-11-11 19:54:21 +00001444 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001445 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001446
Steve Naroff7b8fa472007-11-13 23:01:27 +00001447 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001448}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001449
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001450Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Steve Naroffe6016792008-02-05 21:27:35 +00001451 if (Tok.isObjCAtKeyword(tok::objc_try)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001452 return ParseObjCTryStmt(AtLoc);
Steve Naroffe6016792008-02-05 21:27:35 +00001453 } else if (Tok.isObjCAtKeyword(tok::objc_throw))
1454 return ParseObjCThrowStmt(AtLoc);
1455 else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1456 return ParseObjCSynchronizedStmt(AtLoc);
Sebastian Redl90893182008-12-11 22:33:27 +00001457 OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001458 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001459 // If the expression is invalid, skip ahead to the next semicolon. Not
1460 // doing this opens us up to the possibility of infinite loops if
1461 // ParseExpression does not consume any tokens.
1462 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001463 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001464 }
1465 // Otherwise, eat the semicolon.
1466 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
Anders Carlsson24824e52009-05-17 21:11:30 +00001467 return Actions.ActOnExprStmt(Actions.FullExpr(Res));
Steve Naroffe6016792008-02-05 21:27:35 +00001468}
1469
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001470Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001471 switch (Tok.getKind()) {
Chris Lattnere002fbe2007-12-12 01:04:12 +00001472 case tok::string_literal: // primary-expression: string-literal
1473 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001474 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001475 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001476 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001477 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001478
Chris Lattner197a3012008-08-05 06:19:09 +00001479 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1480 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001481 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001482 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001483 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001484 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001485 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001486 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001487 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001488 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001489 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001490}
1491
Mike Stump11289f42009-09-09 15:08:12 +00001492/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001493/// '[' objc-receiver objc-message-args ']'
1494///
1495/// objc-receiver:
1496/// expression
1497/// class-name
1498/// type-name
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001499Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001500 assert(Tok.is(tok::l_square) && "'[' expected");
1501 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1502
1503 // Parse receiver
Chris Lattnere10c6da2008-01-25 19:25:00 +00001504 if (isTokObjCMessageIdentifierReceiver()) {
Chris Lattner8f697062008-01-25 18:59:06 +00001505 IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00001506 if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1507 SourceLocation NameLoc = ConsumeToken();
1508 return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1509 ExprArg(Actions));
1510 }
Chris Lattner8f697062008-01-25 18:59:06 +00001511 }
1512
Sebastian Redl59b5e512008-12-11 21:36:32 +00001513 OwningExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001514 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00001515 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001516 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00001517 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001518
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001519 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
Sebastian Redl726a0d92009-02-05 15:02:23 +00001520 0, move(Res));
Chris Lattner8f697062008-01-25 18:59:06 +00001521}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001522
Chris Lattner8f697062008-01-25 18:59:06 +00001523/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1524/// the rest of a message expression.
Mike Stump11289f42009-09-09 15:08:12 +00001525///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001526/// objc-message-args:
1527/// objc-selector
1528/// objc-keywordarg-list
1529///
1530/// objc-keywordarg-list:
1531/// objc-keywordarg
1532/// objc-keywordarg-list objc-keywordarg
1533///
Mike Stump11289f42009-09-09 15:08:12 +00001534/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001535/// selector-name[opt] ':' objc-keywordexpr
1536///
1537/// objc-keywordexpr:
1538/// nonempty-expr-list
1539///
1540/// nonempty-expr-list:
1541/// assignment-expression
1542/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001543///
1544Parser::OwningExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00001545Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Steve Naroff9e4ac112008-11-19 15:54:23 +00001546 SourceLocation NameLoc,
Chris Lattner8f697062008-01-25 18:59:06 +00001547 IdentifierInfo *ReceiverName,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001548 ExprArg ReceiverExpr) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001549 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00001550 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001551 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00001552
Anders Carlsson978f08d2009-02-14 18:21:46 +00001553 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Steve Narofff73590d2007-09-27 14:38:14 +00001555 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00001556 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00001557
Chris Lattner0ef13522007-10-09 17:51:17 +00001558 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001559 while (1) {
1560 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00001561 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00001562
Chris Lattner0ef13522007-10-09 17:51:17 +00001563 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001564 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00001565 // We must manually skip to a ']', otherwise the expression skipper will
1566 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1567 // the enclosing expression.
1568 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001569 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001570 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001571
Steve Narofff73590d2007-09-27 14:38:14 +00001572 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00001573 /// Parse the expression after ':'
Sebastian Redl59b5e512008-12-11 21:36:32 +00001574 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001575 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001576 // We must manually skip to a ']', otherwise the expression skipper will
1577 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1578 // the enclosing expression.
1579 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001580 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00001581 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001582
Steve Naroff486760a2007-09-17 20:25:27 +00001583 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001584 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001585
Steve Naroff486760a2007-09-17 20:25:27 +00001586 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00001587 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001588 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001589 break;
1590 // We have a selector or a colon, continue parsing.
1591 }
1592 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00001593 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001594 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00001595 /// Parse the expression after ','
Sebastian Redl59b5e512008-12-11 21:36:32 +00001596 OwningExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001597 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00001598 // We must manually skip to a ']', otherwise the expression skipper will
1599 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1600 // the enclosing expression.
1601 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001602 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001603 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001604
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00001605 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001606 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001607 }
1608 } else if (!selIdent) {
1609 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001610
Chris Lattner197a3012008-08-05 06:19:09 +00001611 // We must manually skip to a ']', otherwise the expression skipper will
1612 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1613 // the enclosing expression.
1614 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001615 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001616 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001617
Chris Lattner0ef13522007-10-09 17:51:17 +00001618 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001619 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00001620 // We must manually skip to a ']', otherwise the expression skipper will
1621 // stop at the ']' when it skips to the ';'. We want it to skip beyond
1622 // the enclosing expression.
1623 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001624 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00001625 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001626
Chris Lattner8f697062008-01-25 18:59:06 +00001627 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001628
Steve Naroffe61bfa82007-10-05 18:42:47 +00001629 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00001630 if (nKeys == 0)
1631 KeyIdents.push_back(selIdent);
1632 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001633
Chris Lattner5700fab2007-10-07 02:00:24 +00001634 // We've just parsed a keyword message.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001635 if (ReceiverName)
1636 return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
Mike Stump11289f42009-09-09 15:08:12 +00001637 LBracLoc, NameLoc, SelectorLoc,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001638 RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001639 KeyExprs.take(), KeyExprs.size()));
1640 return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
Anders Carlsson978f08d2009-02-14 18:21:46 +00001641 LBracLoc, SelectorLoc, RBracLoc,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001642 KeyExprs.take(), KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001643}
1644
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001645Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
Sebastian Redld65cea82008-12-11 22:51:44 +00001646 OwningExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001647 if (Res.isInvalid()) return move(Res);
1648
Chris Lattnere002fbe2007-12-12 01:04:12 +00001649 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
1650 // expressions. At this point, we know that the only valid thing that starts
1651 // with '@' is an @"".
1652 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00001653 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00001654 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001655 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001656
Chris Lattnere002fbe2007-12-12 01:04:12 +00001657 while (Tok.is(tok::at)) {
1658 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00001659
Sebastian Redlc13f2682008-12-09 20:22:58 +00001660 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001661 if (!isTokenStringLiteral())
1662 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001663
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00001664 OwningExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001665 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001666 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001667
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00001668 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00001669 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001670
1671 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
1672 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00001673}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001674
1675/// objc-encode-expression:
1676/// @encode ( type-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001677Parser::OwningExprResult
1678Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00001679 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001680
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001681 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001682
Chris Lattner197a3012008-08-05 06:19:09 +00001683 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001684 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
1685
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001686 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001687
Douglas Gregor220cac52009-02-18 17:45:20 +00001688 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001689
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001690 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001691
Douglas Gregor220cac52009-02-18 17:45:20 +00001692 if (Ty.isInvalid())
1693 return ExprError();
1694
Mike Stump11289f42009-09-09 15:08:12 +00001695 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00001696 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00001697}
Anders Carlssone01493d2007-08-23 15:25:28 +00001698
1699/// objc-protocol-expression
1700/// @protocol ( protocol-name )
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001701Parser::OwningExprResult
1702Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00001703 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001704
Chris Lattner197a3012008-08-05 06:19:09 +00001705 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001706 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
1707
Anders Carlssone01493d2007-08-23 15:25:28 +00001708 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001709
Chris Lattner197a3012008-08-05 06:19:09 +00001710 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001711 return ExprError(Diag(Tok, diag::err_expected_ident));
1712
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001713 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00001714 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001715
Anders Carlssoncb8f8322007-08-23 15:31:37 +00001716 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00001717
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001718 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1719 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00001720}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001721
1722/// objc-selector-expression
1723/// @selector '(' objc-keyword-selector ')'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001724Parser::OwningExprResult
1725Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001726 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001727
Chris Lattner197a3012008-08-05 06:19:09 +00001728 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001729 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
1730
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001731 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001732 SourceLocation LParenLoc = ConsumeParen();
1733 SourceLocation sLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001734 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001735 if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
1736 return ExprError(Diag(Tok, diag::err_expected_ident));
1737
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001738 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00001739 unsigned nColons = 0;
1740 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001741 while (1) {
Chris Lattner197a3012008-08-05 06:19:09 +00001742 if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001743 return ExprError(Diag(Tok, diag::err_expected_colon));
1744
Chris Lattner5e530bc2007-12-27 19:57:00 +00001745 nColons++;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001746 ConsumeToken(); // Eat the ':'.
1747 if (Tok.is(tok::r_paren))
1748 break;
1749 // Check for another keyword selector.
1750 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00001751 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001752 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001753 if (!SelIdent && Tok.isNot(tok::colon))
1754 break;
1755 }
Steve Naroff152dd812007-12-05 22:21:29 +00001756 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00001757 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00001758 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001759 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
1760 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00001761 }