1*67e74705SXin Li //===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file implements the Objective-C portions of the Parser interface.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li
14*67e74705SXin Li #include "clang/Parse/Parser.h"
15*67e74705SXin Li #include "RAIIObjectsForParser.h"
16*67e74705SXin Li #include "clang/AST/ASTContext.h"
17*67e74705SXin Li #include "clang/Basic/CharInfo.h"
18*67e74705SXin Li #include "clang/Parse/ParseDiagnostic.h"
19*67e74705SXin Li #include "clang/Sema/DeclSpec.h"
20*67e74705SXin Li #include "clang/Sema/PrettyDeclStackTrace.h"
21*67e74705SXin Li #include "clang/Sema/Scope.h"
22*67e74705SXin Li #include "llvm/ADT/SmallVector.h"
23*67e74705SXin Li #include "llvm/ADT/StringExtras.h"
24*67e74705SXin Li
25*67e74705SXin Li using namespace clang;
26*67e74705SXin Li
27*67e74705SXin Li /// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
MaybeSkipAttributes(tok::ObjCKeywordKind Kind)28*67e74705SXin Li void Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
29*67e74705SXin Li ParsedAttributes attrs(AttrFactory);
30*67e74705SXin Li if (Tok.is(tok::kw___attribute)) {
31*67e74705SXin Li if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
32*67e74705SXin Li Diag(Tok, diag::err_objc_postfix_attribute_hint)
33*67e74705SXin Li << (Kind == tok::objc_protocol);
34*67e74705SXin Li else
35*67e74705SXin Li Diag(Tok, diag::err_objc_postfix_attribute);
36*67e74705SXin Li ParseGNUAttributes(attrs);
37*67e74705SXin Li }
38*67e74705SXin Li }
39*67e74705SXin Li
40*67e74705SXin Li /// ParseObjCAtDirectives - Handle parts of the external-declaration production:
41*67e74705SXin Li /// external-declaration: [C99 6.9]
42*67e74705SXin Li /// [OBJC] objc-class-definition
43*67e74705SXin Li /// [OBJC] objc-class-declaration
44*67e74705SXin Li /// [OBJC] objc-alias-declaration
45*67e74705SXin Li /// [OBJC] objc-protocol-definition
46*67e74705SXin Li /// [OBJC] objc-method-definition
47*67e74705SXin Li /// [OBJC] '@' 'end'
ParseObjCAtDirectives()48*67e74705SXin Li Parser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
49*67e74705SXin Li SourceLocation AtLoc = ConsumeToken(); // the "@"
50*67e74705SXin Li
51*67e74705SXin Li if (Tok.is(tok::code_completion)) {
52*67e74705SXin Li Actions.CodeCompleteObjCAtDirective(getCurScope());
53*67e74705SXin Li cutOffParsing();
54*67e74705SXin Li return nullptr;
55*67e74705SXin Li }
56*67e74705SXin Li
57*67e74705SXin Li Decl *SingleDecl = nullptr;
58*67e74705SXin Li switch (Tok.getObjCKeywordID()) {
59*67e74705SXin Li case tok::objc_class:
60*67e74705SXin Li return ParseObjCAtClassDeclaration(AtLoc);
61*67e74705SXin Li case tok::objc_interface: {
62*67e74705SXin Li ParsedAttributes attrs(AttrFactory);
63*67e74705SXin Li SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
64*67e74705SXin Li break;
65*67e74705SXin Li }
66*67e74705SXin Li case tok::objc_protocol: {
67*67e74705SXin Li ParsedAttributes attrs(AttrFactory);
68*67e74705SXin Li return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
69*67e74705SXin Li }
70*67e74705SXin Li case tok::objc_implementation:
71*67e74705SXin Li return ParseObjCAtImplementationDeclaration(AtLoc);
72*67e74705SXin Li case tok::objc_end:
73*67e74705SXin Li return ParseObjCAtEndDeclaration(AtLoc);
74*67e74705SXin Li case tok::objc_compatibility_alias:
75*67e74705SXin Li SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
76*67e74705SXin Li break;
77*67e74705SXin Li case tok::objc_synthesize:
78*67e74705SXin Li SingleDecl = ParseObjCPropertySynthesize(AtLoc);
79*67e74705SXin Li break;
80*67e74705SXin Li case tok::objc_dynamic:
81*67e74705SXin Li SingleDecl = ParseObjCPropertyDynamic(AtLoc);
82*67e74705SXin Li break;
83*67e74705SXin Li case tok::objc_import:
84*67e74705SXin Li if (getLangOpts().Modules || getLangOpts().DebuggerSupport)
85*67e74705SXin Li return ParseModuleImport(AtLoc);
86*67e74705SXin Li Diag(AtLoc, diag::err_atimport);
87*67e74705SXin Li SkipUntil(tok::semi);
88*67e74705SXin Li return Actions.ConvertDeclToDeclGroup(nullptr);
89*67e74705SXin Li default:
90*67e74705SXin Li Diag(AtLoc, diag::err_unexpected_at);
91*67e74705SXin Li SkipUntil(tok::semi);
92*67e74705SXin Li SingleDecl = nullptr;
93*67e74705SXin Li break;
94*67e74705SXin Li }
95*67e74705SXin Li return Actions.ConvertDeclToDeclGroup(SingleDecl);
96*67e74705SXin Li }
97*67e74705SXin Li
98*67e74705SXin Li /// Class to handle popping type parameters when leaving the scope.
99*67e74705SXin Li class Parser::ObjCTypeParamListScope {
100*67e74705SXin Li Sema &Actions;
101*67e74705SXin Li Scope *S;
102*67e74705SXin Li ObjCTypeParamList *Params;
103*67e74705SXin Li
104*67e74705SXin Li public:
ObjCTypeParamListScope(Sema & Actions,Scope * S)105*67e74705SXin Li ObjCTypeParamListScope(Sema &Actions, Scope *S)
106*67e74705SXin Li : Actions(Actions), S(S), Params(nullptr) {}
107*67e74705SXin Li
~ObjCTypeParamListScope()108*67e74705SXin Li ~ObjCTypeParamListScope() {
109*67e74705SXin Li leave();
110*67e74705SXin Li }
111*67e74705SXin Li
enter(ObjCTypeParamList * P)112*67e74705SXin Li void enter(ObjCTypeParamList *P) {
113*67e74705SXin Li assert(!Params);
114*67e74705SXin Li Params = P;
115*67e74705SXin Li }
116*67e74705SXin Li
leave()117*67e74705SXin Li void leave() {
118*67e74705SXin Li if (Params)
119*67e74705SXin Li Actions.popObjCTypeParamList(S, Params);
120*67e74705SXin Li Params = nullptr;
121*67e74705SXin Li }
122*67e74705SXin Li };
123*67e74705SXin Li
124*67e74705SXin Li ///
125*67e74705SXin Li /// objc-class-declaration:
126*67e74705SXin Li /// '@' 'class' objc-class-forward-decl (',' objc-class-forward-decl)* ';'
127*67e74705SXin Li ///
128*67e74705SXin Li /// objc-class-forward-decl:
129*67e74705SXin Li /// identifier objc-type-parameter-list[opt]
130*67e74705SXin Li ///
131*67e74705SXin Li Parser::DeclGroupPtrTy
ParseObjCAtClassDeclaration(SourceLocation atLoc)132*67e74705SXin Li Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
133*67e74705SXin Li ConsumeToken(); // the identifier "class"
134*67e74705SXin Li SmallVector<IdentifierInfo *, 8> ClassNames;
135*67e74705SXin Li SmallVector<SourceLocation, 8> ClassLocs;
136*67e74705SXin Li SmallVector<ObjCTypeParamList *, 8> ClassTypeParams;
137*67e74705SXin Li
138*67e74705SXin Li while (1) {
139*67e74705SXin Li MaybeSkipAttributes(tok::objc_class);
140*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
141*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
142*67e74705SXin Li SkipUntil(tok::semi);
143*67e74705SXin Li return Actions.ConvertDeclToDeclGroup(nullptr);
144*67e74705SXin Li }
145*67e74705SXin Li ClassNames.push_back(Tok.getIdentifierInfo());
146*67e74705SXin Li ClassLocs.push_back(Tok.getLocation());
147*67e74705SXin Li ConsumeToken();
148*67e74705SXin Li
149*67e74705SXin Li // Parse the optional objc-type-parameter-list.
150*67e74705SXin Li ObjCTypeParamList *TypeParams = nullptr;
151*67e74705SXin Li if (Tok.is(tok::less))
152*67e74705SXin Li TypeParams = parseObjCTypeParamList();
153*67e74705SXin Li ClassTypeParams.push_back(TypeParams);
154*67e74705SXin Li if (!TryConsumeToken(tok::comma))
155*67e74705SXin Li break;
156*67e74705SXin Li }
157*67e74705SXin Li
158*67e74705SXin Li // Consume the ';'.
159*67e74705SXin Li if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@class"))
160*67e74705SXin Li return Actions.ConvertDeclToDeclGroup(nullptr);
161*67e74705SXin Li
162*67e74705SXin Li return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
163*67e74705SXin Li ClassLocs.data(),
164*67e74705SXin Li ClassTypeParams,
165*67e74705SXin Li ClassNames.size());
166*67e74705SXin Li }
167*67e74705SXin Li
CheckNestedObjCContexts(SourceLocation AtLoc)168*67e74705SXin Li void Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
169*67e74705SXin Li {
170*67e74705SXin Li Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
171*67e74705SXin Li if (ock == Sema::OCK_None)
172*67e74705SXin Li return;
173*67e74705SXin Li
174*67e74705SXin Li Decl *Decl = Actions.getObjCDeclContext();
175*67e74705SXin Li if (CurParsedObjCImpl) {
176*67e74705SXin Li CurParsedObjCImpl->finish(AtLoc);
177*67e74705SXin Li } else {
178*67e74705SXin Li Actions.ActOnAtEnd(getCurScope(), AtLoc);
179*67e74705SXin Li }
180*67e74705SXin Li Diag(AtLoc, diag::err_objc_missing_end)
181*67e74705SXin Li << FixItHint::CreateInsertion(AtLoc, "@end\n");
182*67e74705SXin Li if (Decl)
183*67e74705SXin Li Diag(Decl->getLocStart(), diag::note_objc_container_start)
184*67e74705SXin Li << (int) ock;
185*67e74705SXin Li }
186*67e74705SXin Li
187*67e74705SXin Li ///
188*67e74705SXin Li /// objc-interface:
189*67e74705SXin Li /// objc-class-interface-attributes[opt] objc-class-interface
190*67e74705SXin Li /// objc-category-interface
191*67e74705SXin Li ///
192*67e74705SXin Li /// objc-class-interface:
193*67e74705SXin Li /// '@' 'interface' identifier objc-type-parameter-list[opt]
194*67e74705SXin Li /// objc-superclass[opt] objc-protocol-refs[opt]
195*67e74705SXin Li /// objc-class-instance-variables[opt]
196*67e74705SXin Li /// objc-interface-decl-list
197*67e74705SXin Li /// @end
198*67e74705SXin Li ///
199*67e74705SXin Li /// objc-category-interface:
200*67e74705SXin Li /// '@' 'interface' identifier objc-type-parameter-list[opt]
201*67e74705SXin Li /// '(' identifier[opt] ')' objc-protocol-refs[opt]
202*67e74705SXin Li /// objc-interface-decl-list
203*67e74705SXin Li /// @end
204*67e74705SXin Li ///
205*67e74705SXin Li /// objc-superclass:
206*67e74705SXin Li /// ':' identifier objc-type-arguments[opt]
207*67e74705SXin Li ///
208*67e74705SXin Li /// objc-class-interface-attributes:
209*67e74705SXin Li /// __attribute__((visibility("default")))
210*67e74705SXin Li /// __attribute__((visibility("hidden")))
211*67e74705SXin Li /// __attribute__((deprecated))
212*67e74705SXin Li /// __attribute__((unavailable))
213*67e74705SXin Li /// __attribute__((objc_exception)) - used by NSException on 64-bit
214*67e74705SXin Li /// __attribute__((objc_root_class))
215*67e74705SXin Li ///
ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,ParsedAttributes & attrs)216*67e74705SXin Li Decl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
217*67e74705SXin Li ParsedAttributes &attrs) {
218*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
219*67e74705SXin Li "ParseObjCAtInterfaceDeclaration(): Expected @interface");
220*67e74705SXin Li CheckNestedObjCContexts(AtLoc);
221*67e74705SXin Li ConsumeToken(); // the "interface" identifier
222*67e74705SXin Li
223*67e74705SXin Li // Code completion after '@interface'.
224*67e74705SXin Li if (Tok.is(tok::code_completion)) {
225*67e74705SXin Li Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
226*67e74705SXin Li cutOffParsing();
227*67e74705SXin Li return nullptr;
228*67e74705SXin Li }
229*67e74705SXin Li
230*67e74705SXin Li MaybeSkipAttributes(tok::objc_interface);
231*67e74705SXin Li
232*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
233*67e74705SXin Li Diag(Tok, diag::err_expected)
234*67e74705SXin Li << tok::identifier; // missing class or category name.
235*67e74705SXin Li return nullptr;
236*67e74705SXin Li }
237*67e74705SXin Li
238*67e74705SXin Li // We have a class or category name - consume it.
239*67e74705SXin Li IdentifierInfo *nameId = Tok.getIdentifierInfo();
240*67e74705SXin Li SourceLocation nameLoc = ConsumeToken();
241*67e74705SXin Li
242*67e74705SXin Li // Parse the objc-type-parameter-list or objc-protocol-refs. For the latter
243*67e74705SXin Li // case, LAngleLoc will be valid and ProtocolIdents will capture the
244*67e74705SXin Li // protocol references (that have not yet been resolved).
245*67e74705SXin Li SourceLocation LAngleLoc, EndProtoLoc;
246*67e74705SXin Li SmallVector<IdentifierLocPair, 8> ProtocolIdents;
247*67e74705SXin Li ObjCTypeParamList *typeParameterList = nullptr;
248*67e74705SXin Li ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
249*67e74705SXin Li if (Tok.is(tok::less))
250*67e74705SXin Li typeParameterList = parseObjCTypeParamListOrProtocolRefs(
251*67e74705SXin Li typeParamScope, LAngleLoc, ProtocolIdents, EndProtoLoc);
252*67e74705SXin Li
253*67e74705SXin Li if (Tok.is(tok::l_paren) &&
254*67e74705SXin Li !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
255*67e74705SXin Li
256*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
257*67e74705SXin Li T.consumeOpen();
258*67e74705SXin Li
259*67e74705SXin Li SourceLocation categoryLoc;
260*67e74705SXin Li IdentifierInfo *categoryId = nullptr;
261*67e74705SXin Li if (Tok.is(tok::code_completion)) {
262*67e74705SXin Li Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
263*67e74705SXin Li cutOffParsing();
264*67e74705SXin Li return nullptr;
265*67e74705SXin Li }
266*67e74705SXin Li
267*67e74705SXin Li // For ObjC2, the category name is optional (not an error).
268*67e74705SXin Li if (Tok.is(tok::identifier)) {
269*67e74705SXin Li categoryId = Tok.getIdentifierInfo();
270*67e74705SXin Li categoryLoc = ConsumeToken();
271*67e74705SXin Li }
272*67e74705SXin Li else if (!getLangOpts().ObjC2) {
273*67e74705SXin Li Diag(Tok, diag::err_expected)
274*67e74705SXin Li << tok::identifier; // missing category name.
275*67e74705SXin Li return nullptr;
276*67e74705SXin Li }
277*67e74705SXin Li
278*67e74705SXin Li T.consumeClose();
279*67e74705SXin Li if (T.getCloseLocation().isInvalid())
280*67e74705SXin Li return nullptr;
281*67e74705SXin Li
282*67e74705SXin Li if (!attrs.empty()) { // categories don't support attributes.
283*67e74705SXin Li Diag(nameLoc, diag::err_objc_no_attributes_on_category);
284*67e74705SXin Li attrs.clear();
285*67e74705SXin Li }
286*67e74705SXin Li
287*67e74705SXin Li // Next, we need to check for any protocol references.
288*67e74705SXin Li assert(LAngleLoc.isInvalid() && "Cannot have already parsed protocols");
289*67e74705SXin Li SmallVector<Decl *, 8> ProtocolRefs;
290*67e74705SXin Li SmallVector<SourceLocation, 8> ProtocolLocs;
291*67e74705SXin Li if (Tok.is(tok::less) &&
292*67e74705SXin Li ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true, true,
293*67e74705SXin Li LAngleLoc, EndProtoLoc,
294*67e74705SXin Li /*consumeLastToken=*/true))
295*67e74705SXin Li return nullptr;
296*67e74705SXin Li
297*67e74705SXin Li Decl *CategoryType =
298*67e74705SXin Li Actions.ActOnStartCategoryInterface(AtLoc,
299*67e74705SXin Li nameId, nameLoc,
300*67e74705SXin Li typeParameterList,
301*67e74705SXin Li categoryId, categoryLoc,
302*67e74705SXin Li ProtocolRefs.data(),
303*67e74705SXin Li ProtocolRefs.size(),
304*67e74705SXin Li ProtocolLocs.data(),
305*67e74705SXin Li EndProtoLoc);
306*67e74705SXin Li
307*67e74705SXin Li if (Tok.is(tok::l_brace))
308*67e74705SXin Li ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
309*67e74705SXin Li
310*67e74705SXin Li ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
311*67e74705SXin Li
312*67e74705SXin Li return CategoryType;
313*67e74705SXin Li }
314*67e74705SXin Li // Parse a class interface.
315*67e74705SXin Li IdentifierInfo *superClassId = nullptr;
316*67e74705SXin Li SourceLocation superClassLoc;
317*67e74705SXin Li SourceLocation typeArgsLAngleLoc;
318*67e74705SXin Li SmallVector<ParsedType, 4> typeArgs;
319*67e74705SXin Li SourceLocation typeArgsRAngleLoc;
320*67e74705SXin Li SmallVector<Decl *, 4> protocols;
321*67e74705SXin Li SmallVector<SourceLocation, 4> protocolLocs;
322*67e74705SXin Li if (Tok.is(tok::colon)) { // a super class is specified.
323*67e74705SXin Li ConsumeToken();
324*67e74705SXin Li
325*67e74705SXin Li // Code completion of superclass names.
326*67e74705SXin Li if (Tok.is(tok::code_completion)) {
327*67e74705SXin Li Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
328*67e74705SXin Li cutOffParsing();
329*67e74705SXin Li return nullptr;
330*67e74705SXin Li }
331*67e74705SXin Li
332*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
333*67e74705SXin Li Diag(Tok, diag::err_expected)
334*67e74705SXin Li << tok::identifier; // missing super class name.
335*67e74705SXin Li return nullptr;
336*67e74705SXin Li }
337*67e74705SXin Li superClassId = Tok.getIdentifierInfo();
338*67e74705SXin Li superClassLoc = ConsumeToken();
339*67e74705SXin Li
340*67e74705SXin Li // Type arguments for the superclass or protocol conformances.
341*67e74705SXin Li if (Tok.is(tok::less)) {
342*67e74705SXin Li parseObjCTypeArgsOrProtocolQualifiers(
343*67e74705SXin Li nullptr, typeArgsLAngleLoc, typeArgs, typeArgsRAngleLoc, LAngleLoc,
344*67e74705SXin Li protocols, protocolLocs, EndProtoLoc,
345*67e74705SXin Li /*consumeLastToken=*/true,
346*67e74705SXin Li /*warnOnIncompleteProtocols=*/true);
347*67e74705SXin Li }
348*67e74705SXin Li }
349*67e74705SXin Li
350*67e74705SXin Li // Next, we need to check for any protocol references.
351*67e74705SXin Li if (LAngleLoc.isValid()) {
352*67e74705SXin Li if (!ProtocolIdents.empty()) {
353*67e74705SXin Li // We already parsed the protocols named when we thought we had a
354*67e74705SXin Li // type parameter list. Translate them into actual protocol references.
355*67e74705SXin Li for (const auto &pair : ProtocolIdents) {
356*67e74705SXin Li protocolLocs.push_back(pair.second);
357*67e74705SXin Li }
358*67e74705SXin Li Actions.FindProtocolDeclaration(/*WarnOnDeclarations=*/true,
359*67e74705SXin Li /*ForObjCContainer=*/true,
360*67e74705SXin Li ProtocolIdents, protocols);
361*67e74705SXin Li }
362*67e74705SXin Li } else if (protocols.empty() && Tok.is(tok::less) &&
363*67e74705SXin Li ParseObjCProtocolReferences(protocols, protocolLocs, true, true,
364*67e74705SXin Li LAngleLoc, EndProtoLoc,
365*67e74705SXin Li /*consumeLastToken=*/true)) {
366*67e74705SXin Li return nullptr;
367*67e74705SXin Li }
368*67e74705SXin Li
369*67e74705SXin Li if (Tok.isNot(tok::less))
370*67e74705SXin Li Actions.ActOnTypedefedProtocols(protocols, superClassId, superClassLoc);
371*67e74705SXin Li
372*67e74705SXin Li Decl *ClsType =
373*67e74705SXin Li Actions.ActOnStartClassInterface(getCurScope(), AtLoc, nameId, nameLoc,
374*67e74705SXin Li typeParameterList, superClassId,
375*67e74705SXin Li superClassLoc,
376*67e74705SXin Li typeArgs,
377*67e74705SXin Li SourceRange(typeArgsLAngleLoc,
378*67e74705SXin Li typeArgsRAngleLoc),
379*67e74705SXin Li protocols.data(), protocols.size(),
380*67e74705SXin Li protocolLocs.data(),
381*67e74705SXin Li EndProtoLoc, attrs.getList());
382*67e74705SXin Li
383*67e74705SXin Li if (Tok.is(tok::l_brace))
384*67e74705SXin Li ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
385*67e74705SXin Li
386*67e74705SXin Li ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
387*67e74705SXin Li
388*67e74705SXin Li return ClsType;
389*67e74705SXin Li }
390*67e74705SXin Li
391*67e74705SXin Li /// Add an attribute for a context-sensitive type nullability to the given
392*67e74705SXin Li /// declarator.
addContextSensitiveTypeNullability(Parser & P,Declarator & D,NullabilityKind nullability,SourceLocation nullabilityLoc,bool & addedToDeclSpec)393*67e74705SXin Li static void addContextSensitiveTypeNullability(Parser &P,
394*67e74705SXin Li Declarator &D,
395*67e74705SXin Li NullabilityKind nullability,
396*67e74705SXin Li SourceLocation nullabilityLoc,
397*67e74705SXin Li bool &addedToDeclSpec) {
398*67e74705SXin Li // Create the attribute.
399*67e74705SXin Li auto getNullabilityAttr = [&]() -> AttributeList * {
400*67e74705SXin Li return D.getAttributePool().create(
401*67e74705SXin Li P.getNullabilityKeyword(nullability),
402*67e74705SXin Li SourceRange(nullabilityLoc),
403*67e74705SXin Li nullptr, SourceLocation(),
404*67e74705SXin Li nullptr, 0,
405*67e74705SXin Li AttributeList::AS_ContextSensitiveKeyword);
406*67e74705SXin Li };
407*67e74705SXin Li
408*67e74705SXin Li if (D.getNumTypeObjects() > 0) {
409*67e74705SXin Li // Add the attribute to the declarator chunk nearest the declarator.
410*67e74705SXin Li auto nullabilityAttr = getNullabilityAttr();
411*67e74705SXin Li DeclaratorChunk &chunk = D.getTypeObject(0);
412*67e74705SXin Li nullabilityAttr->setNext(chunk.getAttrListRef());
413*67e74705SXin Li chunk.getAttrListRef() = nullabilityAttr;
414*67e74705SXin Li } else if (!addedToDeclSpec) {
415*67e74705SXin Li // Otherwise, just put it on the declaration specifiers (if one
416*67e74705SXin Li // isn't there already).
417*67e74705SXin Li D.getMutableDeclSpec().addAttributes(getNullabilityAttr());
418*67e74705SXin Li addedToDeclSpec = true;
419*67e74705SXin Li }
420*67e74705SXin Li }
421*67e74705SXin Li
422*67e74705SXin Li /// Parse an Objective-C type parameter list, if present, or capture
423*67e74705SXin Li /// the locations of the protocol identifiers for a list of protocol
424*67e74705SXin Li /// references.
425*67e74705SXin Li ///
426*67e74705SXin Li /// objc-type-parameter-list:
427*67e74705SXin Li /// '<' objc-type-parameter (',' objc-type-parameter)* '>'
428*67e74705SXin Li ///
429*67e74705SXin Li /// objc-type-parameter:
430*67e74705SXin Li /// objc-type-parameter-variance? identifier objc-type-parameter-bound[opt]
431*67e74705SXin Li ///
432*67e74705SXin Li /// objc-type-parameter-bound:
433*67e74705SXin Li /// ':' type-name
434*67e74705SXin Li ///
435*67e74705SXin Li /// objc-type-parameter-variance:
436*67e74705SXin Li /// '__covariant'
437*67e74705SXin Li /// '__contravariant'
438*67e74705SXin Li ///
439*67e74705SXin Li /// \param lAngleLoc The location of the starting '<'.
440*67e74705SXin Li ///
441*67e74705SXin Li /// \param protocolIdents Will capture the list of identifiers, if the
442*67e74705SXin Li /// angle brackets contain a list of protocol references rather than a
443*67e74705SXin Li /// type parameter list.
444*67e74705SXin Li ///
445*67e74705SXin Li /// \param rAngleLoc The location of the ending '>'.
parseObjCTypeParamListOrProtocolRefs(ObjCTypeParamListScope & Scope,SourceLocation & lAngleLoc,SmallVectorImpl<IdentifierLocPair> & protocolIdents,SourceLocation & rAngleLoc,bool mayBeProtocolList)446*67e74705SXin Li ObjCTypeParamList *Parser::parseObjCTypeParamListOrProtocolRefs(
447*67e74705SXin Li ObjCTypeParamListScope &Scope, SourceLocation &lAngleLoc,
448*67e74705SXin Li SmallVectorImpl<IdentifierLocPair> &protocolIdents,
449*67e74705SXin Li SourceLocation &rAngleLoc, bool mayBeProtocolList) {
450*67e74705SXin Li assert(Tok.is(tok::less) && "Not at the beginning of a type parameter list");
451*67e74705SXin Li
452*67e74705SXin Li // Within the type parameter list, don't treat '>' as an operator.
453*67e74705SXin Li GreaterThanIsOperatorScope G(GreaterThanIsOperator, false);
454*67e74705SXin Li
455*67e74705SXin Li // Local function to "flush" the protocol identifiers, turning them into
456*67e74705SXin Li // type parameters.
457*67e74705SXin Li SmallVector<Decl *, 4> typeParams;
458*67e74705SXin Li auto makeProtocolIdentsIntoTypeParameters = [&]() {
459*67e74705SXin Li unsigned index = 0;
460*67e74705SXin Li for (const auto &pair : protocolIdents) {
461*67e74705SXin Li DeclResult typeParam = Actions.actOnObjCTypeParam(
462*67e74705SXin Li getCurScope(), ObjCTypeParamVariance::Invariant, SourceLocation(),
463*67e74705SXin Li index++, pair.first, pair.second, SourceLocation(), nullptr);
464*67e74705SXin Li if (typeParam.isUsable())
465*67e74705SXin Li typeParams.push_back(typeParam.get());
466*67e74705SXin Li }
467*67e74705SXin Li
468*67e74705SXin Li protocolIdents.clear();
469*67e74705SXin Li mayBeProtocolList = false;
470*67e74705SXin Li };
471*67e74705SXin Li
472*67e74705SXin Li bool invalid = false;
473*67e74705SXin Li lAngleLoc = ConsumeToken();
474*67e74705SXin Li
475*67e74705SXin Li do {
476*67e74705SXin Li // Parse the variance, if any.
477*67e74705SXin Li SourceLocation varianceLoc;
478*67e74705SXin Li ObjCTypeParamVariance variance = ObjCTypeParamVariance::Invariant;
479*67e74705SXin Li if (Tok.is(tok::kw___covariant) || Tok.is(tok::kw___contravariant)) {
480*67e74705SXin Li variance = Tok.is(tok::kw___covariant)
481*67e74705SXin Li ? ObjCTypeParamVariance::Covariant
482*67e74705SXin Li : ObjCTypeParamVariance::Contravariant;
483*67e74705SXin Li varianceLoc = ConsumeToken();
484*67e74705SXin Li
485*67e74705SXin Li // Once we've seen a variance specific , we know this is not a
486*67e74705SXin Li // list of protocol references.
487*67e74705SXin Li if (mayBeProtocolList) {
488*67e74705SXin Li // Up until now, we have been queuing up parameters because they
489*67e74705SXin Li // might be protocol references. Turn them into parameters now.
490*67e74705SXin Li makeProtocolIdentsIntoTypeParameters();
491*67e74705SXin Li }
492*67e74705SXin Li }
493*67e74705SXin Li
494*67e74705SXin Li // Parse the identifier.
495*67e74705SXin Li if (!Tok.is(tok::identifier)) {
496*67e74705SXin Li // Code completion.
497*67e74705SXin Li if (Tok.is(tok::code_completion)) {
498*67e74705SXin Li // FIXME: If these aren't protocol references, we'll need different
499*67e74705SXin Li // completions.
500*67e74705SXin Li Actions.CodeCompleteObjCProtocolReferences(protocolIdents);
501*67e74705SXin Li cutOffParsing();
502*67e74705SXin Li
503*67e74705SXin Li // FIXME: Better recovery here?.
504*67e74705SXin Li return nullptr;
505*67e74705SXin Li }
506*67e74705SXin Li
507*67e74705SXin Li Diag(Tok, diag::err_objc_expected_type_parameter);
508*67e74705SXin Li invalid = true;
509*67e74705SXin Li break;
510*67e74705SXin Li }
511*67e74705SXin Li
512*67e74705SXin Li IdentifierInfo *paramName = Tok.getIdentifierInfo();
513*67e74705SXin Li SourceLocation paramLoc = ConsumeToken();
514*67e74705SXin Li
515*67e74705SXin Li // If there is a bound, parse it.
516*67e74705SXin Li SourceLocation colonLoc;
517*67e74705SXin Li TypeResult boundType;
518*67e74705SXin Li if (TryConsumeToken(tok::colon, colonLoc)) {
519*67e74705SXin Li // Once we've seen a bound, we know this is not a list of protocol
520*67e74705SXin Li // references.
521*67e74705SXin Li if (mayBeProtocolList) {
522*67e74705SXin Li // Up until now, we have been queuing up parameters because they
523*67e74705SXin Li // might be protocol references. Turn them into parameters now.
524*67e74705SXin Li makeProtocolIdentsIntoTypeParameters();
525*67e74705SXin Li }
526*67e74705SXin Li
527*67e74705SXin Li // type-name
528*67e74705SXin Li boundType = ParseTypeName();
529*67e74705SXin Li if (boundType.isInvalid())
530*67e74705SXin Li invalid = true;
531*67e74705SXin Li } else if (mayBeProtocolList) {
532*67e74705SXin Li // If this could still be a protocol list, just capture the identifier.
533*67e74705SXin Li // We don't want to turn it into a parameter.
534*67e74705SXin Li protocolIdents.push_back(std::make_pair(paramName, paramLoc));
535*67e74705SXin Li continue;
536*67e74705SXin Li }
537*67e74705SXin Li
538*67e74705SXin Li // Create the type parameter.
539*67e74705SXin Li DeclResult typeParam = Actions.actOnObjCTypeParam(
540*67e74705SXin Li getCurScope(), variance, varianceLoc, typeParams.size(), paramName,
541*67e74705SXin Li paramLoc, colonLoc, boundType.isUsable() ? boundType.get() : nullptr);
542*67e74705SXin Li if (typeParam.isUsable())
543*67e74705SXin Li typeParams.push_back(typeParam.get());
544*67e74705SXin Li } while (TryConsumeToken(tok::comma));
545*67e74705SXin Li
546*67e74705SXin Li // Parse the '>'.
547*67e74705SXin Li if (invalid) {
548*67e74705SXin Li SkipUntil(tok::greater, tok::at, StopBeforeMatch);
549*67e74705SXin Li if (Tok.is(tok::greater))
550*67e74705SXin Li ConsumeToken();
551*67e74705SXin Li } else if (ParseGreaterThanInTemplateList(rAngleLoc,
552*67e74705SXin Li /*ConsumeLastToken=*/true,
553*67e74705SXin Li /*ObjCGenericList=*/true)) {
554*67e74705SXin Li Diag(lAngleLoc, diag::note_matching) << "'<'";
555*67e74705SXin Li SkipUntil({tok::greater, tok::greaterequal, tok::at, tok::minus,
556*67e74705SXin Li tok::minus, tok::plus, tok::colon, tok::l_paren, tok::l_brace,
557*67e74705SXin Li tok::comma, tok::semi },
558*67e74705SXin Li StopBeforeMatch);
559*67e74705SXin Li if (Tok.is(tok::greater))
560*67e74705SXin Li ConsumeToken();
561*67e74705SXin Li }
562*67e74705SXin Li
563*67e74705SXin Li if (mayBeProtocolList) {
564*67e74705SXin Li // A type parameter list must be followed by either a ':' (indicating the
565*67e74705SXin Li // presence of a superclass) or a '(' (indicating that this is a category
566*67e74705SXin Li // or extension). This disambiguates between an objc-type-parameter-list
567*67e74705SXin Li // and a objc-protocol-refs.
568*67e74705SXin Li if (Tok.isNot(tok::colon) && Tok.isNot(tok::l_paren)) {
569*67e74705SXin Li // Returning null indicates that we don't have a type parameter list.
570*67e74705SXin Li // The results the caller needs to handle the protocol references are
571*67e74705SXin Li // captured in the reference parameters already.
572*67e74705SXin Li return nullptr;
573*67e74705SXin Li }
574*67e74705SXin Li
575*67e74705SXin Li // We have a type parameter list that looks like a list of protocol
576*67e74705SXin Li // references. Turn that parameter list into type parameters.
577*67e74705SXin Li makeProtocolIdentsIntoTypeParameters();
578*67e74705SXin Li }
579*67e74705SXin Li
580*67e74705SXin Li // Form the type parameter list and enter its scope.
581*67e74705SXin Li ObjCTypeParamList *list = Actions.actOnObjCTypeParamList(
582*67e74705SXin Li getCurScope(),
583*67e74705SXin Li lAngleLoc,
584*67e74705SXin Li typeParams,
585*67e74705SXin Li rAngleLoc);
586*67e74705SXin Li Scope.enter(list);
587*67e74705SXin Li
588*67e74705SXin Li // Clear out the angle locations; they're used by the caller to indicate
589*67e74705SXin Li // whether there are any protocol references.
590*67e74705SXin Li lAngleLoc = SourceLocation();
591*67e74705SXin Li rAngleLoc = SourceLocation();
592*67e74705SXin Li return invalid ? nullptr : list;
593*67e74705SXin Li }
594*67e74705SXin Li
595*67e74705SXin Li /// Parse an objc-type-parameter-list.
parseObjCTypeParamList()596*67e74705SXin Li ObjCTypeParamList *Parser::parseObjCTypeParamList() {
597*67e74705SXin Li SourceLocation lAngleLoc;
598*67e74705SXin Li SmallVector<IdentifierLocPair, 1> protocolIdents;
599*67e74705SXin Li SourceLocation rAngleLoc;
600*67e74705SXin Li
601*67e74705SXin Li ObjCTypeParamListScope Scope(Actions, getCurScope());
602*67e74705SXin Li return parseObjCTypeParamListOrProtocolRefs(Scope, lAngleLoc, protocolIdents,
603*67e74705SXin Li rAngleLoc,
604*67e74705SXin Li /*mayBeProtocolList=*/false);
605*67e74705SXin Li }
606*67e74705SXin Li
607*67e74705SXin Li /// objc-interface-decl-list:
608*67e74705SXin Li /// empty
609*67e74705SXin Li /// objc-interface-decl-list objc-property-decl [OBJC2]
610*67e74705SXin Li /// objc-interface-decl-list objc-method-requirement [OBJC2]
611*67e74705SXin Li /// objc-interface-decl-list objc-method-proto ';'
612*67e74705SXin Li /// objc-interface-decl-list declaration
613*67e74705SXin Li /// objc-interface-decl-list ';'
614*67e74705SXin Li ///
615*67e74705SXin Li /// objc-method-requirement: [OBJC2]
616*67e74705SXin Li /// @required
617*67e74705SXin Li /// @optional
618*67e74705SXin Li ///
ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,Decl * CDecl)619*67e74705SXin Li void Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
620*67e74705SXin Li Decl *CDecl) {
621*67e74705SXin Li SmallVector<Decl *, 32> allMethods;
622*67e74705SXin Li SmallVector<DeclGroupPtrTy, 8> allTUVariables;
623*67e74705SXin Li tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
624*67e74705SXin Li
625*67e74705SXin Li SourceRange AtEnd;
626*67e74705SXin Li
627*67e74705SXin Li while (1) {
628*67e74705SXin Li // If this is a method prototype, parse it.
629*67e74705SXin Li if (Tok.isOneOf(tok::minus, tok::plus)) {
630*67e74705SXin Li if (Decl *methodPrototype =
631*67e74705SXin Li ParseObjCMethodPrototype(MethodImplKind, false))
632*67e74705SXin Li allMethods.push_back(methodPrototype);
633*67e74705SXin Li // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
634*67e74705SXin Li // method definitions.
635*67e74705SXin Li if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
636*67e74705SXin Li // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
637*67e74705SXin Li SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
638*67e74705SXin Li if (Tok.is(tok::semi))
639*67e74705SXin Li ConsumeToken();
640*67e74705SXin Li }
641*67e74705SXin Li continue;
642*67e74705SXin Li }
643*67e74705SXin Li if (Tok.is(tok::l_paren)) {
644*67e74705SXin Li Diag(Tok, diag::err_expected_minus_or_plus);
645*67e74705SXin Li ParseObjCMethodDecl(Tok.getLocation(),
646*67e74705SXin Li tok::minus,
647*67e74705SXin Li MethodImplKind, false);
648*67e74705SXin Li continue;
649*67e74705SXin Li }
650*67e74705SXin Li // Ignore excess semicolons.
651*67e74705SXin Li if (Tok.is(tok::semi)) {
652*67e74705SXin Li ConsumeToken();
653*67e74705SXin Li continue;
654*67e74705SXin Li }
655*67e74705SXin Li
656*67e74705SXin Li // If we got to the end of the file, exit the loop.
657*67e74705SXin Li if (isEofOrEom())
658*67e74705SXin Li break;
659*67e74705SXin Li
660*67e74705SXin Li // Code completion within an Objective-C interface.
661*67e74705SXin Li if (Tok.is(tok::code_completion)) {
662*67e74705SXin Li Actions.CodeCompleteOrdinaryName(getCurScope(),
663*67e74705SXin Li CurParsedObjCImpl? Sema::PCC_ObjCImplementation
664*67e74705SXin Li : Sema::PCC_ObjCInterface);
665*67e74705SXin Li return cutOffParsing();
666*67e74705SXin Li }
667*67e74705SXin Li
668*67e74705SXin Li // If we don't have an @ directive, parse it as a function definition.
669*67e74705SXin Li if (Tok.isNot(tok::at)) {
670*67e74705SXin Li // The code below does not consume '}'s because it is afraid of eating the
671*67e74705SXin Li // end of a namespace. Because of the way this code is structured, an
672*67e74705SXin Li // erroneous r_brace would cause an infinite loop if not handled here.
673*67e74705SXin Li if (Tok.is(tok::r_brace))
674*67e74705SXin Li break;
675*67e74705SXin Li ParsedAttributesWithRange attrs(AttrFactory);
676*67e74705SXin Li allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
677*67e74705SXin Li continue;
678*67e74705SXin Li }
679*67e74705SXin Li
680*67e74705SXin Li // Otherwise, we have an @ directive, eat the @.
681*67e74705SXin Li SourceLocation AtLoc = ConsumeToken(); // the "@"
682*67e74705SXin Li if (Tok.is(tok::code_completion)) {
683*67e74705SXin Li Actions.CodeCompleteObjCAtDirective(getCurScope());
684*67e74705SXin Li return cutOffParsing();
685*67e74705SXin Li }
686*67e74705SXin Li
687*67e74705SXin Li tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
688*67e74705SXin Li
689*67e74705SXin Li if (DirectiveKind == tok::objc_end) { // @end -> terminate list
690*67e74705SXin Li AtEnd.setBegin(AtLoc);
691*67e74705SXin Li AtEnd.setEnd(Tok.getLocation());
692*67e74705SXin Li break;
693*67e74705SXin Li } else if (DirectiveKind == tok::objc_not_keyword) {
694*67e74705SXin Li Diag(Tok, diag::err_objc_unknown_at);
695*67e74705SXin Li SkipUntil(tok::semi);
696*67e74705SXin Li continue;
697*67e74705SXin Li }
698*67e74705SXin Li
699*67e74705SXin Li // Eat the identifier.
700*67e74705SXin Li ConsumeToken();
701*67e74705SXin Li
702*67e74705SXin Li switch (DirectiveKind) {
703*67e74705SXin Li default:
704*67e74705SXin Li // FIXME: If someone forgets an @end on a protocol, this loop will
705*67e74705SXin Li // continue to eat up tons of stuff and spew lots of nonsense errors. It
706*67e74705SXin Li // would probably be better to bail out if we saw an @class or @interface
707*67e74705SXin Li // or something like that.
708*67e74705SXin Li Diag(AtLoc, diag::err_objc_illegal_interface_qual);
709*67e74705SXin Li // Skip until we see an '@' or '}' or ';'.
710*67e74705SXin Li SkipUntil(tok::r_brace, tok::at, StopAtSemi);
711*67e74705SXin Li break;
712*67e74705SXin Li
713*67e74705SXin Li case tok::objc_implementation:
714*67e74705SXin Li case tok::objc_interface:
715*67e74705SXin Li Diag(AtLoc, diag::err_objc_missing_end)
716*67e74705SXin Li << FixItHint::CreateInsertion(AtLoc, "@end\n");
717*67e74705SXin Li Diag(CDecl->getLocStart(), diag::note_objc_container_start)
718*67e74705SXin Li << (int) Actions.getObjCContainerKind();
719*67e74705SXin Li ConsumeToken();
720*67e74705SXin Li break;
721*67e74705SXin Li
722*67e74705SXin Li case tok::objc_required:
723*67e74705SXin Li case tok::objc_optional:
724*67e74705SXin Li // This is only valid on protocols.
725*67e74705SXin Li // FIXME: Should this check for ObjC2 being enabled?
726*67e74705SXin Li if (contextKey != tok::objc_protocol)
727*67e74705SXin Li Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
728*67e74705SXin Li else
729*67e74705SXin Li MethodImplKind = DirectiveKind;
730*67e74705SXin Li break;
731*67e74705SXin Li
732*67e74705SXin Li case tok::objc_property:
733*67e74705SXin Li if (!getLangOpts().ObjC2)
734*67e74705SXin Li Diag(AtLoc, diag::err_objc_properties_require_objc2);
735*67e74705SXin Li
736*67e74705SXin Li ObjCDeclSpec OCDS;
737*67e74705SXin Li SourceLocation LParenLoc;
738*67e74705SXin Li // Parse property attribute list, if any.
739*67e74705SXin Li if (Tok.is(tok::l_paren)) {
740*67e74705SXin Li LParenLoc = Tok.getLocation();
741*67e74705SXin Li ParseObjCPropertyAttribute(OCDS);
742*67e74705SXin Li }
743*67e74705SXin Li
744*67e74705SXin Li bool addedToDeclSpec = false;
745*67e74705SXin Li auto ObjCPropertyCallback = [&](ParsingFieldDeclarator &FD) {
746*67e74705SXin Li if (FD.D.getIdentifier() == nullptr) {
747*67e74705SXin Li Diag(AtLoc, diag::err_objc_property_requires_field_name)
748*67e74705SXin Li << FD.D.getSourceRange();
749*67e74705SXin Li return;
750*67e74705SXin Li }
751*67e74705SXin Li if (FD.BitfieldSize) {
752*67e74705SXin Li Diag(AtLoc, diag::err_objc_property_bitfield)
753*67e74705SXin Li << FD.D.getSourceRange();
754*67e74705SXin Li return;
755*67e74705SXin Li }
756*67e74705SXin Li
757*67e74705SXin Li // Map a nullability property attribute to a context-sensitive keyword
758*67e74705SXin Li // attribute.
759*67e74705SXin Li if (OCDS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
760*67e74705SXin Li addContextSensitiveTypeNullability(*this, FD.D, OCDS.getNullability(),
761*67e74705SXin Li OCDS.getNullabilityLoc(),
762*67e74705SXin Li addedToDeclSpec);
763*67e74705SXin Li
764*67e74705SXin Li // Install the property declarator into interfaceDecl.
765*67e74705SXin Li IdentifierInfo *SelName =
766*67e74705SXin Li OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
767*67e74705SXin Li
768*67e74705SXin Li Selector GetterSel = PP.getSelectorTable().getNullarySelector(SelName);
769*67e74705SXin Li IdentifierInfo *SetterName = OCDS.getSetterName();
770*67e74705SXin Li Selector SetterSel;
771*67e74705SXin Li if (SetterName)
772*67e74705SXin Li SetterSel = PP.getSelectorTable().getSelector(1, &SetterName);
773*67e74705SXin Li else
774*67e74705SXin Li SetterSel = SelectorTable::constructSetterSelector(
775*67e74705SXin Li PP.getIdentifierTable(), PP.getSelectorTable(),
776*67e74705SXin Li FD.D.getIdentifier());
777*67e74705SXin Li Decl *Property = Actions.ActOnProperty(
778*67e74705SXin Li getCurScope(), AtLoc, LParenLoc, FD, OCDS, GetterSel, SetterSel,
779*67e74705SXin Li MethodImplKind);
780*67e74705SXin Li
781*67e74705SXin Li FD.complete(Property);
782*67e74705SXin Li };
783*67e74705SXin Li
784*67e74705SXin Li // Parse all the comma separated declarators.
785*67e74705SXin Li ParsingDeclSpec DS(*this);
786*67e74705SXin Li ParseStructDeclaration(DS, ObjCPropertyCallback);
787*67e74705SXin Li
788*67e74705SXin Li ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
789*67e74705SXin Li break;
790*67e74705SXin Li }
791*67e74705SXin Li }
792*67e74705SXin Li
793*67e74705SXin Li // We break out of the big loop in two cases: when we see @end or when we see
794*67e74705SXin Li // EOF. In the former case, eat the @end. In the later case, emit an error.
795*67e74705SXin Li if (Tok.is(tok::code_completion)) {
796*67e74705SXin Li Actions.CodeCompleteObjCAtDirective(getCurScope());
797*67e74705SXin Li return cutOffParsing();
798*67e74705SXin Li } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
799*67e74705SXin Li ConsumeToken(); // the "end" identifier
800*67e74705SXin Li } else {
801*67e74705SXin Li Diag(Tok, diag::err_objc_missing_end)
802*67e74705SXin Li << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
803*67e74705SXin Li Diag(CDecl->getLocStart(), diag::note_objc_container_start)
804*67e74705SXin Li << (int) Actions.getObjCContainerKind();
805*67e74705SXin Li AtEnd.setBegin(Tok.getLocation());
806*67e74705SXin Li AtEnd.setEnd(Tok.getLocation());
807*67e74705SXin Li }
808*67e74705SXin Li
809*67e74705SXin Li // Insert collected methods declarations into the @interface object.
810*67e74705SXin Li // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
811*67e74705SXin Li Actions.ActOnAtEnd(getCurScope(), AtEnd, allMethods, allTUVariables);
812*67e74705SXin Li }
813*67e74705SXin Li
814*67e74705SXin Li /// Diagnose redundant or conflicting nullability information.
diagnoseRedundantPropertyNullability(Parser & P,ObjCDeclSpec & DS,NullabilityKind nullability,SourceLocation nullabilityLoc)815*67e74705SXin Li static void diagnoseRedundantPropertyNullability(Parser &P,
816*67e74705SXin Li ObjCDeclSpec &DS,
817*67e74705SXin Li NullabilityKind nullability,
818*67e74705SXin Li SourceLocation nullabilityLoc){
819*67e74705SXin Li if (DS.getNullability() == nullability) {
820*67e74705SXin Li P.Diag(nullabilityLoc, diag::warn_nullability_duplicate)
821*67e74705SXin Li << DiagNullabilityKind(nullability, true)
822*67e74705SXin Li << SourceRange(DS.getNullabilityLoc());
823*67e74705SXin Li return;
824*67e74705SXin Li }
825*67e74705SXin Li
826*67e74705SXin Li P.Diag(nullabilityLoc, diag::err_nullability_conflicting)
827*67e74705SXin Li << DiagNullabilityKind(nullability, true)
828*67e74705SXin Li << DiagNullabilityKind(DS.getNullability(), true)
829*67e74705SXin Li << SourceRange(DS.getNullabilityLoc());
830*67e74705SXin Li }
831*67e74705SXin Li
832*67e74705SXin Li /// Parse property attribute declarations.
833*67e74705SXin Li ///
834*67e74705SXin Li /// property-attr-decl: '(' property-attrlist ')'
835*67e74705SXin Li /// property-attrlist:
836*67e74705SXin Li /// property-attribute
837*67e74705SXin Li /// property-attrlist ',' property-attribute
838*67e74705SXin Li /// property-attribute:
839*67e74705SXin Li /// getter '=' identifier
840*67e74705SXin Li /// setter '=' identifier ':'
841*67e74705SXin Li /// readonly
842*67e74705SXin Li /// readwrite
843*67e74705SXin Li /// assign
844*67e74705SXin Li /// retain
845*67e74705SXin Li /// copy
846*67e74705SXin Li /// nonatomic
847*67e74705SXin Li /// atomic
848*67e74705SXin Li /// strong
849*67e74705SXin Li /// weak
850*67e74705SXin Li /// unsafe_unretained
851*67e74705SXin Li /// nonnull
852*67e74705SXin Li /// nullable
853*67e74705SXin Li /// null_unspecified
854*67e74705SXin Li /// null_resettable
855*67e74705SXin Li /// class
856*67e74705SXin Li ///
ParseObjCPropertyAttribute(ObjCDeclSpec & DS)857*67e74705SXin Li void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
858*67e74705SXin Li assert(Tok.getKind() == tok::l_paren);
859*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
860*67e74705SXin Li T.consumeOpen();
861*67e74705SXin Li
862*67e74705SXin Li while (1) {
863*67e74705SXin Li if (Tok.is(tok::code_completion)) {
864*67e74705SXin Li Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
865*67e74705SXin Li return cutOffParsing();
866*67e74705SXin Li }
867*67e74705SXin Li const IdentifierInfo *II = Tok.getIdentifierInfo();
868*67e74705SXin Li
869*67e74705SXin Li // If this is not an identifier at all, bail out early.
870*67e74705SXin Li if (!II) {
871*67e74705SXin Li T.consumeClose();
872*67e74705SXin Li return;
873*67e74705SXin Li }
874*67e74705SXin Li
875*67e74705SXin Li SourceLocation AttrName = ConsumeToken(); // consume last attribute name
876*67e74705SXin Li
877*67e74705SXin Li if (II->isStr("readonly"))
878*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
879*67e74705SXin Li else if (II->isStr("assign"))
880*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
881*67e74705SXin Li else if (II->isStr("unsafe_unretained"))
882*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
883*67e74705SXin Li else if (II->isStr("readwrite"))
884*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
885*67e74705SXin Li else if (II->isStr("retain"))
886*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
887*67e74705SXin Li else if (II->isStr("strong"))
888*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
889*67e74705SXin Li else if (II->isStr("copy"))
890*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
891*67e74705SXin Li else if (II->isStr("nonatomic"))
892*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
893*67e74705SXin Li else if (II->isStr("atomic"))
894*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
895*67e74705SXin Li else if (II->isStr("weak"))
896*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
897*67e74705SXin Li else if (II->isStr("getter") || II->isStr("setter")) {
898*67e74705SXin Li bool IsSetter = II->getNameStart()[0] == 's';
899*67e74705SXin Li
900*67e74705SXin Li // getter/setter require extra treatment.
901*67e74705SXin Li unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
902*67e74705SXin Li diag::err_objc_expected_equal_for_getter;
903*67e74705SXin Li
904*67e74705SXin Li if (ExpectAndConsume(tok::equal, DiagID)) {
905*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
906*67e74705SXin Li return;
907*67e74705SXin Li }
908*67e74705SXin Li
909*67e74705SXin Li if (Tok.is(tok::code_completion)) {
910*67e74705SXin Li if (IsSetter)
911*67e74705SXin Li Actions.CodeCompleteObjCPropertySetter(getCurScope());
912*67e74705SXin Li else
913*67e74705SXin Li Actions.CodeCompleteObjCPropertyGetter(getCurScope());
914*67e74705SXin Li return cutOffParsing();
915*67e74705SXin Li }
916*67e74705SXin Li
917*67e74705SXin Li SourceLocation SelLoc;
918*67e74705SXin Li IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
919*67e74705SXin Li
920*67e74705SXin Li if (!SelIdent) {
921*67e74705SXin Li Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
922*67e74705SXin Li << IsSetter;
923*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
924*67e74705SXin Li return;
925*67e74705SXin Li }
926*67e74705SXin Li
927*67e74705SXin Li if (IsSetter) {
928*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
929*67e74705SXin Li DS.setSetterName(SelIdent);
930*67e74705SXin Li
931*67e74705SXin Li if (ExpectAndConsume(tok::colon,
932*67e74705SXin Li diag::err_expected_colon_after_setter_name)) {
933*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
934*67e74705SXin Li return;
935*67e74705SXin Li }
936*67e74705SXin Li } else {
937*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
938*67e74705SXin Li DS.setGetterName(SelIdent);
939*67e74705SXin Li }
940*67e74705SXin Li } else if (II->isStr("nonnull")) {
941*67e74705SXin Li if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
942*67e74705SXin Li diagnoseRedundantPropertyNullability(*this, DS,
943*67e74705SXin Li NullabilityKind::NonNull,
944*67e74705SXin Li Tok.getLocation());
945*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
946*67e74705SXin Li DS.setNullability(Tok.getLocation(), NullabilityKind::NonNull);
947*67e74705SXin Li } else if (II->isStr("nullable")) {
948*67e74705SXin Li if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
949*67e74705SXin Li diagnoseRedundantPropertyNullability(*this, DS,
950*67e74705SXin Li NullabilityKind::Nullable,
951*67e74705SXin Li Tok.getLocation());
952*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
953*67e74705SXin Li DS.setNullability(Tok.getLocation(), NullabilityKind::Nullable);
954*67e74705SXin Li } else if (II->isStr("null_unspecified")) {
955*67e74705SXin Li if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
956*67e74705SXin Li diagnoseRedundantPropertyNullability(*this, DS,
957*67e74705SXin Li NullabilityKind::Unspecified,
958*67e74705SXin Li Tok.getLocation());
959*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
960*67e74705SXin Li DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
961*67e74705SXin Li } else if (II->isStr("null_resettable")) {
962*67e74705SXin Li if (DS.getPropertyAttributes() & ObjCDeclSpec::DQ_PR_nullability)
963*67e74705SXin Li diagnoseRedundantPropertyNullability(*this, DS,
964*67e74705SXin Li NullabilityKind::Unspecified,
965*67e74705SXin Li Tok.getLocation());
966*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nullability);
967*67e74705SXin Li DS.setNullability(Tok.getLocation(), NullabilityKind::Unspecified);
968*67e74705SXin Li
969*67e74705SXin Li // Also set the null_resettable bit.
970*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_null_resettable);
971*67e74705SXin Li } else if (II->isStr("class")) {
972*67e74705SXin Li DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_class);
973*67e74705SXin Li } else {
974*67e74705SXin Li Diag(AttrName, diag::err_objc_expected_property_attr) << II;
975*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
976*67e74705SXin Li return;
977*67e74705SXin Li }
978*67e74705SXin Li
979*67e74705SXin Li if (Tok.isNot(tok::comma))
980*67e74705SXin Li break;
981*67e74705SXin Li
982*67e74705SXin Li ConsumeToken();
983*67e74705SXin Li }
984*67e74705SXin Li
985*67e74705SXin Li T.consumeClose();
986*67e74705SXin Li }
987*67e74705SXin Li
988*67e74705SXin Li /// objc-method-proto:
989*67e74705SXin Li /// objc-instance-method objc-method-decl objc-method-attributes[opt]
990*67e74705SXin Li /// objc-class-method objc-method-decl objc-method-attributes[opt]
991*67e74705SXin Li ///
992*67e74705SXin Li /// objc-instance-method: '-'
993*67e74705SXin Li /// objc-class-method: '+'
994*67e74705SXin Li ///
995*67e74705SXin Li /// objc-method-attributes: [OBJC2]
996*67e74705SXin Li /// __attribute__((deprecated))
997*67e74705SXin Li ///
ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,bool MethodDefinition)998*67e74705SXin Li Decl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
999*67e74705SXin Li bool MethodDefinition) {
1000*67e74705SXin Li assert(Tok.isOneOf(tok::minus, tok::plus) && "expected +/-");
1001*67e74705SXin Li
1002*67e74705SXin Li tok::TokenKind methodType = Tok.getKind();
1003*67e74705SXin Li SourceLocation mLoc = ConsumeToken();
1004*67e74705SXin Li Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
1005*67e74705SXin Li MethodDefinition);
1006*67e74705SXin Li // Since this rule is used for both method declarations and definitions,
1007*67e74705SXin Li // the caller is (optionally) responsible for consuming the ';'.
1008*67e74705SXin Li return MDecl;
1009*67e74705SXin Li }
1010*67e74705SXin Li
1011*67e74705SXin Li /// objc-selector:
1012*67e74705SXin Li /// identifier
1013*67e74705SXin Li /// one of
1014*67e74705SXin Li /// enum struct union if else while do for switch case default
1015*67e74705SXin Li /// break continue return goto asm sizeof typeof __alignof
1016*67e74705SXin Li /// unsigned long const short volatile signed restrict _Complex
1017*67e74705SXin Li /// in out inout bycopy byref oneway int char float double void _Bool
1018*67e74705SXin Li ///
ParseObjCSelectorPiece(SourceLocation & SelectorLoc)1019*67e74705SXin Li IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
1020*67e74705SXin Li
1021*67e74705SXin Li switch (Tok.getKind()) {
1022*67e74705SXin Li default:
1023*67e74705SXin Li return nullptr;
1024*67e74705SXin Li case tok::ampamp:
1025*67e74705SXin Li case tok::ampequal:
1026*67e74705SXin Li case tok::amp:
1027*67e74705SXin Li case tok::pipe:
1028*67e74705SXin Li case tok::tilde:
1029*67e74705SXin Li case tok::exclaim:
1030*67e74705SXin Li case tok::exclaimequal:
1031*67e74705SXin Li case tok::pipepipe:
1032*67e74705SXin Li case tok::pipeequal:
1033*67e74705SXin Li case tok::caret:
1034*67e74705SXin Li case tok::caretequal: {
1035*67e74705SXin Li std::string ThisTok(PP.getSpelling(Tok));
1036*67e74705SXin Li if (isLetter(ThisTok[0])) {
1037*67e74705SXin Li IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
1038*67e74705SXin Li Tok.setKind(tok::identifier);
1039*67e74705SXin Li SelectorLoc = ConsumeToken();
1040*67e74705SXin Li return II;
1041*67e74705SXin Li }
1042*67e74705SXin Li return nullptr;
1043*67e74705SXin Li }
1044*67e74705SXin Li
1045*67e74705SXin Li case tok::identifier:
1046*67e74705SXin Li case tok::kw_asm:
1047*67e74705SXin Li case tok::kw_auto:
1048*67e74705SXin Li case tok::kw_bool:
1049*67e74705SXin Li case tok::kw_break:
1050*67e74705SXin Li case tok::kw_case:
1051*67e74705SXin Li case tok::kw_catch:
1052*67e74705SXin Li case tok::kw_char:
1053*67e74705SXin Li case tok::kw_class:
1054*67e74705SXin Li case tok::kw_const:
1055*67e74705SXin Li case tok::kw_const_cast:
1056*67e74705SXin Li case tok::kw_continue:
1057*67e74705SXin Li case tok::kw_default:
1058*67e74705SXin Li case tok::kw_delete:
1059*67e74705SXin Li case tok::kw_do:
1060*67e74705SXin Li case tok::kw_double:
1061*67e74705SXin Li case tok::kw_dynamic_cast:
1062*67e74705SXin Li case tok::kw_else:
1063*67e74705SXin Li case tok::kw_enum:
1064*67e74705SXin Li case tok::kw_explicit:
1065*67e74705SXin Li case tok::kw_export:
1066*67e74705SXin Li case tok::kw_extern:
1067*67e74705SXin Li case tok::kw_false:
1068*67e74705SXin Li case tok::kw_float:
1069*67e74705SXin Li case tok::kw_for:
1070*67e74705SXin Li case tok::kw_friend:
1071*67e74705SXin Li case tok::kw_goto:
1072*67e74705SXin Li case tok::kw_if:
1073*67e74705SXin Li case tok::kw_inline:
1074*67e74705SXin Li case tok::kw_int:
1075*67e74705SXin Li case tok::kw_long:
1076*67e74705SXin Li case tok::kw_mutable:
1077*67e74705SXin Li case tok::kw_namespace:
1078*67e74705SXin Li case tok::kw_new:
1079*67e74705SXin Li case tok::kw_operator:
1080*67e74705SXin Li case tok::kw_private:
1081*67e74705SXin Li case tok::kw_protected:
1082*67e74705SXin Li case tok::kw_public:
1083*67e74705SXin Li case tok::kw_register:
1084*67e74705SXin Li case tok::kw_reinterpret_cast:
1085*67e74705SXin Li case tok::kw_restrict:
1086*67e74705SXin Li case tok::kw_return:
1087*67e74705SXin Li case tok::kw_short:
1088*67e74705SXin Li case tok::kw_signed:
1089*67e74705SXin Li case tok::kw_sizeof:
1090*67e74705SXin Li case tok::kw_static:
1091*67e74705SXin Li case tok::kw_static_cast:
1092*67e74705SXin Li case tok::kw_struct:
1093*67e74705SXin Li case tok::kw_switch:
1094*67e74705SXin Li case tok::kw_template:
1095*67e74705SXin Li case tok::kw_this:
1096*67e74705SXin Li case tok::kw_throw:
1097*67e74705SXin Li case tok::kw_true:
1098*67e74705SXin Li case tok::kw_try:
1099*67e74705SXin Li case tok::kw_typedef:
1100*67e74705SXin Li case tok::kw_typeid:
1101*67e74705SXin Li case tok::kw_typename:
1102*67e74705SXin Li case tok::kw_typeof:
1103*67e74705SXin Li case tok::kw_union:
1104*67e74705SXin Li case tok::kw_unsigned:
1105*67e74705SXin Li case tok::kw_using:
1106*67e74705SXin Li case tok::kw_virtual:
1107*67e74705SXin Li case tok::kw_void:
1108*67e74705SXin Li case tok::kw_volatile:
1109*67e74705SXin Li case tok::kw_wchar_t:
1110*67e74705SXin Li case tok::kw_while:
1111*67e74705SXin Li case tok::kw__Bool:
1112*67e74705SXin Li case tok::kw__Complex:
1113*67e74705SXin Li case tok::kw___alignof:
1114*67e74705SXin Li case tok::kw___auto_type:
1115*67e74705SXin Li IdentifierInfo *II = Tok.getIdentifierInfo();
1116*67e74705SXin Li SelectorLoc = ConsumeToken();
1117*67e74705SXin Li return II;
1118*67e74705SXin Li }
1119*67e74705SXin Li }
1120*67e74705SXin Li
1121*67e74705SXin Li /// objc-for-collection-in: 'in'
1122*67e74705SXin Li ///
isTokIdentifier_in() const1123*67e74705SXin Li bool Parser::isTokIdentifier_in() const {
1124*67e74705SXin Li // FIXME: May have to do additional look-ahead to only allow for
1125*67e74705SXin Li // valid tokens following an 'in'; such as an identifier, unary operators,
1126*67e74705SXin Li // '[' etc.
1127*67e74705SXin Li return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
1128*67e74705SXin Li Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
1129*67e74705SXin Li }
1130*67e74705SXin Li
1131*67e74705SXin Li /// ParseObjCTypeQualifierList - This routine parses the objective-c's type
1132*67e74705SXin Li /// qualifier list and builds their bitmask representation in the input
1133*67e74705SXin Li /// argument.
1134*67e74705SXin Li ///
1135*67e74705SXin Li /// objc-type-qualifiers:
1136*67e74705SXin Li /// objc-type-qualifier
1137*67e74705SXin Li /// objc-type-qualifiers objc-type-qualifier
1138*67e74705SXin Li ///
1139*67e74705SXin Li /// objc-type-qualifier:
1140*67e74705SXin Li /// 'in'
1141*67e74705SXin Li /// 'out'
1142*67e74705SXin Li /// 'inout'
1143*67e74705SXin Li /// 'oneway'
1144*67e74705SXin Li /// 'bycopy'
1145*67e74705SXin Li /// 'byref'
1146*67e74705SXin Li /// 'nonnull'
1147*67e74705SXin Li /// 'nullable'
1148*67e74705SXin Li /// 'null_unspecified'
1149*67e74705SXin Li ///
ParseObjCTypeQualifierList(ObjCDeclSpec & DS,Declarator::TheContext Context)1150*67e74705SXin Li void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
1151*67e74705SXin Li Declarator::TheContext Context) {
1152*67e74705SXin Li assert(Context == Declarator::ObjCParameterContext ||
1153*67e74705SXin Li Context == Declarator::ObjCResultContext);
1154*67e74705SXin Li
1155*67e74705SXin Li while (1) {
1156*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1157*67e74705SXin Li Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
1158*67e74705SXin Li Context == Declarator::ObjCParameterContext);
1159*67e74705SXin Li return cutOffParsing();
1160*67e74705SXin Li }
1161*67e74705SXin Li
1162*67e74705SXin Li if (Tok.isNot(tok::identifier))
1163*67e74705SXin Li return;
1164*67e74705SXin Li
1165*67e74705SXin Li const IdentifierInfo *II = Tok.getIdentifierInfo();
1166*67e74705SXin Li for (unsigned i = 0; i != objc_NumQuals; ++i) {
1167*67e74705SXin Li if (II != ObjCTypeQuals[i] ||
1168*67e74705SXin Li NextToken().is(tok::less) ||
1169*67e74705SXin Li NextToken().is(tok::coloncolon))
1170*67e74705SXin Li continue;
1171*67e74705SXin Li
1172*67e74705SXin Li ObjCDeclSpec::ObjCDeclQualifier Qual;
1173*67e74705SXin Li NullabilityKind Nullability;
1174*67e74705SXin Li switch (i) {
1175*67e74705SXin Li default: llvm_unreachable("Unknown decl qualifier");
1176*67e74705SXin Li case objc_in: Qual = ObjCDeclSpec::DQ_In; break;
1177*67e74705SXin Li case objc_out: Qual = ObjCDeclSpec::DQ_Out; break;
1178*67e74705SXin Li case objc_inout: Qual = ObjCDeclSpec::DQ_Inout; break;
1179*67e74705SXin Li case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
1180*67e74705SXin Li case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
1181*67e74705SXin Li case objc_byref: Qual = ObjCDeclSpec::DQ_Byref; break;
1182*67e74705SXin Li
1183*67e74705SXin Li case objc_nonnull:
1184*67e74705SXin Li Qual = ObjCDeclSpec::DQ_CSNullability;
1185*67e74705SXin Li Nullability = NullabilityKind::NonNull;
1186*67e74705SXin Li break;
1187*67e74705SXin Li
1188*67e74705SXin Li case objc_nullable:
1189*67e74705SXin Li Qual = ObjCDeclSpec::DQ_CSNullability;
1190*67e74705SXin Li Nullability = NullabilityKind::Nullable;
1191*67e74705SXin Li break;
1192*67e74705SXin Li
1193*67e74705SXin Li case objc_null_unspecified:
1194*67e74705SXin Li Qual = ObjCDeclSpec::DQ_CSNullability;
1195*67e74705SXin Li Nullability = NullabilityKind::Unspecified;
1196*67e74705SXin Li break;
1197*67e74705SXin Li }
1198*67e74705SXin Li
1199*67e74705SXin Li // FIXME: Diagnose redundant specifiers.
1200*67e74705SXin Li DS.setObjCDeclQualifier(Qual);
1201*67e74705SXin Li if (Qual == ObjCDeclSpec::DQ_CSNullability)
1202*67e74705SXin Li DS.setNullability(Tok.getLocation(), Nullability);
1203*67e74705SXin Li
1204*67e74705SXin Li ConsumeToken();
1205*67e74705SXin Li II = nullptr;
1206*67e74705SXin Li break;
1207*67e74705SXin Li }
1208*67e74705SXin Li
1209*67e74705SXin Li // If this wasn't a recognized qualifier, bail out.
1210*67e74705SXin Li if (II) return;
1211*67e74705SXin Li }
1212*67e74705SXin Li }
1213*67e74705SXin Li
1214*67e74705SXin Li /// Take all the decl attributes out of the given list and add
1215*67e74705SXin Li /// them to the given attribute set.
takeDeclAttributes(ParsedAttributes & attrs,AttributeList * list)1216*67e74705SXin Li static void takeDeclAttributes(ParsedAttributes &attrs,
1217*67e74705SXin Li AttributeList *list) {
1218*67e74705SXin Li while (list) {
1219*67e74705SXin Li AttributeList *cur = list;
1220*67e74705SXin Li list = cur->getNext();
1221*67e74705SXin Li
1222*67e74705SXin Li if (!cur->isUsedAsTypeAttr()) {
1223*67e74705SXin Li // Clear out the next pointer. We're really completely
1224*67e74705SXin Li // destroying the internal invariants of the declarator here,
1225*67e74705SXin Li // but it doesn't matter because we're done with it.
1226*67e74705SXin Li cur->setNext(nullptr);
1227*67e74705SXin Li attrs.add(cur);
1228*67e74705SXin Li }
1229*67e74705SXin Li }
1230*67e74705SXin Li }
1231*67e74705SXin Li
1232*67e74705SXin Li /// takeDeclAttributes - Take all the decl attributes from the given
1233*67e74705SXin Li /// declarator and add them to the given list.
takeDeclAttributes(ParsedAttributes & attrs,Declarator & D)1234*67e74705SXin Li static void takeDeclAttributes(ParsedAttributes &attrs,
1235*67e74705SXin Li Declarator &D) {
1236*67e74705SXin Li // First, take ownership of all attributes.
1237*67e74705SXin Li attrs.getPool().takeAllFrom(D.getAttributePool());
1238*67e74705SXin Li attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
1239*67e74705SXin Li
1240*67e74705SXin Li // Now actually move the attributes over.
1241*67e74705SXin Li takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
1242*67e74705SXin Li takeDeclAttributes(attrs, D.getAttributes());
1243*67e74705SXin Li for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
1244*67e74705SXin Li takeDeclAttributes(attrs,
1245*67e74705SXin Li const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
1246*67e74705SXin Li }
1247*67e74705SXin Li
1248*67e74705SXin Li /// objc-type-name:
1249*67e74705SXin Li /// '(' objc-type-qualifiers[opt] type-name ')'
1250*67e74705SXin Li /// '(' objc-type-qualifiers[opt] ')'
1251*67e74705SXin Li ///
ParseObjCTypeName(ObjCDeclSpec & DS,Declarator::TheContext context,ParsedAttributes * paramAttrs)1252*67e74705SXin Li ParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
1253*67e74705SXin Li Declarator::TheContext context,
1254*67e74705SXin Li ParsedAttributes *paramAttrs) {
1255*67e74705SXin Li assert(context == Declarator::ObjCParameterContext ||
1256*67e74705SXin Li context == Declarator::ObjCResultContext);
1257*67e74705SXin Li assert((paramAttrs != nullptr) ==
1258*67e74705SXin Li (context == Declarator::ObjCParameterContext));
1259*67e74705SXin Li
1260*67e74705SXin Li assert(Tok.is(tok::l_paren) && "expected (");
1261*67e74705SXin Li
1262*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
1263*67e74705SXin Li T.consumeOpen();
1264*67e74705SXin Li
1265*67e74705SXin Li SourceLocation TypeStartLoc = Tok.getLocation();
1266*67e74705SXin Li ObjCDeclContextSwitch ObjCDC(*this);
1267*67e74705SXin Li
1268*67e74705SXin Li // Parse type qualifiers, in, inout, etc.
1269*67e74705SXin Li ParseObjCTypeQualifierList(DS, context);
1270*67e74705SXin Li
1271*67e74705SXin Li ParsedType Ty;
1272*67e74705SXin Li if (isTypeSpecifierQualifier() || isObjCInstancetype()) {
1273*67e74705SXin Li // Parse an abstract declarator.
1274*67e74705SXin Li DeclSpec declSpec(AttrFactory);
1275*67e74705SXin Li declSpec.setObjCQualifiers(&DS);
1276*67e74705SXin Li DeclSpecContext dsContext = DSC_normal;
1277*67e74705SXin Li if (context == Declarator::ObjCResultContext)
1278*67e74705SXin Li dsContext = DSC_objc_method_result;
1279*67e74705SXin Li ParseSpecifierQualifierList(declSpec, AS_none, dsContext);
1280*67e74705SXin Li Declarator declarator(declSpec, context);
1281*67e74705SXin Li ParseDeclarator(declarator);
1282*67e74705SXin Li
1283*67e74705SXin Li // If that's not invalid, extract a type.
1284*67e74705SXin Li if (!declarator.isInvalidType()) {
1285*67e74705SXin Li // Map a nullability specifier to a context-sensitive keyword attribute.
1286*67e74705SXin Li bool addedToDeclSpec = false;
1287*67e74705SXin Li if (DS.getObjCDeclQualifier() & ObjCDeclSpec::DQ_CSNullability)
1288*67e74705SXin Li addContextSensitiveTypeNullability(*this, declarator,
1289*67e74705SXin Li DS.getNullability(),
1290*67e74705SXin Li DS.getNullabilityLoc(),
1291*67e74705SXin Li addedToDeclSpec);
1292*67e74705SXin Li
1293*67e74705SXin Li TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
1294*67e74705SXin Li if (!type.isInvalid())
1295*67e74705SXin Li Ty = type.get();
1296*67e74705SXin Li
1297*67e74705SXin Li // If we're parsing a parameter, steal all the decl attributes
1298*67e74705SXin Li // and add them to the decl spec.
1299*67e74705SXin Li if (context == Declarator::ObjCParameterContext)
1300*67e74705SXin Li takeDeclAttributes(*paramAttrs, declarator);
1301*67e74705SXin Li }
1302*67e74705SXin Li }
1303*67e74705SXin Li
1304*67e74705SXin Li if (Tok.is(tok::r_paren))
1305*67e74705SXin Li T.consumeClose();
1306*67e74705SXin Li else if (Tok.getLocation() == TypeStartLoc) {
1307*67e74705SXin Li // If we didn't eat any tokens, then this isn't a type.
1308*67e74705SXin Li Diag(Tok, diag::err_expected_type);
1309*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
1310*67e74705SXin Li } else {
1311*67e74705SXin Li // Otherwise, we found *something*, but didn't get a ')' in the right
1312*67e74705SXin Li // place. Emit an error then return what we have as the type.
1313*67e74705SXin Li T.consumeClose();
1314*67e74705SXin Li }
1315*67e74705SXin Li return Ty;
1316*67e74705SXin Li }
1317*67e74705SXin Li
1318*67e74705SXin Li /// objc-method-decl:
1319*67e74705SXin Li /// objc-selector
1320*67e74705SXin Li /// objc-keyword-selector objc-parmlist[opt]
1321*67e74705SXin Li /// objc-type-name objc-selector
1322*67e74705SXin Li /// objc-type-name objc-keyword-selector objc-parmlist[opt]
1323*67e74705SXin Li ///
1324*67e74705SXin Li /// objc-keyword-selector:
1325*67e74705SXin Li /// objc-keyword-decl
1326*67e74705SXin Li /// objc-keyword-selector objc-keyword-decl
1327*67e74705SXin Li ///
1328*67e74705SXin Li /// objc-keyword-decl:
1329*67e74705SXin Li /// objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
1330*67e74705SXin Li /// objc-selector ':' objc-keyword-attributes[opt] identifier
1331*67e74705SXin Li /// ':' objc-type-name objc-keyword-attributes[opt] identifier
1332*67e74705SXin Li /// ':' objc-keyword-attributes[opt] identifier
1333*67e74705SXin Li ///
1334*67e74705SXin Li /// objc-parmlist:
1335*67e74705SXin Li /// objc-parms objc-ellipsis[opt]
1336*67e74705SXin Li ///
1337*67e74705SXin Li /// objc-parms:
1338*67e74705SXin Li /// objc-parms , parameter-declaration
1339*67e74705SXin Li ///
1340*67e74705SXin Li /// objc-ellipsis:
1341*67e74705SXin Li /// , ...
1342*67e74705SXin Li ///
1343*67e74705SXin Li /// objc-keyword-attributes: [OBJC2]
1344*67e74705SXin Li /// __attribute__((unused))
1345*67e74705SXin Li ///
ParseObjCMethodDecl(SourceLocation mLoc,tok::TokenKind mType,tok::ObjCKeywordKind MethodImplKind,bool MethodDefinition)1346*67e74705SXin Li Decl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
1347*67e74705SXin Li tok::TokenKind mType,
1348*67e74705SXin Li tok::ObjCKeywordKind MethodImplKind,
1349*67e74705SXin Li bool MethodDefinition) {
1350*67e74705SXin Li ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
1351*67e74705SXin Li
1352*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1353*67e74705SXin Li Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1354*67e74705SXin Li /*ReturnType=*/nullptr);
1355*67e74705SXin Li cutOffParsing();
1356*67e74705SXin Li return nullptr;
1357*67e74705SXin Li }
1358*67e74705SXin Li
1359*67e74705SXin Li // Parse the return type if present.
1360*67e74705SXin Li ParsedType ReturnType;
1361*67e74705SXin Li ObjCDeclSpec DSRet;
1362*67e74705SXin Li if (Tok.is(tok::l_paren))
1363*67e74705SXin Li ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext,
1364*67e74705SXin Li nullptr);
1365*67e74705SXin Li
1366*67e74705SXin Li // If attributes exist before the method, parse them.
1367*67e74705SXin Li ParsedAttributes methodAttrs(AttrFactory);
1368*67e74705SXin Li if (getLangOpts().ObjC2)
1369*67e74705SXin Li MaybeParseGNUAttributes(methodAttrs);
1370*67e74705SXin Li
1371*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1372*67e74705SXin Li Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1373*67e74705SXin Li ReturnType);
1374*67e74705SXin Li cutOffParsing();
1375*67e74705SXin Li return nullptr;
1376*67e74705SXin Li }
1377*67e74705SXin Li
1378*67e74705SXin Li // Now parse the selector.
1379*67e74705SXin Li SourceLocation selLoc;
1380*67e74705SXin Li IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
1381*67e74705SXin Li
1382*67e74705SXin Li // An unnamed colon is valid.
1383*67e74705SXin Li if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
1384*67e74705SXin Li Diag(Tok, diag::err_expected_selector_for_method)
1385*67e74705SXin Li << SourceRange(mLoc, Tok.getLocation());
1386*67e74705SXin Li // Skip until we get a ; or @.
1387*67e74705SXin Li SkipUntil(tok::at, StopAtSemi | StopBeforeMatch);
1388*67e74705SXin Li return nullptr;
1389*67e74705SXin Li }
1390*67e74705SXin Li
1391*67e74705SXin Li SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
1392*67e74705SXin Li if (Tok.isNot(tok::colon)) {
1393*67e74705SXin Li // If attributes exist after the method, parse them.
1394*67e74705SXin Li if (getLangOpts().ObjC2)
1395*67e74705SXin Li MaybeParseGNUAttributes(methodAttrs);
1396*67e74705SXin Li
1397*67e74705SXin Li Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
1398*67e74705SXin Li Decl *Result
1399*67e74705SXin Li = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1400*67e74705SXin Li mType, DSRet, ReturnType,
1401*67e74705SXin Li selLoc, Sel, nullptr,
1402*67e74705SXin Li CParamInfo.data(), CParamInfo.size(),
1403*67e74705SXin Li methodAttrs.getList(), MethodImplKind,
1404*67e74705SXin Li false, MethodDefinition);
1405*67e74705SXin Li PD.complete(Result);
1406*67e74705SXin Li return Result;
1407*67e74705SXin Li }
1408*67e74705SXin Li
1409*67e74705SXin Li SmallVector<IdentifierInfo *, 12> KeyIdents;
1410*67e74705SXin Li SmallVector<SourceLocation, 12> KeyLocs;
1411*67e74705SXin Li SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
1412*67e74705SXin Li ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
1413*67e74705SXin Li Scope::FunctionDeclarationScope | Scope::DeclScope);
1414*67e74705SXin Li
1415*67e74705SXin Li AttributePool allParamAttrs(AttrFactory);
1416*67e74705SXin Li while (1) {
1417*67e74705SXin Li ParsedAttributes paramAttrs(AttrFactory);
1418*67e74705SXin Li Sema::ObjCArgInfo ArgInfo;
1419*67e74705SXin Li
1420*67e74705SXin Li // Each iteration parses a single keyword argument.
1421*67e74705SXin Li if (ExpectAndConsume(tok::colon))
1422*67e74705SXin Li break;
1423*67e74705SXin Li
1424*67e74705SXin Li ArgInfo.Type = nullptr;
1425*67e74705SXin Li if (Tok.is(tok::l_paren)) // Parse the argument type if present.
1426*67e74705SXin Li ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1427*67e74705SXin Li Declarator::ObjCParameterContext,
1428*67e74705SXin Li ¶mAttrs);
1429*67e74705SXin Li
1430*67e74705SXin Li // If attributes exist before the argument name, parse them.
1431*67e74705SXin Li // Regardless, collect all the attributes we've parsed so far.
1432*67e74705SXin Li ArgInfo.ArgAttrs = nullptr;
1433*67e74705SXin Li if (getLangOpts().ObjC2) {
1434*67e74705SXin Li MaybeParseGNUAttributes(paramAttrs);
1435*67e74705SXin Li ArgInfo.ArgAttrs = paramAttrs.getList();
1436*67e74705SXin Li }
1437*67e74705SXin Li
1438*67e74705SXin Li // Code completion for the next piece of the selector.
1439*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1440*67e74705SXin Li KeyIdents.push_back(SelIdent);
1441*67e74705SXin Li Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1442*67e74705SXin Li mType == tok::minus,
1443*67e74705SXin Li /*AtParameterName=*/true,
1444*67e74705SXin Li ReturnType, KeyIdents);
1445*67e74705SXin Li cutOffParsing();
1446*67e74705SXin Li return nullptr;
1447*67e74705SXin Li }
1448*67e74705SXin Li
1449*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
1450*67e74705SXin Li Diag(Tok, diag::err_expected)
1451*67e74705SXin Li << tok::identifier; // missing argument name.
1452*67e74705SXin Li break;
1453*67e74705SXin Li }
1454*67e74705SXin Li
1455*67e74705SXin Li ArgInfo.Name = Tok.getIdentifierInfo();
1456*67e74705SXin Li ArgInfo.NameLoc = Tok.getLocation();
1457*67e74705SXin Li ConsumeToken(); // Eat the identifier.
1458*67e74705SXin Li
1459*67e74705SXin Li ArgInfos.push_back(ArgInfo);
1460*67e74705SXin Li KeyIdents.push_back(SelIdent);
1461*67e74705SXin Li KeyLocs.push_back(selLoc);
1462*67e74705SXin Li
1463*67e74705SXin Li // Make sure the attributes persist.
1464*67e74705SXin Li allParamAttrs.takeAllFrom(paramAttrs.getPool());
1465*67e74705SXin Li
1466*67e74705SXin Li // Code completion for the next piece of the selector.
1467*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1468*67e74705SXin Li Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
1469*67e74705SXin Li mType == tok::minus,
1470*67e74705SXin Li /*AtParameterName=*/false,
1471*67e74705SXin Li ReturnType, KeyIdents);
1472*67e74705SXin Li cutOffParsing();
1473*67e74705SXin Li return nullptr;
1474*67e74705SXin Li }
1475*67e74705SXin Li
1476*67e74705SXin Li // Check for another keyword selector.
1477*67e74705SXin Li SelIdent = ParseObjCSelectorPiece(selLoc);
1478*67e74705SXin Li if (!SelIdent && Tok.isNot(tok::colon))
1479*67e74705SXin Li break;
1480*67e74705SXin Li if (!SelIdent) {
1481*67e74705SXin Li SourceLocation ColonLoc = Tok.getLocation();
1482*67e74705SXin Li if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
1483*67e74705SXin Li Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
1484*67e74705SXin Li Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
1485*67e74705SXin Li Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
1486*67e74705SXin Li }
1487*67e74705SXin Li }
1488*67e74705SXin Li // We have a selector or a colon, continue parsing.
1489*67e74705SXin Li }
1490*67e74705SXin Li
1491*67e74705SXin Li bool isVariadic = false;
1492*67e74705SXin Li bool cStyleParamWarned = false;
1493*67e74705SXin Li // Parse the (optional) parameter list.
1494*67e74705SXin Li while (Tok.is(tok::comma)) {
1495*67e74705SXin Li ConsumeToken();
1496*67e74705SXin Li if (Tok.is(tok::ellipsis)) {
1497*67e74705SXin Li isVariadic = true;
1498*67e74705SXin Li ConsumeToken();
1499*67e74705SXin Li break;
1500*67e74705SXin Li }
1501*67e74705SXin Li if (!cStyleParamWarned) {
1502*67e74705SXin Li Diag(Tok, diag::warn_cstyle_param);
1503*67e74705SXin Li cStyleParamWarned = true;
1504*67e74705SXin Li }
1505*67e74705SXin Li DeclSpec DS(AttrFactory);
1506*67e74705SXin Li ParseDeclarationSpecifiers(DS);
1507*67e74705SXin Li // Parse the declarator.
1508*67e74705SXin Li Declarator ParmDecl(DS, Declarator::PrototypeContext);
1509*67e74705SXin Li ParseDeclarator(ParmDecl);
1510*67e74705SXin Li IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1511*67e74705SXin Li Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
1512*67e74705SXin Li CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
1513*67e74705SXin Li ParmDecl.getIdentifierLoc(),
1514*67e74705SXin Li Param,
1515*67e74705SXin Li nullptr));
1516*67e74705SXin Li }
1517*67e74705SXin Li
1518*67e74705SXin Li // FIXME: Add support for optional parameter list...
1519*67e74705SXin Li // If attributes exist after the method, parse them.
1520*67e74705SXin Li if (getLangOpts().ObjC2)
1521*67e74705SXin Li MaybeParseGNUAttributes(methodAttrs);
1522*67e74705SXin Li
1523*67e74705SXin Li if (KeyIdents.size() == 0)
1524*67e74705SXin Li return nullptr;
1525*67e74705SXin Li
1526*67e74705SXin Li Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1527*67e74705SXin Li &KeyIdents[0]);
1528*67e74705SXin Li Decl *Result
1529*67e74705SXin Li = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1530*67e74705SXin Li mType, DSRet, ReturnType,
1531*67e74705SXin Li KeyLocs, Sel, &ArgInfos[0],
1532*67e74705SXin Li CParamInfo.data(), CParamInfo.size(),
1533*67e74705SXin Li methodAttrs.getList(),
1534*67e74705SXin Li MethodImplKind, isVariadic, MethodDefinition);
1535*67e74705SXin Li
1536*67e74705SXin Li PD.complete(Result);
1537*67e74705SXin Li return Result;
1538*67e74705SXin Li }
1539*67e74705SXin Li
1540*67e74705SXin Li /// objc-protocol-refs:
1541*67e74705SXin Li /// '<' identifier-list '>'
1542*67e74705SXin Li ///
1543*67e74705SXin Li bool Parser::
ParseObjCProtocolReferences(SmallVectorImpl<Decl * > & Protocols,SmallVectorImpl<SourceLocation> & ProtocolLocs,bool WarnOnDeclarations,bool ForObjCContainer,SourceLocation & LAngleLoc,SourceLocation & EndLoc,bool consumeLastToken)1544*67e74705SXin Li ParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
1545*67e74705SXin Li SmallVectorImpl<SourceLocation> &ProtocolLocs,
1546*67e74705SXin Li bool WarnOnDeclarations, bool ForObjCContainer,
1547*67e74705SXin Li SourceLocation &LAngleLoc, SourceLocation &EndLoc,
1548*67e74705SXin Li bool consumeLastToken) {
1549*67e74705SXin Li assert(Tok.is(tok::less) && "expected <");
1550*67e74705SXin Li
1551*67e74705SXin Li LAngleLoc = ConsumeToken(); // the "<"
1552*67e74705SXin Li
1553*67e74705SXin Li SmallVector<IdentifierLocPair, 8> ProtocolIdents;
1554*67e74705SXin Li
1555*67e74705SXin Li while (1) {
1556*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1557*67e74705SXin Li Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents);
1558*67e74705SXin Li cutOffParsing();
1559*67e74705SXin Li return true;
1560*67e74705SXin Li }
1561*67e74705SXin Li
1562*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
1563*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
1564*67e74705SXin Li SkipUntil(tok::greater, StopAtSemi);
1565*67e74705SXin Li return true;
1566*67e74705SXin Li }
1567*67e74705SXin Li ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1568*67e74705SXin Li Tok.getLocation()));
1569*67e74705SXin Li ProtocolLocs.push_back(Tok.getLocation());
1570*67e74705SXin Li ConsumeToken();
1571*67e74705SXin Li
1572*67e74705SXin Li if (!TryConsumeToken(tok::comma))
1573*67e74705SXin Li break;
1574*67e74705SXin Li }
1575*67e74705SXin Li
1576*67e74705SXin Li // Consume the '>'.
1577*67e74705SXin Li if (ParseGreaterThanInTemplateList(EndLoc, consumeLastToken,
1578*67e74705SXin Li /*ObjCGenericList=*/false))
1579*67e74705SXin Li return true;
1580*67e74705SXin Li
1581*67e74705SXin Li // Convert the list of protocols identifiers into a list of protocol decls.
1582*67e74705SXin Li Actions.FindProtocolDeclaration(WarnOnDeclarations, ForObjCContainer,
1583*67e74705SXin Li ProtocolIdents, Protocols);
1584*67e74705SXin Li return false;
1585*67e74705SXin Li }
1586*67e74705SXin Li
parseObjCProtocolQualifierType(SourceLocation & rAngleLoc)1587*67e74705SXin Li TypeResult Parser::parseObjCProtocolQualifierType(SourceLocation &rAngleLoc) {
1588*67e74705SXin Li assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
1589*67e74705SXin Li assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
1590*67e74705SXin Li
1591*67e74705SXin Li SourceLocation lAngleLoc;
1592*67e74705SXin Li SmallVector<Decl *, 8> protocols;
1593*67e74705SXin Li SmallVector<SourceLocation, 8> protocolLocs;
1594*67e74705SXin Li (void)ParseObjCProtocolReferences(protocols, protocolLocs, false, false,
1595*67e74705SXin Li lAngleLoc, rAngleLoc,
1596*67e74705SXin Li /*consumeLastToken=*/true);
1597*67e74705SXin Li TypeResult result = Actions.actOnObjCProtocolQualifierType(lAngleLoc,
1598*67e74705SXin Li protocols,
1599*67e74705SXin Li protocolLocs,
1600*67e74705SXin Li rAngleLoc);
1601*67e74705SXin Li if (result.isUsable()) {
1602*67e74705SXin Li Diag(lAngleLoc, diag::warn_objc_protocol_qualifier_missing_id)
1603*67e74705SXin Li << FixItHint::CreateInsertion(lAngleLoc, "id")
1604*67e74705SXin Li << SourceRange(lAngleLoc, rAngleLoc);
1605*67e74705SXin Li }
1606*67e74705SXin Li
1607*67e74705SXin Li return result;
1608*67e74705SXin Li }
1609*67e74705SXin Li
1610*67e74705SXin Li /// Parse Objective-C type arguments or protocol qualifiers.
1611*67e74705SXin Li ///
1612*67e74705SXin Li /// objc-type-arguments:
1613*67e74705SXin Li /// '<' type-name '...'[opt] (',' type-name '...'[opt])* '>'
1614*67e74705SXin Li ///
parseObjCTypeArgsOrProtocolQualifiers(ParsedType baseType,SourceLocation & typeArgsLAngleLoc,SmallVectorImpl<ParsedType> & typeArgs,SourceLocation & typeArgsRAngleLoc,SourceLocation & protocolLAngleLoc,SmallVectorImpl<Decl * > & protocols,SmallVectorImpl<SourceLocation> & protocolLocs,SourceLocation & protocolRAngleLoc,bool consumeLastToken,bool warnOnIncompleteProtocols)1615*67e74705SXin Li void Parser::parseObjCTypeArgsOrProtocolQualifiers(
1616*67e74705SXin Li ParsedType baseType,
1617*67e74705SXin Li SourceLocation &typeArgsLAngleLoc,
1618*67e74705SXin Li SmallVectorImpl<ParsedType> &typeArgs,
1619*67e74705SXin Li SourceLocation &typeArgsRAngleLoc,
1620*67e74705SXin Li SourceLocation &protocolLAngleLoc,
1621*67e74705SXin Li SmallVectorImpl<Decl *> &protocols,
1622*67e74705SXin Li SmallVectorImpl<SourceLocation> &protocolLocs,
1623*67e74705SXin Li SourceLocation &protocolRAngleLoc,
1624*67e74705SXin Li bool consumeLastToken,
1625*67e74705SXin Li bool warnOnIncompleteProtocols) {
1626*67e74705SXin Li assert(Tok.is(tok::less) && "Not at the start of type args or protocols");
1627*67e74705SXin Li SourceLocation lAngleLoc = ConsumeToken();
1628*67e74705SXin Li
1629*67e74705SXin Li // Whether all of the elements we've parsed thus far are single
1630*67e74705SXin Li // identifiers, which might be types or might be protocols.
1631*67e74705SXin Li bool allSingleIdentifiers = true;
1632*67e74705SXin Li SmallVector<IdentifierInfo *, 4> identifiers;
1633*67e74705SXin Li SmallVectorImpl<SourceLocation> &identifierLocs = protocolLocs;
1634*67e74705SXin Li
1635*67e74705SXin Li // Parse a list of comma-separated identifiers, bailing out if we
1636*67e74705SXin Li // see something different.
1637*67e74705SXin Li do {
1638*67e74705SXin Li // Parse a single identifier.
1639*67e74705SXin Li if (Tok.is(tok::identifier) &&
1640*67e74705SXin Li (NextToken().is(tok::comma) ||
1641*67e74705SXin Li NextToken().is(tok::greater) ||
1642*67e74705SXin Li NextToken().is(tok::greatergreater))) {
1643*67e74705SXin Li identifiers.push_back(Tok.getIdentifierInfo());
1644*67e74705SXin Li identifierLocs.push_back(ConsumeToken());
1645*67e74705SXin Li continue;
1646*67e74705SXin Li }
1647*67e74705SXin Li
1648*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1649*67e74705SXin Li // FIXME: Also include types here.
1650*67e74705SXin Li SmallVector<IdentifierLocPair, 4> identifierLocPairs;
1651*67e74705SXin Li for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1652*67e74705SXin Li identifierLocPairs.push_back(IdentifierLocPair(identifiers[i],
1653*67e74705SXin Li identifierLocs[i]));
1654*67e74705SXin Li }
1655*67e74705SXin Li
1656*67e74705SXin Li QualType BaseT = Actions.GetTypeFromParser(baseType);
1657*67e74705SXin Li if (!BaseT.isNull() && BaseT->acceptsObjCTypeParams()) {
1658*67e74705SXin Li Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Type);
1659*67e74705SXin Li } else {
1660*67e74705SXin Li Actions.CodeCompleteObjCProtocolReferences(identifierLocPairs);
1661*67e74705SXin Li }
1662*67e74705SXin Li cutOffParsing();
1663*67e74705SXin Li return;
1664*67e74705SXin Li }
1665*67e74705SXin Li
1666*67e74705SXin Li allSingleIdentifiers = false;
1667*67e74705SXin Li break;
1668*67e74705SXin Li } while (TryConsumeToken(tok::comma));
1669*67e74705SXin Li
1670*67e74705SXin Li // If we parsed an identifier list, semantic analysis sorts out
1671*67e74705SXin Li // whether it refers to protocols or to type arguments.
1672*67e74705SXin Li if (allSingleIdentifiers) {
1673*67e74705SXin Li // Parse the closing '>'.
1674*67e74705SXin Li SourceLocation rAngleLoc;
1675*67e74705SXin Li (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
1676*67e74705SXin Li /*ObjCGenericList=*/true);
1677*67e74705SXin Li
1678*67e74705SXin Li // Let Sema figure out what we parsed.
1679*67e74705SXin Li Actions.actOnObjCTypeArgsOrProtocolQualifiers(getCurScope(),
1680*67e74705SXin Li baseType,
1681*67e74705SXin Li lAngleLoc,
1682*67e74705SXin Li identifiers,
1683*67e74705SXin Li identifierLocs,
1684*67e74705SXin Li rAngleLoc,
1685*67e74705SXin Li typeArgsLAngleLoc,
1686*67e74705SXin Li typeArgs,
1687*67e74705SXin Li typeArgsRAngleLoc,
1688*67e74705SXin Li protocolLAngleLoc,
1689*67e74705SXin Li protocols,
1690*67e74705SXin Li protocolRAngleLoc,
1691*67e74705SXin Li warnOnIncompleteProtocols);
1692*67e74705SXin Li return;
1693*67e74705SXin Li }
1694*67e74705SXin Li
1695*67e74705SXin Li // We parsed an identifier list but stumbled into non single identifiers, this
1696*67e74705SXin Li // means we might (a) check that what we already parsed is a legitimate type
1697*67e74705SXin Li // (not a protocol or unknown type) and (b) parse the remaining ones, which
1698*67e74705SXin Li // must all be type args.
1699*67e74705SXin Li
1700*67e74705SXin Li // Convert the identifiers into type arguments.
1701*67e74705SXin Li bool invalid = false;
1702*67e74705SXin Li IdentifierInfo *foundProtocolId = nullptr, *foundValidTypeId = nullptr;
1703*67e74705SXin Li SourceLocation foundProtocolSrcLoc, foundValidTypeSrcLoc;
1704*67e74705SXin Li SmallVector<IdentifierInfo *, 2> unknownTypeArgs;
1705*67e74705SXin Li SmallVector<SourceLocation, 2> unknownTypeArgsLoc;
1706*67e74705SXin Li
1707*67e74705SXin Li for (unsigned i = 0, n = identifiers.size(); i != n; ++i) {
1708*67e74705SXin Li ParsedType typeArg
1709*67e74705SXin Li = Actions.getTypeName(*identifiers[i], identifierLocs[i], getCurScope());
1710*67e74705SXin Li if (typeArg) {
1711*67e74705SXin Li DeclSpec DS(AttrFactory);
1712*67e74705SXin Li const char *prevSpec = nullptr;
1713*67e74705SXin Li unsigned diagID;
1714*67e74705SXin Li DS.SetTypeSpecType(TST_typename, identifierLocs[i], prevSpec, diagID,
1715*67e74705SXin Li typeArg, Actions.getASTContext().getPrintingPolicy());
1716*67e74705SXin Li
1717*67e74705SXin Li // Form a declarator to turn this into a type.
1718*67e74705SXin Li Declarator D(DS, Declarator::TypeNameContext);
1719*67e74705SXin Li TypeResult fullTypeArg = Actions.ActOnTypeName(getCurScope(), D);
1720*67e74705SXin Li if (fullTypeArg.isUsable()) {
1721*67e74705SXin Li typeArgs.push_back(fullTypeArg.get());
1722*67e74705SXin Li if (!foundValidTypeId) {
1723*67e74705SXin Li foundValidTypeId = identifiers[i];
1724*67e74705SXin Li foundValidTypeSrcLoc = identifierLocs[i];
1725*67e74705SXin Li }
1726*67e74705SXin Li } else {
1727*67e74705SXin Li invalid = true;
1728*67e74705SXin Li unknownTypeArgs.push_back(identifiers[i]);
1729*67e74705SXin Li unknownTypeArgsLoc.push_back(identifierLocs[i]);
1730*67e74705SXin Li }
1731*67e74705SXin Li } else {
1732*67e74705SXin Li invalid = true;
1733*67e74705SXin Li if (!Actions.LookupProtocol(identifiers[i], identifierLocs[i])) {
1734*67e74705SXin Li unknownTypeArgs.push_back(identifiers[i]);
1735*67e74705SXin Li unknownTypeArgsLoc.push_back(identifierLocs[i]);
1736*67e74705SXin Li } else if (!foundProtocolId) {
1737*67e74705SXin Li foundProtocolId = identifiers[i];
1738*67e74705SXin Li foundProtocolSrcLoc = identifierLocs[i];
1739*67e74705SXin Li }
1740*67e74705SXin Li }
1741*67e74705SXin Li }
1742*67e74705SXin Li
1743*67e74705SXin Li // Continue parsing type-names.
1744*67e74705SXin Li do {
1745*67e74705SXin Li Token CurTypeTok = Tok;
1746*67e74705SXin Li TypeResult typeArg = ParseTypeName();
1747*67e74705SXin Li
1748*67e74705SXin Li // Consume the '...' for a pack expansion.
1749*67e74705SXin Li SourceLocation ellipsisLoc;
1750*67e74705SXin Li TryConsumeToken(tok::ellipsis, ellipsisLoc);
1751*67e74705SXin Li if (typeArg.isUsable() && ellipsisLoc.isValid()) {
1752*67e74705SXin Li typeArg = Actions.ActOnPackExpansion(typeArg.get(), ellipsisLoc);
1753*67e74705SXin Li }
1754*67e74705SXin Li
1755*67e74705SXin Li if (typeArg.isUsable()) {
1756*67e74705SXin Li typeArgs.push_back(typeArg.get());
1757*67e74705SXin Li if (!foundValidTypeId) {
1758*67e74705SXin Li foundValidTypeId = CurTypeTok.getIdentifierInfo();
1759*67e74705SXin Li foundValidTypeSrcLoc = CurTypeTok.getLocation();
1760*67e74705SXin Li }
1761*67e74705SXin Li } else {
1762*67e74705SXin Li invalid = true;
1763*67e74705SXin Li }
1764*67e74705SXin Li } while (TryConsumeToken(tok::comma));
1765*67e74705SXin Li
1766*67e74705SXin Li // Diagnose the mix between type args and protocols.
1767*67e74705SXin Li if (foundProtocolId && foundValidTypeId)
1768*67e74705SXin Li Actions.DiagnoseTypeArgsAndProtocols(foundProtocolId, foundProtocolSrcLoc,
1769*67e74705SXin Li foundValidTypeId,
1770*67e74705SXin Li foundValidTypeSrcLoc);
1771*67e74705SXin Li
1772*67e74705SXin Li // Diagnose unknown arg types.
1773*67e74705SXin Li ParsedType T;
1774*67e74705SXin Li if (unknownTypeArgs.size())
1775*67e74705SXin Li for (unsigned i = 0, e = unknownTypeArgsLoc.size(); i < e; ++i)
1776*67e74705SXin Li Actions.DiagnoseUnknownTypeName(unknownTypeArgs[i], unknownTypeArgsLoc[i],
1777*67e74705SXin Li getCurScope(), nullptr, T);
1778*67e74705SXin Li
1779*67e74705SXin Li // Parse the closing '>'.
1780*67e74705SXin Li SourceLocation rAngleLoc;
1781*67e74705SXin Li (void)ParseGreaterThanInTemplateList(rAngleLoc, consumeLastToken,
1782*67e74705SXin Li /*ObjCGenericList=*/true);
1783*67e74705SXin Li
1784*67e74705SXin Li if (invalid) {
1785*67e74705SXin Li typeArgs.clear();
1786*67e74705SXin Li return;
1787*67e74705SXin Li }
1788*67e74705SXin Li
1789*67e74705SXin Li // Record left/right angle locations.
1790*67e74705SXin Li typeArgsLAngleLoc = lAngleLoc;
1791*67e74705SXin Li typeArgsRAngleLoc = rAngleLoc;
1792*67e74705SXin Li }
1793*67e74705SXin Li
parseObjCTypeArgsAndProtocolQualifiers(ParsedType baseType,SourceLocation & typeArgsLAngleLoc,SmallVectorImpl<ParsedType> & typeArgs,SourceLocation & typeArgsRAngleLoc,SourceLocation & protocolLAngleLoc,SmallVectorImpl<Decl * > & protocols,SmallVectorImpl<SourceLocation> & protocolLocs,SourceLocation & protocolRAngleLoc,bool consumeLastToken)1794*67e74705SXin Li void Parser::parseObjCTypeArgsAndProtocolQualifiers(
1795*67e74705SXin Li ParsedType baseType,
1796*67e74705SXin Li SourceLocation &typeArgsLAngleLoc,
1797*67e74705SXin Li SmallVectorImpl<ParsedType> &typeArgs,
1798*67e74705SXin Li SourceLocation &typeArgsRAngleLoc,
1799*67e74705SXin Li SourceLocation &protocolLAngleLoc,
1800*67e74705SXin Li SmallVectorImpl<Decl *> &protocols,
1801*67e74705SXin Li SmallVectorImpl<SourceLocation> &protocolLocs,
1802*67e74705SXin Li SourceLocation &protocolRAngleLoc,
1803*67e74705SXin Li bool consumeLastToken) {
1804*67e74705SXin Li assert(Tok.is(tok::less));
1805*67e74705SXin Li
1806*67e74705SXin Li // Parse the first angle-bracket-delimited clause.
1807*67e74705SXin Li parseObjCTypeArgsOrProtocolQualifiers(baseType,
1808*67e74705SXin Li typeArgsLAngleLoc,
1809*67e74705SXin Li typeArgs,
1810*67e74705SXin Li typeArgsRAngleLoc,
1811*67e74705SXin Li protocolLAngleLoc,
1812*67e74705SXin Li protocols,
1813*67e74705SXin Li protocolLocs,
1814*67e74705SXin Li protocolRAngleLoc,
1815*67e74705SXin Li consumeLastToken,
1816*67e74705SXin Li /*warnOnIncompleteProtocols=*/false);
1817*67e74705SXin Li
1818*67e74705SXin Li // An Objective-C object pointer followed by type arguments
1819*67e74705SXin Li // can then be followed again by a set of protocol references, e.g.,
1820*67e74705SXin Li // \c NSArray<NSView><NSTextDelegate>
1821*67e74705SXin Li if ((consumeLastToken && Tok.is(tok::less)) ||
1822*67e74705SXin Li (!consumeLastToken && NextToken().is(tok::less))) {
1823*67e74705SXin Li // If we aren't consuming the last token, the prior '>' is still hanging
1824*67e74705SXin Li // there. Consume it before we parse the protocol qualifiers.
1825*67e74705SXin Li if (!consumeLastToken)
1826*67e74705SXin Li ConsumeToken();
1827*67e74705SXin Li
1828*67e74705SXin Li if (!protocols.empty()) {
1829*67e74705SXin Li SkipUntilFlags skipFlags = SkipUntilFlags();
1830*67e74705SXin Li if (!consumeLastToken)
1831*67e74705SXin Li skipFlags = skipFlags | StopBeforeMatch;
1832*67e74705SXin Li Diag(Tok, diag::err_objc_type_args_after_protocols)
1833*67e74705SXin Li << SourceRange(protocolLAngleLoc, protocolRAngleLoc);
1834*67e74705SXin Li SkipUntil(tok::greater, tok::greatergreater, skipFlags);
1835*67e74705SXin Li } else {
1836*67e74705SXin Li ParseObjCProtocolReferences(protocols, protocolLocs,
1837*67e74705SXin Li /*WarnOnDeclarations=*/false,
1838*67e74705SXin Li /*ForObjCContainer=*/false,
1839*67e74705SXin Li protocolLAngleLoc, protocolRAngleLoc,
1840*67e74705SXin Li consumeLastToken);
1841*67e74705SXin Li }
1842*67e74705SXin Li }
1843*67e74705SXin Li }
1844*67e74705SXin Li
parseObjCTypeArgsAndProtocolQualifiers(SourceLocation loc,ParsedType type,bool consumeLastToken,SourceLocation & endLoc)1845*67e74705SXin Li TypeResult Parser::parseObjCTypeArgsAndProtocolQualifiers(
1846*67e74705SXin Li SourceLocation loc,
1847*67e74705SXin Li ParsedType type,
1848*67e74705SXin Li bool consumeLastToken,
1849*67e74705SXin Li SourceLocation &endLoc) {
1850*67e74705SXin Li assert(Tok.is(tok::less));
1851*67e74705SXin Li SourceLocation typeArgsLAngleLoc;
1852*67e74705SXin Li SmallVector<ParsedType, 4> typeArgs;
1853*67e74705SXin Li SourceLocation typeArgsRAngleLoc;
1854*67e74705SXin Li SourceLocation protocolLAngleLoc;
1855*67e74705SXin Li SmallVector<Decl *, 4> protocols;
1856*67e74705SXin Li SmallVector<SourceLocation, 4> protocolLocs;
1857*67e74705SXin Li SourceLocation protocolRAngleLoc;
1858*67e74705SXin Li
1859*67e74705SXin Li // Parse type arguments and protocol qualifiers.
1860*67e74705SXin Li parseObjCTypeArgsAndProtocolQualifiers(type, typeArgsLAngleLoc, typeArgs,
1861*67e74705SXin Li typeArgsRAngleLoc, protocolLAngleLoc,
1862*67e74705SXin Li protocols, protocolLocs,
1863*67e74705SXin Li protocolRAngleLoc, consumeLastToken);
1864*67e74705SXin Li
1865*67e74705SXin Li // Compute the location of the last token.
1866*67e74705SXin Li if (consumeLastToken)
1867*67e74705SXin Li endLoc = PrevTokLocation;
1868*67e74705SXin Li else
1869*67e74705SXin Li endLoc = Tok.getLocation();
1870*67e74705SXin Li
1871*67e74705SXin Li return Actions.actOnObjCTypeArgsAndProtocolQualifiers(
1872*67e74705SXin Li getCurScope(),
1873*67e74705SXin Li loc,
1874*67e74705SXin Li type,
1875*67e74705SXin Li typeArgsLAngleLoc,
1876*67e74705SXin Li typeArgs,
1877*67e74705SXin Li typeArgsRAngleLoc,
1878*67e74705SXin Li protocolLAngleLoc,
1879*67e74705SXin Li protocols,
1880*67e74705SXin Li protocolLocs,
1881*67e74705SXin Li protocolRAngleLoc);
1882*67e74705SXin Li }
1883*67e74705SXin Li
HelperActionsForIvarDeclarations(Decl * interfaceDecl,SourceLocation atLoc,BalancedDelimiterTracker & T,SmallVectorImpl<Decl * > & AllIvarDecls,bool RBraceMissing)1884*67e74705SXin Li void Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
1885*67e74705SXin Li BalancedDelimiterTracker &T,
1886*67e74705SXin Li SmallVectorImpl<Decl *> &AllIvarDecls,
1887*67e74705SXin Li bool RBraceMissing) {
1888*67e74705SXin Li if (!RBraceMissing)
1889*67e74705SXin Li T.consumeClose();
1890*67e74705SXin Li
1891*67e74705SXin Li Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1892*67e74705SXin Li Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
1893*67e74705SXin Li Actions.ActOnObjCContainerFinishDefinition();
1894*67e74705SXin Li // Call ActOnFields() even if we don't have any decls. This is useful
1895*67e74705SXin Li // for code rewriting tools that need to be aware of the empty list.
1896*67e74705SXin Li Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1897*67e74705SXin Li AllIvarDecls,
1898*67e74705SXin Li T.getOpenLocation(), T.getCloseLocation(), nullptr);
1899*67e74705SXin Li }
1900*67e74705SXin Li
1901*67e74705SXin Li /// objc-class-instance-variables:
1902*67e74705SXin Li /// '{' objc-instance-variable-decl-list[opt] '}'
1903*67e74705SXin Li ///
1904*67e74705SXin Li /// objc-instance-variable-decl-list:
1905*67e74705SXin Li /// objc-visibility-spec
1906*67e74705SXin Li /// objc-instance-variable-decl ';'
1907*67e74705SXin Li /// ';'
1908*67e74705SXin Li /// objc-instance-variable-decl-list objc-visibility-spec
1909*67e74705SXin Li /// objc-instance-variable-decl-list objc-instance-variable-decl ';'
1910*67e74705SXin Li /// objc-instance-variable-decl-list ';'
1911*67e74705SXin Li ///
1912*67e74705SXin Li /// objc-visibility-spec:
1913*67e74705SXin Li /// @private
1914*67e74705SXin Li /// @protected
1915*67e74705SXin Li /// @public
1916*67e74705SXin Li /// @package [OBJC2]
1917*67e74705SXin Li ///
1918*67e74705SXin Li /// objc-instance-variable-decl:
1919*67e74705SXin Li /// struct-declaration
1920*67e74705SXin Li ///
ParseObjCClassInstanceVariables(Decl * interfaceDecl,tok::ObjCKeywordKind visibility,SourceLocation atLoc)1921*67e74705SXin Li void Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
1922*67e74705SXin Li tok::ObjCKeywordKind visibility,
1923*67e74705SXin Li SourceLocation atLoc) {
1924*67e74705SXin Li assert(Tok.is(tok::l_brace) && "expected {");
1925*67e74705SXin Li SmallVector<Decl *, 32> AllIvarDecls;
1926*67e74705SXin Li
1927*67e74705SXin Li ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
1928*67e74705SXin Li ObjCDeclContextSwitch ObjCDC(*this);
1929*67e74705SXin Li
1930*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_brace);
1931*67e74705SXin Li T.consumeOpen();
1932*67e74705SXin Li // While we still have something to read, read the instance variables.
1933*67e74705SXin Li while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
1934*67e74705SXin Li // Each iteration of this loop reads one objc-instance-variable-decl.
1935*67e74705SXin Li
1936*67e74705SXin Li // Check for extraneous top-level semicolon.
1937*67e74705SXin Li if (Tok.is(tok::semi)) {
1938*67e74705SXin Li ConsumeExtraSemi(InstanceVariableList);
1939*67e74705SXin Li continue;
1940*67e74705SXin Li }
1941*67e74705SXin Li
1942*67e74705SXin Li // Set the default visibility to private.
1943*67e74705SXin Li if (TryConsumeToken(tok::at)) { // parse objc-visibility-spec
1944*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1945*67e74705SXin Li Actions.CodeCompleteObjCAtVisibility(getCurScope());
1946*67e74705SXin Li return cutOffParsing();
1947*67e74705SXin Li }
1948*67e74705SXin Li
1949*67e74705SXin Li switch (Tok.getObjCKeywordID()) {
1950*67e74705SXin Li case tok::objc_private:
1951*67e74705SXin Li case tok::objc_public:
1952*67e74705SXin Li case tok::objc_protected:
1953*67e74705SXin Li case tok::objc_package:
1954*67e74705SXin Li visibility = Tok.getObjCKeywordID();
1955*67e74705SXin Li ConsumeToken();
1956*67e74705SXin Li continue;
1957*67e74705SXin Li
1958*67e74705SXin Li case tok::objc_end:
1959*67e74705SXin Li Diag(Tok, diag::err_objc_unexpected_atend);
1960*67e74705SXin Li Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
1961*67e74705SXin Li Tok.setKind(tok::at);
1962*67e74705SXin Li Tok.setLength(1);
1963*67e74705SXin Li PP.EnterToken(Tok);
1964*67e74705SXin Li HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
1965*67e74705SXin Li T, AllIvarDecls, true);
1966*67e74705SXin Li return;
1967*67e74705SXin Li
1968*67e74705SXin Li default:
1969*67e74705SXin Li Diag(Tok, diag::err_objc_illegal_visibility_spec);
1970*67e74705SXin Li continue;
1971*67e74705SXin Li }
1972*67e74705SXin Li }
1973*67e74705SXin Li
1974*67e74705SXin Li if (Tok.is(tok::code_completion)) {
1975*67e74705SXin Li Actions.CodeCompleteOrdinaryName(getCurScope(),
1976*67e74705SXin Li Sema::PCC_ObjCInstanceVariableList);
1977*67e74705SXin Li return cutOffParsing();
1978*67e74705SXin Li }
1979*67e74705SXin Li
1980*67e74705SXin Li auto ObjCIvarCallback = [&](ParsingFieldDeclarator &FD) {
1981*67e74705SXin Li Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1982*67e74705SXin Li // Install the declarator into the interface decl.
1983*67e74705SXin Li FD.D.setObjCIvar(true);
1984*67e74705SXin Li Decl *Field = Actions.ActOnIvar(
1985*67e74705SXin Li getCurScope(), FD.D.getDeclSpec().getSourceRange().getBegin(), FD.D,
1986*67e74705SXin Li FD.BitfieldSize, visibility);
1987*67e74705SXin Li Actions.ActOnObjCContainerFinishDefinition();
1988*67e74705SXin Li if (Field)
1989*67e74705SXin Li AllIvarDecls.push_back(Field);
1990*67e74705SXin Li FD.complete(Field);
1991*67e74705SXin Li };
1992*67e74705SXin Li
1993*67e74705SXin Li // Parse all the comma separated declarators.
1994*67e74705SXin Li ParsingDeclSpec DS(*this);
1995*67e74705SXin Li ParseStructDeclaration(DS, ObjCIvarCallback);
1996*67e74705SXin Li
1997*67e74705SXin Li if (Tok.is(tok::semi)) {
1998*67e74705SXin Li ConsumeToken();
1999*67e74705SXin Li } else {
2000*67e74705SXin Li Diag(Tok, diag::err_expected_semi_decl_list);
2001*67e74705SXin Li // Skip to end of block or statement
2002*67e74705SXin Li SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
2003*67e74705SXin Li }
2004*67e74705SXin Li }
2005*67e74705SXin Li HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
2006*67e74705SXin Li T, AllIvarDecls, false);
2007*67e74705SXin Li }
2008*67e74705SXin Li
2009*67e74705SXin Li /// objc-protocol-declaration:
2010*67e74705SXin Li /// objc-protocol-definition
2011*67e74705SXin Li /// objc-protocol-forward-reference
2012*67e74705SXin Li ///
2013*67e74705SXin Li /// objc-protocol-definition:
2014*67e74705SXin Li /// \@protocol identifier
2015*67e74705SXin Li /// objc-protocol-refs[opt]
2016*67e74705SXin Li /// objc-interface-decl-list
2017*67e74705SXin Li /// \@end
2018*67e74705SXin Li ///
2019*67e74705SXin Li /// objc-protocol-forward-reference:
2020*67e74705SXin Li /// \@protocol identifier-list ';'
2021*67e74705SXin Li ///
2022*67e74705SXin Li /// "\@protocol identifier ;" should be resolved as "\@protocol
2023*67e74705SXin Li /// identifier-list ;": objc-interface-decl-list may not start with a
2024*67e74705SXin Li /// semicolon in the first alternative if objc-protocol-refs are omitted.
2025*67e74705SXin Li Parser::DeclGroupPtrTy
ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,ParsedAttributes & attrs)2026*67e74705SXin Li Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
2027*67e74705SXin Li ParsedAttributes &attrs) {
2028*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
2029*67e74705SXin Li "ParseObjCAtProtocolDeclaration(): Expected @protocol");
2030*67e74705SXin Li ConsumeToken(); // the "protocol" identifier
2031*67e74705SXin Li
2032*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2033*67e74705SXin Li Actions.CodeCompleteObjCProtocolDecl(getCurScope());
2034*67e74705SXin Li cutOffParsing();
2035*67e74705SXin Li return nullptr;
2036*67e74705SXin Li }
2037*67e74705SXin Li
2038*67e74705SXin Li MaybeSkipAttributes(tok::objc_protocol);
2039*67e74705SXin Li
2040*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2041*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier; // missing protocol name.
2042*67e74705SXin Li return nullptr;
2043*67e74705SXin Li }
2044*67e74705SXin Li // Save the protocol name, then consume it.
2045*67e74705SXin Li IdentifierInfo *protocolName = Tok.getIdentifierInfo();
2046*67e74705SXin Li SourceLocation nameLoc = ConsumeToken();
2047*67e74705SXin Li
2048*67e74705SXin Li if (TryConsumeToken(tok::semi)) { // forward declaration of one protocol.
2049*67e74705SXin Li IdentifierLocPair ProtoInfo(protocolName, nameLoc);
2050*67e74705SXin Li return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtoInfo,
2051*67e74705SXin Li attrs.getList());
2052*67e74705SXin Li }
2053*67e74705SXin Li
2054*67e74705SXin Li CheckNestedObjCContexts(AtLoc);
2055*67e74705SXin Li
2056*67e74705SXin Li if (Tok.is(tok::comma)) { // list of forward declarations.
2057*67e74705SXin Li SmallVector<IdentifierLocPair, 8> ProtocolRefs;
2058*67e74705SXin Li ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
2059*67e74705SXin Li
2060*67e74705SXin Li // Parse the list of forward declarations.
2061*67e74705SXin Li while (1) {
2062*67e74705SXin Li ConsumeToken(); // the ','
2063*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2064*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
2065*67e74705SXin Li SkipUntil(tok::semi);
2066*67e74705SXin Li return nullptr;
2067*67e74705SXin Li }
2068*67e74705SXin Li ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
2069*67e74705SXin Li Tok.getLocation()));
2070*67e74705SXin Li ConsumeToken(); // the identifier
2071*67e74705SXin Li
2072*67e74705SXin Li if (Tok.isNot(tok::comma))
2073*67e74705SXin Li break;
2074*67e74705SXin Li }
2075*67e74705SXin Li // Consume the ';'.
2076*67e74705SXin Li if (ExpectAndConsume(tok::semi, diag::err_expected_after, "@protocol"))
2077*67e74705SXin Li return nullptr;
2078*67e74705SXin Li
2079*67e74705SXin Li return Actions.ActOnForwardProtocolDeclaration(AtLoc, ProtocolRefs,
2080*67e74705SXin Li attrs.getList());
2081*67e74705SXin Li }
2082*67e74705SXin Li
2083*67e74705SXin Li // Last, and definitely not least, parse a protocol declaration.
2084*67e74705SXin Li SourceLocation LAngleLoc, EndProtoLoc;
2085*67e74705SXin Li
2086*67e74705SXin Li SmallVector<Decl *, 8> ProtocolRefs;
2087*67e74705SXin Li SmallVector<SourceLocation, 8> ProtocolLocs;
2088*67e74705SXin Li if (Tok.is(tok::less) &&
2089*67e74705SXin Li ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false, true,
2090*67e74705SXin Li LAngleLoc, EndProtoLoc,
2091*67e74705SXin Li /*consumeLastToken=*/true))
2092*67e74705SXin Li return nullptr;
2093*67e74705SXin Li
2094*67e74705SXin Li Decl *ProtoType =
2095*67e74705SXin Li Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
2096*67e74705SXin Li ProtocolRefs.data(),
2097*67e74705SXin Li ProtocolRefs.size(),
2098*67e74705SXin Li ProtocolLocs.data(),
2099*67e74705SXin Li EndProtoLoc, attrs.getList());
2100*67e74705SXin Li
2101*67e74705SXin Li ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
2102*67e74705SXin Li return Actions.ConvertDeclToDeclGroup(ProtoType);
2103*67e74705SXin Li }
2104*67e74705SXin Li
2105*67e74705SXin Li /// objc-implementation:
2106*67e74705SXin Li /// objc-class-implementation-prologue
2107*67e74705SXin Li /// objc-category-implementation-prologue
2108*67e74705SXin Li ///
2109*67e74705SXin Li /// objc-class-implementation-prologue:
2110*67e74705SXin Li /// @implementation identifier objc-superclass[opt]
2111*67e74705SXin Li /// objc-class-instance-variables[opt]
2112*67e74705SXin Li ///
2113*67e74705SXin Li /// objc-category-implementation-prologue:
2114*67e74705SXin Li /// @implementation identifier ( identifier )
2115*67e74705SXin Li Parser::DeclGroupPtrTy
ParseObjCAtImplementationDeclaration(SourceLocation AtLoc)2116*67e74705SXin Li Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
2117*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
2118*67e74705SXin Li "ParseObjCAtImplementationDeclaration(): Expected @implementation");
2119*67e74705SXin Li CheckNestedObjCContexts(AtLoc);
2120*67e74705SXin Li ConsumeToken(); // the "implementation" identifier
2121*67e74705SXin Li
2122*67e74705SXin Li // Code completion after '@implementation'.
2123*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2124*67e74705SXin Li Actions.CodeCompleteObjCImplementationDecl(getCurScope());
2125*67e74705SXin Li cutOffParsing();
2126*67e74705SXin Li return nullptr;
2127*67e74705SXin Li }
2128*67e74705SXin Li
2129*67e74705SXin Li MaybeSkipAttributes(tok::objc_implementation);
2130*67e74705SXin Li
2131*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2132*67e74705SXin Li Diag(Tok, diag::err_expected)
2133*67e74705SXin Li << tok::identifier; // missing class or category name.
2134*67e74705SXin Li return nullptr;
2135*67e74705SXin Li }
2136*67e74705SXin Li // We have a class or category name - consume it.
2137*67e74705SXin Li IdentifierInfo *nameId = Tok.getIdentifierInfo();
2138*67e74705SXin Li SourceLocation nameLoc = ConsumeToken(); // consume class or category name
2139*67e74705SXin Li Decl *ObjCImpDecl = nullptr;
2140*67e74705SXin Li
2141*67e74705SXin Li // Neither a type parameter list nor a list of protocol references is
2142*67e74705SXin Li // permitted here. Parse and diagnose them.
2143*67e74705SXin Li if (Tok.is(tok::less)) {
2144*67e74705SXin Li SourceLocation lAngleLoc, rAngleLoc;
2145*67e74705SXin Li SmallVector<IdentifierLocPair, 8> protocolIdents;
2146*67e74705SXin Li SourceLocation diagLoc = Tok.getLocation();
2147*67e74705SXin Li ObjCTypeParamListScope typeParamScope(Actions, getCurScope());
2148*67e74705SXin Li if (parseObjCTypeParamListOrProtocolRefs(typeParamScope, lAngleLoc,
2149*67e74705SXin Li protocolIdents, rAngleLoc)) {
2150*67e74705SXin Li Diag(diagLoc, diag::err_objc_parameterized_implementation)
2151*67e74705SXin Li << SourceRange(diagLoc, PrevTokLocation);
2152*67e74705SXin Li } else if (lAngleLoc.isValid()) {
2153*67e74705SXin Li Diag(lAngleLoc, diag::err_unexpected_protocol_qualifier)
2154*67e74705SXin Li << FixItHint::CreateRemoval(SourceRange(lAngleLoc, rAngleLoc));
2155*67e74705SXin Li }
2156*67e74705SXin Li }
2157*67e74705SXin Li
2158*67e74705SXin Li if (Tok.is(tok::l_paren)) {
2159*67e74705SXin Li // we have a category implementation.
2160*67e74705SXin Li ConsumeParen();
2161*67e74705SXin Li SourceLocation categoryLoc, rparenLoc;
2162*67e74705SXin Li IdentifierInfo *categoryId = nullptr;
2163*67e74705SXin Li
2164*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2165*67e74705SXin Li Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
2166*67e74705SXin Li cutOffParsing();
2167*67e74705SXin Li return nullptr;
2168*67e74705SXin Li }
2169*67e74705SXin Li
2170*67e74705SXin Li if (Tok.is(tok::identifier)) {
2171*67e74705SXin Li categoryId = Tok.getIdentifierInfo();
2172*67e74705SXin Li categoryLoc = ConsumeToken();
2173*67e74705SXin Li } else {
2174*67e74705SXin Li Diag(Tok, diag::err_expected)
2175*67e74705SXin Li << tok::identifier; // missing category name.
2176*67e74705SXin Li return nullptr;
2177*67e74705SXin Li }
2178*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
2179*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::r_paren;
2180*67e74705SXin Li SkipUntil(tok::r_paren); // don't stop at ';'
2181*67e74705SXin Li return nullptr;
2182*67e74705SXin Li }
2183*67e74705SXin Li rparenLoc = ConsumeParen();
2184*67e74705SXin Li if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2185*67e74705SXin Li Diag(Tok, diag::err_unexpected_protocol_qualifier);
2186*67e74705SXin Li SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2187*67e74705SXin Li SmallVector<Decl *, 4> protocols;
2188*67e74705SXin Li SmallVector<SourceLocation, 4> protocolLocs;
2189*67e74705SXin Li (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2190*67e74705SXin Li /*warnOnIncompleteProtocols=*/false,
2191*67e74705SXin Li /*ForObjCContainer=*/false,
2192*67e74705SXin Li protocolLAngleLoc, protocolRAngleLoc,
2193*67e74705SXin Li /*consumeLastToken=*/true);
2194*67e74705SXin Li }
2195*67e74705SXin Li ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
2196*67e74705SXin Li AtLoc, nameId, nameLoc, categoryId,
2197*67e74705SXin Li categoryLoc);
2198*67e74705SXin Li
2199*67e74705SXin Li } else {
2200*67e74705SXin Li // We have a class implementation
2201*67e74705SXin Li SourceLocation superClassLoc;
2202*67e74705SXin Li IdentifierInfo *superClassId = nullptr;
2203*67e74705SXin Li if (TryConsumeToken(tok::colon)) {
2204*67e74705SXin Li // We have a super class
2205*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2206*67e74705SXin Li Diag(Tok, diag::err_expected)
2207*67e74705SXin Li << tok::identifier; // missing super class name.
2208*67e74705SXin Li return nullptr;
2209*67e74705SXin Li }
2210*67e74705SXin Li superClassId = Tok.getIdentifierInfo();
2211*67e74705SXin Li superClassLoc = ConsumeToken(); // Consume super class name
2212*67e74705SXin Li }
2213*67e74705SXin Li ObjCImpDecl = Actions.ActOnStartClassImplementation(
2214*67e74705SXin Li AtLoc, nameId, nameLoc,
2215*67e74705SXin Li superClassId, superClassLoc);
2216*67e74705SXin Li
2217*67e74705SXin Li if (Tok.is(tok::l_brace)) // we have ivars
2218*67e74705SXin Li ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
2219*67e74705SXin Li else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
2220*67e74705SXin Li Diag(Tok, diag::err_unexpected_protocol_qualifier);
2221*67e74705SXin Li
2222*67e74705SXin Li SourceLocation protocolLAngleLoc, protocolRAngleLoc;
2223*67e74705SXin Li SmallVector<Decl *, 4> protocols;
2224*67e74705SXin Li SmallVector<SourceLocation, 4> protocolLocs;
2225*67e74705SXin Li (void)ParseObjCProtocolReferences(protocols, protocolLocs,
2226*67e74705SXin Li /*warnOnIncompleteProtocols=*/false,
2227*67e74705SXin Li /*ForObjCContainer=*/false,
2228*67e74705SXin Li protocolLAngleLoc, protocolRAngleLoc,
2229*67e74705SXin Li /*consumeLastToken=*/true);
2230*67e74705SXin Li }
2231*67e74705SXin Li }
2232*67e74705SXin Li assert(ObjCImpDecl);
2233*67e74705SXin Li
2234*67e74705SXin Li SmallVector<Decl *, 8> DeclsInGroup;
2235*67e74705SXin Li
2236*67e74705SXin Li {
2237*67e74705SXin Li ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
2238*67e74705SXin Li while (!ObjCImplParsing.isFinished() && !isEofOrEom()) {
2239*67e74705SXin Li ParsedAttributesWithRange attrs(AttrFactory);
2240*67e74705SXin Li MaybeParseCXX11Attributes(attrs);
2241*67e74705SXin Li MaybeParseMicrosoftAttributes(attrs);
2242*67e74705SXin Li if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
2243*67e74705SXin Li DeclGroupRef DG = DGP.get();
2244*67e74705SXin Li DeclsInGroup.append(DG.begin(), DG.end());
2245*67e74705SXin Li }
2246*67e74705SXin Li }
2247*67e74705SXin Li }
2248*67e74705SXin Li
2249*67e74705SXin Li return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
2250*67e74705SXin Li }
2251*67e74705SXin Li
2252*67e74705SXin Li Parser::DeclGroupPtrTy
ParseObjCAtEndDeclaration(SourceRange atEnd)2253*67e74705SXin Li Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
2254*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_end) &&
2255*67e74705SXin Li "ParseObjCAtEndDeclaration(): Expected @end");
2256*67e74705SXin Li ConsumeToken(); // the "end" identifier
2257*67e74705SXin Li if (CurParsedObjCImpl)
2258*67e74705SXin Li CurParsedObjCImpl->finish(atEnd);
2259*67e74705SXin Li else
2260*67e74705SXin Li // missing @implementation
2261*67e74705SXin Li Diag(atEnd.getBegin(), diag::err_expected_objc_container);
2262*67e74705SXin Li return nullptr;
2263*67e74705SXin Li }
2264*67e74705SXin Li
~ObjCImplParsingDataRAII()2265*67e74705SXin Li Parser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
2266*67e74705SXin Li if (!Finished) {
2267*67e74705SXin Li finish(P.Tok.getLocation());
2268*67e74705SXin Li if (P.isEofOrEom()) {
2269*67e74705SXin Li P.Diag(P.Tok, diag::err_objc_missing_end)
2270*67e74705SXin Li << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
2271*67e74705SXin Li P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
2272*67e74705SXin Li << Sema::OCK_Implementation;
2273*67e74705SXin Li }
2274*67e74705SXin Li }
2275*67e74705SXin Li P.CurParsedObjCImpl = nullptr;
2276*67e74705SXin Li assert(LateParsedObjCMethods.empty());
2277*67e74705SXin Li }
2278*67e74705SXin Li
finish(SourceRange AtEnd)2279*67e74705SXin Li void Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
2280*67e74705SXin Li assert(!Finished);
2281*67e74705SXin Li P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
2282*67e74705SXin Li for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2283*67e74705SXin Li P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2284*67e74705SXin Li true/*Methods*/);
2285*67e74705SXin Li
2286*67e74705SXin Li P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
2287*67e74705SXin Li
2288*67e74705SXin Li if (HasCFunction)
2289*67e74705SXin Li for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
2290*67e74705SXin Li P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
2291*67e74705SXin Li false/*c-functions*/);
2292*67e74705SXin Li
2293*67e74705SXin Li /// \brief Clear and free the cached objc methods.
2294*67e74705SXin Li for (LateParsedObjCMethodContainer::iterator
2295*67e74705SXin Li I = LateParsedObjCMethods.begin(),
2296*67e74705SXin Li E = LateParsedObjCMethods.end(); I != E; ++I)
2297*67e74705SXin Li delete *I;
2298*67e74705SXin Li LateParsedObjCMethods.clear();
2299*67e74705SXin Li
2300*67e74705SXin Li Finished = true;
2301*67e74705SXin Li }
2302*67e74705SXin Li
2303*67e74705SXin Li /// compatibility-alias-decl:
2304*67e74705SXin Li /// @compatibility_alias alias-name class-name ';'
2305*67e74705SXin Li ///
ParseObjCAtAliasDeclaration(SourceLocation atLoc)2306*67e74705SXin Li Decl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
2307*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
2308*67e74705SXin Li "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
2309*67e74705SXin Li ConsumeToken(); // consume compatibility_alias
2310*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2311*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
2312*67e74705SXin Li return nullptr;
2313*67e74705SXin Li }
2314*67e74705SXin Li IdentifierInfo *aliasId = Tok.getIdentifierInfo();
2315*67e74705SXin Li SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
2316*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2317*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
2318*67e74705SXin Li return nullptr;
2319*67e74705SXin Li }
2320*67e74705SXin Li IdentifierInfo *classId = Tok.getIdentifierInfo();
2321*67e74705SXin Li SourceLocation classLoc = ConsumeToken(); // consume class-name;
2322*67e74705SXin Li ExpectAndConsume(tok::semi, diag::err_expected_after, "@compatibility_alias");
2323*67e74705SXin Li return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
2324*67e74705SXin Li classId, classLoc);
2325*67e74705SXin Li }
2326*67e74705SXin Li
2327*67e74705SXin Li /// property-synthesis:
2328*67e74705SXin Li /// @synthesize property-ivar-list ';'
2329*67e74705SXin Li ///
2330*67e74705SXin Li /// property-ivar-list:
2331*67e74705SXin Li /// property-ivar
2332*67e74705SXin Li /// property-ivar-list ',' property-ivar
2333*67e74705SXin Li ///
2334*67e74705SXin Li /// property-ivar:
2335*67e74705SXin Li /// identifier
2336*67e74705SXin Li /// identifier '=' identifier
2337*67e74705SXin Li ///
ParseObjCPropertySynthesize(SourceLocation atLoc)2338*67e74705SXin Li Decl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
2339*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
2340*67e74705SXin Li "ParseObjCPropertySynthesize(): Expected '@synthesize'");
2341*67e74705SXin Li ConsumeToken(); // consume synthesize
2342*67e74705SXin Li
2343*67e74705SXin Li while (true) {
2344*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2345*67e74705SXin Li Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
2346*67e74705SXin Li cutOffParsing();
2347*67e74705SXin Li return nullptr;
2348*67e74705SXin Li }
2349*67e74705SXin Li
2350*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2351*67e74705SXin Li Diag(Tok, diag::err_synthesized_property_name);
2352*67e74705SXin Li SkipUntil(tok::semi);
2353*67e74705SXin Li return nullptr;
2354*67e74705SXin Li }
2355*67e74705SXin Li
2356*67e74705SXin Li IdentifierInfo *propertyIvar = nullptr;
2357*67e74705SXin Li IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2358*67e74705SXin Li SourceLocation propertyLoc = ConsumeToken(); // consume property name
2359*67e74705SXin Li SourceLocation propertyIvarLoc;
2360*67e74705SXin Li if (TryConsumeToken(tok::equal)) {
2361*67e74705SXin Li // property '=' ivar-name
2362*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2363*67e74705SXin Li Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
2364*67e74705SXin Li cutOffParsing();
2365*67e74705SXin Li return nullptr;
2366*67e74705SXin Li }
2367*67e74705SXin Li
2368*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2369*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
2370*67e74705SXin Li break;
2371*67e74705SXin Li }
2372*67e74705SXin Li propertyIvar = Tok.getIdentifierInfo();
2373*67e74705SXin Li propertyIvarLoc = ConsumeToken(); // consume ivar-name
2374*67e74705SXin Li }
2375*67e74705SXin Li Actions.ActOnPropertyImplDecl(
2376*67e74705SXin Li getCurScope(), atLoc, propertyLoc, true,
2377*67e74705SXin Li propertyId, propertyIvar, propertyIvarLoc,
2378*67e74705SXin Li ObjCPropertyQueryKind::OBJC_PR_query_unknown);
2379*67e74705SXin Li if (Tok.isNot(tok::comma))
2380*67e74705SXin Li break;
2381*67e74705SXin Li ConsumeToken(); // consume ','
2382*67e74705SXin Li }
2383*67e74705SXin Li ExpectAndConsume(tok::semi, diag::err_expected_after, "@synthesize");
2384*67e74705SXin Li return nullptr;
2385*67e74705SXin Li }
2386*67e74705SXin Li
2387*67e74705SXin Li /// property-dynamic:
2388*67e74705SXin Li /// @dynamic property-list
2389*67e74705SXin Li ///
2390*67e74705SXin Li /// property-list:
2391*67e74705SXin Li /// identifier
2392*67e74705SXin Li /// property-list ',' identifier
2393*67e74705SXin Li ///
ParseObjCPropertyDynamic(SourceLocation atLoc)2394*67e74705SXin Li Decl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
2395*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
2396*67e74705SXin Li "ParseObjCPropertyDynamic(): Expected '@dynamic'");
2397*67e74705SXin Li ConsumeToken(); // consume dynamic
2398*67e74705SXin Li
2399*67e74705SXin Li bool isClassProperty = false;
2400*67e74705SXin Li if (Tok.is(tok::l_paren)) {
2401*67e74705SXin Li ConsumeParen();
2402*67e74705SXin Li const IdentifierInfo *II = Tok.getIdentifierInfo();
2403*67e74705SXin Li
2404*67e74705SXin Li if (!II) {
2405*67e74705SXin Li Diag(Tok, diag::err_objc_expected_property_attr) << II;
2406*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2407*67e74705SXin Li } else {
2408*67e74705SXin Li SourceLocation AttrName = ConsumeToken(); // consume attribute name
2409*67e74705SXin Li if (II->isStr("class")) {
2410*67e74705SXin Li isClassProperty = true;
2411*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
2412*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::r_paren;
2413*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2414*67e74705SXin Li } else
2415*67e74705SXin Li ConsumeParen();
2416*67e74705SXin Li } else {
2417*67e74705SXin Li Diag(AttrName, diag::err_objc_expected_property_attr) << II;
2418*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2419*67e74705SXin Li }
2420*67e74705SXin Li }
2421*67e74705SXin Li }
2422*67e74705SXin Li
2423*67e74705SXin Li while (true) {
2424*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2425*67e74705SXin Li Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
2426*67e74705SXin Li cutOffParsing();
2427*67e74705SXin Li return nullptr;
2428*67e74705SXin Li }
2429*67e74705SXin Li
2430*67e74705SXin Li if (Tok.isNot(tok::identifier)) {
2431*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier;
2432*67e74705SXin Li SkipUntil(tok::semi);
2433*67e74705SXin Li return nullptr;
2434*67e74705SXin Li }
2435*67e74705SXin Li
2436*67e74705SXin Li IdentifierInfo *propertyId = Tok.getIdentifierInfo();
2437*67e74705SXin Li SourceLocation propertyLoc = ConsumeToken(); // consume property name
2438*67e74705SXin Li Actions.ActOnPropertyImplDecl(
2439*67e74705SXin Li getCurScope(), atLoc, propertyLoc, false,
2440*67e74705SXin Li propertyId, nullptr, SourceLocation(),
2441*67e74705SXin Li isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
2442*67e74705SXin Li ObjCPropertyQueryKind::OBJC_PR_query_unknown);
2443*67e74705SXin Li
2444*67e74705SXin Li if (Tok.isNot(tok::comma))
2445*67e74705SXin Li break;
2446*67e74705SXin Li ConsumeToken(); // consume ','
2447*67e74705SXin Li }
2448*67e74705SXin Li ExpectAndConsume(tok::semi, diag::err_expected_after, "@dynamic");
2449*67e74705SXin Li return nullptr;
2450*67e74705SXin Li }
2451*67e74705SXin Li
2452*67e74705SXin Li /// objc-throw-statement:
2453*67e74705SXin Li /// throw expression[opt];
2454*67e74705SXin Li ///
ParseObjCThrowStmt(SourceLocation atLoc)2455*67e74705SXin Li StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
2456*67e74705SXin Li ExprResult Res;
2457*67e74705SXin Li ConsumeToken(); // consume throw
2458*67e74705SXin Li if (Tok.isNot(tok::semi)) {
2459*67e74705SXin Li Res = ParseExpression();
2460*67e74705SXin Li if (Res.isInvalid()) {
2461*67e74705SXin Li SkipUntil(tok::semi);
2462*67e74705SXin Li return StmtError();
2463*67e74705SXin Li }
2464*67e74705SXin Li }
2465*67e74705SXin Li // consume ';'
2466*67e74705SXin Li ExpectAndConsume(tok::semi, diag::err_expected_after, "@throw");
2467*67e74705SXin Li return Actions.ActOnObjCAtThrowStmt(atLoc, Res.get(), getCurScope());
2468*67e74705SXin Li }
2469*67e74705SXin Li
2470*67e74705SXin Li /// objc-synchronized-statement:
2471*67e74705SXin Li /// @synchronized '(' expression ')' compound-statement
2472*67e74705SXin Li ///
2473*67e74705SXin Li StmtResult
ParseObjCSynchronizedStmt(SourceLocation atLoc)2474*67e74705SXin Li Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
2475*67e74705SXin Li ConsumeToken(); // consume synchronized
2476*67e74705SXin Li if (Tok.isNot(tok::l_paren)) {
2477*67e74705SXin Li Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
2478*67e74705SXin Li return StmtError();
2479*67e74705SXin Li }
2480*67e74705SXin Li
2481*67e74705SXin Li // The operand is surrounded with parentheses.
2482*67e74705SXin Li ConsumeParen(); // '('
2483*67e74705SXin Li ExprResult operand(ParseExpression());
2484*67e74705SXin Li
2485*67e74705SXin Li if (Tok.is(tok::r_paren)) {
2486*67e74705SXin Li ConsumeParen(); // ')'
2487*67e74705SXin Li } else {
2488*67e74705SXin Li if (!operand.isInvalid())
2489*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::r_paren;
2490*67e74705SXin Li
2491*67e74705SXin Li // Skip forward until we see a left brace, but don't consume it.
2492*67e74705SXin Li SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
2493*67e74705SXin Li }
2494*67e74705SXin Li
2495*67e74705SXin Li // Require a compound statement.
2496*67e74705SXin Li if (Tok.isNot(tok::l_brace)) {
2497*67e74705SXin Li if (!operand.isInvalid())
2498*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::l_brace;
2499*67e74705SXin Li return StmtError();
2500*67e74705SXin Li }
2501*67e74705SXin Li
2502*67e74705SXin Li // Check the @synchronized operand now.
2503*67e74705SXin Li if (!operand.isInvalid())
2504*67e74705SXin Li operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.get());
2505*67e74705SXin Li
2506*67e74705SXin Li // Parse the compound statement within a new scope.
2507*67e74705SXin Li ParseScope bodyScope(this, Scope::DeclScope);
2508*67e74705SXin Li StmtResult body(ParseCompoundStatementBody());
2509*67e74705SXin Li bodyScope.Exit();
2510*67e74705SXin Li
2511*67e74705SXin Li // If there was a semantic or parse error earlier with the
2512*67e74705SXin Li // operand, fail now.
2513*67e74705SXin Li if (operand.isInvalid())
2514*67e74705SXin Li return StmtError();
2515*67e74705SXin Li
2516*67e74705SXin Li if (body.isInvalid())
2517*67e74705SXin Li body = Actions.ActOnNullStmt(Tok.getLocation());
2518*67e74705SXin Li
2519*67e74705SXin Li return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
2520*67e74705SXin Li }
2521*67e74705SXin Li
2522*67e74705SXin Li /// objc-try-catch-statement:
2523*67e74705SXin Li /// @try compound-statement objc-catch-list[opt]
2524*67e74705SXin Li /// @try compound-statement objc-catch-list[opt] @finally compound-statement
2525*67e74705SXin Li ///
2526*67e74705SXin Li /// objc-catch-list:
2527*67e74705SXin Li /// @catch ( parameter-declaration ) compound-statement
2528*67e74705SXin Li /// objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
2529*67e74705SXin Li /// catch-parameter-declaration:
2530*67e74705SXin Li /// parameter-declaration
2531*67e74705SXin Li /// '...' [OBJC2]
2532*67e74705SXin Li ///
ParseObjCTryStmt(SourceLocation atLoc)2533*67e74705SXin Li StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
2534*67e74705SXin Li bool catch_or_finally_seen = false;
2535*67e74705SXin Li
2536*67e74705SXin Li ConsumeToken(); // consume try
2537*67e74705SXin Li if (Tok.isNot(tok::l_brace)) {
2538*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::l_brace;
2539*67e74705SXin Li return StmtError();
2540*67e74705SXin Li }
2541*67e74705SXin Li StmtVector CatchStmts;
2542*67e74705SXin Li StmtResult FinallyStmt;
2543*67e74705SXin Li ParseScope TryScope(this, Scope::DeclScope);
2544*67e74705SXin Li StmtResult TryBody(ParseCompoundStatementBody());
2545*67e74705SXin Li TryScope.Exit();
2546*67e74705SXin Li if (TryBody.isInvalid())
2547*67e74705SXin Li TryBody = Actions.ActOnNullStmt(Tok.getLocation());
2548*67e74705SXin Li
2549*67e74705SXin Li while (Tok.is(tok::at)) {
2550*67e74705SXin Li // At this point, we need to lookahead to determine if this @ is the start
2551*67e74705SXin Li // of an @catch or @finally. We don't want to consume the @ token if this
2552*67e74705SXin Li // is an @try or @encode or something else.
2553*67e74705SXin Li Token AfterAt = GetLookAheadToken(1);
2554*67e74705SXin Li if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
2555*67e74705SXin Li !AfterAt.isObjCAtKeyword(tok::objc_finally))
2556*67e74705SXin Li break;
2557*67e74705SXin Li
2558*67e74705SXin Li SourceLocation AtCatchFinallyLoc = ConsumeToken();
2559*67e74705SXin Li if (Tok.isObjCAtKeyword(tok::objc_catch)) {
2560*67e74705SXin Li Decl *FirstPart = nullptr;
2561*67e74705SXin Li ConsumeToken(); // consume catch
2562*67e74705SXin Li if (Tok.is(tok::l_paren)) {
2563*67e74705SXin Li ConsumeParen();
2564*67e74705SXin Li ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
2565*67e74705SXin Li if (Tok.isNot(tok::ellipsis)) {
2566*67e74705SXin Li DeclSpec DS(AttrFactory);
2567*67e74705SXin Li ParseDeclarationSpecifiers(DS);
2568*67e74705SXin Li Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
2569*67e74705SXin Li ParseDeclarator(ParmDecl);
2570*67e74705SXin Li
2571*67e74705SXin Li // Inform the actions module about the declarator, so it
2572*67e74705SXin Li // gets added to the current scope.
2573*67e74705SXin Li FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
2574*67e74705SXin Li } else
2575*67e74705SXin Li ConsumeToken(); // consume '...'
2576*67e74705SXin Li
2577*67e74705SXin Li SourceLocation RParenLoc;
2578*67e74705SXin Li
2579*67e74705SXin Li if (Tok.is(tok::r_paren))
2580*67e74705SXin Li RParenLoc = ConsumeParen();
2581*67e74705SXin Li else // Skip over garbage, until we get to ')'. Eat the ')'.
2582*67e74705SXin Li SkipUntil(tok::r_paren, StopAtSemi);
2583*67e74705SXin Li
2584*67e74705SXin Li StmtResult CatchBody(true);
2585*67e74705SXin Li if (Tok.is(tok::l_brace))
2586*67e74705SXin Li CatchBody = ParseCompoundStatementBody();
2587*67e74705SXin Li else
2588*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::l_brace;
2589*67e74705SXin Li if (CatchBody.isInvalid())
2590*67e74705SXin Li CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
2591*67e74705SXin Li
2592*67e74705SXin Li StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
2593*67e74705SXin Li RParenLoc,
2594*67e74705SXin Li FirstPart,
2595*67e74705SXin Li CatchBody.get());
2596*67e74705SXin Li if (!Catch.isInvalid())
2597*67e74705SXin Li CatchStmts.push_back(Catch.get());
2598*67e74705SXin Li
2599*67e74705SXin Li } else {
2600*67e74705SXin Li Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
2601*67e74705SXin Li << "@catch clause";
2602*67e74705SXin Li return StmtError();
2603*67e74705SXin Li }
2604*67e74705SXin Li catch_or_finally_seen = true;
2605*67e74705SXin Li } else {
2606*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
2607*67e74705SXin Li ConsumeToken(); // consume finally
2608*67e74705SXin Li ParseScope FinallyScope(this, Scope::DeclScope);
2609*67e74705SXin Li
2610*67e74705SXin Li StmtResult FinallyBody(true);
2611*67e74705SXin Li if (Tok.is(tok::l_brace))
2612*67e74705SXin Li FinallyBody = ParseCompoundStatementBody();
2613*67e74705SXin Li else
2614*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::l_brace;
2615*67e74705SXin Li if (FinallyBody.isInvalid())
2616*67e74705SXin Li FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
2617*67e74705SXin Li FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
2618*67e74705SXin Li FinallyBody.get());
2619*67e74705SXin Li catch_or_finally_seen = true;
2620*67e74705SXin Li break;
2621*67e74705SXin Li }
2622*67e74705SXin Li }
2623*67e74705SXin Li if (!catch_or_finally_seen) {
2624*67e74705SXin Li Diag(atLoc, diag::err_missing_catch_finally);
2625*67e74705SXin Li return StmtError();
2626*67e74705SXin Li }
2627*67e74705SXin Li
2628*67e74705SXin Li return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.get(),
2629*67e74705SXin Li CatchStmts,
2630*67e74705SXin Li FinallyStmt.get());
2631*67e74705SXin Li }
2632*67e74705SXin Li
2633*67e74705SXin Li /// objc-autoreleasepool-statement:
2634*67e74705SXin Li /// @autoreleasepool compound-statement
2635*67e74705SXin Li ///
2636*67e74705SXin Li StmtResult
ParseObjCAutoreleasePoolStmt(SourceLocation atLoc)2637*67e74705SXin Li Parser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
2638*67e74705SXin Li ConsumeToken(); // consume autoreleasepool
2639*67e74705SXin Li if (Tok.isNot(tok::l_brace)) {
2640*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::l_brace;
2641*67e74705SXin Li return StmtError();
2642*67e74705SXin Li }
2643*67e74705SXin Li // Enter a scope to hold everything within the compound stmt. Compound
2644*67e74705SXin Li // statements can always hold declarations.
2645*67e74705SXin Li ParseScope BodyScope(this, Scope::DeclScope);
2646*67e74705SXin Li
2647*67e74705SXin Li StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
2648*67e74705SXin Li
2649*67e74705SXin Li BodyScope.Exit();
2650*67e74705SXin Li if (AutoreleasePoolBody.isInvalid())
2651*67e74705SXin Li AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
2652*67e74705SXin Li return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
2653*67e74705SXin Li AutoreleasePoolBody.get());
2654*67e74705SXin Li }
2655*67e74705SXin Li
2656*67e74705SXin Li /// StashAwayMethodOrFunctionBodyTokens - Consume the tokens and store them
2657*67e74705SXin Li /// for later parsing.
StashAwayMethodOrFunctionBodyTokens(Decl * MDecl)2658*67e74705SXin Li void Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
2659*67e74705SXin Li if (SkipFunctionBodies && (!MDecl || Actions.canSkipFunctionBody(MDecl)) &&
2660*67e74705SXin Li trySkippingFunctionBody()) {
2661*67e74705SXin Li Actions.ActOnSkippedFunctionBody(MDecl);
2662*67e74705SXin Li return;
2663*67e74705SXin Li }
2664*67e74705SXin Li
2665*67e74705SXin Li LexedMethod* LM = new LexedMethod(this, MDecl);
2666*67e74705SXin Li CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
2667*67e74705SXin Li CachedTokens &Toks = LM->Toks;
2668*67e74705SXin Li // Begin by storing the '{' or 'try' or ':' token.
2669*67e74705SXin Li Toks.push_back(Tok);
2670*67e74705SXin Li if (Tok.is(tok::kw_try)) {
2671*67e74705SXin Li ConsumeToken();
2672*67e74705SXin Li if (Tok.is(tok::colon)) {
2673*67e74705SXin Li Toks.push_back(Tok);
2674*67e74705SXin Li ConsumeToken();
2675*67e74705SXin Li while (Tok.isNot(tok::l_brace)) {
2676*67e74705SXin Li ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2677*67e74705SXin Li ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2678*67e74705SXin Li }
2679*67e74705SXin Li }
2680*67e74705SXin Li Toks.push_back(Tok); // also store '{'
2681*67e74705SXin Li }
2682*67e74705SXin Li else if (Tok.is(tok::colon)) {
2683*67e74705SXin Li ConsumeToken();
2684*67e74705SXin Li // FIXME: This is wrong, due to C++11 braced initialization.
2685*67e74705SXin Li while (Tok.isNot(tok::l_brace)) {
2686*67e74705SXin Li ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
2687*67e74705SXin Li ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
2688*67e74705SXin Li }
2689*67e74705SXin Li Toks.push_back(Tok); // also store '{'
2690*67e74705SXin Li }
2691*67e74705SXin Li ConsumeBrace();
2692*67e74705SXin Li // Consume everything up to (and including) the matching right brace.
2693*67e74705SXin Li ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2694*67e74705SXin Li while (Tok.is(tok::kw_catch)) {
2695*67e74705SXin Li ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
2696*67e74705SXin Li ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
2697*67e74705SXin Li }
2698*67e74705SXin Li }
2699*67e74705SXin Li
2700*67e74705SXin Li /// objc-method-def: objc-method-proto ';'[opt] '{' body '}'
2701*67e74705SXin Li ///
ParseObjCMethodDefinition()2702*67e74705SXin Li Decl *Parser::ParseObjCMethodDefinition() {
2703*67e74705SXin Li Decl *MDecl = ParseObjCMethodPrototype();
2704*67e74705SXin Li
2705*67e74705SXin Li PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2706*67e74705SXin Li "parsing Objective-C method");
2707*67e74705SXin Li
2708*67e74705SXin Li // parse optional ';'
2709*67e74705SXin Li if (Tok.is(tok::semi)) {
2710*67e74705SXin Li if (CurParsedObjCImpl) {
2711*67e74705SXin Li Diag(Tok, diag::warn_semicolon_before_method_body)
2712*67e74705SXin Li << FixItHint::CreateRemoval(Tok.getLocation());
2713*67e74705SXin Li }
2714*67e74705SXin Li ConsumeToken();
2715*67e74705SXin Li }
2716*67e74705SXin Li
2717*67e74705SXin Li // We should have an opening brace now.
2718*67e74705SXin Li if (Tok.isNot(tok::l_brace)) {
2719*67e74705SXin Li Diag(Tok, diag::err_expected_method_body);
2720*67e74705SXin Li
2721*67e74705SXin Li // Skip over garbage, until we get to '{'. Don't eat the '{'.
2722*67e74705SXin Li SkipUntil(tok::l_brace, StopAtSemi | StopBeforeMatch);
2723*67e74705SXin Li
2724*67e74705SXin Li // If we didn't find the '{', bail out.
2725*67e74705SXin Li if (Tok.isNot(tok::l_brace))
2726*67e74705SXin Li return nullptr;
2727*67e74705SXin Li }
2728*67e74705SXin Li
2729*67e74705SXin Li if (!MDecl) {
2730*67e74705SXin Li ConsumeBrace();
2731*67e74705SXin Li SkipUntil(tok::r_brace);
2732*67e74705SXin Li return nullptr;
2733*67e74705SXin Li }
2734*67e74705SXin Li
2735*67e74705SXin Li // Allow the rest of sema to find private method decl implementations.
2736*67e74705SXin Li Actions.AddAnyMethodToGlobalPool(MDecl);
2737*67e74705SXin Li assert (CurParsedObjCImpl
2738*67e74705SXin Li && "ParseObjCMethodDefinition - Method out of @implementation");
2739*67e74705SXin Li // Consume the tokens and store them for later parsing.
2740*67e74705SXin Li StashAwayMethodOrFunctionBodyTokens(MDecl);
2741*67e74705SXin Li return MDecl;
2742*67e74705SXin Li }
2743*67e74705SXin Li
ParseObjCAtStatement(SourceLocation AtLoc)2744*67e74705SXin Li StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
2745*67e74705SXin Li if (Tok.is(tok::code_completion)) {
2746*67e74705SXin Li Actions.CodeCompleteObjCAtStatement(getCurScope());
2747*67e74705SXin Li cutOffParsing();
2748*67e74705SXin Li return StmtError();
2749*67e74705SXin Li }
2750*67e74705SXin Li
2751*67e74705SXin Li if (Tok.isObjCAtKeyword(tok::objc_try))
2752*67e74705SXin Li return ParseObjCTryStmt(AtLoc);
2753*67e74705SXin Li
2754*67e74705SXin Li if (Tok.isObjCAtKeyword(tok::objc_throw))
2755*67e74705SXin Li return ParseObjCThrowStmt(AtLoc);
2756*67e74705SXin Li
2757*67e74705SXin Li if (Tok.isObjCAtKeyword(tok::objc_synchronized))
2758*67e74705SXin Li return ParseObjCSynchronizedStmt(AtLoc);
2759*67e74705SXin Li
2760*67e74705SXin Li if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2761*67e74705SXin Li return ParseObjCAutoreleasePoolStmt(AtLoc);
2762*67e74705SXin Li
2763*67e74705SXin Li if (Tok.isObjCAtKeyword(tok::objc_import) &&
2764*67e74705SXin Li getLangOpts().DebuggerSupport) {
2765*67e74705SXin Li SkipUntil(tok::semi);
2766*67e74705SXin Li return Actions.ActOnNullStmt(Tok.getLocation());
2767*67e74705SXin Li }
2768*67e74705SXin Li
2769*67e74705SXin Li ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
2770*67e74705SXin Li if (Res.isInvalid()) {
2771*67e74705SXin Li // If the expression is invalid, skip ahead to the next semicolon. Not
2772*67e74705SXin Li // doing this opens us up to the possibility of infinite loops if
2773*67e74705SXin Li // ParseExpression does not consume any tokens.
2774*67e74705SXin Li SkipUntil(tok::semi);
2775*67e74705SXin Li return StmtError();
2776*67e74705SXin Li }
2777*67e74705SXin Li
2778*67e74705SXin Li // Otherwise, eat the semicolon.
2779*67e74705SXin Li ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
2780*67e74705SXin Li return Actions.ActOnExprStmt(Res);
2781*67e74705SXin Li }
2782*67e74705SXin Li
ParseObjCAtExpression(SourceLocation AtLoc)2783*67e74705SXin Li ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
2784*67e74705SXin Li switch (Tok.getKind()) {
2785*67e74705SXin Li case tok::code_completion:
2786*67e74705SXin Li Actions.CodeCompleteObjCAtExpression(getCurScope());
2787*67e74705SXin Li cutOffParsing();
2788*67e74705SXin Li return ExprError();
2789*67e74705SXin Li
2790*67e74705SXin Li case tok::minus:
2791*67e74705SXin Li case tok::plus: {
2792*67e74705SXin Li tok::TokenKind Kind = Tok.getKind();
2793*67e74705SXin Li SourceLocation OpLoc = ConsumeToken();
2794*67e74705SXin Li
2795*67e74705SXin Li if (!Tok.is(tok::numeric_constant)) {
2796*67e74705SXin Li const char *Symbol = nullptr;
2797*67e74705SXin Li switch (Kind) {
2798*67e74705SXin Li case tok::minus: Symbol = "-"; break;
2799*67e74705SXin Li case tok::plus: Symbol = "+"; break;
2800*67e74705SXin Li default: llvm_unreachable("missing unary operator case");
2801*67e74705SXin Li }
2802*67e74705SXin Li Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2803*67e74705SXin Li << Symbol;
2804*67e74705SXin Li return ExprError();
2805*67e74705SXin Li }
2806*67e74705SXin Li
2807*67e74705SXin Li ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2808*67e74705SXin Li if (Lit.isInvalid()) {
2809*67e74705SXin Li return Lit;
2810*67e74705SXin Li }
2811*67e74705SXin Li ConsumeToken(); // Consume the literal token.
2812*67e74705SXin Li
2813*67e74705SXin Li Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.get());
2814*67e74705SXin Li if (Lit.isInvalid())
2815*67e74705SXin Li return Lit;
2816*67e74705SXin Li
2817*67e74705SXin Li return ParsePostfixExpressionSuffix(
2818*67e74705SXin Li Actions.BuildObjCNumericLiteral(AtLoc, Lit.get()));
2819*67e74705SXin Li }
2820*67e74705SXin Li
2821*67e74705SXin Li case tok::string_literal: // primary-expression: string-literal
2822*67e74705SXin Li case tok::wide_string_literal:
2823*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
2824*67e74705SXin Li
2825*67e74705SXin Li case tok::char_constant:
2826*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2827*67e74705SXin Li
2828*67e74705SXin Li case tok::numeric_constant:
2829*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2830*67e74705SXin Li
2831*67e74705SXin Li case tok::kw_true: // Objective-C++, etc.
2832*67e74705SXin Li case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2833*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2834*67e74705SXin Li case tok::kw_false: // Objective-C++, etc.
2835*67e74705SXin Li case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2836*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2837*67e74705SXin Li
2838*67e74705SXin Li case tok::l_square:
2839*67e74705SXin Li // Objective-C array literal
2840*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2841*67e74705SXin Li
2842*67e74705SXin Li case tok::l_brace:
2843*67e74705SXin Li // Objective-C dictionary literal
2844*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2845*67e74705SXin Li
2846*67e74705SXin Li case tok::l_paren:
2847*67e74705SXin Li // Objective-C boxed expression
2848*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2849*67e74705SXin Li
2850*67e74705SXin Li default:
2851*67e74705SXin Li if (Tok.getIdentifierInfo() == nullptr)
2852*67e74705SXin Li return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2853*67e74705SXin Li
2854*67e74705SXin Li switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
2855*67e74705SXin Li case tok::objc_encode:
2856*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
2857*67e74705SXin Li case tok::objc_protocol:
2858*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
2859*67e74705SXin Li case tok::objc_selector:
2860*67e74705SXin Li return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
2861*67e74705SXin Li default: {
2862*67e74705SXin Li const char *str = nullptr;
2863*67e74705SXin Li if (GetLookAheadToken(1).is(tok::l_brace)) {
2864*67e74705SXin Li char ch = Tok.getIdentifierInfo()->getNameStart()[0];
2865*67e74705SXin Li str =
2866*67e74705SXin Li ch == 't' ? "try"
2867*67e74705SXin Li : (ch == 'f' ? "finally"
2868*67e74705SXin Li : (ch == 'a' ? "autoreleasepool" : nullptr));
2869*67e74705SXin Li }
2870*67e74705SXin Li if (str) {
2871*67e74705SXin Li SourceLocation kwLoc = Tok.getLocation();
2872*67e74705SXin Li return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
2873*67e74705SXin Li FixItHint::CreateReplacement(kwLoc, str));
2874*67e74705SXin Li }
2875*67e74705SXin Li else
2876*67e74705SXin Li return ExprError(Diag(AtLoc, diag::err_unexpected_at));
2877*67e74705SXin Li }
2878*67e74705SXin Li }
2879*67e74705SXin Li }
2880*67e74705SXin Li }
2881*67e74705SXin Li
2882*67e74705SXin Li /// \brief Parse the receiver of an Objective-C++ message send.
2883*67e74705SXin Li ///
2884*67e74705SXin Li /// This routine parses the receiver of a message send in
2885*67e74705SXin Li /// Objective-C++ either as a type or as an expression. Note that this
2886*67e74705SXin Li /// routine must not be called to parse a send to 'super', since it
2887*67e74705SXin Li /// has no way to return such a result.
2888*67e74705SXin Li ///
2889*67e74705SXin Li /// \param IsExpr Whether the receiver was parsed as an expression.
2890*67e74705SXin Li ///
2891*67e74705SXin Li /// \param TypeOrExpr If the receiver was parsed as an expression (\c
2892*67e74705SXin Li /// IsExpr is true), the parsed expression. If the receiver was parsed
2893*67e74705SXin Li /// as a type (\c IsExpr is false), the parsed type.
2894*67e74705SXin Li ///
2895*67e74705SXin Li /// \returns True if an error occurred during parsing or semantic
2896*67e74705SXin Li /// analysis, in which case the arguments do not have valid
2897*67e74705SXin Li /// values. Otherwise, returns false for a successful parse.
2898*67e74705SXin Li ///
2899*67e74705SXin Li /// objc-receiver: [C++]
2900*67e74705SXin Li /// 'super' [not parsed here]
2901*67e74705SXin Li /// expression
2902*67e74705SXin Li /// simple-type-specifier
2903*67e74705SXin Li /// typename-specifier
ParseObjCXXMessageReceiver(bool & IsExpr,void * & TypeOrExpr)2904*67e74705SXin Li bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
2905*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, true);
2906*67e74705SXin Li
2907*67e74705SXin Li if (Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_typename,
2908*67e74705SXin Li tok::annot_cxxscope))
2909*67e74705SXin Li TryAnnotateTypeOrScopeToken();
2910*67e74705SXin Li
2911*67e74705SXin Li if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
2912*67e74705SXin Li // objc-receiver:
2913*67e74705SXin Li // expression
2914*67e74705SXin Li // Make sure any typos in the receiver are corrected or diagnosed, so that
2915*67e74705SXin Li // proper recovery can happen. FIXME: Perhaps filter the corrected expr to
2916*67e74705SXin Li // only the things that are valid ObjC receivers?
2917*67e74705SXin Li ExprResult Receiver = Actions.CorrectDelayedTyposInExpr(ParseExpression());
2918*67e74705SXin Li if (Receiver.isInvalid())
2919*67e74705SXin Li return true;
2920*67e74705SXin Li
2921*67e74705SXin Li IsExpr = true;
2922*67e74705SXin Li TypeOrExpr = Receiver.get();
2923*67e74705SXin Li return false;
2924*67e74705SXin Li }
2925*67e74705SXin Li
2926*67e74705SXin Li // objc-receiver:
2927*67e74705SXin Li // typename-specifier
2928*67e74705SXin Li // simple-type-specifier
2929*67e74705SXin Li // expression (that starts with one of the above)
2930*67e74705SXin Li DeclSpec DS(AttrFactory);
2931*67e74705SXin Li ParseCXXSimpleTypeSpecifier(DS);
2932*67e74705SXin Li
2933*67e74705SXin Li if (Tok.is(tok::l_paren)) {
2934*67e74705SXin Li // If we see an opening parentheses at this point, we are
2935*67e74705SXin Li // actually parsing an expression that starts with a
2936*67e74705SXin Li // function-style cast, e.g.,
2937*67e74705SXin Li //
2938*67e74705SXin Li // postfix-expression:
2939*67e74705SXin Li // simple-type-specifier ( expression-list [opt] )
2940*67e74705SXin Li // typename-specifier ( expression-list [opt] )
2941*67e74705SXin Li //
2942*67e74705SXin Li // Parse the remainder of this case, then the (optional)
2943*67e74705SXin Li // postfix-expression suffix, followed by the (optional)
2944*67e74705SXin Li // right-hand side of the binary expression. We have an
2945*67e74705SXin Li // instance method.
2946*67e74705SXin Li ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
2947*67e74705SXin Li if (!Receiver.isInvalid())
2948*67e74705SXin Li Receiver = ParsePostfixExpressionSuffix(Receiver.get());
2949*67e74705SXin Li if (!Receiver.isInvalid())
2950*67e74705SXin Li Receiver = ParseRHSOfBinaryExpression(Receiver.get(), prec::Comma);
2951*67e74705SXin Li if (Receiver.isInvalid())
2952*67e74705SXin Li return true;
2953*67e74705SXin Li
2954*67e74705SXin Li IsExpr = true;
2955*67e74705SXin Li TypeOrExpr = Receiver.get();
2956*67e74705SXin Li return false;
2957*67e74705SXin Li }
2958*67e74705SXin Li
2959*67e74705SXin Li // We have a class message. Turn the simple-type-specifier or
2960*67e74705SXin Li // typename-specifier we parsed into a type and parse the
2961*67e74705SXin Li // remainder of the class message.
2962*67e74705SXin Li Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
2963*67e74705SXin Li TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
2964*67e74705SXin Li if (Type.isInvalid())
2965*67e74705SXin Li return true;
2966*67e74705SXin Li
2967*67e74705SXin Li IsExpr = false;
2968*67e74705SXin Li TypeOrExpr = Type.get().getAsOpaquePtr();
2969*67e74705SXin Li return false;
2970*67e74705SXin Li }
2971*67e74705SXin Li
2972*67e74705SXin Li /// \brief Determine whether the parser is currently referring to a an
2973*67e74705SXin Li /// Objective-C message send, using a simplified heuristic to avoid overhead.
2974*67e74705SXin Li ///
2975*67e74705SXin Li /// This routine will only return true for a subset of valid message-send
2976*67e74705SXin Li /// expressions.
isSimpleObjCMessageExpression()2977*67e74705SXin Li bool Parser::isSimpleObjCMessageExpression() {
2978*67e74705SXin Li assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
2979*67e74705SXin Li "Incorrect start for isSimpleObjCMessageExpression");
2980*67e74705SXin Li return GetLookAheadToken(1).is(tok::identifier) &&
2981*67e74705SXin Li GetLookAheadToken(2).is(tok::identifier);
2982*67e74705SXin Li }
2983*67e74705SXin Li
isStartOfObjCClassMessageMissingOpenBracket()2984*67e74705SXin Li bool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
2985*67e74705SXin Li if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
2986*67e74705SXin Li InMessageExpression)
2987*67e74705SXin Li return false;
2988*67e74705SXin Li
2989*67e74705SXin Li ParsedType Type;
2990*67e74705SXin Li
2991*67e74705SXin Li if (Tok.is(tok::annot_typename))
2992*67e74705SXin Li Type = getTypeAnnotation(Tok);
2993*67e74705SXin Li else if (Tok.is(tok::identifier))
2994*67e74705SXin Li Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
2995*67e74705SXin Li getCurScope());
2996*67e74705SXin Li else
2997*67e74705SXin Li return false;
2998*67e74705SXin Li
2999*67e74705SXin Li if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
3000*67e74705SXin Li const Token &AfterNext = GetLookAheadToken(2);
3001*67e74705SXin Li if (AfterNext.isOneOf(tok::colon, tok::r_square)) {
3002*67e74705SXin Li if (Tok.is(tok::identifier))
3003*67e74705SXin Li TryAnnotateTypeOrScopeToken();
3004*67e74705SXin Li
3005*67e74705SXin Li return Tok.is(tok::annot_typename);
3006*67e74705SXin Li }
3007*67e74705SXin Li }
3008*67e74705SXin Li
3009*67e74705SXin Li return false;
3010*67e74705SXin Li }
3011*67e74705SXin Li
3012*67e74705SXin Li /// objc-message-expr:
3013*67e74705SXin Li /// '[' objc-receiver objc-message-args ']'
3014*67e74705SXin Li ///
3015*67e74705SXin Li /// objc-receiver: [C]
3016*67e74705SXin Li /// 'super'
3017*67e74705SXin Li /// expression
3018*67e74705SXin Li /// class-name
3019*67e74705SXin Li /// type-name
3020*67e74705SXin Li ///
ParseObjCMessageExpression()3021*67e74705SXin Li ExprResult Parser::ParseObjCMessageExpression() {
3022*67e74705SXin Li assert(Tok.is(tok::l_square) && "'[' expected");
3023*67e74705SXin Li SourceLocation LBracLoc = ConsumeBracket(); // consume '['
3024*67e74705SXin Li
3025*67e74705SXin Li if (Tok.is(tok::code_completion)) {
3026*67e74705SXin Li Actions.CodeCompleteObjCMessageReceiver(getCurScope());
3027*67e74705SXin Li cutOffParsing();
3028*67e74705SXin Li return ExprError();
3029*67e74705SXin Li }
3030*67e74705SXin Li
3031*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, true);
3032*67e74705SXin Li
3033*67e74705SXin Li if (getLangOpts().CPlusPlus) {
3034*67e74705SXin Li // We completely separate the C and C++ cases because C++ requires
3035*67e74705SXin Li // more complicated (read: slower) parsing.
3036*67e74705SXin Li
3037*67e74705SXin Li // Handle send to super.
3038*67e74705SXin Li // FIXME: This doesn't benefit from the same typo-correction we
3039*67e74705SXin Li // get in Objective-C.
3040*67e74705SXin Li if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
3041*67e74705SXin Li NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
3042*67e74705SXin Li return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3043*67e74705SXin Li nullptr);
3044*67e74705SXin Li
3045*67e74705SXin Li // Parse the receiver, which is either a type or an expression.
3046*67e74705SXin Li bool IsExpr;
3047*67e74705SXin Li void *TypeOrExpr = nullptr;
3048*67e74705SXin Li if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
3049*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3050*67e74705SXin Li return ExprError();
3051*67e74705SXin Li }
3052*67e74705SXin Li
3053*67e74705SXin Li if (IsExpr)
3054*67e74705SXin Li return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3055*67e74705SXin Li static_cast<Expr *>(TypeOrExpr));
3056*67e74705SXin Li
3057*67e74705SXin Li return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
3058*67e74705SXin Li ParsedType::getFromOpaquePtr(TypeOrExpr),
3059*67e74705SXin Li nullptr);
3060*67e74705SXin Li }
3061*67e74705SXin Li
3062*67e74705SXin Li if (Tok.is(tok::identifier)) {
3063*67e74705SXin Li IdentifierInfo *Name = Tok.getIdentifierInfo();
3064*67e74705SXin Li SourceLocation NameLoc = Tok.getLocation();
3065*67e74705SXin Li ParsedType ReceiverType;
3066*67e74705SXin Li switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
3067*67e74705SXin Li Name == Ident_super,
3068*67e74705SXin Li NextToken().is(tok::period),
3069*67e74705SXin Li ReceiverType)) {
3070*67e74705SXin Li case Sema::ObjCSuperMessage:
3071*67e74705SXin Li return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), nullptr,
3072*67e74705SXin Li nullptr);
3073*67e74705SXin Li
3074*67e74705SXin Li case Sema::ObjCClassMessage:
3075*67e74705SXin Li if (!ReceiverType) {
3076*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3077*67e74705SXin Li return ExprError();
3078*67e74705SXin Li }
3079*67e74705SXin Li
3080*67e74705SXin Li ConsumeToken(); // the type name
3081*67e74705SXin Li
3082*67e74705SXin Li // Parse type arguments and protocol qualifiers.
3083*67e74705SXin Li if (Tok.is(tok::less)) {
3084*67e74705SXin Li SourceLocation NewEndLoc;
3085*67e74705SXin Li TypeResult NewReceiverType
3086*67e74705SXin Li = parseObjCTypeArgsAndProtocolQualifiers(NameLoc, ReceiverType,
3087*67e74705SXin Li /*consumeLastToken=*/true,
3088*67e74705SXin Li NewEndLoc);
3089*67e74705SXin Li if (!NewReceiverType.isUsable()) {
3090*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3091*67e74705SXin Li return ExprError();
3092*67e74705SXin Li }
3093*67e74705SXin Li
3094*67e74705SXin Li ReceiverType = NewReceiverType.get();
3095*67e74705SXin Li }
3096*67e74705SXin Li
3097*67e74705SXin Li return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
3098*67e74705SXin Li ReceiverType, nullptr);
3099*67e74705SXin Li
3100*67e74705SXin Li case Sema::ObjCInstanceMessage:
3101*67e74705SXin Li // Fall through to parse an expression.
3102*67e74705SXin Li break;
3103*67e74705SXin Li }
3104*67e74705SXin Li }
3105*67e74705SXin Li
3106*67e74705SXin Li // Otherwise, an arbitrary expression can be the receiver of a send.
3107*67e74705SXin Li ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
3108*67e74705SXin Li if (Res.isInvalid()) {
3109*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3110*67e74705SXin Li return Res;
3111*67e74705SXin Li }
3112*67e74705SXin Li
3113*67e74705SXin Li return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), nullptr,
3114*67e74705SXin Li Res.get());
3115*67e74705SXin Li }
3116*67e74705SXin Li
3117*67e74705SXin Li /// \brief Parse the remainder of an Objective-C message following the
3118*67e74705SXin Li /// '[' objc-receiver.
3119*67e74705SXin Li ///
3120*67e74705SXin Li /// This routine handles sends to super, class messages (sent to a
3121*67e74705SXin Li /// class name), and instance messages (sent to an object), and the
3122*67e74705SXin Li /// target is represented by \p SuperLoc, \p ReceiverType, or \p
3123*67e74705SXin Li /// ReceiverExpr, respectively. Only one of these parameters may have
3124*67e74705SXin Li /// a valid value.
3125*67e74705SXin Li ///
3126*67e74705SXin Li /// \param LBracLoc The location of the opening '['.
3127*67e74705SXin Li ///
3128*67e74705SXin Li /// \param SuperLoc If this is a send to 'super', the location of the
3129*67e74705SXin Li /// 'super' keyword that indicates a send to the superclass.
3130*67e74705SXin Li ///
3131*67e74705SXin Li /// \param ReceiverType If this is a class message, the type of the
3132*67e74705SXin Li /// class we are sending a message to.
3133*67e74705SXin Li ///
3134*67e74705SXin Li /// \param ReceiverExpr If this is an instance message, the expression
3135*67e74705SXin Li /// used to compute the receiver object.
3136*67e74705SXin Li ///
3137*67e74705SXin Li /// objc-message-args:
3138*67e74705SXin Li /// objc-selector
3139*67e74705SXin Li /// objc-keywordarg-list
3140*67e74705SXin Li ///
3141*67e74705SXin Li /// objc-keywordarg-list:
3142*67e74705SXin Li /// objc-keywordarg
3143*67e74705SXin Li /// objc-keywordarg-list objc-keywordarg
3144*67e74705SXin Li ///
3145*67e74705SXin Li /// objc-keywordarg:
3146*67e74705SXin Li /// selector-name[opt] ':' objc-keywordexpr
3147*67e74705SXin Li ///
3148*67e74705SXin Li /// objc-keywordexpr:
3149*67e74705SXin Li /// nonempty-expr-list
3150*67e74705SXin Li ///
3151*67e74705SXin Li /// nonempty-expr-list:
3152*67e74705SXin Li /// assignment-expression
3153*67e74705SXin Li /// nonempty-expr-list , assignment-expression
3154*67e74705SXin Li ///
3155*67e74705SXin Li ExprResult
ParseObjCMessageExpressionBody(SourceLocation LBracLoc,SourceLocation SuperLoc,ParsedType ReceiverType,Expr * ReceiverExpr)3156*67e74705SXin Li Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
3157*67e74705SXin Li SourceLocation SuperLoc,
3158*67e74705SXin Li ParsedType ReceiverType,
3159*67e74705SXin Li Expr *ReceiverExpr) {
3160*67e74705SXin Li InMessageExpressionRAIIObject InMessage(*this, true);
3161*67e74705SXin Li
3162*67e74705SXin Li if (Tok.is(tok::code_completion)) {
3163*67e74705SXin Li if (SuperLoc.isValid())
3164*67e74705SXin Li Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, None,
3165*67e74705SXin Li false);
3166*67e74705SXin Li else if (ReceiverType)
3167*67e74705SXin Li Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, None,
3168*67e74705SXin Li false);
3169*67e74705SXin Li else
3170*67e74705SXin Li Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
3171*67e74705SXin Li None, false);
3172*67e74705SXin Li cutOffParsing();
3173*67e74705SXin Li return ExprError();
3174*67e74705SXin Li }
3175*67e74705SXin Li
3176*67e74705SXin Li // Parse objc-selector
3177*67e74705SXin Li SourceLocation Loc;
3178*67e74705SXin Li IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
3179*67e74705SXin Li
3180*67e74705SXin Li SmallVector<IdentifierInfo *, 12> KeyIdents;
3181*67e74705SXin Li SmallVector<SourceLocation, 12> KeyLocs;
3182*67e74705SXin Li ExprVector KeyExprs;
3183*67e74705SXin Li
3184*67e74705SXin Li if (Tok.is(tok::colon)) {
3185*67e74705SXin Li while (1) {
3186*67e74705SXin Li // Each iteration parses a single keyword argument.
3187*67e74705SXin Li KeyIdents.push_back(selIdent);
3188*67e74705SXin Li KeyLocs.push_back(Loc);
3189*67e74705SXin Li
3190*67e74705SXin Li if (ExpectAndConsume(tok::colon)) {
3191*67e74705SXin Li // We must manually skip to a ']', otherwise the expression skipper will
3192*67e74705SXin Li // stop at the ']' when it skips to the ';'. We want it to skip beyond
3193*67e74705SXin Li // the enclosing expression.
3194*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3195*67e74705SXin Li return ExprError();
3196*67e74705SXin Li }
3197*67e74705SXin Li
3198*67e74705SXin Li /// Parse the expression after ':'
3199*67e74705SXin Li
3200*67e74705SXin Li if (Tok.is(tok::code_completion)) {
3201*67e74705SXin Li if (SuperLoc.isValid())
3202*67e74705SXin Li Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
3203*67e74705SXin Li KeyIdents,
3204*67e74705SXin Li /*AtArgumentEpression=*/true);
3205*67e74705SXin Li else if (ReceiverType)
3206*67e74705SXin Li Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
3207*67e74705SXin Li KeyIdents,
3208*67e74705SXin Li /*AtArgumentEpression=*/true);
3209*67e74705SXin Li else
3210*67e74705SXin Li Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
3211*67e74705SXin Li KeyIdents,
3212*67e74705SXin Li /*AtArgumentEpression=*/true);
3213*67e74705SXin Li
3214*67e74705SXin Li cutOffParsing();
3215*67e74705SXin Li return ExprError();
3216*67e74705SXin Li }
3217*67e74705SXin Li
3218*67e74705SXin Li ExprResult Expr;
3219*67e74705SXin Li if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
3220*67e74705SXin Li Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3221*67e74705SXin Li Expr = ParseBraceInitializer();
3222*67e74705SXin Li } else
3223*67e74705SXin Li Expr = ParseAssignmentExpression();
3224*67e74705SXin Li
3225*67e74705SXin Li ExprResult Res(Expr);
3226*67e74705SXin Li if (Res.isInvalid()) {
3227*67e74705SXin Li // We must manually skip to a ']', otherwise the expression skipper will
3228*67e74705SXin Li // stop at the ']' when it skips to the ';'. We want it to skip beyond
3229*67e74705SXin Li // the enclosing expression.
3230*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3231*67e74705SXin Li return Res;
3232*67e74705SXin Li }
3233*67e74705SXin Li
3234*67e74705SXin Li // We have a valid expression.
3235*67e74705SXin Li KeyExprs.push_back(Res.get());
3236*67e74705SXin Li
3237*67e74705SXin Li // Code completion after each argument.
3238*67e74705SXin Li if (Tok.is(tok::code_completion)) {
3239*67e74705SXin Li if (SuperLoc.isValid())
3240*67e74705SXin Li Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
3241*67e74705SXin Li KeyIdents,
3242*67e74705SXin Li /*AtArgumentEpression=*/false);
3243*67e74705SXin Li else if (ReceiverType)
3244*67e74705SXin Li Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
3245*67e74705SXin Li KeyIdents,
3246*67e74705SXin Li /*AtArgumentEpression=*/false);
3247*67e74705SXin Li else
3248*67e74705SXin Li Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
3249*67e74705SXin Li KeyIdents,
3250*67e74705SXin Li /*AtArgumentEpression=*/false);
3251*67e74705SXin Li cutOffParsing();
3252*67e74705SXin Li return ExprError();
3253*67e74705SXin Li }
3254*67e74705SXin Li
3255*67e74705SXin Li // Check for another keyword selector.
3256*67e74705SXin Li selIdent = ParseObjCSelectorPiece(Loc);
3257*67e74705SXin Li if (!selIdent && Tok.isNot(tok::colon))
3258*67e74705SXin Li break;
3259*67e74705SXin Li // We have a selector or a colon, continue parsing.
3260*67e74705SXin Li }
3261*67e74705SXin Li // Parse the, optional, argument list, comma separated.
3262*67e74705SXin Li while (Tok.is(tok::comma)) {
3263*67e74705SXin Li SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
3264*67e74705SXin Li /// Parse the expression after ','
3265*67e74705SXin Li ExprResult Res(ParseAssignmentExpression());
3266*67e74705SXin Li if (Tok.is(tok::colon))
3267*67e74705SXin Li Res = Actions.CorrectDelayedTyposInExpr(Res);
3268*67e74705SXin Li if (Res.isInvalid()) {
3269*67e74705SXin Li if (Tok.is(tok::colon)) {
3270*67e74705SXin Li Diag(commaLoc, diag::note_extra_comma_message_arg) <<
3271*67e74705SXin Li FixItHint::CreateRemoval(commaLoc);
3272*67e74705SXin Li }
3273*67e74705SXin Li // We must manually skip to a ']', otherwise the expression skipper will
3274*67e74705SXin Li // stop at the ']' when it skips to the ';'. We want it to skip beyond
3275*67e74705SXin Li // the enclosing expression.
3276*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3277*67e74705SXin Li return Res;
3278*67e74705SXin Li }
3279*67e74705SXin Li
3280*67e74705SXin Li // We have a valid expression.
3281*67e74705SXin Li KeyExprs.push_back(Res.get());
3282*67e74705SXin Li }
3283*67e74705SXin Li } else if (!selIdent) {
3284*67e74705SXin Li Diag(Tok, diag::err_expected) << tok::identifier; // missing selector name.
3285*67e74705SXin Li
3286*67e74705SXin Li // We must manually skip to a ']', otherwise the expression skipper will
3287*67e74705SXin Li // stop at the ']' when it skips to the ';'. We want it to skip beyond
3288*67e74705SXin Li // the enclosing expression.
3289*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3290*67e74705SXin Li return ExprError();
3291*67e74705SXin Li }
3292*67e74705SXin Li
3293*67e74705SXin Li if (Tok.isNot(tok::r_square)) {
3294*67e74705SXin Li Diag(Tok, diag::err_expected)
3295*67e74705SXin Li << (Tok.is(tok::identifier) ? tok::colon : tok::r_square);
3296*67e74705SXin Li // We must manually skip to a ']', otherwise the expression skipper will
3297*67e74705SXin Li // stop at the ']' when it skips to the ';'. We want it to skip beyond
3298*67e74705SXin Li // the enclosing expression.
3299*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3300*67e74705SXin Li return ExprError();
3301*67e74705SXin Li }
3302*67e74705SXin Li
3303*67e74705SXin Li SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
3304*67e74705SXin Li
3305*67e74705SXin Li unsigned nKeys = KeyIdents.size();
3306*67e74705SXin Li if (nKeys == 0) {
3307*67e74705SXin Li KeyIdents.push_back(selIdent);
3308*67e74705SXin Li KeyLocs.push_back(Loc);
3309*67e74705SXin Li }
3310*67e74705SXin Li Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
3311*67e74705SXin Li
3312*67e74705SXin Li if (SuperLoc.isValid())
3313*67e74705SXin Li return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
3314*67e74705SXin Li LBracLoc, KeyLocs, RBracLoc, KeyExprs);
3315*67e74705SXin Li else if (ReceiverType)
3316*67e74705SXin Li return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
3317*67e74705SXin Li LBracLoc, KeyLocs, RBracLoc, KeyExprs);
3318*67e74705SXin Li return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
3319*67e74705SXin Li LBracLoc, KeyLocs, RBracLoc, KeyExprs);
3320*67e74705SXin Li }
3321*67e74705SXin Li
ParseObjCStringLiteral(SourceLocation AtLoc)3322*67e74705SXin Li ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
3323*67e74705SXin Li ExprResult Res(ParseStringLiteralExpression());
3324*67e74705SXin Li if (Res.isInvalid()) return Res;
3325*67e74705SXin Li
3326*67e74705SXin Li // @"foo" @"bar" is a valid concatenated string. Eat any subsequent string
3327*67e74705SXin Li // expressions. At this point, we know that the only valid thing that starts
3328*67e74705SXin Li // with '@' is an @"".
3329*67e74705SXin Li SmallVector<SourceLocation, 4> AtLocs;
3330*67e74705SXin Li ExprVector AtStrings;
3331*67e74705SXin Li AtLocs.push_back(AtLoc);
3332*67e74705SXin Li AtStrings.push_back(Res.get());
3333*67e74705SXin Li
3334*67e74705SXin Li while (Tok.is(tok::at)) {
3335*67e74705SXin Li AtLocs.push_back(ConsumeToken()); // eat the @.
3336*67e74705SXin Li
3337*67e74705SXin Li // Invalid unless there is a string literal.
3338*67e74705SXin Li if (!isTokenStringLiteral())
3339*67e74705SXin Li return ExprError(Diag(Tok, diag::err_objc_concat_string));
3340*67e74705SXin Li
3341*67e74705SXin Li ExprResult Lit(ParseStringLiteralExpression());
3342*67e74705SXin Li if (Lit.isInvalid())
3343*67e74705SXin Li return Lit;
3344*67e74705SXin Li
3345*67e74705SXin Li AtStrings.push_back(Lit.get());
3346*67e74705SXin Li }
3347*67e74705SXin Li
3348*67e74705SXin Li return Actions.ParseObjCStringLiteral(AtLocs.data(), AtStrings);
3349*67e74705SXin Li }
3350*67e74705SXin Li
3351*67e74705SXin Li /// ParseObjCBooleanLiteral -
3352*67e74705SXin Li /// objc-scalar-literal : '@' boolean-keyword
3353*67e74705SXin Li /// ;
3354*67e74705SXin Li /// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
3355*67e74705SXin Li /// ;
ParseObjCBooleanLiteral(SourceLocation AtLoc,bool ArgValue)3356*67e74705SXin Li ExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
3357*67e74705SXin Li bool ArgValue) {
3358*67e74705SXin Li SourceLocation EndLoc = ConsumeToken(); // consume the keyword.
3359*67e74705SXin Li return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
3360*67e74705SXin Li }
3361*67e74705SXin Li
3362*67e74705SXin Li /// ParseObjCCharacterLiteral -
3363*67e74705SXin Li /// objc-scalar-literal : '@' character-literal
3364*67e74705SXin Li /// ;
ParseObjCCharacterLiteral(SourceLocation AtLoc)3365*67e74705SXin Li ExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
3366*67e74705SXin Li ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
3367*67e74705SXin Li if (Lit.isInvalid()) {
3368*67e74705SXin Li return Lit;
3369*67e74705SXin Li }
3370*67e74705SXin Li ConsumeToken(); // Consume the literal token.
3371*67e74705SXin Li return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
3372*67e74705SXin Li }
3373*67e74705SXin Li
3374*67e74705SXin Li /// ParseObjCNumericLiteral -
3375*67e74705SXin Li /// objc-scalar-literal : '@' scalar-literal
3376*67e74705SXin Li /// ;
3377*67e74705SXin Li /// scalar-literal : | numeric-constant /* any numeric constant. */
3378*67e74705SXin Li /// ;
ParseObjCNumericLiteral(SourceLocation AtLoc)3379*67e74705SXin Li ExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
3380*67e74705SXin Li ExprResult Lit(Actions.ActOnNumericConstant(Tok));
3381*67e74705SXin Li if (Lit.isInvalid()) {
3382*67e74705SXin Li return Lit;
3383*67e74705SXin Li }
3384*67e74705SXin Li ConsumeToken(); // Consume the literal token.
3385*67e74705SXin Li return Actions.BuildObjCNumericLiteral(AtLoc, Lit.get());
3386*67e74705SXin Li }
3387*67e74705SXin Li
3388*67e74705SXin Li /// ParseObjCBoxedExpr -
3389*67e74705SXin Li /// objc-box-expression:
3390*67e74705SXin Li /// @( assignment-expression )
3391*67e74705SXin Li ExprResult
ParseObjCBoxedExpr(SourceLocation AtLoc)3392*67e74705SXin Li Parser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
3393*67e74705SXin Li if (Tok.isNot(tok::l_paren))
3394*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
3395*67e74705SXin Li
3396*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
3397*67e74705SXin Li T.consumeOpen();
3398*67e74705SXin Li ExprResult ValueExpr(ParseAssignmentExpression());
3399*67e74705SXin Li if (T.consumeClose())
3400*67e74705SXin Li return ExprError();
3401*67e74705SXin Li
3402*67e74705SXin Li if (ValueExpr.isInvalid())
3403*67e74705SXin Li return ExprError();
3404*67e74705SXin Li
3405*67e74705SXin Li // Wrap the sub-expression in a parenthesized expression, to distinguish
3406*67e74705SXin Li // a boxed expression from a literal.
3407*67e74705SXin Li SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
3408*67e74705SXin Li ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.get());
3409*67e74705SXin Li return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
3410*67e74705SXin Li ValueExpr.get());
3411*67e74705SXin Li }
3412*67e74705SXin Li
ParseObjCArrayLiteral(SourceLocation AtLoc)3413*67e74705SXin Li ExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
3414*67e74705SXin Li ExprVector ElementExprs; // array elements.
3415*67e74705SXin Li ConsumeBracket(); // consume the l_square.
3416*67e74705SXin Li
3417*67e74705SXin Li while (Tok.isNot(tok::r_square)) {
3418*67e74705SXin Li // Parse list of array element expressions (all must be id types).
3419*67e74705SXin Li ExprResult Res(ParseAssignmentExpression());
3420*67e74705SXin Li if (Res.isInvalid()) {
3421*67e74705SXin Li // We must manually skip to a ']', otherwise the expression skipper will
3422*67e74705SXin Li // stop at the ']' when it skips to the ';'. We want it to skip beyond
3423*67e74705SXin Li // the enclosing expression.
3424*67e74705SXin Li SkipUntil(tok::r_square, StopAtSemi);
3425*67e74705SXin Li return Res;
3426*67e74705SXin Li }
3427*67e74705SXin Li
3428*67e74705SXin Li // Parse the ellipsis that indicates a pack expansion.
3429*67e74705SXin Li if (Tok.is(tok::ellipsis))
3430*67e74705SXin Li Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
3431*67e74705SXin Li if (Res.isInvalid())
3432*67e74705SXin Li return true;
3433*67e74705SXin Li
3434*67e74705SXin Li ElementExprs.push_back(Res.get());
3435*67e74705SXin Li
3436*67e74705SXin Li if (Tok.is(tok::comma))
3437*67e74705SXin Li ConsumeToken(); // Eat the ','.
3438*67e74705SXin Li else if (Tok.isNot(tok::r_square))
3439*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_square
3440*67e74705SXin Li << tok::comma);
3441*67e74705SXin Li }
3442*67e74705SXin Li SourceLocation EndLoc = ConsumeBracket(); // location of ']'
3443*67e74705SXin Li MultiExprArg Args(ElementExprs);
3444*67e74705SXin Li return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
3445*67e74705SXin Li }
3446*67e74705SXin Li
ParseObjCDictionaryLiteral(SourceLocation AtLoc)3447*67e74705SXin Li ExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
3448*67e74705SXin Li SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
3449*67e74705SXin Li ConsumeBrace(); // consume the l_square.
3450*67e74705SXin Li while (Tok.isNot(tok::r_brace)) {
3451*67e74705SXin Li // Parse the comma separated key : value expressions.
3452*67e74705SXin Li ExprResult KeyExpr;
3453*67e74705SXin Li {
3454*67e74705SXin Li ColonProtectionRAIIObject X(*this);
3455*67e74705SXin Li KeyExpr = ParseAssignmentExpression();
3456*67e74705SXin Li if (KeyExpr.isInvalid()) {
3457*67e74705SXin Li // We must manually skip to a '}', otherwise the expression skipper will
3458*67e74705SXin Li // stop at the '}' when it skips to the ';'. We want it to skip beyond
3459*67e74705SXin Li // the enclosing expression.
3460*67e74705SXin Li SkipUntil(tok::r_brace, StopAtSemi);
3461*67e74705SXin Li return KeyExpr;
3462*67e74705SXin Li }
3463*67e74705SXin Li }
3464*67e74705SXin Li
3465*67e74705SXin Li if (ExpectAndConsume(tok::colon)) {
3466*67e74705SXin Li SkipUntil(tok::r_brace, StopAtSemi);
3467*67e74705SXin Li return ExprError();
3468*67e74705SXin Li }
3469*67e74705SXin Li
3470*67e74705SXin Li ExprResult ValueExpr(ParseAssignmentExpression());
3471*67e74705SXin Li if (ValueExpr.isInvalid()) {
3472*67e74705SXin Li // We must manually skip to a '}', otherwise the expression skipper will
3473*67e74705SXin Li // stop at the '}' when it skips to the ';'. We want it to skip beyond
3474*67e74705SXin Li // the enclosing expression.
3475*67e74705SXin Li SkipUntil(tok::r_brace, StopAtSemi);
3476*67e74705SXin Li return ValueExpr;
3477*67e74705SXin Li }
3478*67e74705SXin Li
3479*67e74705SXin Li // Parse the ellipsis that designates this as a pack expansion.
3480*67e74705SXin Li SourceLocation EllipsisLoc;
3481*67e74705SXin Li if (getLangOpts().CPlusPlus)
3482*67e74705SXin Li TryConsumeToken(tok::ellipsis, EllipsisLoc);
3483*67e74705SXin Li
3484*67e74705SXin Li // We have a valid expression. Collect it in a vector so we can
3485*67e74705SXin Li // build the argument list.
3486*67e74705SXin Li ObjCDictionaryElement Element = {
3487*67e74705SXin Li KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
3488*67e74705SXin Li };
3489*67e74705SXin Li Elements.push_back(Element);
3490*67e74705SXin Li
3491*67e74705SXin Li if (!TryConsumeToken(tok::comma) && Tok.isNot(tok::r_brace))
3492*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected_either) << tok::r_brace
3493*67e74705SXin Li << tok::comma);
3494*67e74705SXin Li }
3495*67e74705SXin Li SourceLocation EndLoc = ConsumeBrace();
3496*67e74705SXin Li
3497*67e74705SXin Li // Create the ObjCDictionaryLiteral.
3498*67e74705SXin Li return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
3499*67e74705SXin Li Elements);
3500*67e74705SXin Li }
3501*67e74705SXin Li
3502*67e74705SXin Li /// objc-encode-expression:
3503*67e74705SXin Li /// \@encode ( type-name )
3504*67e74705SXin Li ExprResult
ParseObjCEncodeExpression(SourceLocation AtLoc)3505*67e74705SXin Li Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
3506*67e74705SXin Li assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
3507*67e74705SXin Li
3508*67e74705SXin Li SourceLocation EncLoc = ConsumeToken();
3509*67e74705SXin Li
3510*67e74705SXin Li if (Tok.isNot(tok::l_paren))
3511*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
3512*67e74705SXin Li
3513*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
3514*67e74705SXin Li T.consumeOpen();
3515*67e74705SXin Li
3516*67e74705SXin Li TypeResult Ty = ParseTypeName();
3517*67e74705SXin Li
3518*67e74705SXin Li T.consumeClose();
3519*67e74705SXin Li
3520*67e74705SXin Li if (Ty.isInvalid())
3521*67e74705SXin Li return ExprError();
3522*67e74705SXin Li
3523*67e74705SXin Li return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
3524*67e74705SXin Li Ty.get(), T.getCloseLocation());
3525*67e74705SXin Li }
3526*67e74705SXin Li
3527*67e74705SXin Li /// objc-protocol-expression
3528*67e74705SXin Li /// \@protocol ( protocol-name )
3529*67e74705SXin Li ExprResult
ParseObjCProtocolExpression(SourceLocation AtLoc)3530*67e74705SXin Li Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
3531*67e74705SXin Li SourceLocation ProtoLoc = ConsumeToken();
3532*67e74705SXin Li
3533*67e74705SXin Li if (Tok.isNot(tok::l_paren))
3534*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
3535*67e74705SXin Li
3536*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
3537*67e74705SXin Li T.consumeOpen();
3538*67e74705SXin Li
3539*67e74705SXin Li if (Tok.isNot(tok::identifier))
3540*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
3541*67e74705SXin Li
3542*67e74705SXin Li IdentifierInfo *protocolId = Tok.getIdentifierInfo();
3543*67e74705SXin Li SourceLocation ProtoIdLoc = ConsumeToken();
3544*67e74705SXin Li
3545*67e74705SXin Li T.consumeClose();
3546*67e74705SXin Li
3547*67e74705SXin Li return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
3548*67e74705SXin Li T.getOpenLocation(), ProtoIdLoc,
3549*67e74705SXin Li T.getCloseLocation());
3550*67e74705SXin Li }
3551*67e74705SXin Li
3552*67e74705SXin Li /// objc-selector-expression
3553*67e74705SXin Li /// @selector '(' '('[opt] objc-keyword-selector ')'[opt] ')'
ParseObjCSelectorExpression(SourceLocation AtLoc)3554*67e74705SXin Li ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
3555*67e74705SXin Li SourceLocation SelectorLoc = ConsumeToken();
3556*67e74705SXin Li
3557*67e74705SXin Li if (Tok.isNot(tok::l_paren))
3558*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
3559*67e74705SXin Li
3560*67e74705SXin Li SmallVector<IdentifierInfo *, 12> KeyIdents;
3561*67e74705SXin Li SourceLocation sLoc;
3562*67e74705SXin Li
3563*67e74705SXin Li BalancedDelimiterTracker T(*this, tok::l_paren);
3564*67e74705SXin Li T.consumeOpen();
3565*67e74705SXin Li bool HasOptionalParen = Tok.is(tok::l_paren);
3566*67e74705SXin Li if (HasOptionalParen)
3567*67e74705SXin Li ConsumeParen();
3568*67e74705SXin Li
3569*67e74705SXin Li if (Tok.is(tok::code_completion)) {
3570*67e74705SXin Li Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
3571*67e74705SXin Li cutOffParsing();
3572*67e74705SXin Li return ExprError();
3573*67e74705SXin Li }
3574*67e74705SXin Li
3575*67e74705SXin Li IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
3576*67e74705SXin Li if (!SelIdent && // missing selector name.
3577*67e74705SXin Li Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
3578*67e74705SXin Li return ExprError(Diag(Tok, diag::err_expected) << tok::identifier);
3579*67e74705SXin Li
3580*67e74705SXin Li KeyIdents.push_back(SelIdent);
3581*67e74705SXin Li
3582*67e74705SXin Li unsigned nColons = 0;
3583*67e74705SXin Li if (Tok.isNot(tok::r_paren)) {
3584*67e74705SXin Li while (1) {
3585*67e74705SXin Li if (TryConsumeToken(tok::coloncolon)) { // Handle :: in C++.
3586*67e74705SXin Li ++nColons;
3587*67e74705SXin Li KeyIdents.push_back(nullptr);
3588*67e74705SXin Li } else if (ExpectAndConsume(tok::colon)) // Otherwise expect ':'.
3589*67e74705SXin Li return ExprError();
3590*67e74705SXin Li ++nColons;
3591*67e74705SXin Li
3592*67e74705SXin Li if (Tok.is(tok::r_paren))
3593*67e74705SXin Li break;
3594*67e74705SXin Li
3595*67e74705SXin Li if (Tok.is(tok::code_completion)) {
3596*67e74705SXin Li Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents);
3597*67e74705SXin Li cutOffParsing();
3598*67e74705SXin Li return ExprError();
3599*67e74705SXin Li }
3600*67e74705SXin Li
3601*67e74705SXin Li // Check for another keyword selector.
3602*67e74705SXin Li SourceLocation Loc;
3603*67e74705SXin Li SelIdent = ParseObjCSelectorPiece(Loc);
3604*67e74705SXin Li KeyIdents.push_back(SelIdent);
3605*67e74705SXin Li if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
3606*67e74705SXin Li break;
3607*67e74705SXin Li }
3608*67e74705SXin Li }
3609*67e74705SXin Li if (HasOptionalParen && Tok.is(tok::r_paren))
3610*67e74705SXin Li ConsumeParen(); // ')'
3611*67e74705SXin Li T.consumeClose();
3612*67e74705SXin Li Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
3613*67e74705SXin Li return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
3614*67e74705SXin Li T.getOpenLocation(),
3615*67e74705SXin Li T.getCloseLocation(),
3616*67e74705SXin Li !HasOptionalParen);
3617*67e74705SXin Li }
3618*67e74705SXin Li
ParseLexedObjCMethodDefs(LexedMethod & LM,bool parseMethod)3619*67e74705SXin Li void Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
3620*67e74705SXin Li // MCDecl might be null due to error in method or c-function prototype, etc.
3621*67e74705SXin Li Decl *MCDecl = LM.D;
3622*67e74705SXin Li bool skip = MCDecl &&
3623*67e74705SXin Li ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
3624*67e74705SXin Li (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
3625*67e74705SXin Li if (skip)
3626*67e74705SXin Li return;
3627*67e74705SXin Li
3628*67e74705SXin Li // Save the current token position.
3629*67e74705SXin Li SourceLocation OrigLoc = Tok.getLocation();
3630*67e74705SXin Li
3631*67e74705SXin Li assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
3632*67e74705SXin Li // Append the current token at the end of the new token stream so that it
3633*67e74705SXin Li // doesn't get lost.
3634*67e74705SXin Li LM.Toks.push_back(Tok);
3635*67e74705SXin Li PP.EnterTokenStream(LM.Toks, true);
3636*67e74705SXin Li
3637*67e74705SXin Li // Consume the previously pushed token.
3638*67e74705SXin Li ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
3639*67e74705SXin Li
3640*67e74705SXin Li assert(Tok.isOneOf(tok::l_brace, tok::kw_try, tok::colon) &&
3641*67e74705SXin Li "Inline objective-c method not starting with '{' or 'try' or ':'");
3642*67e74705SXin Li // Enter a scope for the method or c-function body.
3643*67e74705SXin Li ParseScope BodyScope(this,
3644*67e74705SXin Li parseMethod
3645*67e74705SXin Li ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
3646*67e74705SXin Li : Scope::FnScope|Scope::DeclScope);
3647*67e74705SXin Li
3648*67e74705SXin Li // Tell the actions module that we have entered a method or c-function definition
3649*67e74705SXin Li // with the specified Declarator for the method/function.
3650*67e74705SXin Li if (parseMethod)
3651*67e74705SXin Li Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
3652*67e74705SXin Li else
3653*67e74705SXin Li Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
3654*67e74705SXin Li if (Tok.is(tok::kw_try))
3655*67e74705SXin Li ParseFunctionTryBlock(MCDecl, BodyScope);
3656*67e74705SXin Li else {
3657*67e74705SXin Li if (Tok.is(tok::colon))
3658*67e74705SXin Li ParseConstructorInitializer(MCDecl);
3659*67e74705SXin Li else
3660*67e74705SXin Li Actions.ActOnDefaultCtorInitializers(MCDecl);
3661*67e74705SXin Li ParseFunctionStatementBody(MCDecl, BodyScope);
3662*67e74705SXin Li }
3663*67e74705SXin Li
3664*67e74705SXin Li if (Tok.getLocation() != OrigLoc) {
3665*67e74705SXin Li // Due to parsing error, we either went over the cached tokens or
3666*67e74705SXin Li // there are still cached tokens left. If it's the latter case skip the
3667*67e74705SXin Li // leftover tokens.
3668*67e74705SXin Li // Since this is an uncommon situation that should be avoided, use the
3669*67e74705SXin Li // expensive isBeforeInTranslationUnit call.
3670*67e74705SXin Li if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
3671*67e74705SXin Li OrigLoc))
3672*67e74705SXin Li while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
3673*67e74705SXin Li ConsumeAnyToken();
3674*67e74705SXin Li }
3675*67e74705SXin Li }
3676