blob: 4f28321827a286cb3f796d100a08adcf90cc4373 [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: {
John McCall084e83d2011-03-24 11:26:52 +000044 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000045 return ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
46 }
47 case tok::objc_protocol: {
John McCall084e83d2011-03-24 11:26:52 +000048 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +000049 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 =
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000330 ParseObjCMethodPrototype(interfaceDecl, MethodImplKind, false);
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,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000343 MethodImplKind, false);
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 McCall084e83d2011-03-24 11:26:52 +0000374 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000375 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.
John McCall084e83d2011-03-24 11:26:52 +0000442 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +0000443 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +0000444
John McCall405988b2011-03-26 01:53:26 +0000445 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000446 break;
Steve Naroffca85d1d2007-09-05 23:30:30 +0000447 }
Steve Naroff99264b42007-08-22 16:35:03 +0000448 }
Chris Lattnerda9fb152008-10-20 06:10:06 +0000449
450 // We break out of the big loop in two cases: when we see @end or when we see
451 // EOF. In the former case, eat the @end. In the later case, emit an error.
Douglas Gregorf48706c2009-12-07 09:27:33 +0000452 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000453 Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000454 ConsumeCodeCompletionToken();
Douglas Gregorf48706c2009-12-07 09:27:33 +0000455 } else if (Tok.isObjCAtKeyword(tok::objc_end))
Chris Lattnerda9fb152008-10-20 06:10:06 +0000456 ConsumeToken(); // the "end" identifier
457 else
458 Diag(Tok, diag::err_objc_missing_end);
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattnerbb8cc182008-10-20 05:57:40 +0000460 // Insert collected methods declarations into the @interface object.
Chris Lattnerda9fb152008-10-20 06:10:06 +0000461 // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
Douglas Gregor0be31a22010-07-02 17:43:08 +0000462 Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
Mike Stump11289f42009-09-09 15:08:12 +0000463 allMethods.data(), allMethods.size(),
Jay Foad7d0479f2009-05-21 09:52:38 +0000464 allProperties.data(), allProperties.size(),
465 allTUVariables.data(), allTUVariables.size());
Steve Naroff99264b42007-08-22 16:35:03 +0000466}
467
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000468/// Parse property attribute declarations.
469///
470/// property-attr-decl: '(' property-attrlist ')'
471/// property-attrlist:
472/// property-attribute
473/// property-attrlist ',' property-attribute
474/// property-attribute:
475/// getter '=' identifier
476/// setter '=' identifier ':'
477/// readonly
478/// readwrite
479/// assign
480/// retain
481/// copy
482/// nonatomic
John McCall31168b02011-06-15 23:02:42 +0000483/// atomic
484/// strong
485/// weak
486/// unsafe_unretained
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000487///
Douglas Gregor87e92752010-12-21 17:34:17 +0000488void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000489 assert(Tok.getKind() == tok::l_paren);
Chris Lattner43c76c32008-10-20 07:00:43 +0000490 SourceLocation LHSLoc = ConsumeParen(); // consume '('
Mike Stump11289f42009-09-09 15:08:12 +0000491
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000492 while (1) {
Steve Naroff936354c2009-10-08 21:55:05 +0000493 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000494 Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000495 ConsumeCodeCompletionToken();
Steve Naroff936354c2009-10-08 21:55:05 +0000496 }
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000497 const IdentifierInfo *II = Tok.getIdentifierInfo();
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattner76619232008-10-20 07:22:18 +0000499 // If this is not an identifier at all, bail out early.
500 if (II == 0) {
501 MatchRHSPunctuation(tok::r_paren, LHSLoc);
502 return;
503 }
Mike Stump11289f42009-09-09 15:08:12 +0000504
Chris Lattner1db33542008-10-20 07:37:22 +0000505 SourceLocation AttrName = ConsumeToken(); // consume last attribute name
Mike Stump11289f42009-09-09 15:08:12 +0000506
Chris Lattner68e48682008-11-20 04:42:34 +0000507 if (II->isStr("readonly"))
Chris Lattner825bca12008-10-20 07:39:53 +0000508 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
Chris Lattner68e48682008-11-20 04:42:34 +0000509 else if (II->isStr("assign"))
Chris Lattner825bca12008-10-20 07:39:53 +0000510 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
John McCall31168b02011-06-15 23:02:42 +0000511 else if (II->isStr("unsafe_unretained"))
512 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
Chris Lattner68e48682008-11-20 04:42:34 +0000513 else if (II->isStr("readwrite"))
Chris Lattner825bca12008-10-20 07:39:53 +0000514 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
Chris Lattner68e48682008-11-20 04:42:34 +0000515 else if (II->isStr("retain"))
Chris Lattner825bca12008-10-20 07:39:53 +0000516 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
John McCall31168b02011-06-15 23:02:42 +0000517 else if (II->isStr("strong"))
518 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
Chris Lattner68e48682008-11-20 04:42:34 +0000519 else if (II->isStr("copy"))
Chris Lattner825bca12008-10-20 07:39:53 +0000520 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
Chris Lattner68e48682008-11-20 04:42:34 +0000521 else if (II->isStr("nonatomic"))
Chris Lattner825bca12008-10-20 07:39:53 +0000522 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
Fariborz Jahanianc3bcde02011-06-11 00:45:12 +0000523 else if (II->isStr("atomic"))
524 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
John McCall31168b02011-06-15 23:02:42 +0000525 else if (II->isStr("weak"))
526 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
Chris Lattner68e48682008-11-20 04:42:34 +0000527 else if (II->isStr("getter") || II->isStr("setter")) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000528 bool IsSetter = II->getNameStart()[0] == 's';
529
Chris Lattner825bca12008-10-20 07:39:53 +0000530 // getter/setter require extra treatment.
Anders Carlssonfe15a782010-10-02 17:45:21 +0000531 unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
532 diag::err_objc_expected_equal_for_getter;
533
534 if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
Chris Lattner43c76c32008-10-20 07:00:43 +0000535 return;
Mike Stump11289f42009-09-09 15:08:12 +0000536
Douglas Gregorc8537c52009-11-19 07:41:15 +0000537 if (Tok.is(tok::code_completion)) {
Anders Carlssonfe15a782010-10-02 17:45:21 +0000538 if (IsSetter)
Douglas Gregor87e92752010-12-21 17:34:17 +0000539 Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl);
Douglas Gregorc8537c52009-11-19 07:41:15 +0000540 else
Douglas Gregor87e92752010-12-21 17:34:17 +0000541 Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000542 ConsumeCodeCompletionToken();
Douglas Gregorc8537c52009-11-19 07:41:15 +0000543 }
544
Anders Carlssonfe15a782010-10-02 17:45:21 +0000545
546 SourceLocation SelLoc;
547 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
548
549 if (!SelIdent) {
550 Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
551 << IsSetter;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000552 SkipUntil(tok::r_paren);
553 return;
554 }
Mike Stump11289f42009-09-09 15:08:12 +0000555
Anders Carlssonfe15a782010-10-02 17:45:21 +0000556 if (IsSetter) {
Chris Lattnerbeca7702008-10-20 07:24:39 +0000557 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000558 DS.setSetterName(SelIdent);
Mike Stump11289f42009-09-09 15:08:12 +0000559
Fariborz Jahanian8efe0ec2010-02-15 22:20:11 +0000560 if (ExpectAndConsume(tok::colon,
561 diag::err_expected_colon_after_setter_name, "",
Chris Lattner1db33542008-10-20 07:37:22 +0000562 tok::r_paren))
Chris Lattnerbeca7702008-10-20 07:24:39 +0000563 return;
Chris Lattnerbeca7702008-10-20 07:24:39 +0000564 } else {
565 DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
Anders Carlssonfe15a782010-10-02 17:45:21 +0000566 DS.setGetterName(SelIdent);
Chris Lattnerbeca7702008-10-20 07:24:39 +0000567 }
Chris Lattner825bca12008-10-20 07:39:53 +0000568 } else {
Chris Lattner406c0962008-11-19 07:49:38 +0000569 Diag(AttrName, diag::err_objc_expected_property_attr) << II;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000570 SkipUntil(tok::r_paren);
571 return;
Chris Lattner64b1f2f2008-10-20 07:15:22 +0000572 }
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner1db33542008-10-20 07:37:22 +0000574 if (Tok.isNot(tok::comma))
575 break;
Mike Stump11289f42009-09-09 15:08:12 +0000576
Chris Lattner1db33542008-10-20 07:37:22 +0000577 ConsumeToken();
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000578 }
Mike Stump11289f42009-09-09 15:08:12 +0000579
Chris Lattner1db33542008-10-20 07:37:22 +0000580 MatchRHSPunctuation(tok::r_paren, LHSLoc);
Fariborz Jahanian9fca6df2007-08-31 16:11:31 +0000581}
582
Steve Naroff09bf8152007-09-06 21:24:23 +0000583/// objc-method-proto:
Mike Stump11289f42009-09-09 15:08:12 +0000584/// objc-instance-method objc-method-decl objc-method-attributes[opt]
Steve Naroff09bf8152007-09-06 21:24:23 +0000585/// objc-class-method objc-method-decl objc-method-attributes[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000586///
587/// objc-instance-method: '-'
588/// objc-class-method: '+'
589///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000590/// objc-method-attributes: [OBJC2]
591/// __attribute__((deprecated))
592///
John McCall48871652010-08-21 09:40:31 +0000593Decl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000594 tok::ObjCKeywordKind MethodImplKind,
595 bool MethodDefinition) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000596 assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
Steve Naroff99264b42007-08-22 16:35:03 +0000597
Mike Stump11289f42009-09-09 15:08:12 +0000598 tok::TokenKind methodType = Tok.getKind();
Steve Naroff161a92b2007-10-26 20:53:56 +0000599 SourceLocation mLoc = ConsumeToken();
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000600 Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind,
601 MethodDefinition);
Steve Naroff09bf8152007-09-06 21:24:23 +0000602 // Since this rule is used for both method declarations and definitions,
Steve Naroffacb1e742007-09-10 20:51:04 +0000603 // the caller is (optionally) responsible for consuming the ';'.
Steve Naroffca85d1d2007-09-05 23:30:30 +0000604 return MDecl;
Steve Naroff99264b42007-08-22 16:35:03 +0000605}
606
607/// objc-selector:
608/// identifier
609/// one of
610/// enum struct union if else while do for switch case default
611/// break continue return goto asm sizeof typeof __alignof
612/// unsigned long const short volatile signed restrict _Complex
613/// in out inout bycopy byref oneway int char float double void _Bool
614///
Chris Lattner4f472a32009-04-11 18:13:45 +0000615IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
Fariborz Jahanian0389df4a2010-09-03 01:26:16 +0000616
Chris Lattner5700fab2007-10-07 02:00:24 +0000617 switch (Tok.getKind()) {
618 default:
619 return 0;
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000620 case tok::ampamp:
621 case tok::ampequal:
622 case tok::amp:
623 case tok::pipe:
624 case tok::tilde:
625 case tok::exclaim:
626 case tok::exclaimequal:
627 case tok::pipepipe:
628 case tok::pipeequal:
629 case tok::caret:
630 case tok::caretequal: {
Fariborz Jahaniandadfc1c2010-09-03 18:01:09 +0000631 std::string ThisTok(PP.getSpelling(Tok));
Fariborz Jahanian9e42a952010-09-03 17:33:04 +0000632 if (isalpha(ThisTok[0])) {
633 IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
634 Tok.setKind(tok::identifier);
635 SelectorLoc = ConsumeToken();
636 return II;
637 }
638 return 0;
639 }
640
Chris Lattner5700fab2007-10-07 02:00:24 +0000641 case tok::identifier:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000642 case tok::kw_asm:
Chris Lattner5700fab2007-10-07 02:00:24 +0000643 case tok::kw_auto:
Chris Lattnerbb31a422007-11-15 05:25:19 +0000644 case tok::kw_bool:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000645 case tok::kw_break:
646 case tok::kw_case:
647 case tok::kw_catch:
648 case tok::kw_char:
649 case tok::kw_class:
650 case tok::kw_const:
651 case tok::kw_const_cast:
652 case tok::kw_continue:
653 case tok::kw_default:
654 case tok::kw_delete:
655 case tok::kw_do:
656 case tok::kw_double:
657 case tok::kw_dynamic_cast:
658 case tok::kw_else:
659 case tok::kw_enum:
660 case tok::kw_explicit:
661 case tok::kw_export:
662 case tok::kw_extern:
663 case tok::kw_false:
664 case tok::kw_float:
665 case tok::kw_for:
666 case tok::kw_friend:
667 case tok::kw_goto:
668 case tok::kw_if:
669 case tok::kw_inline:
670 case tok::kw_int:
671 case tok::kw_long:
672 case tok::kw_mutable:
673 case tok::kw_namespace:
674 case tok::kw_new:
675 case tok::kw_operator:
676 case tok::kw_private:
677 case tok::kw_protected:
678 case tok::kw_public:
679 case tok::kw_register:
680 case tok::kw_reinterpret_cast:
681 case tok::kw_restrict:
682 case tok::kw_return:
683 case tok::kw_short:
684 case tok::kw_signed:
685 case tok::kw_sizeof:
686 case tok::kw_static:
687 case tok::kw_static_cast:
688 case tok::kw_struct:
689 case tok::kw_switch:
690 case tok::kw_template:
691 case tok::kw_this:
692 case tok::kw_throw:
693 case tok::kw_true:
694 case tok::kw_try:
695 case tok::kw_typedef:
696 case tok::kw_typeid:
697 case tok::kw_typename:
698 case tok::kw_typeof:
699 case tok::kw_union:
700 case tok::kw_unsigned:
701 case tok::kw_using:
702 case tok::kw_virtual:
703 case tok::kw_void:
704 case tok::kw_volatile:
705 case tok::kw_wchar_t:
706 case tok::kw_while:
Chris Lattner5700fab2007-10-07 02:00:24 +0000707 case tok::kw__Bool:
708 case tok::kw__Complex:
Anders Carlssonf93f56a2008-08-23 21:00:01 +0000709 case tok::kw___alignof:
Chris Lattner5700fab2007-10-07 02:00:24 +0000710 IdentifierInfo *II = Tok.getIdentifierInfo();
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000711 SelectorLoc = ConsumeToken();
Chris Lattner5700fab2007-10-07 02:00:24 +0000712 return II;
Fariborz Jahanianfa80e802007-09-27 19:52:15 +0000713 }
Steve Naroff99264b42007-08-22 16:35:03 +0000714}
715
Fariborz Jahanian83615522008-01-02 22:54:34 +0000716/// objc-for-collection-in: 'in'
717///
Fariborz Jahanian3622e592008-01-04 23:04:08 +0000718bool Parser::isTokIdentifier_in() const {
Fariborz Jahanian732b8c22008-01-03 17:55:25 +0000719 // FIXME: May have to do additional look-ahead to only allow for
720 // valid tokens following an 'in'; such as an identifier, unary operators,
721 // '[' etc.
Mike Stump11289f42009-09-09 15:08:12 +0000722 return (getLang().ObjC2 && Tok.is(tok::identifier) &&
Chris Lattnerce90ef52008-08-23 02:02:23 +0000723 Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
Fariborz Jahanian83615522008-01-02 22:54:34 +0000724}
725
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000726/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
Chris Lattner61511e12007-12-12 06:56:32 +0000727/// qualifier list and builds their bitmask representation in the input
728/// argument.
Steve Naroff99264b42007-08-22 16:35:03 +0000729///
730/// objc-type-qualifiers:
731/// objc-type-qualifier
732/// objc-type-qualifiers objc-type-qualifier
733///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000734void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
735 ObjCTypeNameContext Context) {
Chris Lattner61511e12007-12-12 06:56:32 +0000736 while (1) {
Douglas Gregor99fa2642010-08-24 01:06:58 +0000737 if (Tok.is(tok::code_completion)) {
Douglas Gregor95d3e372011-03-08 19:17:54 +0000738 Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
739 Context == OTN_ParameterType);
Douglas Gregor99fa2642010-08-24 01:06:58 +0000740 ConsumeCodeCompletionToken();
741 }
742
Chris Lattner5e530bc2007-12-27 19:57:00 +0000743 if (Tok.isNot(tok::identifier))
Chris Lattner61511e12007-12-12 06:56:32 +0000744 return;
Mike Stump11289f42009-09-09 15:08:12 +0000745
Chris Lattner61511e12007-12-12 06:56:32 +0000746 const IdentifierInfo *II = Tok.getIdentifierInfo();
747 for (unsigned i = 0; i != objc_NumQuals; ++i) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000748 if (II != ObjCTypeQuals[i])
Chris Lattner61511e12007-12-12 06:56:32 +0000749 continue;
Mike Stump11289f42009-09-09 15:08:12 +0000750
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000751 ObjCDeclSpec::ObjCDeclQualifier Qual;
Chris Lattner61511e12007-12-12 06:56:32 +0000752 switch (i) {
753 default: assert(0 && "Unknown decl qualifier");
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000754 case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
755 case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
756 case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
757 case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
758 case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
759 case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
Chris Lattner61511e12007-12-12 06:56:32 +0000760 }
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000761 DS.setObjCDeclQualifier(Qual);
Chris Lattner61511e12007-12-12 06:56:32 +0000762 ConsumeToken();
763 II = 0;
764 break;
765 }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattner61511e12007-12-12 06:56:32 +0000767 // If this wasn't a recognized qualifier, bail out.
768 if (II) return;
769 }
770}
771
772/// objc-type-name:
773/// '(' objc-type-qualifiers[opt] type-name ')'
774/// '(' objc-type-qualifiers[opt] ')'
775///
Douglas Gregor95d3e372011-03-08 19:17:54 +0000776ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
777 ObjCTypeNameContext Context) {
Chris Lattner0ef13522007-10-09 17:51:17 +0000778 assert(Tok.is(tok::l_paren) && "expected (");
Mike Stump11289f42009-09-09 15:08:12 +0000779
Chris Lattnerb7954432008-10-22 03:52:06 +0000780 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner2ebb1782008-08-23 01:48:03 +0000781 SourceLocation TypeStartLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +0000782
Fariborz Jahaniand822d682007-10-31 21:59:43 +0000783 // Parse type qualifiers, in, inout, etc.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000784 ParseObjCTypeQualifierList(DS, Context);
Steve Naroff7e901fd2007-08-22 23:18:22 +0000785
John McCallba7bf592010-08-24 05:47:05 +0000786 ParsedType Ty;
Douglas Gregor220cac52009-02-18 17:45:20 +0000787 if (isTypeSpecifierQualifier()) {
John McCall31168b02011-06-15 23:02:42 +0000788 TypeResult TypeSpec =
789 ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
Douglas Gregor220cac52009-02-18 17:45:20 +0000790 if (!TypeSpec.isInvalid())
791 Ty = TypeSpec.get();
792 }
John McCall31168b02011-06-15 23:02:42 +0000793
Steve Naroff90255b42008-10-21 14:15:04 +0000794 if (Tok.is(tok::r_paren))
Chris Lattnerb7954432008-10-22 03:52:06 +0000795 ConsumeParen();
796 else if (Tok.getLocation() == TypeStartLoc) {
797 // If we didn't eat any tokens, then this isn't a type.
Chris Lattner6d29c102008-11-18 07:48:38 +0000798 Diag(Tok, diag::err_expected_type);
Chris Lattnerb7954432008-10-22 03:52:06 +0000799 SkipUntil(tok::r_paren);
800 } else {
801 // Otherwise, we found *something*, but didn't get a ')' in the right
802 // place. Emit an error then return what we have as the type.
803 MatchRHSPunctuation(tok::r_paren, LParenLoc);
804 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000805 return Ty;
Steve Naroff99264b42007-08-22 16:35:03 +0000806}
807
808/// objc-method-decl:
809/// objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000810/// objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000811/// objc-type-name objc-selector
Steve Narofff1bc45b2007-08-22 18:35:33 +0000812/// objc-type-name objc-keyword-selector objc-parmlist[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000813///
814/// objc-keyword-selector:
Mike Stump11289f42009-09-09 15:08:12 +0000815/// objc-keyword-decl
Steve Naroff99264b42007-08-22 16:35:03 +0000816/// objc-keyword-selector objc-keyword-decl
817///
818/// objc-keyword-decl:
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000819/// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
820/// objc-selector ':' objc-keyword-attributes[opt] identifier
821/// ':' objc-type-name objc-keyword-attributes[opt] identifier
822/// ':' objc-keyword-attributes[opt] identifier
Steve Naroff99264b42007-08-22 16:35:03 +0000823///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000824/// objc-parmlist:
825/// objc-parms objc-ellipsis[opt]
Steve Naroff99264b42007-08-22 16:35:03 +0000826///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000827/// objc-parms:
828/// objc-parms , parameter-declaration
Steve Naroff99264b42007-08-22 16:35:03 +0000829///
Steve Narofff1bc45b2007-08-22 18:35:33 +0000830/// objc-ellipsis:
Steve Naroff99264b42007-08-22 16:35:03 +0000831/// , ...
832///
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000833/// objc-keyword-attributes: [OBJC2]
834/// __attribute__((unused))
835///
John McCall48871652010-08-21 09:40:31 +0000836Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
Douglas Gregorc49f5b22010-08-23 18:23:48 +0000837 tok::TokenKind mType,
838 Decl *IDecl,
Fariborz Jahanianc677f692011-03-12 18:54:30 +0000839 tok::ObjCKeywordKind MethodImplKind,
840 bool MethodDefinition) {
John McCall28a6aea2009-11-04 02:18:39 +0000841 ParsingDeclRAIIObject PD(*this);
842
Douglas Gregor636a61e2010-04-07 00:21:17 +0000843 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000844 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
John McCallba7bf592010-08-24 05:47:05 +0000845 /*ReturnType=*/ ParsedType(), IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000846 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000847 }
848
Chris Lattner2ebb1782008-08-23 01:48:03 +0000849 // Parse the return type if present.
John McCallba7bf592010-08-24 05:47:05 +0000850 ParsedType ReturnType;
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000851 ObjCDeclSpec DSRet;
Chris Lattner0ef13522007-10-09 17:51:17 +0000852 if (Tok.is(tok::l_paren))
Douglas Gregor95d3e372011-03-08 19:17:54 +0000853 ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
Mike Stump11289f42009-09-09 15:08:12 +0000854
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000855 // If attributes exist before the method, parse them.
John McCall084e83d2011-03-24 11:26:52 +0000856 ParsedAttributes methodAttrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +0000857 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000858 MaybeParseGNUAttributes(methodAttrs);
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000859
Douglas Gregor636a61e2010-04-07 00:21:17 +0000860 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +0000861 Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
Douglas Gregor636a61e2010-04-07 00:21:17 +0000862 ReturnType, IDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +0000863 ConsumeCodeCompletionToken();
Douglas Gregor636a61e2010-04-07 00:21:17 +0000864 }
865
Ted Kremenek66f2d6b2010-02-18 23:05:16 +0000866 // Now parse the selector.
John McCall31168b02011-06-15 23:02:42 +0000867 SourceLocation SelectorStartLoc = Tok.getLocation();
Steve Naroff161a92b2007-10-26 20:53:56 +0000868 SourceLocation selLoc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000869 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
Chris Lattner2ebb1782008-08-23 01:48:03 +0000870
Steve Naroff7a54c0d2009-02-11 20:43:13 +0000871 // An unnamed colon is valid.
872 if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
Chris Lattner6d29c102008-11-18 07:48:38 +0000873 Diag(Tok, diag::err_expected_selector_for_method)
874 << SourceRange(mLoc, Tok.getLocation());
Chris Lattner2ebb1782008-08-23 01:48:03 +0000875 // Skip until we get a ; or {}.
876 SkipUntil(tok::r_brace);
John McCall48871652010-08-21 09:40:31 +0000877 return 0;
Chris Lattner2ebb1782008-08-23 01:48:03 +0000878 }
Mike Stump11289f42009-09-09 15:08:12 +0000879
Fariborz Jahanian60462092010-04-08 00:30:06 +0000880 llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
Chris Lattner0ef13522007-10-09 17:51:17 +0000881 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000882 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +0000883 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +0000884 MaybeParseGNUAttributes(methodAttrs);
Mike Stump11289f42009-09-09 15:08:12 +0000885
Chris Lattner5700fab2007-10-07 02:00:24 +0000886 Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
John McCall48871652010-08-21 09:40:31 +0000887 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000888 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Douglas Gregor33823722011-06-11 01:09:30 +0000889 mType, IDecl, DSRet, ReturnType,
890 selLoc, Sel, 0,
Fariborz Jahanian60462092010-04-08 00:30:06 +0000891 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +0000892 methodAttrs.getList(), MethodImplKind,
893 false, MethodDefinition);
John McCall28a6aea2009-11-04 02:18:39 +0000894 PD.complete(Result);
895 return Result;
Chris Lattner5700fab2007-10-07 02:00:24 +0000896 }
Steve Naroffca85d1d2007-09-05 23:30:30 +0000897
Steve Narofff73590d2007-09-27 14:38:14 +0000898 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
John McCallfaf5fb42010-08-26 23:41:50 +0000899 llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000900 ParseScope PrototypeScope(this,
901 Scope::FunctionPrototypeScope|Scope::DeclScope);
John McCall084e83d2011-03-24 11:26:52 +0000902
903 AttributePool allParamAttrs(AttrFactory);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +0000904
Chris Lattner5700fab2007-10-07 02:00:24 +0000905 while (1) {
John McCall084e83d2011-03-24 11:26:52 +0000906 ParsedAttributes paramAttrs(AttrFactory);
John McCallfaf5fb42010-08-26 23:41:50 +0000907 Sema::ObjCArgInfo ArgInfo;
Mike Stump11289f42009-09-09 15:08:12 +0000908
Chris Lattner5700fab2007-10-07 02:00:24 +0000909 // Each iteration parses a single keyword argument.
Chris Lattner0ef13522007-10-09 17:51:17 +0000910 if (Tok.isNot(tok::colon)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000911 Diag(Tok, diag::err_expected_colon);
912 break;
913 }
914 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +0000915
John McCallba7bf592010-08-24 05:47:05 +0000916 ArgInfo.Type = ParsedType();
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000917 if (Tok.is(tok::l_paren)) // Parse the argument type if present.
Douglas Gregor95d3e372011-03-08 19:17:54 +0000918 ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000919
Chris Lattner5700fab2007-10-07 02:00:24 +0000920 // If attributes exist before the argument name, parse them.
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000921 ArgInfo.ArgAttrs = 0;
John McCall53fa7142010-12-24 02:08:15 +0000922 if (getLang().ObjC2) {
John McCall084e83d2011-03-24 11:26:52 +0000923 MaybeParseGNUAttributes(paramAttrs);
924 ArgInfo.ArgAttrs = paramAttrs.getList();
John McCall53fa7142010-12-24 02:08:15 +0000925 }
Steve Naroff0b6a01a2007-08-22 22:17:26 +0000926
Douglas Gregor45879692010-07-08 23:37:41 +0000927 // Code completion for the next piece of the selector.
928 if (Tok.is(tok::code_completion)) {
929 ConsumeCodeCompletionToken();
930 KeyIdents.push_back(SelIdent);
931 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
932 mType == tok::minus,
933 /*AtParameterName=*/true,
934 ReturnType,
935 KeyIdents.data(),
936 KeyIdents.size());
937 KeyIdents.pop_back();
938 break;
939 }
940
Chris Lattner0ef13522007-10-09 17:51:17 +0000941 if (Tok.isNot(tok::identifier)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000942 Diag(Tok, diag::err_expected_ident); // missing argument name.
943 break;
Steve Narofff1bc45b2007-08-22 18:35:33 +0000944 }
Mike Stump11289f42009-09-09 15:08:12 +0000945
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000946 ArgInfo.Name = Tok.getIdentifierInfo();
947 ArgInfo.NameLoc = Tok.getLocation();
Chris Lattner5700fab2007-10-07 02:00:24 +0000948 ConsumeToken(); // Eat the identifier.
Mike Stump11289f42009-09-09 15:08:12 +0000949
Chris Lattnerd8626fd2009-04-11 18:57:04 +0000950 ArgInfos.push_back(ArgInfo);
951 KeyIdents.push_back(SelIdent);
952
John McCall084e83d2011-03-24 11:26:52 +0000953 // Make sure the attributes persist.
954 allParamAttrs.takeAllFrom(paramAttrs.getPool());
955
Douglas Gregor95887f92010-07-08 23:20:03 +0000956 // Code completion for the next piece of the selector.
957 if (Tok.is(tok::code_completion)) {
958 ConsumeCodeCompletionToken();
959 Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
960 mType == tok::minus,
Douglas Gregor45879692010-07-08 23:37:41 +0000961 /*AtParameterName=*/false,
Douglas Gregor95887f92010-07-08 23:20:03 +0000962 ReturnType,
963 KeyIdents.data(),
964 KeyIdents.size());
965 break;
966 }
967
Chris Lattner5700fab2007-10-07 02:00:24 +0000968 // Check for another keyword selector.
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000969 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +0000970 SelIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +0000971 if (!SelIdent && Tok.isNot(tok::colon))
Chris Lattner5700fab2007-10-07 02:00:24 +0000972 break;
973 // We have a selector or a colon, continue parsing.
Steve Narofff1bc45b2007-08-22 18:35:33 +0000974 }
Mike Stump11289f42009-09-09 15:08:12 +0000975
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000976 bool isVariadic = false;
Mike Stump11289f42009-09-09 15:08:12 +0000977
Chris Lattner5700fab2007-10-07 02:00:24 +0000978 // Parse the (optional) parameter list.
Chris Lattner0ef13522007-10-09 17:51:17 +0000979 while (Tok.is(tok::comma)) {
Chris Lattner5700fab2007-10-07 02:00:24 +0000980 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +0000981 if (Tok.is(tok::ellipsis)) {
Steve Naroffd8ea1ac2007-11-15 12:35:21 +0000982 isVariadic = true;
Chris Lattner5700fab2007-10-07 02:00:24 +0000983 ConsumeToken();
984 break;
985 }
John McCall084e83d2011-03-24 11:26:52 +0000986 DeclSpec DS(AttrFactory);
Chris Lattner5700fab2007-10-07 02:00:24 +0000987 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +0000988 // Parse the declarator.
Chris Lattner5700fab2007-10-07 02:00:24 +0000989 Declarator ParmDecl(DS, Declarator::PrototypeContext);
990 ParseDeclarator(ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000991 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
John McCall48871652010-08-21 09:40:31 +0000992 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Fariborz Jahanian60462092010-04-08 00:30:06 +0000993 CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
994 ParmDecl.getIdentifierLoc(),
995 Param,
996 0));
997
Chris Lattner5700fab2007-10-07 02:00:24 +0000998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Cameron Esfahanif6c73c42010-10-12 00:21:25 +00001000 // FIXME: Add support for optional parameter list...
Fariborz Jahanian33d03742007-09-10 20:33:04 +00001001 // If attributes exist after the method, parse them.
John McCall53fa7142010-12-24 02:08:15 +00001002 if (getLang().ObjC2)
John McCall084e83d2011-03-24 11:26:52 +00001003 MaybeParseGNUAttributes(methodAttrs);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001004
1005 if (KeyIdents.size() == 0) {
1006 // Leave prototype scope.
1007 PrototypeScope.Exit();
John McCall48871652010-08-21 09:40:31 +00001008 return 0;
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001009 }
1010
Chris Lattner5700fab2007-10-07 02:00:24 +00001011 Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1012 &KeyIdents[0]);
John McCall48871652010-08-21 09:40:31 +00001013 Decl *Result
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001014 = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
Douglas Gregor33823722011-06-11 01:09:30 +00001015 mType, IDecl, DSRet, ReturnType,
1016 selLoc, Sel, &ArgInfos[0],
Fariborz Jahanian60462092010-04-08 00:30:06 +00001017 CParamInfo.data(), CParamInfo.size(),
John McCall084e83d2011-03-24 11:26:52 +00001018 methodAttrs.getList(),
Fariborz Jahanianc677f692011-03-12 18:54:30 +00001019 MethodImplKind, isVariadic, MethodDefinition);
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00001020 // Leave prototype scope.
1021 PrototypeScope.Exit();
1022
John McCall28a6aea2009-11-04 02:18:39 +00001023 PD.complete(Result);
1024 return Result;
Steve Naroff99264b42007-08-22 16:35:03 +00001025}
1026
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001027/// objc-protocol-refs:
1028/// '<' identifier-list '>'
1029///
Chris Lattnerd7352d62008-07-21 22:17:28 +00001030bool Parser::
John McCall48871652010-08-21 09:40:31 +00001031ParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001032 llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
1033 bool WarnOnDeclarations,
1034 SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
Chris Lattner3bbae002008-07-26 04:03:38 +00001035 assert(Tok.is(tok::less) && "expected <");
Mike Stump11289f42009-09-09 15:08:12 +00001036
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001037 LAngleLoc = ConsumeToken(); // the "<"
Mike Stump11289f42009-09-09 15:08:12 +00001038
Chris Lattner3bbae002008-07-26 04:03:38 +00001039 llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
Mike Stump11289f42009-09-09 15:08:12 +00001040
Chris Lattner3bbae002008-07-26 04:03:38 +00001041 while (1) {
Douglas Gregorbaf69612009-11-18 04:19:12 +00001042 if (Tok.is(tok::code_completion)) {
1043 Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
1044 ProtocolIdents.size());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001045 ConsumeCodeCompletionToken();
Douglas Gregorbaf69612009-11-18 04:19:12 +00001046 }
1047
Chris Lattner3bbae002008-07-26 04:03:38 +00001048 if (Tok.isNot(tok::identifier)) {
1049 Diag(Tok, diag::err_expected_ident);
1050 SkipUntil(tok::greater);
1051 return true;
1052 }
1053 ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1054 Tok.getLocation()));
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001055 ProtocolLocs.push_back(Tok.getLocation());
Chris Lattner3bbae002008-07-26 04:03:38 +00001056 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001057
Chris Lattner3bbae002008-07-26 04:03:38 +00001058 if (Tok.isNot(tok::comma))
1059 break;
1060 ConsumeToken();
1061 }
Mike Stump11289f42009-09-09 15:08:12 +00001062
Chris Lattner3bbae002008-07-26 04:03:38 +00001063 // Consume the '>'.
1064 if (Tok.isNot(tok::greater)) {
1065 Diag(Tok, diag::err_expected_greater);
1066 return true;
1067 }
Mike Stump11289f42009-09-09 15:08:12 +00001068
Chris Lattner3bbae002008-07-26 04:03:38 +00001069 EndLoc = ConsumeAnyToken();
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattner3bbae002008-07-26 04:03:38 +00001071 // Convert the list of protocols identifiers into a list of protocol decls.
1072 Actions.FindProtocolDeclaration(WarnOnDeclarations,
1073 &ProtocolIdents[0], ProtocolIdents.size(),
1074 Protocols);
1075 return false;
1076}
1077
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001078/// \brief Parse the Objective-C protocol qualifiers that follow a typename
1079/// in a decl-specifier-seq, starting at the '<'.
Douglas Gregor3a001f42010-11-19 17:10:50 +00001080bool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001081 assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1082 assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1083 SourceLocation LAngleLoc, EndProtoLoc;
1084 llvm::SmallVector<Decl *, 8> ProtocolDecl;
1085 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Douglas Gregor3a001f42010-11-19 17:10:50 +00001086 bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
1087 LAngleLoc, EndProtoLoc);
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001088 DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
1089 ProtocolLocs.data(), LAngleLoc);
1090 if (EndProtoLoc.isValid())
1091 DS.SetRangeEnd(EndProtoLoc);
Douglas Gregor3a001f42010-11-19 17:10:50 +00001092 return Result;
Douglas Gregor06e41ae2010-10-21 23:17:00 +00001093}
1094
1095
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001096/// objc-class-instance-variables:
1097/// '{' objc-instance-variable-decl-list[opt] '}'
1098///
1099/// objc-instance-variable-decl-list:
1100/// objc-visibility-spec
1101/// objc-instance-variable-decl ';'
1102/// ';'
1103/// objc-instance-variable-decl-list objc-visibility-spec
1104/// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1105/// objc-instance-variable-decl-list ';'
1106///
1107/// objc-visibility-spec:
1108/// @private
1109/// @protected
1110/// @public
Steve Naroff00433d32007-08-21 21:17:12 +00001111/// @package [OBJC2]
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001112///
1113/// objc-instance-variable-decl:
Mike Stump11289f42009-09-09 15:08:12 +00001114/// struct-declaration
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001115///
John McCall48871652010-08-21 09:40:31 +00001116void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001117 tok::ObjCKeywordKind visibility,
Steve Naroff33a1e802007-10-29 21:38:07 +00001118 SourceLocation atLoc) {
Chris Lattner0ef13522007-10-09 17:51:17 +00001119 assert(Tok.is(tok::l_brace) && "expected {");
John McCall48871652010-08-21 09:40:31 +00001120 llvm::SmallVector<Decl *, 32> AllIvarDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00001121
Douglas Gregor45a33ec2009-01-12 18:45:55 +00001122 ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00001123
Steve Naroff00433d32007-08-21 21:17:12 +00001124 SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
Mike Stump11289f42009-09-09 15:08:12 +00001125
Steve Naroff00433d32007-08-21 21:17:12 +00001126 // While we still have something to read, read the instance variables.
Chris Lattner0ef13522007-10-09 17:51:17 +00001127 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001128 // Each iteration of this loop reads one objc-instance-variable-decl.
Mike Stump11289f42009-09-09 15:08:12 +00001129
Steve Naroff00433d32007-08-21 21:17:12 +00001130 // Check for extraneous top-level semicolon.
Chris Lattner0ef13522007-10-09 17:51:17 +00001131 if (Tok.is(tok::semi)) {
Douglas Gregor13d05682010-06-16 23:08:59 +00001132 Diag(Tok, diag::ext_extra_ivar_semi)
Douglas Gregora771f462010-03-31 17:46:05 +00001133 << FixItHint::CreateRemoval(Tok.getLocation());
Steve Naroff00433d32007-08-21 21:17:12 +00001134 ConsumeToken();
1135 continue;
1136 }
Mike Stump11289f42009-09-09 15:08:12 +00001137
Steve Naroff00433d32007-08-21 21:17:12 +00001138 // Set the default visibility to private.
Chris Lattner0ef13522007-10-09 17:51:17 +00001139 if (Tok.is(tok::at)) { // parse objc-visibility-spec
Steve Naroff00433d32007-08-21 21:17:12 +00001140 ConsumeToken(); // eat the @ sign
Douglas Gregor48d46252010-01-13 21:54:15 +00001141
1142 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001143 Actions.CodeCompleteObjCAtVisibility(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001144 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001145 }
1146
Steve Naroff7c348172007-08-23 18:16:40 +00001147 switch (Tok.getObjCKeywordID()) {
Steve Naroff00433d32007-08-21 21:17:12 +00001148 case tok::objc_private:
1149 case tok::objc_public:
1150 case tok::objc_protected:
1151 case tok::objc_package:
Steve Naroff7c348172007-08-23 18:16:40 +00001152 visibility = Tok.getObjCKeywordID();
Steve Naroff00433d32007-08-21 21:17:12 +00001153 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001154 continue;
Steve Naroff00433d32007-08-21 21:17:12 +00001155 default:
1156 Diag(Tok, diag::err_objc_illegal_visibility_spec);
Steve Naroff00433d32007-08-21 21:17:12 +00001157 continue;
1158 }
1159 }
Mike Stump11289f42009-09-09 15:08:12 +00001160
Douglas Gregor48d46252010-01-13 21:54:15 +00001161 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001162 Actions.CodeCompleteOrdinaryName(getCurScope(),
John McCallfaf5fb42010-08-26 23:41:50 +00001163 Sema::PCC_ObjCInstanceVariableList);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001164 ConsumeCodeCompletionToken();
Douglas Gregor48d46252010-01-13 21:54:15 +00001165 }
1166
John McCallcfefb6d2009-11-03 02:38:08 +00001167 struct ObjCIvarCallback : FieldCallback {
1168 Parser &P;
John McCall48871652010-08-21 09:40:31 +00001169 Decl *IDecl;
John McCallcfefb6d2009-11-03 02:38:08 +00001170 tok::ObjCKeywordKind visibility;
John McCall48871652010-08-21 09:40:31 +00001171 llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00001172
John McCall48871652010-08-21 09:40:31 +00001173 ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1174 llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00001175 P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1176 }
1177
John McCall48871652010-08-21 09:40:31 +00001178 Decl *invoke(FieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00001179 // Install the declarator into the interface decl.
John McCall48871652010-08-21 09:40:31 +00001180 Decl *Field
Douglas Gregor0be31a22010-07-02 17:43:08 +00001181 = P.Actions.ActOnIvar(P.getCurScope(),
John McCallcfefb6d2009-11-03 02:38:08 +00001182 FD.D.getDeclSpec().getSourceRange().getBegin(),
1183 IDecl, FD.D, FD.BitfieldSize, visibility);
Fariborz Jahanian6a0a2e02010-04-06 22:43:48 +00001184 if (Field)
1185 AllIvarDecls.push_back(Field);
John McCallcfefb6d2009-11-03 02:38:08 +00001186 return Field;
1187 }
1188 } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001189
Chris Lattnera12405b2008-04-10 06:46:29 +00001190 // Parse all the comma separated declarators.
John McCall084e83d2011-03-24 11:26:52 +00001191 DeclSpec DS(AttrFactory);
John McCallcfefb6d2009-11-03 02:38:08 +00001192 ParseStructDeclaration(DS, Callback);
Mike Stump11289f42009-09-09 15:08:12 +00001193
Chris Lattner0ef13522007-10-09 17:51:17 +00001194 if (Tok.is(tok::semi)) {
Steve Naroff00433d32007-08-21 21:17:12 +00001195 ConsumeToken();
Steve Naroff00433d32007-08-21 21:17:12 +00001196 } else {
1197 Diag(Tok, diag::err_expected_semi_decl_list);
1198 // Skip to end of block or statement
1199 SkipUntil(tok::r_brace, true, true);
1200 }
1201 }
Steve Naroff33a1e802007-10-29 21:38:07 +00001202 SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
Fariborz Jahanian616d3e72010-08-23 22:46:52 +00001203 Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
Steve Naroff9c4fddd2007-10-31 22:11:35 +00001204 // Call ActOnFields() even if we don't have any decls. This is useful
1205 // for code rewriting tools that need to be aware of the empty list.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001206 Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
Jay Foad7d0479f2009-05-21 09:52:38 +00001207 AllIvarDecls.data(), AllIvarDecls.size(),
Daniel Dunbar15619c72008-10-03 02:03:53 +00001208 LBraceLoc, RBraceLoc, 0);
Steve Naroff00433d32007-08-21 21:17:12 +00001209 return;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001210}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001211
1212/// objc-protocol-declaration:
1213/// objc-protocol-definition
1214/// objc-protocol-forward-reference
1215///
1216/// objc-protocol-definition:
Mike Stump11289f42009-09-09 15:08:12 +00001217/// @protocol identifier
1218/// objc-protocol-refs[opt]
1219/// objc-interface-decl-list
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001220/// @end
1221///
1222/// objc-protocol-forward-reference:
1223/// @protocol identifier-list ';'
1224///
1225/// "@protocol identifier ;" should be resolved as "@protocol
Steve Naroff09bf8152007-09-06 21:24:23 +00001226/// identifier-list ;": objc-interface-decl-list may not start with a
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001227/// semicolon in the first alternative if objc-protocol-refs are omitted.
John McCall48871652010-08-21 09:40:31 +00001228Decl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
John McCall53fa7142010-12-24 02:08:15 +00001229 ParsedAttributes &attrs) {
Steve Naroff7c348172007-08-23 18:16:40 +00001230 assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001231 "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1232 ConsumeToken(); // the "protocol" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001233
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001234 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001235 Actions.CodeCompleteObjCProtocolDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001236 ConsumeCodeCompletionToken();
Douglas Gregor5b4671c2009-11-18 04:49:41 +00001237 }
1238
Chris Lattner0ef13522007-10-09 17:51:17 +00001239 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001240 Diag(Tok, diag::err_expected_ident); // missing protocol name.
John McCall48871652010-08-21 09:40:31 +00001241 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001242 }
1243 // Save the protocol name, then consume it.
1244 IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1245 SourceLocation nameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001246
Chris Lattner0ef13522007-10-09 17:51:17 +00001247 if (Tok.is(tok::semi)) { // forward declaration of one protocol.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001248 IdentifierLocPair ProtoInfo(protocolName, nameLoc);
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001249 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00001250 return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
John McCall53fa7142010-12-24 02:08:15 +00001251 attrs.getList());
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001252 }
Mike Stump11289f42009-09-09 15:08:12 +00001253
Chris Lattner0ef13522007-10-09 17:51:17 +00001254 if (Tok.is(tok::comma)) { // list of forward declarations.
Chris Lattnerd7352d62008-07-21 22:17:28 +00001255 llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1256 ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1257
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001258 // Parse the list of forward declarations.
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001259 while (1) {
1260 ConsumeToken(); // the ','
Chris Lattner0ef13522007-10-09 17:51:17 +00001261 if (Tok.isNot(tok::identifier)) {
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001262 Diag(Tok, diag::err_expected_ident);
1263 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001264 return 0;
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001265 }
Chris Lattnerd7352d62008-07-21 22:17:28 +00001266 ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1267 Tok.getLocation()));
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001268 ConsumeToken(); // the identifier
Mike Stump11289f42009-09-09 15:08:12 +00001269
Chris Lattner0ef13522007-10-09 17:51:17 +00001270 if (Tok.isNot(tok::comma))
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001271 break;
1272 }
1273 // Consume the ';'.
1274 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
John McCall48871652010-08-21 09:40:31 +00001275 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001276
Steve Naroff93eb5f12007-10-10 17:32:04 +00001277 return Actions.ActOnForwardProtocolDeclaration(AtLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001278 &ProtocolRefs[0],
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001279 ProtocolRefs.size(),
John McCall53fa7142010-12-24 02:08:15 +00001280 attrs.getList());
Chris Lattnerd7352d62008-07-21 22:17:28 +00001281 }
Mike Stump11289f42009-09-09 15:08:12 +00001282
Steve Naroff0b6a01a2007-08-22 22:17:26 +00001283 // Last, and definitely not least, parse a protocol declaration.
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001284 SourceLocation LAngleLoc, EndProtoLoc;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001285
John McCall48871652010-08-21 09:40:31 +00001286 llvm::SmallVector<Decl *, 8> ProtocolRefs;
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001287 llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
Chris Lattnerd7352d62008-07-21 22:17:28 +00001288 if (Tok.is(tok::less) &&
Argyrios Kyrtzidisfc1f9e42009-09-29 19:41:44 +00001289 ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1290 LAngleLoc, EndProtoLoc))
John McCall48871652010-08-21 09:40:31 +00001291 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001292
John McCall48871652010-08-21 09:40:31 +00001293 Decl *ProtoType =
Chris Lattner3bbae002008-07-26 04:03:38 +00001294 Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
Jay Foad7d0479f2009-05-21 09:52:38 +00001295 ProtocolRefs.data(),
1296 ProtocolRefs.size(),
Douglas Gregor002b6712010-01-16 15:02:53 +00001297 ProtocolLocs.data(),
John McCall53fa7142010-12-24 02:08:15 +00001298 EndProtoLoc, attrs.getList());
Fariborz Jahanian39d641f2007-09-17 21:07:36 +00001299 ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
Chris Lattnerda9fb152008-10-20 06:10:06 +00001300 return ProtoType;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001301}
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001302
1303/// objc-implementation:
1304/// objc-class-implementation-prologue
1305/// objc-category-implementation-prologue
1306///
1307/// objc-class-implementation-prologue:
1308/// @implementation identifier objc-superclass[opt]
1309/// objc-class-instance-variables[opt]
1310///
1311/// objc-category-implementation-prologue:
1312/// @implementation identifier ( identifier )
John McCall48871652010-08-21 09:40:31 +00001313Decl *Parser::ParseObjCAtImplementationDeclaration(
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001314 SourceLocation atLoc) {
1315 assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1316 "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1317 ConsumeToken(); // the "implementation" identifier
Mike Stump11289f42009-09-09 15:08:12 +00001318
Douglas Gregor49c22a72009-11-18 16:26:39 +00001319 // Code completion after '@implementation'.
1320 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001321 Actions.CodeCompleteObjCImplementationDecl(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001322 ConsumeCodeCompletionToken();
Douglas Gregor49c22a72009-11-18 16:26:39 +00001323 }
1324
Chris Lattner0ef13522007-10-09 17:51:17 +00001325 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001326 Diag(Tok, diag::err_expected_ident); // missing class or category name.
John McCall48871652010-08-21 09:40:31 +00001327 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001328 }
1329 // We have a class or category name - consume it.
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001330 IdentifierInfo *nameId = Tok.getIdentifierInfo();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001331 SourceLocation nameLoc = ConsumeToken(); // consume class or category name
Mike Stump11289f42009-09-09 15:08:12 +00001332
1333 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001334 // we have a category implementation.
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001335 ConsumeParen();
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001336 SourceLocation categoryLoc, rparenLoc;
1337 IdentifierInfo *categoryId = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001338
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001339 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001340 Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001341 ConsumeCodeCompletionToken();
Douglas Gregor5d34fd32009-11-18 19:08:43 +00001342 }
1343
Chris Lattner0ef13522007-10-09 17:51:17 +00001344 if (Tok.is(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001345 categoryId = Tok.getIdentifierInfo();
1346 categoryLoc = ConsumeToken();
1347 } else {
1348 Diag(Tok, diag::err_expected_ident); // missing category name.
John McCall48871652010-08-21 09:40:31 +00001349 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001350 }
Chris Lattner0ef13522007-10-09 17:51:17 +00001351 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001352 Diag(Tok, diag::err_expected_rparen);
1353 SkipUntil(tok::r_paren, false); // don't stop at ';'
John McCall48871652010-08-21 09:40:31 +00001354 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001355 }
1356 rparenLoc = ConsumeParen();
John McCall48871652010-08-21 09:40:31 +00001357 Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
Mike Stump11289f42009-09-09 15:08:12 +00001358 atLoc, nameId, nameLoc, categoryId,
Fariborz Jahanian89b8ef92007-10-02 16:38:50 +00001359 categoryLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001360 ObjCImpDecl = ImplCatType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001361 PendingObjCImpDecl.push_back(ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001362 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001363 }
1364 // We have a class implementation
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001365 SourceLocation superClassLoc;
1366 IdentifierInfo *superClassId = 0;
Chris Lattner0ef13522007-10-09 17:51:17 +00001367 if (Tok.is(tok::colon)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001368 // We have a super class
1369 ConsumeToken();
Chris Lattner0ef13522007-10-09 17:51:17 +00001370 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001371 Diag(Tok, diag::err_expected_ident); // missing super class name.
John McCall48871652010-08-21 09:40:31 +00001372 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001373 }
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001374 superClassId = Tok.getIdentifierInfo();
1375 superClassLoc = ConsumeToken(); // Consume super class name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001376 }
John McCall48871652010-08-21 09:40:31 +00001377 Decl *ImplClsType = Actions.ActOnStartClassImplementation(
Chris Lattner5e530bc2007-12-27 19:57:00 +00001378 atLoc, nameId, nameLoc,
Fariborz Jahanianbfe13c52007-09-25 18:38:09 +00001379 superClassId, superClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001380
Steve Naroff33a1e802007-10-29 21:38:07 +00001381 if (Tok.is(tok::l_brace)) // we have ivars
Fariborz Jahanian4c172c62010-02-22 23:04:20 +00001382 ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
Fariborz Jahaniana9effb52010-03-22 19:04:14 +00001383 tok::objc_private, atLoc);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001384 ObjCImpDecl = ImplClsType;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001385 PendingObjCImpDecl.push_back(ObjCImpDecl);
1386
John McCall48871652010-08-21 09:40:31 +00001387 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001388}
Steve Naroff33a1e802007-10-29 21:38:07 +00001389
John McCall48871652010-08-21 09:40:31 +00001390Decl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001391 assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1392 "ParseObjCAtEndDeclaration(): Expected @end");
John McCall48871652010-08-21 09:40:31 +00001393 Decl *Result = ObjCImpDecl;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001394 ConsumeToken(); // the "end" identifier
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001395 if (ObjCImpDecl) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001396 Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
John McCall48871652010-08-21 09:40:31 +00001397 ObjCImpDecl = 0;
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001398 PendingObjCImpDecl.pop_back();
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001399 }
Ted Kremenekc7c64312010-01-07 01:20:12 +00001400 else {
1401 // missing @implementation
Fariborz Jahanianc0577942011-04-22 22:02:28 +00001402 Diag(atEnd.getBegin(), diag::err_expected_implementation);
Ted Kremenekc7c64312010-01-07 01:20:12 +00001403 }
Fariborz Jahanianb8d091c2009-03-04 22:30:12 +00001404 return Result;
Steve Naroff1eb1ad62007-08-20 21:31:48 +00001405}
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001406
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00001407Parser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
1408 Actions.DiagnoseUseOfUnimplementedSelectors();
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001409 if (PendingObjCImpDecl.empty())
John McCall48871652010-08-21 09:40:31 +00001410 return Actions.ConvertDeclToDeclGroup(0);
1411 Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
Douglas Gregor0be31a22010-07-02 17:43:08 +00001412 Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00001413 return Actions.ConvertDeclToDeclGroup(ImpDecl);
1414}
1415
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001416/// compatibility-alias-decl:
1417/// @compatibility_alias alias-name class-name ';'
1418///
John McCall48871652010-08-21 09:40:31 +00001419Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001420 assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1421 "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1422 ConsumeToken(); // consume compatibility_alias
Chris Lattner0ef13522007-10-09 17:51:17 +00001423 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001424 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001425 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001426 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001427 IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1428 SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
Chris Lattner0ef13522007-10-09 17:51:17 +00001429 if (Tok.isNot(tok::identifier)) {
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001430 Diag(Tok, diag::err_expected_ident);
John McCall48871652010-08-21 09:40:31 +00001431 return 0;
Fariborz Jahaniand8e12d32007-09-04 19:26:51 +00001432 }
Fariborz Jahanian49c64252007-10-11 23:42:27 +00001433 IdentifierInfo *classId = Tok.getIdentifierInfo();
1434 SourceLocation classLoc = ConsumeToken(); // consume class-name;
Douglas Gregor78edf992011-01-05 01:10:06 +00001435 ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1436 "@compatibility_alias");
Chris Lattner83f095c2009-03-28 19:18:32 +00001437 return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1438 classId, classLoc);
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001439}
1440
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001441/// property-synthesis:
1442/// @synthesize property-ivar-list ';'
1443///
1444/// property-ivar-list:
1445/// property-ivar
1446/// property-ivar-list ',' property-ivar
1447///
1448/// property-ivar:
1449/// identifier
1450/// identifier '=' identifier
1451///
John McCall48871652010-08-21 09:40:31 +00001452Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001453 assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1454 "ParseObjCPropertyDynamic(): Expected '@synthesize'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001455 ConsumeToken(); // consume synthesize
Mike Stump11289f42009-09-09 15:08:12 +00001456
Douglas Gregor88e72a02009-11-18 19:45:45 +00001457 while (true) {
Douglas Gregor5d649882009-11-18 22:32:06 +00001458 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001459 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001460 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001461 }
1462
Douglas Gregor88e72a02009-11-18 19:45:45 +00001463 if (Tok.isNot(tok::identifier)) {
1464 Diag(Tok, diag::err_synthesized_property_name);
1465 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001466 return 0;
Douglas Gregor88e72a02009-11-18 19:45:45 +00001467 }
1468
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001469 IdentifierInfo *propertyIvar = 0;
1470 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1471 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001472 SourceLocation propertyIvarLoc;
Chris Lattner0ef13522007-10-09 17:51:17 +00001473 if (Tok.is(tok::equal)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001474 // property '=' ivar-name
1475 ConsumeToken(); // consume '='
Douglas Gregor5d649882009-11-18 22:32:06 +00001476
1477 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001478 Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
Douglas Gregor5d649882009-11-18 22:32:06 +00001479 ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001480 ConsumeCodeCompletionToken();
Douglas Gregor5d649882009-11-18 22:32:06 +00001481 }
1482
Chris Lattner0ef13522007-10-09 17:51:17 +00001483 if (Tok.isNot(tok::identifier)) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001484 Diag(Tok, diag::err_expected_ident);
1485 break;
1486 }
Fariborz Jahanianffe97a32008-04-18 00:19:30 +00001487 propertyIvar = Tok.getIdentifierInfo();
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001488 propertyIvarLoc = ConsumeToken(); // consume ivar-name
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001489 }
Douglas Gregor0be31a22010-07-02 17:43:08 +00001490 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001491 propertyId, propertyIvar, propertyIvarLoc);
Chris Lattner0ef13522007-10-09 17:51:17 +00001492 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001493 break;
1494 ConsumeToken(); // consume ','
1495 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001496 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
John McCall48871652010-08-21 09:40:31 +00001497 return 0;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001498}
1499
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001500/// property-dynamic:
1501/// @dynamic property-list
1502///
1503/// property-list:
1504/// identifier
1505/// property-list ',' identifier
1506///
John McCall48871652010-08-21 09:40:31 +00001507Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001508 assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1509 "ParseObjCPropertyDynamic(): Expected '@dynamic'");
Jeffrey Yasskin8dfa5f12011-01-18 02:00:16 +00001510 ConsumeToken(); // consume dynamic
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001511 while (true) {
1512 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001513 Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001514 ConsumeCodeCompletionToken();
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001515 }
1516
1517 if (Tok.isNot(tok::identifier)) {
1518 Diag(Tok, diag::err_expected_ident);
1519 SkipUntil(tok::semi);
John McCall48871652010-08-21 09:40:31 +00001520 return 0;
Douglas Gregor52e78bd2009-11-18 22:56:13 +00001521 }
1522
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001523 IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1524 SourceLocation propertyLoc = ConsumeToken(); // consume property name
Douglas Gregor0be31a22010-07-02 17:43:08 +00001525 Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
Douglas Gregorb1b71e52010-11-17 01:03:52 +00001526 propertyId, 0, SourceLocation());
Fariborz Jahanianf2a7d7c2008-04-21 21:05:54 +00001527
Chris Lattner0ef13522007-10-09 17:51:17 +00001528 if (Tok.isNot(tok::comma))
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001529 break;
1530 ConsumeToken(); // consume ','
1531 }
Douglas Gregor78edf992011-01-05 01:10:06 +00001532 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
John McCall48871652010-08-21 09:40:31 +00001533 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001534}
Mike Stump11289f42009-09-09 15:08:12 +00001535
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001536/// objc-throw-statement:
1537/// throw expression[opt];
1538///
John McCalldadc5752010-08-24 06:29:42 +00001539StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1540 ExprResult Res;
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001541 ConsumeToken(); // consume throw
Chris Lattner0ef13522007-10-09 17:51:17 +00001542 if (Tok.isNot(tok::semi)) {
Fariborz Jahanianadfbbc32007-11-07 02:00:49 +00001543 Res = ParseExpression();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001544 if (Res.isInvalid()) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001545 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001546 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001547 }
1548 }
Ted Kremenek15a81e52010-04-20 21:21:51 +00001549 // consume ';'
1550 ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
John McCallb268a282010-08-23 23:25:46 +00001551 return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001552}
1553
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001554/// objc-synchronized-statement:
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001555/// @synchronized '(' expression ')' compound-statement
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001556///
John McCalldadc5752010-08-24 06:29:42 +00001557StmtResult
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001558Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001559 ConsumeToken(); // consume synchronized
1560 if (Tok.isNot(tok::l_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001561 Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001562 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001563 }
1564 ConsumeParen(); // '('
John McCalldadc5752010-08-24 06:29:42 +00001565 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001566 if (Res.isInvalid()) {
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001567 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001568 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001569 }
1570 if (Tok.isNot(tok::r_paren)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001571 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001572 return StmtError();
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001573 }
1574 ConsumeParen(); // ')'
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001575 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001576 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001577 return StmtError();
Fariborz Jahanian049fa582008-01-30 17:38:29 +00001578 }
Steve Naroffd9c26072008-06-04 20:36:13 +00001579 // Enter a scope to hold everything within the compound stmt. Compound
1580 // statements can always hold declarations.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001581 ParseScope BodyScope(this, Scope::DeclScope);
Steve Naroffd9c26072008-06-04 20:36:13 +00001582
John McCalldadc5752010-08-24 06:29:42 +00001583 StmtResult SynchBody(ParseCompoundStatementBody());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001584
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001585 BodyScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001586 if (SynchBody.isInvalid())
Fariborz Jahanian48085b82008-01-29 19:14:59 +00001587 SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
John McCallb268a282010-08-23 23:25:46 +00001588 return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
Fariborz Jahanianf89ca382008-01-29 18:21:32 +00001589}
1590
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001591/// objc-try-catch-statement:
1592/// @try compound-statement objc-catch-list[opt]
1593/// @try compound-statement objc-catch-list[opt] @finally compound-statement
1594///
1595/// objc-catch-list:
1596/// @catch ( parameter-declaration ) compound-statement
1597/// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1598/// catch-parameter-declaration:
1599/// parameter-declaration
1600/// '...' [OBJC2]
1601///
John McCalldadc5752010-08-24 06:29:42 +00001602StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001603 bool catch_or_finally_seen = false;
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001604
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001605 ConsumeToken(); // consume try
Chris Lattner0ef13522007-10-09 17:51:17 +00001606 if (Tok.isNot(tok::l_brace)) {
Chris Lattner6d29c102008-11-18 07:48:38 +00001607 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001608 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001609 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001610 StmtVector CatchStmts(Actions);
John McCalldadc5752010-08-24 06:29:42 +00001611 StmtResult FinallyStmt;
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001612 ParseScope TryScope(this, Scope::DeclScope);
John McCalldadc5752010-08-24 06:29:42 +00001613 StmtResult TryBody(ParseCompoundStatementBody());
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001614 TryScope.Exit();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001615 if (TryBody.isInvalid())
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001616 TryBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl511ed552008-11-25 22:21:31 +00001617
Chris Lattner0ef13522007-10-09 17:51:17 +00001618 while (Tok.is(tok::at)) {
Chris Lattner3e468322008-03-10 06:06:04 +00001619 // At this point, we need to lookahead to determine if this @ is the start
1620 // of an @catch or @finally. We don't want to consume the @ token if this
1621 // is an @try or @encode or something else.
1622 Token AfterAt = GetLookAheadToken(1);
1623 if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1624 !AfterAt.isObjCAtKeyword(tok::objc_finally))
1625 break;
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001626
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001627 SourceLocation AtCatchFinallyLoc = ConsumeToken();
Chris Lattner5e530bc2007-12-27 19:57:00 +00001628 if (Tok.isObjCAtKeyword(tok::objc_catch)) {
John McCall48871652010-08-21 09:40:31 +00001629 Decl *FirstPart = 0;
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001630 ConsumeToken(); // consume catch
Chris Lattner0ef13522007-10-09 17:51:17 +00001631 if (Tok.is(tok::l_paren)) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001632 ConsumeParen();
Steve Naroff5ee2c022009-02-11 20:05:44 +00001633 ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
Chris Lattner0ef13522007-10-09 17:51:17 +00001634 if (Tok.isNot(tok::ellipsis)) {
John McCall084e83d2011-03-24 11:26:52 +00001635 DeclSpec DS(AttrFactory);
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001636 ParseDeclarationSpecifiers(DS);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001637 // For some odd reason, the name of the exception variable is
Mike Stump11289f42009-09-09 15:08:12 +00001638 // optional. As a result, we need to use "PrototypeContext", because
Steve Naroff371b8fb2009-03-03 19:52:17 +00001639 // we must accept either 'declarator' or 'abstract-declarator' here.
1640 Declarator ParmDecl(DS, Declarator::PrototypeContext);
1641 ParseDeclarator(ParmDecl);
1642
Douglas Gregore11ee112010-04-23 23:01:43 +00001643 // Inform the actions module about the declarator, so it
Steve Naroff371b8fb2009-03-03 19:52:17 +00001644 // gets added to the current scope.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001645 FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
Steve Naroffe6016792008-02-05 21:27:35 +00001646 } else
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001647 ConsumeToken(); // consume '...'
Mike Stump11289f42009-09-09 15:08:12 +00001648
Steve Naroff65a00892009-04-07 22:56:58 +00001649 SourceLocation RParenLoc;
Mike Stump11289f42009-09-09 15:08:12 +00001650
Steve Naroff65a00892009-04-07 22:56:58 +00001651 if (Tok.is(tok::r_paren))
1652 RParenLoc = ConsumeParen();
1653 else // Skip over garbage, until we get to ')'. Eat the ')'.
1654 SkipUntil(tok::r_paren, true, false);
1655
John McCalldadc5752010-08-24 06:29:42 +00001656 StmtResult CatchBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001657 if (Tok.is(tok::l_brace))
1658 CatchBody = ParseCompoundStatementBody();
1659 else
1660 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001661 if (CatchBody.isInvalid())
Fariborz Jahanian9e63b982007-11-01 23:59:59 +00001662 CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
Douglas Gregor96c79492010-04-23 22:50:49 +00001663
John McCalldadc5752010-08-24 06:29:42 +00001664 StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
Douglas Gregor96c79492010-04-23 22:50:49 +00001665 RParenLoc,
1666 FirstPart,
John McCallb268a282010-08-23 23:25:46 +00001667 CatchBody.take());
Douglas Gregor96c79492010-04-23 22:50:49 +00001668 if (!Catch.isInvalid())
1669 CatchStmts.push_back(Catch.release());
1670
Steve Naroffe6016792008-02-05 21:27:35 +00001671 } else {
Chris Lattner6d29c102008-11-18 07:48:38 +00001672 Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1673 << "@catch clause";
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001674 return StmtError();
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001675 }
1676 catch_or_finally_seen = true;
Chris Lattner3e468322008-03-10 06:06:04 +00001677 } else {
1678 assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
Steve Naroffe6016792008-02-05 21:27:35 +00001679 ConsumeToken(); // consume finally
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001680 ParseScope FinallyScope(this, Scope::DeclScope);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001681
John McCalldadc5752010-08-24 06:29:42 +00001682 StmtResult FinallyBody(true);
Chris Lattner99a59b62008-02-14 19:27:54 +00001683 if (Tok.is(tok::l_brace))
1684 FinallyBody = ParseCompoundStatementBody();
1685 else
1686 Diag(Tok, diag::err_expected_lbrace);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001687 if (FinallyBody.isInvalid())
Fariborz Jahanian71234d82007-11-02 00:18:53 +00001688 FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001689 FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
John McCallb268a282010-08-23 23:25:46 +00001690 FinallyBody.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001691 catch_or_finally_seen = true;
1692 break;
1693 }
1694 }
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001695 if (!catch_or_finally_seen) {
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001696 Diag(atLoc, diag::err_missing_catch_finally);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001697 return StmtError();
Fariborz Jahanianf859ef22007-11-02 15:39:31 +00001698 }
Douglas Gregor96c79492010-04-23 22:50:49 +00001699
John McCallb268a282010-08-23 23:25:46 +00001700 return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
Douglas Gregor96c79492010-04-23 22:50:49 +00001701 move_arg(CatchStmts),
John McCallb268a282010-08-23 23:25:46 +00001702 FinallyStmt.take());
Fariborz Jahanian62fd2b42007-09-19 19:14:32 +00001703}
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001704
John McCall31168b02011-06-15 23:02:42 +00001705/// objc-autoreleasepool-statement:
1706/// @autoreleasepool compound-statement
1707///
1708StmtResult
1709Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1710 ConsumeToken(); // consume autoreleasepool
1711 if (Tok.isNot(tok::l_brace)) {
1712 Diag(Tok, diag::err_expected_lbrace);
1713 return StmtError();
1714 }
1715 // Enter a scope to hold everything within the compound stmt. Compound
1716 // statements can always hold declarations.
1717 ParseScope BodyScope(this, Scope::DeclScope);
1718
1719 StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1720
1721 BodyScope.Exit();
1722 if (AutoreleasePoolBody.isInvalid())
1723 AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1724 return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1725 AutoreleasePoolBody.take());
1726}
1727
Steve Naroff09bf8152007-09-06 21:24:23 +00001728/// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001729///
John McCall48871652010-08-21 09:40:31 +00001730Decl *Parser::ParseObjCMethodDefinition() {
1731 Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001732
John McCallfaf5fb42010-08-26 23:41:50 +00001733 PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1734 "parsing Objective-C method");
Mike Stump11289f42009-09-09 15:08:12 +00001735
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001736 // parse optional ';'
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001737 if (Tok.is(tok::semi)) {
Ted Kremenek0b61a802009-11-10 22:55:49 +00001738 if (ObjCImpDecl) {
1739 Diag(Tok, diag::warn_semicolon_before_method_body)
Douglas Gregora771f462010-03-31 17:46:05 +00001740 << FixItHint::CreateRemoval(Tok.getLocation());
Ted Kremenek0b61a802009-11-10 22:55:49 +00001741 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001742 ConsumeToken();
Fariborz Jahanian040d75d2009-10-20 16:39:13 +00001743 }
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001744
Steve Naroffbb875722007-11-11 19:54:21 +00001745 // We should have an opening brace now.
Chris Lattner0ef13522007-10-09 17:51:17 +00001746 if (Tok.isNot(tok::l_brace)) {
Steve Naroff83777fe2008-02-29 21:48:07 +00001747 Diag(Tok, diag::err_expected_method_body);
Mike Stump11289f42009-09-09 15:08:12 +00001748
Steve Naroffbb875722007-11-11 19:54:21 +00001749 // Skip over garbage, until we get to '{'. Don't eat the '{'.
1750 SkipUntil(tok::l_brace, true, true);
Mike Stump11289f42009-09-09 15:08:12 +00001751
Steve Naroffbb875722007-11-11 19:54:21 +00001752 // If we didn't find the '{', bail out.
1753 if (Tok.isNot(tok::l_brace))
John McCall48871652010-08-21 09:40:31 +00001754 return 0;
Fariborz Jahanian53cacae2007-09-01 00:26:16 +00001755 }
Steve Naroffbb875722007-11-11 19:54:21 +00001756 SourceLocation BraceLoc = Tok.getLocation();
Mike Stump11289f42009-09-09 15:08:12 +00001757
Steve Naroffbb875722007-11-11 19:54:21 +00001758 // Enter a scope for the method body.
Chris Lattnerc2ebb032010-04-12 05:38:43 +00001759 ParseScope BodyScope(this,
1760 Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
Mike Stump11289f42009-09-09 15:08:12 +00001761
Steve Naroffbb875722007-11-11 19:54:21 +00001762 // Tell the actions module that we have entered a method definition with the
Steve Naroff542cd5d2008-07-25 17:57:26 +00001763 // specified Declarator for the method.
Douglas Gregor0be31a22010-07-02 17:43:08 +00001764 Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
Sebastian Redl042ad952008-12-11 19:30:53 +00001765
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001766 if (PP.isCodeCompletionEnabled()) {
1767 if (trySkippingFunctionBodyForCodeCompletion()) {
1768 BodyScope.Exit();
Argyrios Kyrtzidisbd9dfb22011-01-04 00:27:27 +00001769 return Actions.ActOnFinishFunctionBody(MDecl, 0);
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001770 }
1771 }
Argyrios Kyrtzidisd5756a62011-01-03 22:33:06 +00001772
John McCalldadc5752010-08-24 06:29:42 +00001773 StmtResult FnBody(ParseCompoundStatementBody());
Sebastian Redl042ad952008-12-11 19:30:53 +00001774
Steve Naroffbb875722007-11-11 19:54:21 +00001775 // If the function body could not be parsed, make a bogus compoundstmt.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001776 if (FnBody.isInvalid())
Sebastian Redl52f03ba2008-12-21 12:04:03 +00001777 FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1778 MultiStmtArg(Actions), false);
Sebastian Redlc675bab2008-12-13 16:23:55 +00001779
Steve Naroffbb875722007-11-11 19:54:21 +00001780 // Leave the function body scope.
Douglas Gregor7307d6c2008-12-10 06:34:36 +00001781 BodyScope.Exit();
Douglas Gregora0ff0c32011-03-16 17:05:57 +00001782
1783 // TODO: Pass argument information.
1784 Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
Steve Naroff7b8fa472007-11-13 23:01:27 +00001785 return MDecl;
Chris Lattnerda59c2f2006-11-05 02:08:13 +00001786}
Anders Carlsson76f4a902007-08-21 17:43:55 +00001787
John McCalldadc5752010-08-24 06:29:42 +00001788StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001789 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001790 Actions.CodeCompleteObjCAtStatement(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001791 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001792 return StmtError();
Chris Lattner3ababf52009-12-07 16:33:19 +00001793 }
1794
1795 if (Tok.isObjCAtKeyword(tok::objc_try))
Chris Lattner3e468322008-03-10 06:06:04 +00001796 return ParseObjCTryStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001797
1798 if (Tok.isObjCAtKeyword(tok::objc_throw))
Steve Naroffe6016792008-02-05 21:27:35 +00001799 return ParseObjCThrowStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001800
1801 if (Tok.isObjCAtKeyword(tok::objc_synchronized))
Steve Naroffe6016792008-02-05 21:27:35 +00001802 return ParseObjCSynchronizedStmt(AtLoc);
John McCall31168b02011-06-15 23:02:42 +00001803
1804 if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1805 return ParseObjCAutoreleasePoolStmt(AtLoc);
Chris Lattner3ababf52009-12-07 16:33:19 +00001806
John McCalldadc5752010-08-24 06:29:42 +00001807 ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00001808 if (Res.isInvalid()) {
Steve Naroffe6016792008-02-05 21:27:35 +00001809 // If the expression is invalid, skip ahead to the next semicolon. Not
1810 // doing this opens us up to the possibility of infinite loops if
1811 // ParseExpression does not consume any tokens.
1812 SkipUntil(tok::semi);
Sebastian Redlbab9a4b2008-12-11 20:12:42 +00001813 return StmtError();
Steve Naroffe6016792008-02-05 21:27:35 +00001814 }
Chris Lattner3ababf52009-12-07 16:33:19 +00001815
Steve Naroffe6016792008-02-05 21:27:35 +00001816 // Otherwise, eat the semicolon.
Douglas Gregor45d6bdf2010-09-07 15:23:11 +00001817 ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
John McCallb268a282010-08-23 23:25:46 +00001818 return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
Steve Naroffe6016792008-02-05 21:27:35 +00001819}
1820
John McCalldadc5752010-08-24 06:29:42 +00001821ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
Anders Carlsson76f4a902007-08-21 17:43:55 +00001822 switch (Tok.getKind()) {
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001823 case tok::code_completion:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001824 Actions.CodeCompleteObjCAtExpression(getCurScope());
Douglas Gregor6da3db42010-05-25 05:58:43 +00001825 ConsumeCodeCompletionToken();
Douglas Gregorbc7c5e42009-12-07 09:51:25 +00001826 return ExprError();
1827
Chris Lattnere002fbe2007-12-12 01:04:12 +00001828 case tok::string_literal: // primary-expression: string-literal
1829 case tok::wide_string_literal:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001830 return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
Chris Lattnere002fbe2007-12-12 01:04:12 +00001831 default:
Chris Lattner197a3012008-08-05 06:19:09 +00001832 if (Tok.getIdentifierInfo() == 0)
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001833 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Sebastian Redl59b5e512008-12-11 21:36:32 +00001834
Chris Lattner197a3012008-08-05 06:19:09 +00001835 switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1836 case tok::objc_encode:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001837 return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001838 case tok::objc_protocol:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001839 return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001840 case tok::objc_selector:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001841 return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
Chris Lattner197a3012008-08-05 06:19:09 +00001842 default:
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00001843 return ExprError(Diag(AtLoc, diag::err_unexpected_at));
Chris Lattner197a3012008-08-05 06:19:09 +00001844 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001845 }
Anders Carlsson76f4a902007-08-21 17:43:55 +00001846}
1847
Douglas Gregor8d4de672010-04-21 22:36:40 +00001848/// \brirg Parse the receiver of an Objective-C++ message send.
1849///
1850/// This routine parses the receiver of a message send in
1851/// Objective-C++ either as a type or as an expression. Note that this
1852/// routine must not be called to parse a send to 'super', since it
1853/// has no way to return such a result.
1854///
1855/// \param IsExpr Whether the receiver was parsed as an expression.
1856///
1857/// \param TypeOrExpr If the receiver was parsed as an expression (\c
1858/// IsExpr is true), the parsed expression. If the receiver was parsed
1859/// as a type (\c IsExpr is false), the parsed type.
1860///
1861/// \returns True if an error occurred during parsing or semantic
1862/// analysis, in which case the arguments do not have valid
1863/// values. Otherwise, returns false for a successful parse.
1864///
1865/// objc-receiver: [C++]
1866/// 'super' [not parsed here]
1867/// expression
1868/// simple-type-specifier
1869/// typename-specifier
Douglas Gregor8d4de672010-04-21 22:36:40 +00001870bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001871 InMessageExpressionRAIIObject InMessage(*this, true);
1872
Douglas Gregor8d4de672010-04-21 22:36:40 +00001873 if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1874 Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1875 TryAnnotateTypeOrScopeToken();
1876
1877 if (!isCXXSimpleTypeSpecifier()) {
1878 // objc-receiver:
1879 // expression
John McCalldadc5752010-08-24 06:29:42 +00001880 ExprResult Receiver = ParseExpression();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001881 if (Receiver.isInvalid())
1882 return true;
1883
1884 IsExpr = true;
1885 TypeOrExpr = Receiver.take();
1886 return false;
1887 }
1888
1889 // objc-receiver:
1890 // typename-specifier
1891 // simple-type-specifier
1892 // expression (that starts with one of the above)
John McCall084e83d2011-03-24 11:26:52 +00001893 DeclSpec DS(AttrFactory);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001894 ParseCXXSimpleTypeSpecifier(DS);
1895
1896 if (Tok.is(tok::l_paren)) {
1897 // If we see an opening parentheses at this point, we are
1898 // actually parsing an expression that starts with a
1899 // function-style cast, e.g.,
1900 //
1901 // postfix-expression:
1902 // simple-type-specifier ( expression-list [opt] )
1903 // typename-specifier ( expression-list [opt] )
1904 //
1905 // Parse the remainder of this case, then the (optional)
1906 // postfix-expression suffix, followed by the (optional)
1907 // right-hand side of the binary expression. We have an
1908 // instance method.
John McCalldadc5752010-08-24 06:29:42 +00001909 ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001910 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001911 Receiver = ParsePostfixExpressionSuffix(Receiver.take());
Douglas Gregor8d4de672010-04-21 22:36:40 +00001912 if (!Receiver.isInvalid())
John McCallb268a282010-08-23 23:25:46 +00001913 Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001914 if (Receiver.isInvalid())
1915 return true;
1916
1917 IsExpr = true;
1918 TypeOrExpr = Receiver.take();
1919 return false;
1920 }
1921
1922 // We have a class message. Turn the simple-type-specifier or
1923 // typename-specifier we parsed into a type and parse the
1924 // remainder of the class message.
1925 Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001926 TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Douglas Gregor8d4de672010-04-21 22:36:40 +00001927 if (Type.isInvalid())
1928 return true;
1929
1930 IsExpr = false;
John McCallba7bf592010-08-24 05:47:05 +00001931 TypeOrExpr = Type.get().getAsOpaquePtr();
Douglas Gregor8d4de672010-04-21 22:36:40 +00001932 return false;
1933}
1934
Douglas Gregor990ccac2010-05-31 14:40:22 +00001935/// \brief Determine whether the parser is currently referring to a an
1936/// Objective-C message send, using a simplified heuristic to avoid overhead.
1937///
1938/// This routine will only return true for a subset of valid message-send
1939/// expressions.
1940bool Parser::isSimpleObjCMessageExpression() {
Chris Lattner47054fb2010-05-31 18:18:22 +00001941 assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
Douglas Gregor990ccac2010-05-31 14:40:22 +00001942 "Incorrect start for isSimpleObjCMessageExpression");
Douglas Gregor990ccac2010-05-31 14:40:22 +00001943 return GetLookAheadToken(1).is(tok::identifier) &&
1944 GetLookAheadToken(2).is(tok::identifier);
1945}
1946
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00001947bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
1948 if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
1949 InMessageExpression)
1950 return false;
1951
1952
1953 ParsedType Type;
1954
1955 if (Tok.is(tok::annot_typename))
1956 Type = getTypeAnnotation(Tok);
1957 else if (Tok.is(tok::identifier))
1958 Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
1959 getCurScope());
1960 else
1961 return false;
1962
1963 if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
1964 const Token &AfterNext = GetLookAheadToken(2);
1965 if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
1966 if (Tok.is(tok::identifier))
1967 TryAnnotateTypeOrScopeToken();
1968
1969 return Tok.is(tok::annot_typename);
1970 }
1971 }
1972
1973 return false;
1974}
1975
Mike Stump11289f42009-09-09 15:08:12 +00001976/// objc-message-expr:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001977/// '[' objc-receiver objc-message-args ']'
1978///
Douglas Gregor0c78ad92010-04-21 19:57:20 +00001979/// objc-receiver: [C]
Chris Lattnera36ec422010-04-11 08:28:14 +00001980/// 'super'
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00001981/// expression
1982/// class-name
1983/// type-name
Douglas Gregor8d4de672010-04-21 22:36:40 +00001984///
John McCalldadc5752010-08-24 06:29:42 +00001985ExprResult Parser::ParseObjCMessageExpression() {
Chris Lattner8f697062008-01-25 18:59:06 +00001986 assert(Tok.is(tok::l_square) && "'[' expected");
1987 SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1988
Douglas Gregora817a192010-05-27 23:06:34 +00001989 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001990 Actions.CodeCompleteObjCMessageReceiver(getCurScope());
Douglas Gregora817a192010-05-27 23:06:34 +00001991 ConsumeCodeCompletionToken();
1992 SkipUntil(tok::r_square);
1993 return ExprError();
1994 }
1995
Douglas Gregore9bba4f2010-09-15 14:51:05 +00001996 InMessageExpressionRAIIObject InMessage(*this, true);
1997
Douglas Gregor8d4de672010-04-21 22:36:40 +00001998 if (getLang().CPlusPlus) {
1999 // We completely separate the C and C++ cases because C++ requires
2000 // more complicated (read: slower) parsing.
2001
2002 // Handle send to super.
2003 // FIXME: This doesn't benefit from the same typo-correction we
2004 // get in Objective-C.
2005 if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002006 NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
John McCallba7bf592010-08-24 05:47:05 +00002007 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2008 ParsedType(), 0);
Douglas Gregor8d4de672010-04-21 22:36:40 +00002009
2010 // Parse the receiver, which is either a type or an expression.
2011 bool IsExpr;
Nick Lewycky970aed92010-09-15 18:35:19 +00002012 void *TypeOrExpr = NULL;
Douglas Gregor8d4de672010-04-21 22:36:40 +00002013 if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
2014 SkipUntil(tok::r_square);
2015 return ExprError();
2016 }
2017
2018 if (IsExpr)
John McCallba7bf592010-08-24 05:47:05 +00002019 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2020 ParsedType(),
John McCallb268a282010-08-23 23:25:46 +00002021 static_cast<Expr*>(TypeOrExpr));
Douglas Gregor8d4de672010-04-21 22:36:40 +00002022
2023 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallba7bf592010-08-24 05:47:05 +00002024 ParsedType::getFromOpaquePtr(TypeOrExpr),
2025 0);
Chris Lattner47054fb2010-05-31 18:18:22 +00002026 }
2027
2028 if (Tok.is(tok::identifier)) {
Douglas Gregora148a1d2010-04-14 02:22:16 +00002029 IdentifierInfo *Name = Tok.getIdentifierInfo();
2030 SourceLocation NameLoc = Tok.getLocation();
John McCallba7bf592010-08-24 05:47:05 +00002031 ParsedType ReceiverType;
Douglas Gregor0be31a22010-07-02 17:43:08 +00002032 switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
Douglas Gregora148a1d2010-04-14 02:22:16 +00002033 Name == Ident_super,
Douglas Gregore5798dc2010-04-21 20:38:13 +00002034 NextToken().is(tok::period),
2035 ReceiverType)) {
John McCallfaf5fb42010-08-26 23:41:50 +00002036 case Sema::ObjCSuperMessage:
John McCallba7bf592010-08-24 05:47:05 +00002037 return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2038 ParsedType(), 0);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002039
John McCallfaf5fb42010-08-26 23:41:50 +00002040 case Sema::ObjCClassMessage:
Douglas Gregore5798dc2010-04-21 20:38:13 +00002041 if (!ReceiverType) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002042 SkipUntil(tok::r_square);
2043 return ExprError();
2044 }
2045
Douglas Gregore5798dc2010-04-21 20:38:13 +00002046 ConsumeToken(); // the type name
2047
2048 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
John McCallb268a282010-08-23 23:25:46 +00002049 ReceiverType, 0);
Douglas Gregora148a1d2010-04-14 02:22:16 +00002050
John McCallfaf5fb42010-08-26 23:41:50 +00002051 case Sema::ObjCInstanceMessage:
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002052 // Fall through to parse an expression.
Douglas Gregora148a1d2010-04-14 02:22:16 +00002053 break;
Fariborz Jahanianfc58ca42009-04-08 19:50:10 +00002054 }
Chris Lattner8f697062008-01-25 18:59:06 +00002055 }
Chris Lattnera36ec422010-04-11 08:28:14 +00002056
2057 // Otherwise, an arbitrary expression can be the receiver of a send.
John McCalldadc5752010-08-24 06:29:42 +00002058 ExprResult Res(ParseExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002059 if (Res.isInvalid()) {
Chris Lattner77927cc2008-01-25 19:43:26 +00002060 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002061 return move(Res);
Chris Lattner8f697062008-01-25 18:59:06 +00002062 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002063
John McCallba7bf592010-08-24 05:47:05 +00002064 return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2065 ParsedType(), Res.take());
Chris Lattner8f697062008-01-25 18:59:06 +00002066}
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002067
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002068/// \brief Parse the remainder of an Objective-C message following the
2069/// '[' objc-receiver.
2070///
2071/// This routine handles sends to super, class messages (sent to a
2072/// class name), and instance messages (sent to an object), and the
2073/// target is represented by \p SuperLoc, \p ReceiverType, or \p
2074/// ReceiverExpr, respectively. Only one of these parameters may have
2075/// a valid value.
2076///
2077/// \param LBracLoc The location of the opening '['.
2078///
2079/// \param SuperLoc If this is a send to 'super', the location of the
2080/// 'super' keyword that indicates a send to the superclass.
2081///
2082/// \param ReceiverType If this is a class message, the type of the
2083/// class we are sending a message to.
2084///
2085/// \param ReceiverExpr If this is an instance message, the expression
2086/// used to compute the receiver object.
Mike Stump11289f42009-09-09 15:08:12 +00002087///
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002088/// objc-message-args:
2089/// objc-selector
2090/// objc-keywordarg-list
2091///
2092/// objc-keywordarg-list:
2093/// objc-keywordarg
2094/// objc-keywordarg-list objc-keywordarg
2095///
Mike Stump11289f42009-09-09 15:08:12 +00002096/// objc-keywordarg:
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002097/// selector-name[opt] ':' objc-keywordexpr
2098///
2099/// objc-keywordexpr:
2100/// nonempty-expr-list
2101///
2102/// nonempty-expr-list:
2103/// assignment-expression
2104/// nonempty-expr-list , assignment-expression
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002105///
John McCalldadc5752010-08-24 06:29:42 +00002106ExprResult
Chris Lattner8f697062008-01-25 18:59:06 +00002107Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002108 SourceLocation SuperLoc,
John McCallba7bf592010-08-24 05:47:05 +00002109 ParsedType ReceiverType,
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002110 ExprArg ReceiverExpr) {
Douglas Gregore9bba4f2010-09-15 14:51:05 +00002111 InMessageExpressionRAIIObject InMessage(*this, true);
2112
Steve Naroffeae65032009-11-07 02:08:14 +00002113 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002114 if (SuperLoc.isValid())
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002115 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
2116 false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002117 else if (ReceiverType)
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002118 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
2119 false);
Steve Naroffeae65032009-11-07 02:08:14 +00002120 else
John McCallb268a282010-08-23 23:25:46 +00002121 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002122 0, 0, false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002123 ConsumeCodeCompletionToken();
Steve Naroffeae65032009-11-07 02:08:14 +00002124 }
Douglas Gregor1b605f72009-11-19 01:08:35 +00002125
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002126 // Parse objc-selector
Fariborz Jahanian70e8f102007-10-11 00:55:41 +00002127 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002128 IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
Steve Narofff73590d2007-09-27 14:38:14 +00002129
Anders Carlsson978f08d2009-02-14 18:21:46 +00002130 SourceLocation SelectorLoc = Loc;
Mike Stump11289f42009-09-09 15:08:12 +00002131
Steve Narofff73590d2007-09-27 14:38:14 +00002132 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Sebastian Redl511ed552008-11-25 22:21:31 +00002133 ExprVector KeyExprs(Actions);
Steve Narofff73590d2007-09-27 14:38:14 +00002134
Chris Lattner0ef13522007-10-09 17:51:17 +00002135 if (Tok.is(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002136 while (1) {
2137 // Each iteration parses a single keyword argument.
Steve Narofff73590d2007-09-27 14:38:14 +00002138 KeyIdents.push_back(selIdent);
Steve Naroff486760a2007-09-17 20:25:27 +00002139
Chris Lattner0ef13522007-10-09 17:51:17 +00002140 if (Tok.isNot(tok::colon)) {
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002141 Diag(Tok, diag::err_expected_colon);
Chris Lattner197a3012008-08-05 06:19:09 +00002142 // We must manually skip to a ']', otherwise the expression skipper will
2143 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2144 // the enclosing expression.
2145 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002146 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002147 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002148
Steve Narofff73590d2007-09-27 14:38:14 +00002149 ConsumeToken(); // Eat the ':'.
Mike Stump11289f42009-09-09 15:08:12 +00002150 /// Parse the expression after ':'
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002151
2152 if (Tok.is(tok::code_completion)) {
2153 if (SuperLoc.isValid())
2154 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2155 KeyIdents.data(),
2156 KeyIdents.size(),
2157 /*AtArgumentEpression=*/true);
2158 else if (ReceiverType)
2159 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2160 KeyIdents.data(),
2161 KeyIdents.size(),
2162 /*AtArgumentEpression=*/true);
2163 else
2164 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2165 KeyIdents.data(),
2166 KeyIdents.size(),
2167 /*AtArgumentEpression=*/true);
2168
2169 ConsumeCodeCompletionToken();
2170 SkipUntil(tok::r_square);
2171 return ExprError();
2172 }
2173
John McCalldadc5752010-08-24 06:29:42 +00002174 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002175 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002176 // We must manually skip to a ']', otherwise the expression skipper will
2177 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2178 // the enclosing expression.
2179 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002180 return move(Res);
Steve Naroff486760a2007-09-17 20:25:27 +00002181 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002182
Steve Naroff486760a2007-09-17 20:25:27 +00002183 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002184 KeyExprs.push_back(Res.release());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002185
Douglas Gregor1b605f72009-11-19 01:08:35 +00002186 // Code completion after each argument.
2187 if (Tok.is(tok::code_completion)) {
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002188 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002189 Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002190 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002191 KeyIdents.size(),
2192 /*AtArgumentEpression=*/false);
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002193 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002194 Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002195 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002196 KeyIdents.size(),
2197 /*AtArgumentEpression=*/false);
Douglas Gregor1b605f72009-11-19 01:08:35 +00002198 else
John McCallb268a282010-08-23 23:25:46 +00002199 Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
Douglas Gregor1b605f72009-11-19 01:08:35 +00002200 KeyIdents.data(),
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002201 KeyIdents.size(),
2202 /*AtArgumentEpression=*/false);
Douglas Gregor6da3db42010-05-25 05:58:43 +00002203 ConsumeCodeCompletionToken();
Douglas Gregorf86e4da2010-09-20 23:34:21 +00002204 SkipUntil(tok::r_square);
2205 return ExprError();
Douglas Gregor1b605f72009-11-19 01:08:35 +00002206 }
2207
Steve Naroff486760a2007-09-17 20:25:27 +00002208 // Check for another keyword selector.
Chris Lattner4f472a32009-04-11 18:13:45 +00002209 selIdent = ParseObjCSelectorPiece(Loc);
Chris Lattner0ef13522007-10-09 17:51:17 +00002210 if (!selIdent && Tok.isNot(tok::colon))
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002211 break;
2212 // We have a selector or a colon, continue parsing.
2213 }
2214 // Parse the, optional, argument list, comma separated.
Chris Lattner0ef13522007-10-09 17:51:17 +00002215 while (Tok.is(tok::comma)) {
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002216 ConsumeToken(); // Eat the ','.
Mike Stump11289f42009-09-09 15:08:12 +00002217 /// Parse the expression after ','
John McCalldadc5752010-08-24 06:29:42 +00002218 ExprResult Res(ParseAssignmentExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002219 if (Res.isInvalid()) {
Chris Lattner197a3012008-08-05 06:19:09 +00002220 // We must manually skip to a ']', otherwise the expression skipper will
2221 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2222 // the enclosing expression.
2223 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002224 return move(Res);
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002225 }
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002226
Steve Naroffe3ffc2f2007-11-15 13:05:42 +00002227 // We have a valid expression.
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002228 KeyExprs.push_back(Res.release());
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002229 }
2230 } else if (!selIdent) {
2231 Diag(Tok, diag::err_expected_ident); // missing selector name.
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002232
Chris Lattner197a3012008-08-05 06:19:09 +00002233 // We must manually skip to a ']', otherwise the expression skipper will
2234 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2235 // the enclosing expression.
2236 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002237 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002238 }
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002239
Chris Lattner0ef13522007-10-09 17:51:17 +00002240 if (Tok.isNot(tok::r_square)) {
Fariborz Jahanian083712f2010-03-31 20:22:35 +00002241 if (Tok.is(tok::identifier))
2242 Diag(Tok, diag::err_expected_colon);
2243 else
2244 Diag(Tok, diag::err_expected_rsquare);
Chris Lattner197a3012008-08-05 06:19:09 +00002245 // We must manually skip to a ']', otherwise the expression skipper will
2246 // stop at the ']' when it skips to the ';'. We want it to skip beyond
2247 // the enclosing expression.
2248 SkipUntil(tok::r_square);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002249 return ExprError();
Fariborz Jahanianbd25f7d2007-09-05 23:08:20 +00002250 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002251
Chris Lattner8f697062008-01-25 18:59:06 +00002252 SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002253
Steve Naroffe61bfa82007-10-05 18:42:47 +00002254 unsigned nKeys = KeyIdents.size();
Chris Lattner5700fab2007-10-07 02:00:24 +00002255 if (nKeys == 0)
2256 KeyIdents.push_back(selIdent);
2257 Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002258
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002259 if (SuperLoc.isValid())
Douglas Gregor0be31a22010-07-02 17:43:08 +00002260 return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002261 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002262 MultiExprArg(Actions,
2263 KeyExprs.take(),
2264 KeyExprs.size()));
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002265 else if (ReceiverType)
Douglas Gregor0be31a22010-07-02 17:43:08 +00002266 return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002267 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002268 MultiExprArg(Actions,
2269 KeyExprs.take(),
2270 KeyExprs.size()));
John McCallb268a282010-08-23 23:25:46 +00002271 return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
Douglas Gregor0c78ad92010-04-21 19:57:20 +00002272 LBracLoc, SelectorLoc, RBracLoc,
John McCallfaf5fb42010-08-26 23:41:50 +00002273 MultiExprArg(Actions,
2274 KeyExprs.take(),
2275 KeyExprs.size()));
Fariborz Jahanian7db004d2007-09-05 19:52:07 +00002276}
2277
John McCalldadc5752010-08-24 06:29:42 +00002278ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2279 ExprResult Res(ParseStringLiteralExpression());
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002280 if (Res.isInvalid()) return move(Res);
2281
Chris Lattnere002fbe2007-12-12 01:04:12 +00002282 // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
2283 // expressions. At this point, we know that the only valid thing that starts
2284 // with '@' is an @"".
2285 llvm::SmallVector<SourceLocation, 4> AtLocs;
Sebastian Redl511ed552008-11-25 22:21:31 +00002286 ExprVector AtStrings(Actions);
Chris Lattnere002fbe2007-12-12 01:04:12 +00002287 AtLocs.push_back(AtLoc);
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002288 AtStrings.push_back(Res.release());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002289
Chris Lattnere002fbe2007-12-12 01:04:12 +00002290 while (Tok.is(tok::at)) {
2291 AtLocs.push_back(ConsumeToken()); // eat the @.
Anders Carlsson76f4a902007-08-21 17:43:55 +00002292
Sebastian Redlc13f2682008-12-09 20:22:58 +00002293 // Invalid unless there is a string literal.
Chris Lattnerd3b5d5d2009-02-18 05:56:09 +00002294 if (!isTokenStringLiteral())
2295 return ExprError(Diag(Tok, diag::err_objc_concat_string));
Chris Lattnere002fbe2007-12-12 01:04:12 +00002296
John McCalldadc5752010-08-24 06:29:42 +00002297 ExprResult Lit(ParseStringLiteralExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002298 if (Lit.isInvalid())
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002299 return move(Lit);
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002300
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002301 AtStrings.push_back(Lit.release());
Chris Lattnere002fbe2007-12-12 01:04:12 +00002302 }
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002303
2304 return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2305 AtStrings.size()));
Anders Carlsson76f4a902007-08-21 17:43:55 +00002306}
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002307
2308/// objc-encode-expression:
2309/// @encode ( type-name )
John McCalldadc5752010-08-24 06:29:42 +00002310ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002311Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
Steve Naroff7c348172007-08-23 18:16:40 +00002312 assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002313
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002314 SourceLocation EncLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002315
Chris Lattner197a3012008-08-05 06:19:09 +00002316 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002317 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2318
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002319 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002320
Douglas Gregor220cac52009-02-18 17:45:20 +00002321 TypeResult Ty = ParseTypeName();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002322
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002323 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002324
Douglas Gregor220cac52009-02-18 17:45:20 +00002325 if (Ty.isInvalid())
2326 return ExprError();
2327
Mike Stump11289f42009-09-09 15:08:12 +00002328 return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
Douglas Gregor220cac52009-02-18 17:45:20 +00002329 Ty.get(), RParenLoc));
Anders Carlssonc5a81eb2007-08-22 15:14:15 +00002330}
Anders Carlssone01493d2007-08-23 15:25:28 +00002331
2332/// objc-protocol-expression
2333/// @protocol ( protocol-name )
John McCalldadc5752010-08-24 06:29:42 +00002334ExprResult
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002335Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
Anders Carlssone01493d2007-08-23 15:25:28 +00002336 SourceLocation ProtoLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002337
Chris Lattner197a3012008-08-05 06:19:09 +00002338 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002339 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2340
Anders Carlssone01493d2007-08-23 15:25:28 +00002341 SourceLocation LParenLoc = ConsumeParen();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002342
Chris Lattner197a3012008-08-05 06:19:09 +00002343 if (Tok.isNot(tok::identifier))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002344 return ExprError(Diag(Tok, diag::err_expected_ident));
2345
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002346 IdentifierInfo *protocolId = Tok.getIdentifierInfo();
Anders Carlssone01493d2007-08-23 15:25:28 +00002347 ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002348
Anders Carlssoncb8f8322007-08-23 15:31:37 +00002349 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Anders Carlssone01493d2007-08-23 15:25:28 +00002350
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002351 return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2352 LParenLoc, RParenLoc));
Anders Carlssone01493d2007-08-23 15:25:28 +00002353}
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002354
2355/// objc-selector-expression
2356/// @selector '(' objc-keyword-selector ')'
John McCalldadc5752010-08-24 06:29:42 +00002357ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002358 SourceLocation SelectorLoc = ConsumeToken();
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002359
Chris Lattner197a3012008-08-05 06:19:09 +00002360 if (Tok.isNot(tok::l_paren))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002361 return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2362
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002363 llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002364 SourceLocation LParenLoc = ConsumeParen();
2365 SourceLocation sLoc;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002366
2367 if (Tok.is(tok::code_completion)) {
2368 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2369 KeyIdents.size());
2370 ConsumeCodeCompletionToken();
2371 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2372 return ExprError();
2373 }
2374
Chris Lattner4f472a32009-04-11 18:13:45 +00002375 IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
Chris Lattner1ba64452010-08-27 22:32:41 +00002376 if (!SelIdent && // missing selector name.
2377 Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002378 return ExprError(Diag(Tok, diag::err_expected_ident));
2379
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002380 KeyIdents.push_back(SelIdent);
Steve Naroff152dd812007-12-05 22:21:29 +00002381 unsigned nColons = 0;
2382 if (Tok.isNot(tok::r_paren)) {
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002383 while (1) {
Chris Lattner1ba64452010-08-27 22:32:41 +00002384 if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
2385 ++nColons;
2386 KeyIdents.push_back(0);
2387 } else if (Tok.isNot(tok::colon))
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002388 return ExprError(Diag(Tok, diag::err_expected_colon));
2389
Chris Lattner1ba64452010-08-27 22:32:41 +00002390 ++nColons;
Chris Lattner85222c62011-03-26 18:11:38 +00002391 ConsumeToken(); // Eat the ':' or '::'.
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002392 if (Tok.is(tok::r_paren))
2393 break;
Douglas Gregor67c692c2010-08-26 15:07:07 +00002394
2395 if (Tok.is(tok::code_completion)) {
2396 Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2397 KeyIdents.size());
2398 ConsumeCodeCompletionToken();
2399 MatchRHSPunctuation(tok::r_paren, LParenLoc);
2400 return ExprError();
2401 }
2402
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002403 // Check for another keyword selector.
2404 SourceLocation Loc;
Chris Lattner4f472a32009-04-11 18:13:45 +00002405 SelIdent = ParseObjCSelectorPiece(Loc);
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00002406 KeyIdents.push_back(SelIdent);
Chris Lattner85222c62011-03-26 18:11:38 +00002407 if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002408 break;
2409 }
Steve Naroff152dd812007-12-05 22:21:29 +00002410 }
Fariborz Jahanian76a94272007-10-15 23:39:13 +00002411 SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
Steve Naroff152dd812007-12-05 22:21:29 +00002412 Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
Sebastian Redlcb6e2c62008-12-13 15:32:12 +00002413 return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2414 LParenLoc, RParenLoc));
Gabor Greif24032f12007-10-19 15:38:32 +00002415 }