blob: b6b1551907f5cb82e081eacca64b45905cd7626f [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
Chris Lattner60f36222009-01-29 05:15:15 +000014#include "clang/Parse/ParseDiagnostic.h"
John McCall8b0666c2010-08-20 18:27:03 +000015#include "clang/Parse/Parser.h"
Douglas Gregore9bba4f2010-09-15 14:51:05 +000016#include "RAIIObjectsForParser.h"
John McCall8b0666c2010-08-20 18:27:03 +000017#include "clang/Sema/DeclSpec.h"
John McCallfaf5fb42010-08-26 23:41:50 +000018#include "clang/Sema/PrettyDeclStackTrace.h"
John McCall8b0666c2010-08-20 18:27:03 +000019#include "clang/Sema/Scope.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000020#include "llvm/ADT/SmallVector.h"
Chris Lattnerda59c2f2006-11-05 02:08:13 +000021using namespace clang;
22
23
Chris Lattner3a907162008-12-08 21:53:24 +000024/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000025/// external-declaration: [C99 6.9]
26/// [OBJC] objc-class-definition
Steve Naroffe0933392007-10-29 21:39:29 +000027/// [OBJC] objc-class-declaration
28/// [OBJC] objc-alias-declaration
29/// [OBJC] objc-protocol-definition
30/// [OBJC] objc-method-definition
31/// [OBJC] '@' 'end'
John McCall48871652010-08-21 09:40:31 +000032Decl *Parser::ParseObjCAtDirectives() {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000033 SourceLocation AtLoc = ConsumeToken(); // the "@"
Mike Stump11289f42009-09-09 15:08:12 +000034
Douglas Gregorf48706c2009-12-07 09:27:33 +000035 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +000036 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +000037 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +000038 }
39
Steve Naroff7c348172007-08-23 18:16:40 +000040 switch (Tok.getObjCKeywordID()) {
Chris Lattnerce90ef52008-08-23 02:02:23 +000041 case tok::objc_class:
42 return ParseObjCAtClassDeclaration(AtLoc);
John McCall53fa7142010-12-24 02:08:15 +000043 case tok::objc_interface: {
44 ParsedAttributes attrs;
45 return ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
46 }
47 case tok::objc_protocol: {
48 ParsedAttributes attrs;
49 return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
50 }
Chris Lattnerce90ef52008-08-23 02:02:23 +000051 case tok::objc_implementation:
52 return ParseObjCAtImplementationDeclaration(AtLoc);
53 case tok::objc_end:
54 return ParseObjCAtEndDeclaration(AtLoc);
55 case tok::objc_compatibility_alias:
56 return ParseObjCAtAliasDeclaration(AtLoc);
57 case tok::objc_synthesize:
58 return ParseObjCPropertySynthesize(AtLoc);
59 case tok::objc_dynamic:
60 return ParseObjCPropertyDynamic(AtLoc);
61 default:
62 Diag(AtLoc, diag::err_unexpected_at);
63 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +000064 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000065 }
66}
67
68///
Mike Stump11289f42009-09-09 15:08:12 +000069/// objc-class-declaration:
Chris Lattnerda59c2f2006-11-05 02:08:13 +000070/// '@' 'class' identifier-list ';'
Mike Stump11289f42009-09-09 15:08:12 +000071///
John McCall48871652010-08-21 09:40:31 +000072Decl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
Chris Lattnerda59c2f2006-11-05 02:08:13 +000073 ConsumeToken(); // the identifier "class"
Chris Lattner23b7eb62007-06-15 23:05:46 +000074 llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
Ted Kremeneka26da852009-11-17 23:12:20 +000075 llvm::SmallVector<SourceLocation, 8> ClassLocs;
76
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattnerda59c2f2006-11-05 02:08:13 +000078 while (1) {
Chris Lattner0ef13522007-10-09 17:51:17 +000079 if (Tok.isNot(tok::identifier)) {
Chris Lattnerbd638922006-11-10 05:19:25 +000080 Diag(Tok, diag::err_expected_ident);
Chris Lattnerda59c2f2006-11-05 02:08:13 +000081 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +000082 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +000083 }
Chris Lattnerda59c2f2006-11-05 02:08:13 +000084 ClassNames.push_back(Tok.getIdentifierInfo());
Ted Kremeneka26da852009-11-17 23:12:20 +000085 ClassLocs.push_back(Tok.getLocation());
Chris Lattnerda59c2f2006-11-05 02:08:13 +000086 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +000087
Chris Lattner0ef13522007-10-09 17:51:17 +000088 if (Tok.isNot(tok::comma))
Chris Lattnerda59c2f2006-11-05 02:08:13 +000089 break;
Mike Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerda59c2f2006-11-05 02:08:13 +000091 ConsumeToken();
92 }
Mike Stump11289f42009-09-09 15:08:12 +000093
Chris Lattnerda59c2f2006-11-05 02:08:13 +000094 // Consume the ';'.
95 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
John McCall48871652010-08-21 09:40:31 +000096 return 0;
Mike Stump11289f42009-09-09 15:08:12 +000097
Ted Kremeneka26da852009-11-17 23:12:20 +000098 return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
99 ClassLocs.data(),
100 ClassNames.size());
Chris Lattnerda59c2f2006-11-05 02:08:13 +0000101}
102
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000103///
104/// objc-interface:
105/// objc-class-interface-attributes[opt] objc-class-interface
106/// objc-category-interface
107///
108/// objc-class-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000109/// '@' 'interface' identifier objc-superclass[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000110/// objc-protocol-refs[opt]
Mike Stump11289f42009-09-09 15:08:12 +0000111/// objc-class-instance-variables[opt]
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000112/// objc-interface-decl-list
113/// @end
114///
115/// objc-category-interface:
Mike Stump11289f42009-09-09 15:08:12 +0000116/// '@' 'interface' identifier '(' identifier[opt] ')'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000117/// objc-protocol-refs[opt]
118/// objc-interface-decl-list
119/// @end
120///
121/// objc-superclass:
122/// ':' identifier
123///
124/// objc-class-interface-attributes:
125/// __attribute__((visibility("default")))
126/// __attribute__((visibility("hidden")))
127/// __attribute__((deprecated))
128/// __attribute__((unavailable))
129/// __attribute__((objc_exception)) - used by NSException on 64-bit
130///
John McCall53fa7142010-12-24 02:08:15 +0000131Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
132 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +0000133 assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000134 "ParseObjCAtInterfaceDeclaration(): Expected @interface");
135 ConsumeToken(); // the "interface" identifier
Mike Stump11289f42009-09-09 15:08:12 +0000136
Douglas Gregor49c22a72009-11-18 16:26:39 +0000137 // Code completion after '@interface'.
138 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000139 Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +0000140 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000141 }
142
Chris Lattner0ef13522007-10-09 17:51:17 +0000143 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000144 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +0000145 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000146 }
Fariborz Jahanian9290ede2009-11-16 18:57:01 +0000147
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000148 // We have a class or category name - consume it.
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000149 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000150 SourceLocation nameLoc = ConsumeToken();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000151 if (Tok.is(tok::l_paren) &&
152 !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +0000153 // TODO(dgregor): Use the return value from the next line to provide better
154 // recovery.
155 ConsumeParen();
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000156 SourceLocation categoryLoc, rparenLoc;
157 IdentifierInfo *categoryId = 0;
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000158 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000159 Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000160 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +0000161 }
162
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000163 // For ObjC2, the category name is optional (not an error).
Chris Lattner0ef13522007-10-09 17:51:17 +0000164 if (Tok.is(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000165 categoryId = Tok.getIdentifierInfo();
166 categoryLoc = ConsumeToken();
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000167 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000168 else if (!getLang().ObjC2) {
Steve Naroff4e1f80d2007-08-23 19:56:30 +0000169 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +0000170 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000171 }
Chris Lattner0ef13522007-10-09 17:51:17 +0000172 if (Tok.isNot(tok::r_paren)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000173 Diag(Tok, diag::err_expected_rparen);
174 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +0000175 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000176 }
177 rparenLoc = ConsumeParen();
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000178 // Next, we need to check for any protocol references.
179 SourceLocation LAngleLoc, EndProtoLoc;
John McCall48871652010-08-21 09:40:31 +0000180 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000181 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
182 if (Tok.is(tok::less) &&
183 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000184 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000185 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000186
John McCall53fa7142010-12-24 02:08:15 +0000187 if (!attrs.empty()) // categories don't support attributes.
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000188 Diag(Tok, diag::err_objc_no_attributes_on_category);
Mike Stump11289f42009-09-09 15:08:12 +0000189
John McCall48871652010-08-21 09:40:31 +0000190 Decl *CategoryType =
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000191 Actions.ActOnStartCategoryInterface(atLoc,
192 nameId, nameLoc,
193 categoryId, categoryLoc,
194 ProtocolRefs.data(),
195 ProtocolRefs.size(),
196 ProtocolLocs.data(),
197 EndProtoLoc);
198 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000199 ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
200 atLoc);
201
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000202 ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
203 return CategoryType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000204 }
205 // Parse a class interface.
206 IdentifierInfo *superClassId = 0;
207 SourceLocation superClassLoc;
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000208
Chris Lattner0ef13522007-10-09 17:51:17 +0000209 if (Tok.is(tok::colon)) { // a super class is specified.
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000210 ConsumeToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000211
212 // Code completion of superclass names.
213 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000214 Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000215 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +0000216 }
217
Chris Lattner0ef13522007-10-09 17:51:17 +0000218 if (Tok.isNot(tok::identifier)) {
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000219 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +0000220 return 0;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000221 }
222 superClassId = Tok.getIdentifierInfo();
223 superClassLoc = ConsumeToken();
224 }
225 // Next, we need to check for any protocol references.
John McCall48871652010-08-21 09:40:31 +0000226 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000227 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
228 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000229 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +0000230 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
231 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +0000232 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000233
John McCall48871652010-08-21 09:40:31 +0000234 Decl *ClsType =
Mike Stump11289f42009-09-09 15:08:12 +0000235 Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000236 superClassId, superClassLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +0000237 ProtocolRefs.data(), ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +0000238 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +0000239 EndProtoLoc, attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +0000240
Chris Lattner0ef13522007-10-09 17:51:17 +0000241 if (Tok.is(tok::l_brace))
Fariborz Jahanian4c172c62010-02-22 23:04:20 +0000242 ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000243
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000244 ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
Fariborz Jahanian65654df2010-04-26 21:18:08 +0000245 return ClsType;
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000246}
247
John McCall064d77b2009-12-03 22:31:13 +0000248/// The Objective-C property callback. This should be defined where
249/// it's used, but instead it's been lifted to here to support VS2005.
250struct Parser::ObjCPropertyCallback : FieldCallback {
251 Parser &P;
John McCall48871652010-08-21 09:40:31 +0000252 Decl *IDecl;
253 llvm::SmallVectorImpl<Decl *> &Props;
John McCall064d77b2009-12-03 22:31:13 +0000254 ObjCDeclSpec &OCDS;
255 SourceLocation AtLoc;
256 tok::ObjCKeywordKind MethodImplKind;
257
John McCall48871652010-08-21 09:40:31 +0000258 ObjCPropertyCallback(Parser &P, Decl *IDecl,
259 llvm::SmallVectorImpl<Decl *> &Props,
John McCall064d77b2009-12-03 22:31:13 +0000260 ObjCDeclSpec &OCDS, SourceLocation AtLoc,
261 tok::ObjCKeywordKind MethodImplKind) :
262 P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
263 MethodImplKind(MethodImplKind) {
264 }
265
John McCall48871652010-08-21 09:40:31 +0000266 Decl *invoke(FieldDeclarator &FD) {
John McCall064d77b2009-12-03 22:31:13 +0000267 if (FD.D.getIdentifier() == 0) {
268 P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
269 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000270 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000271 }
272 if (FD.BitfieldSize) {
273 P.Diag(AtLoc, diag::err_objc_property_bitfield)
274 << FD.D.getSourceRange();
John McCall48871652010-08-21 09:40:31 +0000275 return 0;
John McCall064d77b2009-12-03 22:31:13 +0000276 }
277
278 // Install the property declarator into interfaceDecl.
279 IdentifierInfo *SelName =
280 OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
281
282 Selector GetterSel =
283 P.PP.getSelectorTable().getNullarySelector(SelName);
284 IdentifierInfo *SetterName = OCDS.getSetterName();
285 Selector SetterSel;
286 if (SetterName)
287 SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
288 else
289 SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
290 P.PP.getSelectorTable(),
291 FD.D.getIdentifier());
292 bool isOverridingProperty = false;
John McCall48871652010-08-21 09:40:31 +0000293 Decl *Property =
Douglas Gregor0be31a22010-07-02 17:43:08 +0000294 P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
John McCall064d77b2009-12-03 22:31:13 +0000295 GetterSel, SetterSel, IDecl,
296 &isOverridingProperty,
297 MethodImplKind);
298 if (!isOverridingProperty)
299 Props.push_back(Property);
300
301 return Property;
302 }
303};
304
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000305/// objc-interface-decl-list:
306/// empty
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000307/// objc-interface-decl-list objc-property-decl [OBJC2]
Steve Naroff99264b42007-08-22 16:35:03 +0000308/// objc-interface-decl-list objc-method-requirement [OBJC2]
Steve Naroff09bf8152007-09-06 21:24:23 +0000309/// objc-interface-decl-list objc-method-proto ';'
Steve Naroff1eb1ad62007-08-20 21:31:48 +0000310/// objc-interface-decl-list declaration
311/// objc-interface-decl-list ';'
312///
Steve Naroff99264b42007-08-22 16:35:03 +0000313/// objc-method-requirement: [OBJC2]
314/// @required
315/// @optional
316///
John McCall48871652010-08-21 09:40:31 +0000317void Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
Chris Lattner5e530bc2007-12-27 19:57:00 +0000318 tok::ObjCKeywordKind contextKey) {
John McCall48871652010-08-21 09:40:31 +0000319 llvm::SmallVector<Decl *, 32> allMethods;
320 llvm::SmallVector<Decl *, 16> allProperties;
Chris Lattner5bbb3c82009-03-29 16:50:03 +0000321 llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
Fariborz Jahanian0c74e9d2007-09-18 00:25:23 +0000322 tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremenekc7c64312010-01-07 01:20:12 +0000324 SourceRange AtEnd;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000325
Steve Naroff99264b42007-08-22 16:35:03 +0000326 while (1) {
Chris Lattner038a3e32008-10-20 05:46:22 +0000327 // If this is a method prototype, parse it.
Chris Lattner0ef13522007-10-09 17:51:17 +0000328 if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
John McCall48871652010-08-21 09:40:31 +0000329 Decl *methodPrototype =
Chris Lattner0ef13522007-10-09 17:51:17 +0000330 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
Fariborz Jahanian39d641f2007-09-17 21:07:36 +0000331 allMethods.push_back(methodPrototype);
Steve Naroff09bf8152007-09-06 21:24:23 +0000332 // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
333 // method definitions.
Chris Lattner8510b902009-02-15 22:24:30 +0000334 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
335 "", tok::semi);
Steve Naroff99264b42007-08-22 16:35:03 +0000336 continue;
337 }
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000338 if (Tok.is(tok::l_paren)) {
339 Diag(Tok, diag::err_expected_minus_or_plus);
John McCall48871652010-08-21 09:40:31 +0000340 ParseObjCMethodDecl(Tok.getLocation(),
341 tok::minus,
342 interfaceDecl,
343 MethodImplKind);
Fariborz Jahaniand077f712010-04-02 23:15:40 +0000344 continue;
345 }
Chris Lattner038a3e32008-10-20 05:46:22 +0000346 // Ignore excess semicolons.
347 if (Tok.is(tok::semi)) {
Steve Naroff99264b42007-08-22 16:35:03 +0000348 ConsumeToken();
Chris Lattner038a3e32008-10-20 05:46:22 +0000349 continue;
350 }
Mike Stump11289f42009-09-09 15:08:12 +0000351
Chris Lattnerda9fb152008-10-20 06:10:06 +0000352 // If we got to the end of the file, exit the loop.
Chris Lattner038a3e32008-10-20 05:46:22 +0000353 if (Tok.is(tok::eof))
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000354 break;
Mike Stump11289f42009-09-09 15:08:12 +0000355
Douglas Gregorf1934162010-01-13 21:24:21 +0000356 // Code completion within an Objective-C interface.
357 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000358 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +0000359 ObjCImpDecl? Sema::PCC_ObjCImplementation
360 : Sema::PCC_ObjCInterface);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000361 ConsumeCodeCompletionToken();
Douglas Gregorf1934162010-01-13 21:24:21 +0000362 }
363
Chris Lattner038a3e32008-10-20 05:46:22 +0000364 // If we don't have an @ directive, parse it as a function definition.
365 if (Tok.isNot(tok::at)) {
Chris Lattnerc7c9ab72009-01-09 04:34:13 +0000366 // The code below does not consume '}'s because it is afraid of eating the
367 // end of a namespace. Because of the way this code is structured, an
368 // erroneous r_brace would cause an infinite loop if not handled here.
369 if (Tok.is(tok::r_brace))
370 break;
Mike Stump11289f42009-09-09 15:08:12 +0000371
Steve Narofff1bc45b2007-08-22 18:35:33 +0000372 // FIXME: as the name implies, this rule allows function definitions.
373 // We could pass a flag or check for functions during semantic analysis.
John McCall53fa7142010-12-24 02:08:15 +0000374 ParsedAttributes attrs;
375 allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
Chris Lattner038a3e32008-10-20 05:46:22 +0000376 continue;
377 }
Mike Stump11289f42009-09-09 15:08:12 +0000378
Chris Lattner038a3e32008-10-20 05:46:22 +0000379 // Otherwise, we have an @ directive, eat the @.
380 SourceLocation AtLoc = ConsumeToken(); // the "@"
Douglas Gregorf48706c2009-12-07 09:27:33 +0000381 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000382 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000383 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000384 break;
385 }
386
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000387 tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
Mike Stump11289f42009-09-09 15:08:12 +0000388
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000389 if (DirectiveKind == tok::objc_end) { // @end -> terminate list
Ted Kremenekc7c64312010-01-07 01:20:12 +0000390 AtEnd.setBegin(AtLoc);
391 AtEnd.setEnd(Tok.getLocation());
Chris Lattner038a3e32008-10-20 05:46:22 +0000392 break;
Douglas Gregor00a0cf72010-03-16 06:04:47 +0000393 } else if (DirectiveKind == tok::objc_not_keyword) {
394 Diag(Tok, diag::err_objc_unknown_at);
395 SkipUntil(tok::semi);
396 continue;
Chris Lattnerda9fb152008-10-20 06:10:06 +0000397 }
Mike Stump11289f42009-09-09 15:08:12 +0000398
Chris Lattnerda9fb152008-10-20 06:10:06 +0000399 // Eat the identifier.
400 ConsumeToken();
401
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000402 switch (DirectiveKind) {
403 default:
Chris Lattnerda9fb152008-10-20 06:10:06 +0000404 // FIXME: If someone forgets an @end on a protocol, this loop will
405 // continue to eat up tons of stuff and spew lots of nonsense errors. It
406 // would probably be better to bail out if we saw an @class or @interface
407 // or something like that.
Chris Lattner76619232008-10-20 07:22:18 +0000408 Diag(AtLoc, diag::err_objc_illegal_interface_qual);
Chris Lattnerda9fb152008-10-20 06:10:06 +0000409 // Skip until we see an '@' or '}' or ';'.
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000410 SkipUntil(tok::r_brace, tok::at);
411 break;
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000412
413 case tok::objc_implementation:
Fariborz Jahaniandbee9862010-11-09 20:38:00 +0000414 case tok::objc_interface:
Fariborz Jahaniand4c53482010-11-02 00:44:43 +0000415 Diag(Tok, diag::err_objc_missing_end);
416 ConsumeToken();
417 break;
418
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000419 case tok::objc_required:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000420 case tok::objc_optional:
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000421 // This is only valid on protocols.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000422 // FIXME: Should this check for ObjC2 being enabled?
Chris Lattner038a3e32008-10-20 05:46:22 +0000423 if (contextKey != tok::objc_protocol)
Chris Lattnerda9fb152008-10-20 06:10:06 +0000424 Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000425 else
Chris Lattnerda9fb152008-10-20 06:10:06 +0000426 MethodImplKind = DirectiveKind;
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000427 break;
Mike Stump11289f42009-09-09 15:08:12 +0000428
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000429 case tok::objc_property:
Chris Lattner76619232008-10-20 07:22:18 +0000430 if (!getLang().ObjC2)
Chris Lattner4da4e252010-12-17 05:40:22 +0000431 Diag(AtLoc, diag::err_objc_properties_require_objc2);
Chris Lattner76619232008-10-20 07:22:18 +0000432
Chris Lattner038a3e32008-10-20 05:46:22 +0000433 ObjCDeclSpec OCDS;
Mike Stump11289f42009-09-09 15:08:12 +0000434 // Parse property attribute list, if any.
Chris Lattnerbeca7702008-10-20 07:24:39 +0000435 if (Tok.is(tok::l_paren))
Douglas Gregor87e92752010-12-21 17:34:17 +0000436 ParseObjCPropertyAttribute(OCDS, interfaceDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000437
John McCall064d77b2009-12-03 22:31:13 +0000438 ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
439 OCDS, AtLoc, MethodImplKind);
John McCallcfefb6d2009-11-03 02:38:08 +0000440
Chris Lattner038a3e32008-10-20 05:46:22 +0000441 // Parse all the comma separated declarators.
442 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +0000443 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000444
Chris Lattnere0d3fe92008-10-20 06:15:13 +0000445 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
446 tok::at);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000447 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000448 }
Steve Naroff99264b42007-08-22 16:35:03 +0000449 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000450
451 // We break out of the big loop in two cases: when we see @end or when we see
452 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000453 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000454 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000455 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000456 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000457 ConsumeToken(); // the "end" identifier
458 else
459 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000460
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000461 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000462 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000463 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000464 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000465 allProperties.data(), allProperties.size(),
466 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000467}
468
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000469/// Parse property attribute declarations.
470///
471/// property-attr-decl: '(' property-attrlist ')'
472/// property-attrlist:
473/// property-attribute
474/// property-attrlist ',' property-attribute
475/// property-attribute:
476/// getter '=' identifier
477/// setter '=' identifier ':'
478/// readonly
479/// readwrite
480/// assign
481/// retain
482/// copy
483/// nonatomic
484///
Douglas Gregor87e92752010-12-21 17:34:17 +0000485void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000486 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000487 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000488
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000489 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000490 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000491 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000492 ConsumeCodeCompletionToken();
Steve Naroff936354c2009-10-08 21:55:05 +0000493 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000494 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000495
Chris Lattner76619232008-10-20 07:22:18 +0000496 // If this is not an identifier at all, bail out early.
497 if (II == 0) {
498 MatchRHSPunctuation(tok::r_paren, LHSLoc);
499 return;
500 }
Mike Stump11289f42009-09-09 15:08:12 +0000501
Chris Lattner1db33542008-10-20 07:37:22 +0000502 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000503
Chris Lattner68e48682008-11-20 04:42:34 +0000504 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000505 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000506 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000507 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
Chris Lattner68e48682008-11-20 04:42:34 +0000508 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000509 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000510 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000511 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
Chris Lattner68e48682008-11-20 04:42:34 +0000512 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000513 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000514 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000515 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanian382c0402010-12-17 22:28:16 +0000516 else if (II->isStr("atomic"))
517 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
Chris Lattner68e48682008-11-20 04:42:34 +0000518 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000519 bool IsSetter = II->getNameStart()[0] == 's';
520
Chris Lattner825bca12008-10-20 07:39:53 +0000521 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000522 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
523 diag::err_objc_expected_equal_for_getter;
524
525 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000526 return;
Mike Stump11289f42009-09-09 15:08:12 +0000527
Douglas Gregorc8537c52009-11-19 07:41:15 +0000528 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000529 if (IsSetter)
Douglas Gregor87e92752010-12-21 17:34:17 +0000530 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl);
Douglas Gregorc8537c52009-11-19 07:41:15 +0000531 else
Douglas Gregor87e92752010-12-21 17:34:17 +0000532 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000533 ConsumeCodeCompletionToken();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000534 }
535
Anders Carlssonfe15a782010-10-02 17:45:21 +0000536
537 SourceLocation SelLoc;
538 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
539
540 if (!SelIdent) {
541 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
542 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000543 SkipUntil(tok::r_paren);
544 return;
545 }
Mike Stump11289f42009-09-09 15:08:12 +0000546
Anders Carlssonfe15a782010-10-02 17:45:21 +0000547 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000548 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000549 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000550
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000551 if (ExpectAndConsume(tok::colon,
552 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000553 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000554 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000555 } else {
556 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000557 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000558 }
Chris Lattner825bca12008-10-20 07:39:53 +0000559 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000560 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000561 SkipUntil(tok::r_paren);
562 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000563 }
Mike Stump11289f42009-09-09 15:08:12 +0000564
Chris Lattner1db33542008-10-20 07:37:22 +0000565 if (Tok.isNot(tok::comma))
566 break;
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattner1db33542008-10-20 07:37:22 +0000568 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000569 }
Mike Stump11289f42009-09-09 15:08:12 +0000570
Chris Lattner1db33542008-10-20 07:37:22 +0000571 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000572}
573
Steve Naroff09bf8152007-09-06 21:24:23 +0000574/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000575/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000576/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000577///
578/// objc-instance-method: '-'
579/// objc-class-method: '+'
580///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000581/// objc-method-attributes: [OBJC2]
582/// __attribute__((deprecated))
583///
John McCall48871652010-08-21 09:40:31 +0000584Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
585 tok::ObjCKeywordKind MethodImplKind) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000586 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000587
Mike Stump11289f42009-09-09 15:08:12 +0000588 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000589 SourceLocation mLoc = ConsumeToken();
John McCall48871652010-08-21 09:40:31 +0000590 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
Steve Naroff09bf8152007-09-06 21:24:23 +0000591 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000592 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000593 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000594}
595
596/// objc-selector:
597/// identifier
598/// one of
599/// enum struct union if else while do for switch case default
600/// break continue return goto asm sizeof typeof __alignof
601/// unsigned long const short volatile signed restrict _Complex
602/// in out inout bycopy byref oneway int char float double void _Bool
603///
Chris Lattner4f472a32009-04-11 18:13:45 +0000604IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000605
Chris Lattner5700fab2007-10-07 02:00:24 +0000606 switch (Tok.getKind()) {
607 default:
608 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000609 case tok::ampamp:
610 case tok::ampequal:
611 case tok::amp:
612 case tok::pipe:
613 case tok::tilde:
614 case tok::exclaim:
615 case tok::exclaimequal:
616 case tok::pipepipe:
617 case tok::pipeequal:
618 case tok::caret:
619 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000620 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000621 if (isalpha(ThisTok[0])) {
622 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
623 Tok.setKind(tok::identifier);
624 SelectorLoc = ConsumeToken();
625 return II;
626 }
627 return 0;
628 }
629
Chris Lattner5700fab2007-10-07 02:00:24 +0000630 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000631 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000632 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000633 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000634 case tok::kw_break:
635 case tok::kw_case:
636 case tok::kw_catch:
637 case tok::kw_char:
638 case tok::kw_class:
639 case tok::kw_const:
640 case tok::kw_const_cast:
641 case tok::kw_continue:
642 case tok::kw_default:
643 case tok::kw_delete:
644 case tok::kw_do:
645 case tok::kw_double:
646 case tok::kw_dynamic_cast:
647 case tok::kw_else:
648 case tok::kw_enum:
649 case tok::kw_explicit:
650 case tok::kw_export:
651 case tok::kw_extern:
652 case tok::kw_false:
653 case tok::kw_float:
654 case tok::kw_for:
655 case tok::kw_friend:
656 case tok::kw_goto:
657 case tok::kw_if:
658 case tok::kw_inline:
659 case tok::kw_int:
660 case tok::kw_long:
661 case tok::kw_mutable:
662 case tok::kw_namespace:
663 case tok::kw_new:
664 case tok::kw_operator:
665 case tok::kw_private:
666 case tok::kw_protected:
667 case tok::kw_public:
668 case tok::kw_register:
669 case tok::kw_reinterpret_cast:
670 case tok::kw_restrict:
671 case tok::kw_return:
672 case tok::kw_short:
673 case tok::kw_signed:
674 case tok::kw_sizeof:
675 case tok::kw_static:
676 case tok::kw_static_cast:
677 case tok::kw_struct:
678 case tok::kw_switch:
679 case tok::kw_template:
680 case tok::kw_this:
681 case tok::kw_throw:
682 case tok::kw_true:
683 case tok::kw_try:
684 case tok::kw_typedef:
685 case tok::kw_typeid:
686 case tok::kw_typename:
687 case tok::kw_typeof:
688 case tok::kw_union:
689 case tok::kw_unsigned:
690 case tok::kw_using:
691 case tok::kw_virtual:
692 case tok::kw_void:
693 case tok::kw_volatile:
694 case tok::kw_wchar_t:
695 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000696 case tok::kw__Bool:
697 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000698 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000699 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000700 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000701 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000702 }
Steve Naroff99264b42007-08-22 16:35:03 +0000703}
704
Fariborz Jahanian83615522008-01-02 22:54:34 +0000705/// objc-for-collection-in: 'in'
706///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000707bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000708 // FIXME: May have to do additional look-ahead to only allow for
709 // valid tokens following an 'in'; such as an identifier, unary operators,
710 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000711 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000712 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000713}
714
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000715/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000716/// qualifier list and builds their bitmask representation in the input
717/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000718///
719/// objc-type-qualifiers:
720/// objc-type-qualifier
721/// objc-type-qualifiers objc-type-qualifier
722///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000723void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
724 ObjCTypeNameContext Context) {
Chris Lattner61511e12007-12-12 06:56:32 +0000725 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000726 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000727 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
728 Context == OTN_ParameterType);
Douglas Gregor99fa2642010-08-24 01:06:58 +0000729 ConsumeCodeCompletionToken();
730 }
731
Chris Lattner5e530bc2007-12-27 19:57:00 +0000732 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000733 return;
Mike Stump11289f42009-09-09 15:08:12 +0000734
Chris Lattner61511e12007-12-12 06:56:32 +0000735 const IdentifierInfo *II = Tok.getIdentifierInfo();
736 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000737 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000738 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000739
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000740 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000741 switch (i) {
742 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000743 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
744 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
745 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
746 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
747 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
748 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000749 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000750 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000751 ConsumeToken();
752 II = 0;
753 break;
754 }
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattner61511e12007-12-12 06:56:32 +0000756 // If this wasn't a recognized qualifier, bail out.
757 if (II) return;
758 }
759}
760
761/// objc-type-name:
762/// '(' objc-type-qualifiers[opt] type-name ')'
763/// '(' objc-type-qualifiers[opt] ')'
764///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000765ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
766 ObjCTypeNameContext Context) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000767 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattnerb7954432008-10-22 03:52:06 +0000769 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000770 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000771
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000772 // Parse type qualifiers, in, inout, etc.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000773 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000774
John McCallba7bf592010-08-24 05:47:05 +0000775 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000776 if (isTypeSpecifierQualifier()) {
777 TypeResult TypeSpec = ParseTypeName();
778 if (!TypeSpec.isInvalid())
779 Ty = TypeSpec.get();
780 }
Mike Stump11289f42009-09-09 15:08:12 +0000781
Steve Naroff90255b42008-10-21 14:15:04 +0000782 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000783 ConsumeParen();
784 else if (Tok.getLocation() == TypeStartLoc) {
785 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000786 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000787 SkipUntil(tok::r_paren);
788 } else {
789 // Otherwise, we found *something*, but didn't get a ')' in the right
790 // place. Emit an error then return what we have as the type.
791 MatchRHSPunctuation(tok::r_paren, LParenLoc);
792 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000793 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000794}
795
796/// objc-method-decl:
797/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000798/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000799/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000800/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000801///
802/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000803/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000804/// objc-keyword-selector objc-keyword-decl
805///
806/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000807/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
808/// objc-selector ':' objc-keyword-attributes[opt] identifier
809/// ':' objc-type-name objc-keyword-attributes[opt] identifier
810/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000811///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000812/// objc-parmlist:
813/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000814///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000815/// objc-parms:
816/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000817///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000818/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000819/// , ...
820///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000821/// objc-keyword-attributes: [OBJC2]
822/// __attribute__((unused))
823///
John McCall48871652010-08-21 09:40:31 +0000824Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000825 tok::TokenKind mType,
826 Decl *IDecl,
827 tok::ObjCKeywordKind MethodImplKind) {
John McCall28a6aea2009-11-04 02:18:39 +0000828 ParsingDeclRAIIObject PD(*this);
829
Douglas Gregor636a61e2010-04-07 00:21:17 +0000830 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000831 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallba7bf592010-08-24 05:47:05 +0000832 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000833 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000834 }
835
Chris Lattner2ebb1782008-08-23 01:48:03 +0000836 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000837 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000838 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000839 if (Tok.is(tok::l_paren))
Douglas Gregor95d3e372011-03-08 19:17:54 +0000840 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump11289f42009-09-09 15:08:12 +0000841
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000842 // If attributes exist before the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000843 ParsedAttributes attrs;
844 if (getLang().ObjC2)
845 MaybeParseGNUAttributes(attrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000846
Douglas Gregor636a61e2010-04-07 00:21:17 +0000847 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000848 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregor636a61e2010-04-07 00:21:17 +0000849 ReturnType, IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000850 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000851 }
852
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000853 // Now parse the selector.
Steve Naroff161a92b2007-10-26 20:53:56 +0000854 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000855 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000856
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000857 // An unnamed colon is valid.
858 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000859 Diag(Tok, diag::err_expected_selector_for_method)
860 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000861 // Skip until we get a ; or {}.
862 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000863 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000864 }
Mike Stump11289f42009-09-09 15:08:12 +0000865
Fariborz Jahanian60462092010-04-08 00:30:06 +0000866 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000867 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000868 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000869 if (getLang().ObjC2)
870 MaybeParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +0000871
Chris Lattner5700fab2007-10-07 02:00:24 +0000872 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +0000873 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000874 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanianb5605172007-11-09 19:52:12 +0000875 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000876 0,
877 CParamInfo.data(), CParamInfo.size(),
John McCall53fa7142010-12-24 02:08:15 +0000878 attrs.getList(), MethodImplKind);
John McCall28a6aea2009-11-04 02:18:39 +0000879 PD.complete(Result);
880 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000881 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000882
Steve Narofff73590d2007-09-27 14:38:14 +0000883 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallfaf5fb42010-08-26 23:41:50 +0000884 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000885 ParseScope PrototypeScope(this,
886 Scope::FunctionPrototypeScope|Scope::DeclScope);
887
Chris Lattner5700fab2007-10-07 02:00:24 +0000888 while (1) {
John McCallfaf5fb42010-08-26 23:41:50 +0000889 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000890
Chris Lattner5700fab2007-10-07 02:00:24 +0000891 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000892 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000893 Diag(Tok, diag::err_expected_colon);
894 break;
895 }
896 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000897
John McCallba7bf592010-08-24 05:47:05 +0000898 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000899 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000900 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000901
Chris Lattner5700fab2007-10-07 02:00:24 +0000902 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000903 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +0000904 if (getLang().ObjC2) {
905 ParsedAttributes attrs;
906 MaybeParseGNUAttributes(attrs);
907 ArgInfo.ArgAttrs = attrs.getList();
908 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000909
Douglas Gregor45879692010-07-08 23:37:41 +0000910 // Code completion for the next piece of the selector.
911 if (Tok.is(tok::code_completion)) {
912 ConsumeCodeCompletionToken();
913 KeyIdents.push_back(SelIdent);
914 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
915 mType == tok::minus,
916 /*AtParameterName=*/true,
917 ReturnType,
918 KeyIdents.data(),
919 KeyIdents.size());
920 KeyIdents.pop_back();
921 break;
922 }
923
Chris Lattner0ef13522007-10-09 17:51:17 +0000924 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000925 Diag(Tok, diag::err_expected_ident); // missing argument name.
926 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000927 }
Mike Stump11289f42009-09-09 15:08:12 +0000928
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000929 ArgInfo.Name = Tok.getIdentifierInfo();
930 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000931 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000933 ArgInfos.push_back(ArgInfo);
934 KeyIdents.push_back(SelIdent);
935
Douglas Gregor95887f92010-07-08 23:20:03 +0000936 // Code completion for the next piece of the selector.
937 if (Tok.is(tok::code_completion)) {
938 ConsumeCodeCompletionToken();
939 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
940 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +0000941 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +0000942 ReturnType,
943 KeyIdents.data(),
944 KeyIdents.size());
945 break;
946 }
947
Chris Lattner5700fab2007-10-07 02:00:24 +0000948 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000949 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000950 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000951 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000952 break;
953 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000954 }
Mike Stump11289f42009-09-09 15:08:12 +0000955
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000956 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000957
Chris Lattner5700fab2007-10-07 02:00:24 +0000958 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000959 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000960 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000961 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000962 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000963 ConsumeToken();
964 break;
965 }
Chris Lattner5700fab2007-10-07 02:00:24 +0000966 DeclSpec DS;
967 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000968 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000969 Declarator ParmDecl(DS, Declarator::PrototypeContext);
970 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000971 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +0000972 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000973 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
974 ParmDecl.getIdentifierLoc(),
975 Param,
976 0));
977
Chris Lattner5700fab2007-10-07 02:00:24 +0000978 }
Mike Stump11289f42009-09-09 15:08:12 +0000979
Cameron Esfahanif6c73c42010-10-12 00:21:25 +0000980 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +0000981 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000982 if (getLang().ObjC2)
983 MaybeParseGNUAttributes(attrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000984
985 if (KeyIdents.size() == 0) {
986 // Leave prototype scope.
987 PrototypeScope.Exit();
John McCall48871652010-08-21 09:40:31 +0000988 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000989 }
990
Chris Lattner5700fab2007-10-07 02:00:24 +0000991 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
992 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +0000993 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000994 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Fariborz Jahanian6e9e4f32009-06-24 17:00:18 +0000995 mType, IDecl, DSRet, ReturnType, Sel,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000996 &ArgInfos[0],
997 CParamInfo.data(), CParamInfo.size(),
John McCall53fa7142010-12-24 02:08:15 +0000998 attrs.getList(),
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000999 MethodImplKind, isVariadic);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001000 // Leave prototype scope.
1001 PrototypeScope.Exit();
1002
John McCall28a6aea2009-11-04 02:18:39 +00001003 PD.complete(Result);
1004 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001005}
1006
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001007/// objc-protocol-refs:
1008/// '<' identifier-list '>'
1009///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001010bool Parser::
John McCall48871652010-08-21 09:40:31 +00001011ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001012 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
1013 bool WarnOnDeclarations,
1014 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001015 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001016
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001017 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001018
Chris Lattner3bbae002008-07-26 04:03:38 +00001019 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001020
Chris Lattner3bbae002008-07-26 04:03:38 +00001021 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001022 if (Tok.is(tok::code_completion)) {
1023 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1024 ProtocolIdents.size());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001025 ConsumeCodeCompletionToken();
Douglas Gregorbaf69612009-11-18 04:19:12 +00001026 }
1027
Chris Lattner3bbae002008-07-26 04:03:38 +00001028 if (Tok.isNot(tok::identifier)) {
1029 Diag(Tok, diag::err_expected_ident);
1030 SkipUntil(tok::greater);
1031 return true;
1032 }
1033 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1034 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001035 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001036 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001037
Chris Lattner3bbae002008-07-26 04:03:38 +00001038 if (Tok.isNot(tok::comma))
1039 break;
1040 ConsumeToken();
1041 }
Mike Stump11289f42009-09-09 15:08:12 +00001042
Chris Lattner3bbae002008-07-26 04:03:38 +00001043 // Consume the '>'.
1044 if (Tok.isNot(tok::greater)) {
1045 Diag(Tok, diag::err_expected_greater);
1046 return true;
1047 }
Mike Stump11289f42009-09-09 15:08:12 +00001048
Chris Lattner3bbae002008-07-26 04:03:38 +00001049 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattner3bbae002008-07-26 04:03:38 +00001051 // Convert the list of protocols identifiers into a list of protocol decls.
1052 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1053 &ProtocolIdents[0], ProtocolIdents.size(),
1054 Protocols);
1055 return false;
1056}
1057
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001058/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1059/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001060bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001061 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1062 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1063 SourceLocation LAngleLoc, EndProtoLoc;
1064 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1065 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001066 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1067 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001068 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1069 ProtocolLocs.data(), LAngleLoc);
1070 if (EndProtoLoc.isValid())
1071 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001072 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001073}
1074
1075
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001076/// objc-class-instance-variables:
1077/// '{' objc-instance-variable-decl-list[opt] '}'
1078///
1079/// objc-instance-variable-decl-list:
1080/// objc-visibility-spec
1081/// objc-instance-variable-decl ';'
1082/// ';'
1083/// objc-instance-variable-decl-list objc-visibility-spec
1084/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1085/// objc-instance-variable-decl-list ';'
1086///
1087/// objc-visibility-spec:
1088/// @private
1089/// @protected
1090/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001091/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001092///
1093/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001094/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001095///
John McCall48871652010-08-21 09:40:31 +00001096void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001097 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001098 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001099 assert(Tok.is(tok::l_brace) && "expected {");
John McCall48871652010-08-21 09:40:31 +00001100 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001101
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001102 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001103
Steve Naroff00433d32007-08-21 21:17:12 +00001104 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +00001105
Steve Naroff00433d32007-08-21 21:17:12 +00001106 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001107 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001108 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001109
Steve Naroff00433d32007-08-21 21:17:12 +00001110 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001111 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001112 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001113 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001114 ConsumeToken();
1115 continue;
1116 }
Mike Stump11289f42009-09-09 15:08:12 +00001117
Steve Naroff00433d32007-08-21 21:17:12 +00001118 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001119 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001120 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001121
1122 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001123 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001124 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001125 }
1126
Steve Naroff7c348172007-08-23 18:16:40 +00001127 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001128 case tok::objc_private:
1129 case tok::objc_public:
1130 case tok::objc_protected:
1131 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001132 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001133 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001134 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001135 default:
1136 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001137 continue;
1138 }
1139 }
Mike Stump11289f42009-09-09 15:08:12 +00001140
Douglas Gregor48d46252010-01-13 21:54:15 +00001141 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001142 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001143 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001144 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001145 }
1146
John McCallcfefb6d2009-11-03 02:38:08 +00001147 struct ObjCIvarCallback : FieldCallback {
1148 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001149 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001150 tok::ObjCKeywordKind visibility;
John McCall48871652010-08-21 09:40:31 +00001151 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001152
John McCall48871652010-08-21 09:40:31 +00001153 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1154 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001155 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1156 }
1157
John McCall48871652010-08-21 09:40:31 +00001158 Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001159 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001160 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001161 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001162 FD.D.getDeclSpec().getSourceRange().getBegin(),
1163 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001164 if (Field)
1165 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001166 return Field;
1167 }
1168 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001169
Chris Lattnera12405b2008-04-10 06:46:29 +00001170 // Parse all the comma separated declarators.
1171 DeclSpec DS;
John McCallcfefb6d2009-11-03 02:38:08 +00001172 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001173
Chris Lattner0ef13522007-10-09 17:51:17 +00001174 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001175 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001176 } else {
1177 Diag(Tok, diag::err_expected_semi_decl_list);
1178 // Skip to end of block or statement
1179 SkipUntil(tok::r_brace, true, true);
1180 }
1181 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001182 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001183 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001184 // Call ActOnFields() even if we don't have any decls. This is useful
1185 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001186 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001187 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001188 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001189 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001190}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001191
1192/// objc-protocol-declaration:
1193/// objc-protocol-definition
1194/// objc-protocol-forward-reference
1195///
1196/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001197/// @protocol identifier
1198/// objc-protocol-refs[opt]
1199/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001200/// @end
1201///
1202/// objc-protocol-forward-reference:
1203/// @protocol identifier-list ';'
1204///
1205/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001206/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001207/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCall48871652010-08-21 09:40:31 +00001208Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +00001209 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001210 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001211 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1212 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001213
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001214 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001215 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001216 ConsumeCodeCompletionToken();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001217 }
1218
Chris Lattner0ef13522007-10-09 17:51:17 +00001219 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001220 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCall48871652010-08-21 09:40:31 +00001221 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001222 }
1223 // Save the protocol name, then consume it.
1224 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1225 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001226
Chris Lattner0ef13522007-10-09 17:51:17 +00001227 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001228 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001229 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001230 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001231 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001232 }
Mike Stump11289f42009-09-09 15:08:12 +00001233
Chris Lattner0ef13522007-10-09 17:51:17 +00001234 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001235 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1236 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1237
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001238 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001239 while (1) {
1240 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001241 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001242 Diag(Tok, diag::err_expected_ident);
1243 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001244 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001245 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001246 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1247 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001248 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001249
Chris Lattner0ef13522007-10-09 17:51:17 +00001250 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001251 break;
1252 }
1253 // Consume the ';'.
1254 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCall48871652010-08-21 09:40:31 +00001255 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001256
Steve Naroff93eb5f12007-10-10 17:32:04 +00001257 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001258 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001259 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001260 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001261 }
Mike Stump11289f42009-09-09 15:08:12 +00001262
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001263 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001264 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001265
John McCall48871652010-08-21 09:40:31 +00001266 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001267 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001268 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001269 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1270 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +00001271 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001272
John McCall48871652010-08-21 09:40:31 +00001273 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001274 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001275 ProtocolRefs.data(),
1276 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001277 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001278 EndProtoLoc, attrs.getList());
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001279 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001280 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001281}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001282
1283/// objc-implementation:
1284/// objc-class-implementation-prologue
1285/// objc-category-implementation-prologue
1286///
1287/// objc-class-implementation-prologue:
1288/// @implementation identifier objc-superclass[opt]
1289/// objc-class-instance-variables[opt]
1290///
1291/// objc-category-implementation-prologue:
1292/// @implementation identifier ( identifier )
John McCall48871652010-08-21 09:40:31 +00001293Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001294 SourceLocation atLoc) {
1295 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1296 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1297 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregor49c22a72009-11-18 16:26:39 +00001299 // Code completion after '@implementation'.
1300 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001301 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001302 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001303 }
1304
Chris Lattner0ef13522007-10-09 17:51:17 +00001305 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001306 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001307 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001308 }
1309 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001310 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001311 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001312
1313 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001314 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001315 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001316 SourceLocation categoryLoc, rparenLoc;
1317 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001318
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001319 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001320 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001321 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001322 }
1323
Chris Lattner0ef13522007-10-09 17:51:17 +00001324 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001325 categoryId = Tok.getIdentifierInfo();
1326 categoryLoc = ConsumeToken();
1327 } else {
1328 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +00001329 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001330 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001331 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001332 Diag(Tok, diag::err_expected_rparen);
1333 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001334 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001335 }
1336 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001337 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001338 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001339 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001340 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001341 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001342 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001343 }
1344 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001345 SourceLocation superClassLoc;
1346 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001347 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001348 // We have a super class
1349 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001350 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001351 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001352 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001353 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001354 superClassId = Tok.getIdentifierInfo();
1355 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001356 }
John McCall48871652010-08-21 09:40:31 +00001357 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001358 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001359 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001360
Steve Naroff33a1e802007-10-29 21:38:07 +00001361 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001362 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahaniana9effb52010-03-22 19:04:14 +00001363 tok::objc_private, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001364 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001365 PendingObjCImpDecl.push_back(ObjCImpDecl);
1366
John McCall48871652010-08-21 09:40:31 +00001367 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001368}
Steve Naroff33a1e802007-10-29 21:38:07 +00001369
John McCall48871652010-08-21 09:40:31 +00001370Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001371 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1372 "ParseObjCAtEndDeclaration(): Expected @end");
John McCall48871652010-08-21 09:40:31 +00001373 Decl *Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001374 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001375 if (ObjCImpDecl) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001376 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001377 ObjCImpDecl = 0;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001378 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001379 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001380 else {
1381 // missing @implementation
1382 Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1383 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001384 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001385}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001386
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001387Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1388 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001389 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001390 return Actions.ConvertDeclToDeclGroup(0);
1391 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001392 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001393 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1394}
1395
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001396/// compatibility-alias-decl:
1397/// @compatibility_alias alias-name class-name ';'
1398///
John McCall48871652010-08-21 09:40:31 +00001399Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001400 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1401 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1402 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001403 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001404 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001405 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001406 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001407 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1408 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001409 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001410 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001411 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001412 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001413 IdentifierInfo *classId = Tok.getIdentifierInfo();
1414 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001415 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1416 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001417 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1418 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001419}
1420
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001421/// property-synthesis:
1422/// @synthesize property-ivar-list ';'
1423///
1424/// property-ivar-list:
1425/// property-ivar
1426/// property-ivar-list ',' property-ivar
1427///
1428/// property-ivar:
1429/// identifier
1430/// identifier '=' identifier
1431///
John McCall48871652010-08-21 09:40:31 +00001432Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001433 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1434 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001435 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001436
Douglas Gregor88e72a02009-11-18 19:45:45 +00001437 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001438 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001439 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001440 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001441 }
1442
Douglas Gregor88e72a02009-11-18 19:45:45 +00001443 if (Tok.isNot(tok::identifier)) {
1444 Diag(Tok, diag::err_synthesized_property_name);
1445 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001446 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001447 }
1448
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001449 IdentifierInfo *propertyIvar = 0;
1450 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1451 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001452 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001453 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001454 // property '=' ivar-name
1455 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001456
1457 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001458 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor5d649882009-11-18 22:32:06 +00001459 ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001460 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001461 }
1462
Chris Lattner0ef13522007-10-09 17:51:17 +00001463 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001464 Diag(Tok, diag::err_expected_ident);
1465 break;
1466 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001467 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001468 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001469 }
Douglas Gregor0be31a22010-07-02 17:43:08 +00001470 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001471 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001472 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001473 break;
1474 ConsumeToken(); // consume ','
1475 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001476 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001477 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001478}
1479
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001480/// property-dynamic:
1481/// @dynamic property-list
1482///
1483/// property-list:
1484/// identifier
1485/// property-list ',' identifier
1486///
John McCall48871652010-08-21 09:40:31 +00001487Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001488 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1489 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001490 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001491 while (true) {
1492 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001493 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001494 ConsumeCodeCompletionToken();
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001495 }
1496
1497 if (Tok.isNot(tok::identifier)) {
1498 Diag(Tok, diag::err_expected_ident);
1499 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001500 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001501 }
1502
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001503 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1504 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor0be31a22010-07-02 17:43:08 +00001505 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001506 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001507
Chris Lattner0ef13522007-10-09 17:51:17 +00001508 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001509 break;
1510 ConsumeToken(); // consume ','
1511 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001512 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001513 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001514}
Mike Stump11289f42009-09-09 15:08:12 +00001515
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001516/// objc-throw-statement:
1517/// throw expression[opt];
1518///
John McCalldadc5752010-08-24 06:29:42 +00001519StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1520 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001521 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001522 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001523 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001524 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001525 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001526 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001527 }
1528 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001529 // consume ';'
1530 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001531 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001532}
1533
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001534/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001535/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001536///
John McCalldadc5752010-08-24 06:29:42 +00001537StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001538Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001539 ConsumeToken(); // consume synchronized
1540 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001541 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001542 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001543 }
1544 ConsumeParen(); // '('
John McCalldadc5752010-08-24 06:29:42 +00001545 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001546 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001547 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001548 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001549 }
1550 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001551 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001552 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001553 }
1554 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001555 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001556 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001557 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001558 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001559 // Enter a scope to hold everything within the compound stmt. Compound
1560 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001561 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001562
John McCalldadc5752010-08-24 06:29:42 +00001563 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001564
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001565 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001566 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001567 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCallb268a282010-08-23 23:25:46 +00001568 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001569}
1570
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001571/// objc-try-catch-statement:
1572/// @try compound-statement objc-catch-list[opt]
1573/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1574///
1575/// objc-catch-list:
1576/// @catch ( parameter-declaration ) compound-statement
1577/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1578/// catch-parameter-declaration:
1579/// parameter-declaration
1580/// '...' [OBJC2]
1581///
John McCalldadc5752010-08-24 06:29:42 +00001582StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001583 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001584
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001585 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001586 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001587 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001588 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001589 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001590 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001591 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001592 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001593 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001594 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001595 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001596 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001597
Chris Lattner0ef13522007-10-09 17:51:17 +00001598 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001599 // At this point, we need to lookahead to determine if this @ is the start
1600 // of an @catch or @finally. We don't want to consume the @ token if this
1601 // is an @try or @encode or something else.
1602 Token AfterAt = GetLookAheadToken(1);
1603 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1604 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1605 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001606
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001607 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001608 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001609 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001610 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001611 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001612 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001613 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001614 if (Tok.isNot(tok::ellipsis)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001615 DeclSpec DS;
1616 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001617 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001618 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001619 // we must accept either 'declarator' or 'abstract-declarator' here.
1620 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1621 ParseDeclarator(ParmDecl);
1622
Douglas Gregore11ee112010-04-23 23:01:43 +00001623 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001624 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001625 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001626 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001627 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001628
Steve Naroff65a00892009-04-07 22:56:58 +00001629 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001630
Steve Naroff65a00892009-04-07 22:56:58 +00001631 if (Tok.is(tok::r_paren))
1632 RParenLoc = ConsumeParen();
1633 else // Skip over garbage, until we get to ')'. Eat the ')'.
1634 SkipUntil(tok::r_paren, true, false);
1635
John McCalldadc5752010-08-24 06:29:42 +00001636 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001637 if (Tok.is(tok::l_brace))
1638 CatchBody = ParseCompoundStatementBody();
1639 else
1640 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001641 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001642 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001643
John McCalldadc5752010-08-24 06:29:42 +00001644 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001645 RParenLoc,
1646 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001647 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001648 if (!Catch.isInvalid())
1649 CatchStmts.push_back(Catch.release());
1650
Steve Naroffe6016792008-02-05 21:27:35 +00001651 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001652 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1653 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001654 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001655 }
1656 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001657 } else {
1658 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001659 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001660 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001661
John McCalldadc5752010-08-24 06:29:42 +00001662 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001663 if (Tok.is(tok::l_brace))
1664 FinallyBody = ParseCompoundStatementBody();
1665 else
1666 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001667 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001668 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001669 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001670 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001671 catch_or_finally_seen = true;
1672 break;
1673 }
1674 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001675 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001676 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001677 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001678 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001679
John McCallb268a282010-08-23 23:25:46 +00001680 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001681 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001682 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001683}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001684
Steve Naroff09bf8152007-09-06 21:24:23 +00001685/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001686///
John McCall48871652010-08-21 09:40:31 +00001687Decl *Parser::ParseObjCMethodDefinition() {
1688 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001689
John McCallfaf5fb42010-08-26 23:41:50 +00001690 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1691 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001692
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001693 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001694 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001695 if (ObjCImpDecl) {
1696 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001697 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001698 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001699 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001700 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001701
Steve Naroffbb875722007-11-11 19:54:21 +00001702 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001703 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001704 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001705
Steve Naroffbb875722007-11-11 19:54:21 +00001706 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1707 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001708
Steve Naroffbb875722007-11-11 19:54:21 +00001709 // If we didn't find the '{', bail out.
1710 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001711 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001712 }
Steve Naroffbb875722007-11-11 19:54:21 +00001713 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001714
Steve Naroffbb875722007-11-11 19:54:21 +00001715 // Enter a scope for the method body.
Chris Lattnerc2ebb032010-04-12 05:38:43 +00001716 ParseScope BodyScope(this,
1717 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001718
Steve Naroffbb875722007-11-11 19:54:21 +00001719 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001720 // specified Declarator for the method.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001721 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001722
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001723 if (PP.isCodeCompletionEnabled())
1724 if (trySkippingFunctionBodyForCodeCompletion())
1725 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001726
John McCalldadc5752010-08-24 06:29:42 +00001727 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001728
Steve Naroffbb875722007-11-11 19:54:21 +00001729 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001730 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001731 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1732 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001733
Steve Naroffb94d7f62009-03-02 22:00:56 +00001734 // TODO: Pass argument information.
John McCallb268a282010-08-23 23:25:46 +00001735 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Mike Stump11289f42009-09-09 15:08:12 +00001736
Steve Naroffbb875722007-11-11 19:54:21 +00001737 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001738 BodyScope.Exit();
Sebastian Redlc675bab2008-12-13 16:23:55 +00001739
Steve Naroff7b8fa472007-11-13 23:01:27 +00001740 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001741}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001742
John McCalldadc5752010-08-24 06:29:42 +00001743StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001744 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001745 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001746 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001747 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001748 }
1749
1750 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001751 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001752
1753 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001754 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001755
1756 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001757 return ParseObjCSynchronizedStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001758
John McCalldadc5752010-08-24 06:29:42 +00001759 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001760 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001761 // If the expression is invalid, skip ahead to the next semicolon. Not
1762 // doing this opens us up to the possibility of infinite loops if
1763 // ParseExpression does not consume any tokens.
1764 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001765 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001766 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001767
Steve Naroffe6016792008-02-05 21:27:35 +00001768 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001769 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001770 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001771}
1772
John McCalldadc5752010-08-24 06:29:42 +00001773ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001774 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001775 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001776 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001777 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001778 return ExprError();
1779
Chris Lattnere002fbe2007-12-12 01:04:12 +00001780 case tok::string_literal: // primary-expression: string-literal
1781 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001782 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001783 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001784 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001785 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001786
Chris Lattner197a3012008-08-05 06:19:09 +00001787 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1788 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001789 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001790 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001791 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001792 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001793 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001794 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001795 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001796 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001797 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001798}
1799
Douglas Gregor8d4de672010-04-21 22:36:40 +00001800/// \brirg Parse the receiver of an Objective-C++ message send.
1801///
1802/// This routine parses the receiver of a message send in
1803/// Objective-C++ either as a type or as an expression. Note that this
1804/// routine must not be called to parse a send to 'super', since it
1805/// has no way to return such a result.
1806///
1807/// \param IsExpr Whether the receiver was parsed as an expression.
1808///
1809/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1810/// IsExpr is true), the parsed expression. If the receiver was parsed
1811/// as a type (\c IsExpr is false), the parsed type.
1812///
1813/// \returns True if an error occurred during parsing or semantic
1814/// analysis, in which case the arguments do not have valid
1815/// values. Otherwise, returns false for a successful parse.
1816///
1817/// objc-receiver: [C++]
1818/// 'super' [not parsed here]
1819/// expression
1820/// simple-type-specifier
1821/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00001822bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001823 InMessageExpressionRAIIObject InMessage(*this, true);
1824
Douglas Gregor8d4de672010-04-21 22:36:40 +00001825 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1826 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1827 TryAnnotateTypeOrScopeToken();
1828
1829 if (!isCXXSimpleTypeSpecifier()) {
1830 // objc-receiver:
1831 // expression
John McCalldadc5752010-08-24 06:29:42 +00001832 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001833 if (Receiver.isInvalid())
1834 return true;
1835
1836 IsExpr = true;
1837 TypeOrExpr = Receiver.take();
1838 return false;
1839 }
1840
1841 // objc-receiver:
1842 // typename-specifier
1843 // simple-type-specifier
1844 // expression (that starts with one of the above)
1845 DeclSpec DS;
1846 ParseCXXSimpleTypeSpecifier(DS);
1847
1848 if (Tok.is(tok::l_paren)) {
1849 // If we see an opening parentheses at this point, we are
1850 // actually parsing an expression that starts with a
1851 // function-style cast, e.g.,
1852 //
1853 // postfix-expression:
1854 // simple-type-specifier ( expression-list [opt] )
1855 // typename-specifier ( expression-list [opt] )
1856 //
1857 // Parse the remainder of this case, then the (optional)
1858 // postfix-expression suffix, followed by the (optional)
1859 // right-hand side of the binary expression. We have an
1860 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00001861 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001862 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001863 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00001864 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001865 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001866 if (Receiver.isInvalid())
1867 return true;
1868
1869 IsExpr = true;
1870 TypeOrExpr = Receiver.take();
1871 return false;
1872 }
1873
1874 // We have a class message. Turn the simple-type-specifier or
1875 // typename-specifier we parsed into a type and parse the
1876 // remainder of the class message.
1877 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001878 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001879 if (Type.isInvalid())
1880 return true;
1881
1882 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00001883 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001884 return false;
1885}
1886
Douglas Gregor990ccac2010-05-31 14:40:22 +00001887/// \brief Determine whether the parser is currently referring to a an
1888/// Objective-C message send, using a simplified heuristic to avoid overhead.
1889///
1890/// This routine will only return true for a subset of valid message-send
1891/// expressions.
1892bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00001893 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00001894 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00001895 return GetLookAheadToken(1).is(tok::identifier) &&
1896 GetLookAheadToken(2).is(tok::identifier);
1897}
1898
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001899bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1900 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1901 InMessageExpression)
1902 return false;
1903
1904
1905 ParsedType Type;
1906
1907 if (Tok.is(tok::annot_typename))
1908 Type = getTypeAnnotation(Tok);
1909 else if (Tok.is(tok::identifier))
1910 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1911 getCurScope());
1912 else
1913 return false;
1914
1915 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1916 const Token &AfterNext = GetLookAheadToken(2);
1917 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1918 if (Tok.is(tok::identifier))
1919 TryAnnotateTypeOrScopeToken();
1920
1921 return Tok.is(tok::annot_typename);
1922 }
1923 }
1924
1925 return false;
1926}
1927
Mike Stump11289f42009-09-09 15:08:12 +00001928/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001929/// '[' objc-receiver objc-message-args ']'
1930///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001931/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00001932/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001933/// expression
1934/// class-name
1935/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00001936///
John McCalldadc5752010-08-24 06:29:42 +00001937ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001938 assert(Tok.is(tok::l_square) && "'[' expected");
1939 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1940
Douglas Gregora817a192010-05-27 23:06:34 +00001941 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001942 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregora817a192010-05-27 23:06:34 +00001943 ConsumeCodeCompletionToken();
1944 SkipUntil(tok::r_square);
1945 return ExprError();
1946 }
1947
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001948 InMessageExpressionRAIIObject InMessage(*this, true);
1949
Douglas Gregor8d4de672010-04-21 22:36:40 +00001950 if (getLang().CPlusPlus) {
1951 // We completely separate the C and C++ cases because C++ requires
1952 // more complicated (read: slower) parsing.
1953
1954 // Handle send to super.
1955 // FIXME: This doesn't benefit from the same typo-correction we
1956 // get in Objective-C.
1957 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00001958 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00001959 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1960 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001961
1962 // Parse the receiver, which is either a type or an expression.
1963 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00001964 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00001965 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1966 SkipUntil(tok::r_square);
1967 return ExprError();
1968 }
1969
1970 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00001971 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1972 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00001973 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00001974
1975 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00001976 ParsedType::getFromOpaquePtr(TypeOrExpr),
1977 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00001978 }
1979
1980 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00001981 IdentifierInfo *Name = Tok.getIdentifierInfo();
1982 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00001983 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00001984 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00001985 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00001986 NextToken().is(tok::period),
1987 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00001988 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00001989 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1990 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001991
John McCallfaf5fb42010-08-26 23:41:50 +00001992 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00001993 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001994 SkipUntil(tok::r_square);
1995 return ExprError();
1996 }
1997
Douglas Gregore5798dc2010-04-21 20:38:13 +00001998 ConsumeToken(); // the type name
1999
2000 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002001 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002002
John McCallfaf5fb42010-08-26 23:41:50 +00002003 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002004 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002005 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002006 }
Chris Lattner8f697062008-01-25 18:59:06 +00002007 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002008
2009 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002010 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002011 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002012 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002013 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002014 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002015
John McCallba7bf592010-08-24 05:47:05 +00002016 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2017 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002018}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002019
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002020/// \brief Parse the remainder of an Objective-C message following the
2021/// '[' objc-receiver.
2022///
2023/// This routine handles sends to super, class messages (sent to a
2024/// class name), and instance messages (sent to an object), and the
2025/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2026/// ReceiverExpr, respectively. Only one of these parameters may have
2027/// a valid value.
2028///
2029/// \param LBracLoc The location of the opening '['.
2030///
2031/// \param SuperLoc If this is a send to 'super', the location of the
2032/// 'super' keyword that indicates a send to the superclass.
2033///
2034/// \param ReceiverType If this is a class message, the type of the
2035/// class we are sending a message to.
2036///
2037/// \param ReceiverExpr If this is an instance message, the expression
2038/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002039///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002040/// objc-message-args:
2041/// objc-selector
2042/// objc-keywordarg-list
2043///
2044/// objc-keywordarg-list:
2045/// objc-keywordarg
2046/// objc-keywordarg-list objc-keywordarg
2047///
Mike Stump11289f42009-09-09 15:08:12 +00002048/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002049/// selector-name[opt] ':' objc-keywordexpr
2050///
2051/// objc-keywordexpr:
2052/// nonempty-expr-list
2053///
2054/// nonempty-expr-list:
2055/// assignment-expression
2056/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002057///
John McCalldadc5752010-08-24 06:29:42 +00002058ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002059Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002060 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002061 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002062 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002063 InMessageExpressionRAIIObject InMessage(*this, true);
2064
Steve Naroffeae65032009-11-07 02:08:14 +00002065 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002066 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002067 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2068 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002069 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002070 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2071 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002072 else
John McCallb268a282010-08-23 23:25:46 +00002073 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002074 0, 0, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002075 ConsumeCodeCompletionToken();
Steve Naroffeae65032009-11-07 02:08:14 +00002076 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002077
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002078 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002079 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002080 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002081
Anders Carlsson978f08d2009-02-14 18:21:46 +00002082 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00002083
Steve Narofff73590d2007-09-27 14:38:14 +00002084 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00002085 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002086
Chris Lattner0ef13522007-10-09 17:51:17 +00002087 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002088 while (1) {
2089 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002090 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00002091
Chris Lattner0ef13522007-10-09 17:51:17 +00002092 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002093 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002094 // We must manually skip to a ']', otherwise the expression skipper will
2095 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2096 // the enclosing expression.
2097 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002098 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002099 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002100
Steve Narofff73590d2007-09-27 14:38:14 +00002101 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002102 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002103
2104 if (Tok.is(tok::code_completion)) {
2105 if (SuperLoc.isValid())
2106 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2107 KeyIdents.data(),
2108 KeyIdents.size(),
2109 /*AtArgumentEpression=*/true);
2110 else if (ReceiverType)
2111 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2112 KeyIdents.data(),
2113 KeyIdents.size(),
2114 /*AtArgumentEpression=*/true);
2115 else
2116 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2117 KeyIdents.data(),
2118 KeyIdents.size(),
2119 /*AtArgumentEpression=*/true);
2120
2121 ConsumeCodeCompletionToken();
2122 SkipUntil(tok::r_square);
2123 return ExprError();
2124 }
2125
John McCalldadc5752010-08-24 06:29:42 +00002126 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002127 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002128 // We must manually skip to a ']', otherwise the expression skipper will
2129 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2130 // the enclosing expression.
2131 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002132 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002133 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002134
Steve Naroff486760a2007-09-17 20:25:27 +00002135 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002136 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002137
Douglas Gregor1b605f72009-11-19 01:08:35 +00002138 // Code completion after each argument.
2139 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002140 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002141 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002142 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002143 KeyIdents.size(),
2144 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002145 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002146 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002147 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002148 KeyIdents.size(),
2149 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002150 else
John McCallb268a282010-08-23 23:25:46 +00002151 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002152 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002153 KeyIdents.size(),
2154 /*AtArgumentEpression=*/false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002155 ConsumeCodeCompletionToken();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002156 SkipUntil(tok::r_square);
2157 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002158 }
2159
Steve Naroff486760a2007-09-17 20:25:27 +00002160 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002161 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002162 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002163 break;
2164 // We have a selector or a colon, continue parsing.
2165 }
2166 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002167 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002168 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002169 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002170 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002171 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002172 // We must manually skip to a ']', otherwise the expression skipper will
2173 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2174 // the enclosing expression.
2175 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002176 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002177 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002178
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002179 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002180 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002181 }
2182 } else if (!selIdent) {
2183 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002184
Chris Lattner197a3012008-08-05 06:19:09 +00002185 // We must manually skip to a ']', otherwise the expression skipper will
2186 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2187 // the enclosing expression.
2188 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002189 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002190 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002191
Chris Lattner0ef13522007-10-09 17:51:17 +00002192 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002193 if (Tok.is(tok::identifier))
2194 Diag(Tok, diag::err_expected_colon);
2195 else
2196 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002197 // We must manually skip to a ']', otherwise the expression skipper will
2198 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2199 // the enclosing expression.
2200 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002201 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002202 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002203
Chris Lattner8f697062008-01-25 18:59:06 +00002204 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002205
Steve Naroffe61bfa82007-10-05 18:42:47 +00002206 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00002207 if (nKeys == 0)
2208 KeyIdents.push_back(selIdent);
2209 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002210
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002211 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002212 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002213 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002214 MultiExprArg(Actions,
2215 KeyExprs.take(),
2216 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002217 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002218 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002219 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002220 MultiExprArg(Actions,
2221 KeyExprs.take(),
2222 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002223 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002224 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002225 MultiExprArg(Actions,
2226 KeyExprs.take(),
2227 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002228}
2229
John McCalldadc5752010-08-24 06:29:42 +00002230ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2231 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002232 if (Res.isInvalid()) return move(Res);
2233
Chris Lattnere002fbe2007-12-12 01:04:12 +00002234 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2235 // expressions. At this point, we know that the only valid thing that starts
2236 // with '@' is an @"".
2237 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002238 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002239 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002240 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002241
Chris Lattnere002fbe2007-12-12 01:04:12 +00002242 while (Tok.is(tok::at)) {
2243 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002244
Sebastian Redlc13f2682008-12-09 20:22:58 +00002245 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002246 if (!isTokenStringLiteral())
2247 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002248
John McCalldadc5752010-08-24 06:29:42 +00002249 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002250 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002251 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002252
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002253 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002254 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002255
2256 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2257 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002258}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002259
2260/// objc-encode-expression:
2261/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002262ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002263Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002264 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002265
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002266 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002267
Chris Lattner197a3012008-08-05 06:19:09 +00002268 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002269 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2270
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002271 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002272
Douglas Gregor220cac52009-02-18 17:45:20 +00002273 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002274
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002275 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002276
Douglas Gregor220cac52009-02-18 17:45:20 +00002277 if (Ty.isInvalid())
2278 return ExprError();
2279
Mike Stump11289f42009-09-09 15:08:12 +00002280 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00002281 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002282}
Anders Carlssone01493d2007-08-23 15:25:28 +00002283
2284/// objc-protocol-expression
2285/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002286ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002287Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002288 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002289
Chris Lattner197a3012008-08-05 06:19:09 +00002290 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002291 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2292
Anders Carlssone01493d2007-08-23 15:25:28 +00002293 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002294
Chris Lattner197a3012008-08-05 06:19:09 +00002295 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002296 return ExprError(Diag(Tok, diag::err_expected_ident));
2297
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002298 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002299 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002300
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002301 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00002302
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002303 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2304 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00002305}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002306
2307/// objc-selector-expression
2308/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002309ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002310 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002311
Chris Lattner197a3012008-08-05 06:19:09 +00002312 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002313 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2314
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002315 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002316 SourceLocation LParenLoc = ConsumeParen();
2317 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002318
2319 if (Tok.is(tok::code_completion)) {
2320 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2321 KeyIdents.size());
2322 ConsumeCodeCompletionToken();
2323 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2324 return ExprError();
2325 }
2326
Chris Lattner4f472a32009-04-11 18:13:45 +00002327 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002328 if (!SelIdent && // missing selector name.
2329 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002330 return ExprError(Diag(Tok, diag::err_expected_ident));
2331
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002332 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002333 unsigned nColons = 0;
2334 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002335 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002336 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2337 ++nColons;
2338 KeyIdents.push_back(0);
2339 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002340 return ExprError(Diag(Tok, diag::err_expected_colon));
2341
Chris Lattner1ba64452010-08-27 22:32:41 +00002342 ++nColons;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002343 ConsumeToken(); // Eat the ':'.
2344 if (Tok.is(tok::r_paren))
2345 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002346
2347 if (Tok.is(tok::code_completion)) {
2348 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2349 KeyIdents.size());
2350 ConsumeCodeCompletionToken();
2351 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2352 return ExprError();
2353 }
2354
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002355 // Check for another keyword selector.
2356 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002357 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002358 KeyIdents.push_back(SelIdent);
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002359 if (!SelIdent && Tok.isNot(tok::colon))
2360 break;
2361 }
Steve Naroff152dd812007-12-05 22:21:29 +00002362 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002363 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00002364 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002365 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2366 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00002367 }