xref: /llvm-project/clang/lib/Parse/ParseObjc.cpp (revision 45879695553e4fcf007227eba100d3b4b366d477)
1 //===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file implements the Objective-C portions of the Parser interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Parse/Parser.h"
15 #include "clang/Parse/DeclSpec.h"
16 #include "clang/Parse/Scope.h"
17 #include "clang/Parse/ParseDiagnostic.h"
18 #include "llvm/ADT/SmallVector.h"
19 using namespace clang;
20 
21 
22 /// ParseObjCAtDirectives - Handle parts of the external-declaration production:
23 ///       external-declaration: [C99 6.9]
24 /// [OBJC]  objc-class-definition
25 /// [OBJC]  objc-class-declaration
26 /// [OBJC]  objc-alias-declaration
27 /// [OBJC]  objc-protocol-definition
28 /// [OBJC]  objc-method-definition
29 /// [OBJC]  '@' 'end'
30 Parser::DeclPtrTy Parser::ParseObjCAtDirectives() {
31   SourceLocation AtLoc = ConsumeToken(); // the "@"
32 
33   if (Tok.is(tok::code_completion)) {
34     Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
35     ConsumeCodeCompletionToken();
36   }
37 
38   switch (Tok.getObjCKeywordID()) {
39   case tok::objc_class:
40     return ParseObjCAtClassDeclaration(AtLoc);
41   case tok::objc_interface:
42     return ParseObjCAtInterfaceDeclaration(AtLoc);
43   case tok::objc_protocol:
44     return ParseObjCAtProtocolDeclaration(AtLoc);
45   case tok::objc_implementation:
46     return ParseObjCAtImplementationDeclaration(AtLoc);
47   case tok::objc_end:
48     return ParseObjCAtEndDeclaration(AtLoc);
49   case tok::objc_compatibility_alias:
50     return ParseObjCAtAliasDeclaration(AtLoc);
51   case tok::objc_synthesize:
52     return ParseObjCPropertySynthesize(AtLoc);
53   case tok::objc_dynamic:
54     return ParseObjCPropertyDynamic(AtLoc);
55   default:
56     Diag(AtLoc, diag::err_unexpected_at);
57     SkipUntil(tok::semi);
58     return DeclPtrTy();
59   }
60 }
61 
62 ///
63 /// objc-class-declaration:
64 ///    '@' 'class' identifier-list ';'
65 ///
66 Parser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
67   ConsumeToken(); // the identifier "class"
68   llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
69   llvm::SmallVector<SourceLocation, 8> ClassLocs;
70 
71 
72   while (1) {
73     if (Tok.isNot(tok::identifier)) {
74       Diag(Tok, diag::err_expected_ident);
75       SkipUntil(tok::semi);
76       return DeclPtrTy();
77     }
78     ClassNames.push_back(Tok.getIdentifierInfo());
79     ClassLocs.push_back(Tok.getLocation());
80     ConsumeToken();
81 
82     if (Tok.isNot(tok::comma))
83       break;
84 
85     ConsumeToken();
86   }
87 
88   // Consume the ';'.
89   if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
90     return DeclPtrTy();
91 
92   return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
93                                               ClassLocs.data(),
94                                               ClassNames.size());
95 }
96 
97 ///
98 ///   objc-interface:
99 ///     objc-class-interface-attributes[opt] objc-class-interface
100 ///     objc-category-interface
101 ///
102 ///   objc-class-interface:
103 ///     '@' 'interface' identifier objc-superclass[opt]
104 ///       objc-protocol-refs[opt]
105 ///       objc-class-instance-variables[opt]
106 ///       objc-interface-decl-list
107 ///     @end
108 ///
109 ///   objc-category-interface:
110 ///     '@' 'interface' identifier '(' identifier[opt] ')'
111 ///       objc-protocol-refs[opt]
112 ///       objc-interface-decl-list
113 ///     @end
114 ///
115 ///   objc-superclass:
116 ///     ':' identifier
117 ///
118 ///   objc-class-interface-attributes:
119 ///     __attribute__((visibility("default")))
120 ///     __attribute__((visibility("hidden")))
121 ///     __attribute__((deprecated))
122 ///     __attribute__((unavailable))
123 ///     __attribute__((objc_exception)) - used by NSException on 64-bit
124 ///
125 Parser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
126   SourceLocation atLoc, AttributeList *attrList) {
127   assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
128          "ParseObjCAtInterfaceDeclaration(): Expected @interface");
129   ConsumeToken(); // the "interface" identifier
130 
131   // Code completion after '@interface'.
132   if (Tok.is(tok::code_completion)) {
133     Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
134     ConsumeCodeCompletionToken();
135   }
136 
137   if (Tok.isNot(tok::identifier)) {
138     Diag(Tok, diag::err_expected_ident); // missing class or category name.
139     return DeclPtrTy();
140   }
141 
142   // We have a class or category name - consume it.
143   IdentifierInfo *nameId = Tok.getIdentifierInfo();
144   SourceLocation nameLoc = ConsumeToken();
145   if (Tok.is(tok::l_paren) &&
146       !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
147     SourceLocation lparenLoc = ConsumeParen();
148     SourceLocation categoryLoc, rparenLoc;
149     IdentifierInfo *categoryId = 0;
150     if (Tok.is(tok::code_completion)) {
151       Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
152       ConsumeCodeCompletionToken();
153     }
154 
155     // For ObjC2, the category name is optional (not an error).
156     if (Tok.is(tok::identifier)) {
157       categoryId = Tok.getIdentifierInfo();
158       categoryLoc = ConsumeToken();
159     }
160     else if (!getLang().ObjC2) {
161       Diag(Tok, diag::err_expected_ident); // missing category name.
162       return DeclPtrTy();
163     }
164     if (Tok.isNot(tok::r_paren)) {
165       Diag(Tok, diag::err_expected_rparen);
166       SkipUntil(tok::r_paren, false); // don't stop at ';'
167       return DeclPtrTy();
168     }
169     rparenLoc = ConsumeParen();
170     // Next, we need to check for any protocol references.
171     SourceLocation LAngleLoc, EndProtoLoc;
172     llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
173     llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
174     if (Tok.is(tok::less) &&
175         ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
176                                     LAngleLoc, EndProtoLoc))
177       return DeclPtrTy();
178 
179     if (attrList) // categories don't support attributes.
180       Diag(Tok, diag::err_objc_no_attributes_on_category);
181 
182     DeclPtrTy CategoryType =
183     Actions.ActOnStartCategoryInterface(atLoc,
184                                         nameId, nameLoc,
185                                         categoryId, categoryLoc,
186                                         ProtocolRefs.data(),
187                                         ProtocolRefs.size(),
188                                         ProtocolLocs.data(),
189                                         EndProtoLoc);
190     if (Tok.is(tok::l_brace))
191       ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
192                                       atLoc);
193 
194     ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
195     return CategoryType;
196   }
197   // Parse a class interface.
198   IdentifierInfo *superClassId = 0;
199   SourceLocation superClassLoc;
200 
201   if (Tok.is(tok::colon)) { // a super class is specified.
202     ConsumeToken();
203 
204     // Code completion of superclass names.
205     if (Tok.is(tok::code_completion)) {
206       Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
207       ConsumeCodeCompletionToken();
208     }
209 
210     if (Tok.isNot(tok::identifier)) {
211       Diag(Tok, diag::err_expected_ident); // missing super class name.
212       return DeclPtrTy();
213     }
214     superClassId = Tok.getIdentifierInfo();
215     superClassLoc = ConsumeToken();
216   }
217   // Next, we need to check for any protocol references.
218   llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
219   llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
220   SourceLocation LAngleLoc, EndProtoLoc;
221   if (Tok.is(tok::less) &&
222       ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
223                                   LAngleLoc, EndProtoLoc))
224     return DeclPtrTy();
225 
226   DeclPtrTy ClsType =
227     Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
228                                      superClassId, superClassLoc,
229                                      ProtocolRefs.data(), ProtocolRefs.size(),
230                                      ProtocolLocs.data(),
231                                      EndProtoLoc, attrList);
232 
233   if (Tok.is(tok::l_brace))
234     ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
235 
236   ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
237   return ClsType;
238 }
239 
240 /// The Objective-C property callback.  This should be defined where
241 /// it's used, but instead it's been lifted to here to support VS2005.
242 struct Parser::ObjCPropertyCallback : FieldCallback {
243   Parser &P;
244   DeclPtrTy IDecl;
245   llvm::SmallVectorImpl<DeclPtrTy> &Props;
246   ObjCDeclSpec &OCDS;
247   SourceLocation AtLoc;
248   tok::ObjCKeywordKind MethodImplKind;
249 
250   ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
251                        llvm::SmallVectorImpl<DeclPtrTy> &Props,
252                        ObjCDeclSpec &OCDS, SourceLocation AtLoc,
253                        tok::ObjCKeywordKind MethodImplKind) :
254     P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
255     MethodImplKind(MethodImplKind) {
256   }
257 
258   DeclPtrTy invoke(FieldDeclarator &FD) {
259     if (FD.D.getIdentifier() == 0) {
260       P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
261         << FD.D.getSourceRange();
262       return DeclPtrTy();
263     }
264     if (FD.BitfieldSize) {
265       P.Diag(AtLoc, diag::err_objc_property_bitfield)
266         << FD.D.getSourceRange();
267       return DeclPtrTy();
268     }
269 
270     // Install the property declarator into interfaceDecl.
271     IdentifierInfo *SelName =
272       OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
273 
274     Selector GetterSel =
275       P.PP.getSelectorTable().getNullarySelector(SelName);
276     IdentifierInfo *SetterName = OCDS.getSetterName();
277     Selector SetterSel;
278     if (SetterName)
279       SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
280     else
281       SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
282                                                      P.PP.getSelectorTable(),
283                                                      FD.D.getIdentifier());
284     bool isOverridingProperty = false;
285     DeclPtrTy Property =
286       P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
287                               GetterSel, SetterSel, IDecl,
288                               &isOverridingProperty,
289                               MethodImplKind);
290     if (!isOverridingProperty)
291       Props.push_back(Property);
292 
293     return Property;
294   }
295 };
296 
297 ///   objc-interface-decl-list:
298 ///     empty
299 ///     objc-interface-decl-list objc-property-decl [OBJC2]
300 ///     objc-interface-decl-list objc-method-requirement [OBJC2]
301 ///     objc-interface-decl-list objc-method-proto ';'
302 ///     objc-interface-decl-list declaration
303 ///     objc-interface-decl-list ';'
304 ///
305 ///   objc-method-requirement: [OBJC2]
306 ///     @required
307 ///     @optional
308 ///
309 void Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
310                                         tok::ObjCKeywordKind contextKey) {
311   llvm::SmallVector<DeclPtrTy, 32> allMethods;
312   llvm::SmallVector<DeclPtrTy, 16> allProperties;
313   llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
314   tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
315 
316   SourceRange AtEnd;
317 
318   while (1) {
319     // If this is a method prototype, parse it.
320     if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
321       DeclPtrTy methodPrototype =
322         ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
323       allMethods.push_back(methodPrototype);
324       // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
325       // method definitions.
326       ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
327                        "", tok::semi);
328       continue;
329     }
330     if (Tok.is(tok::l_paren)) {
331       Diag(Tok, diag::err_expected_minus_or_plus);
332       DeclPtrTy methodPrototype = ParseObjCMethodDecl(Tok.getLocation(),
333                                                       tok::minus,
334                                                       interfaceDecl,
335                                                       MethodImplKind);
336       continue;
337     }
338     // Ignore excess semicolons.
339     if (Tok.is(tok::semi)) {
340       ConsumeToken();
341       continue;
342     }
343 
344     // If we got to the end of the file, exit the loop.
345     if (Tok.is(tok::eof))
346       break;
347 
348     // Code completion within an Objective-C interface.
349     if (Tok.is(tok::code_completion)) {
350       Actions.CodeCompleteOrdinaryName(getCurScope(),
351                                   ObjCImpDecl? Action::CCC_ObjCImplementation
352                                              : Action::CCC_ObjCInterface);
353       ConsumeCodeCompletionToken();
354     }
355 
356     // If we don't have an @ directive, parse it as a function definition.
357     if (Tok.isNot(tok::at)) {
358       // The code below does not consume '}'s because it is afraid of eating the
359       // end of a namespace.  Because of the way this code is structured, an
360       // erroneous r_brace would cause an infinite loop if not handled here.
361       if (Tok.is(tok::r_brace))
362         break;
363 
364       // FIXME: as the name implies, this rule allows function definitions.
365       // We could pass a flag or check for functions during semantic analysis.
366       allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
367       continue;
368     }
369 
370     // Otherwise, we have an @ directive, eat the @.
371     SourceLocation AtLoc = ConsumeToken(); // the "@"
372     if (Tok.is(tok::code_completion)) {
373       Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
374       ConsumeCodeCompletionToken();
375       break;
376     }
377 
378     tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
379 
380     if (DirectiveKind == tok::objc_end) { // @end -> terminate list
381       AtEnd.setBegin(AtLoc);
382       AtEnd.setEnd(Tok.getLocation());
383       break;
384     } else if (DirectiveKind == tok::objc_not_keyword) {
385       Diag(Tok, diag::err_objc_unknown_at);
386       SkipUntil(tok::semi);
387       continue;
388     }
389 
390     // Eat the identifier.
391     ConsumeToken();
392 
393     switch (DirectiveKind) {
394     default:
395       // FIXME: If someone forgets an @end on a protocol, this loop will
396       // continue to eat up tons of stuff and spew lots of nonsense errors.  It
397       // would probably be better to bail out if we saw an @class or @interface
398       // or something like that.
399       Diag(AtLoc, diag::err_objc_illegal_interface_qual);
400       // Skip until we see an '@' or '}' or ';'.
401       SkipUntil(tok::r_brace, tok::at);
402       break;
403 
404     case tok::objc_required:
405     case tok::objc_optional:
406       // This is only valid on protocols.
407       // FIXME: Should this check for ObjC2 being enabled?
408       if (contextKey != tok::objc_protocol)
409         Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
410       else
411         MethodImplKind = DirectiveKind;
412       break;
413 
414     case tok::objc_property:
415       if (!getLang().ObjC2)
416         Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
417 
418       ObjCDeclSpec OCDS;
419       // Parse property attribute list, if any.
420       if (Tok.is(tok::l_paren))
421         ParseObjCPropertyAttribute(OCDS, interfaceDecl,
422                                    allMethods.data(), allMethods.size());
423 
424       ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
425                                     OCDS, AtLoc, MethodImplKind);
426 
427       // Parse all the comma separated declarators.
428       DeclSpec DS;
429       ParseStructDeclaration(DS, Callback);
430 
431       ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
432                        tok::at);
433       break;
434     }
435   }
436 
437   // We break out of the big loop in two cases: when we see @end or when we see
438   // EOF.  In the former case, eat the @end.  In the later case, emit an error.
439   if (Tok.is(tok::code_completion)) {
440     Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
441     ConsumeCodeCompletionToken();
442   } else if (Tok.isObjCAtKeyword(tok::objc_end))
443     ConsumeToken(); // the "end" identifier
444   else
445     Diag(Tok, diag::err_objc_missing_end);
446 
447   // Insert collected methods declarations into the @interface object.
448   // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
449   Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
450                      allMethods.data(), allMethods.size(),
451                      allProperties.data(), allProperties.size(),
452                      allTUVariables.data(), allTUVariables.size());
453 }
454 
455 ///   Parse property attribute declarations.
456 ///
457 ///   property-attr-decl: '(' property-attrlist ')'
458 ///   property-attrlist:
459 ///     property-attribute
460 ///     property-attrlist ',' property-attribute
461 ///   property-attribute:
462 ///     getter '=' identifier
463 ///     setter '=' identifier ':'
464 ///     readonly
465 ///     readwrite
466 ///     assign
467 ///     retain
468 ///     copy
469 ///     nonatomic
470 ///
471 void Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
472                                         DeclPtrTy *Methods,
473                                         unsigned NumMethods) {
474   assert(Tok.getKind() == tok::l_paren);
475   SourceLocation LHSLoc = ConsumeParen(); // consume '('
476 
477   while (1) {
478     if (Tok.is(tok::code_completion)) {
479       Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
480       ConsumeCodeCompletionToken();
481     }
482     const IdentifierInfo *II = Tok.getIdentifierInfo();
483 
484     // If this is not an identifier at all, bail out early.
485     if (II == 0) {
486       MatchRHSPunctuation(tok::r_paren, LHSLoc);
487       return;
488     }
489 
490     SourceLocation AttrName = ConsumeToken(); // consume last attribute name
491 
492     if (II->isStr("readonly"))
493       DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
494     else if (II->isStr("assign"))
495       DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
496     else if (II->isStr("readwrite"))
497       DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
498     else if (II->isStr("retain"))
499       DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
500     else if (II->isStr("copy"))
501       DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
502     else if (II->isStr("nonatomic"))
503       DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
504     else if (II->isStr("getter") || II->isStr("setter")) {
505       // getter/setter require extra treatment.
506       if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
507                            tok::r_paren))
508         return;
509 
510       if (Tok.is(tok::code_completion)) {
511         if (II->getNameStart()[0] == 's')
512           Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl,
513                                                  Methods, NumMethods);
514         else
515           Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl,
516                                                  Methods, NumMethods);
517         ConsumeCodeCompletionToken();
518       }
519 
520       if (Tok.isNot(tok::identifier)) {
521         Diag(Tok, diag::err_expected_ident);
522         SkipUntil(tok::r_paren);
523         return;
524       }
525 
526       if (II->getNameStart()[0] == 's') {
527         DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
528         DS.setSetterName(Tok.getIdentifierInfo());
529         ConsumeToken();  // consume method name
530 
531         if (ExpectAndConsume(tok::colon,
532                              diag::err_expected_colon_after_setter_name, "",
533                              tok::r_paren))
534           return;
535       } else {
536         DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
537         DS.setGetterName(Tok.getIdentifierInfo());
538         ConsumeToken();  // consume method name
539       }
540     } else {
541       Diag(AttrName, diag::err_objc_expected_property_attr) << II;
542       SkipUntil(tok::r_paren);
543       return;
544     }
545 
546     if (Tok.isNot(tok::comma))
547       break;
548 
549     ConsumeToken();
550   }
551 
552   MatchRHSPunctuation(tok::r_paren, LHSLoc);
553 }
554 
555 ///   objc-method-proto:
556 ///     objc-instance-method objc-method-decl objc-method-attributes[opt]
557 ///     objc-class-method objc-method-decl objc-method-attributes[opt]
558 ///
559 ///   objc-instance-method: '-'
560 ///   objc-class-method: '+'
561 ///
562 ///   objc-method-attributes:         [OBJC2]
563 ///     __attribute__((deprecated))
564 ///
565 Parser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
566                                           tok::ObjCKeywordKind MethodImplKind) {
567   assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
568 
569   tok::TokenKind methodType = Tok.getKind();
570   SourceLocation mLoc = ConsumeToken();
571 
572   DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
573   // Since this rule is used for both method declarations and definitions,
574   // the caller is (optionally) responsible for consuming the ';'.
575   return MDecl;
576 }
577 
578 ///   objc-selector:
579 ///     identifier
580 ///     one of
581 ///       enum struct union if else while do for switch case default
582 ///       break continue return goto asm sizeof typeof __alignof
583 ///       unsigned long const short volatile signed restrict _Complex
584 ///       in out inout bycopy byref oneway int char float double void _Bool
585 ///
586 IdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
587   switch (Tok.getKind()) {
588   default:
589     return 0;
590   case tok::identifier:
591   case tok::kw_asm:
592   case tok::kw_auto:
593   case tok::kw_bool:
594   case tok::kw_break:
595   case tok::kw_case:
596   case tok::kw_catch:
597   case tok::kw_char:
598   case tok::kw_class:
599   case tok::kw_const:
600   case tok::kw_const_cast:
601   case tok::kw_continue:
602   case tok::kw_default:
603   case tok::kw_delete:
604   case tok::kw_do:
605   case tok::kw_double:
606   case tok::kw_dynamic_cast:
607   case tok::kw_else:
608   case tok::kw_enum:
609   case tok::kw_explicit:
610   case tok::kw_export:
611   case tok::kw_extern:
612   case tok::kw_false:
613   case tok::kw_float:
614   case tok::kw_for:
615   case tok::kw_friend:
616   case tok::kw_goto:
617   case tok::kw_if:
618   case tok::kw_inline:
619   case tok::kw_int:
620   case tok::kw_long:
621   case tok::kw_mutable:
622   case tok::kw_namespace:
623   case tok::kw_new:
624   case tok::kw_operator:
625   case tok::kw_private:
626   case tok::kw_protected:
627   case tok::kw_public:
628   case tok::kw_register:
629   case tok::kw_reinterpret_cast:
630   case tok::kw_restrict:
631   case tok::kw_return:
632   case tok::kw_short:
633   case tok::kw_signed:
634   case tok::kw_sizeof:
635   case tok::kw_static:
636   case tok::kw_static_cast:
637   case tok::kw_struct:
638   case tok::kw_switch:
639   case tok::kw_template:
640   case tok::kw_this:
641   case tok::kw_throw:
642   case tok::kw_true:
643   case tok::kw_try:
644   case tok::kw_typedef:
645   case tok::kw_typeid:
646   case tok::kw_typename:
647   case tok::kw_typeof:
648   case tok::kw_union:
649   case tok::kw_unsigned:
650   case tok::kw_using:
651   case tok::kw_virtual:
652   case tok::kw_void:
653   case tok::kw_volatile:
654   case tok::kw_wchar_t:
655   case tok::kw_while:
656   case tok::kw__Bool:
657   case tok::kw__Complex:
658   case tok::kw___alignof:
659     IdentifierInfo *II = Tok.getIdentifierInfo();
660     SelectorLoc = ConsumeToken();
661     return II;
662   }
663 }
664 
665 ///  objc-for-collection-in: 'in'
666 ///
667 bool Parser::isTokIdentifier_in() const {
668   // FIXME: May have to do additional look-ahead to only allow for
669   // valid tokens following an 'in'; such as an identifier, unary operators,
670   // '[' etc.
671   return (getLang().ObjC2 && Tok.is(tok::identifier) &&
672           Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
673 }
674 
675 /// ParseObjCTypeQualifierList - This routine parses the objective-c's type
676 /// qualifier list and builds their bitmask representation in the input
677 /// argument.
678 ///
679 ///   objc-type-qualifiers:
680 ///     objc-type-qualifier
681 ///     objc-type-qualifiers objc-type-qualifier
682 ///
683 void Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
684   while (1) {
685     if (Tok.isNot(tok::identifier))
686       return;
687 
688     const IdentifierInfo *II = Tok.getIdentifierInfo();
689     for (unsigned i = 0; i != objc_NumQuals; ++i) {
690       if (II != ObjCTypeQuals[i])
691         continue;
692 
693       ObjCDeclSpec::ObjCDeclQualifier Qual;
694       switch (i) {
695       default: assert(0 && "Unknown decl qualifier");
696       case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
697       case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
698       case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
699       case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
700       case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
701       case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
702       }
703       DS.setObjCDeclQualifier(Qual);
704       ConsumeToken();
705       II = 0;
706       break;
707     }
708 
709     // If this wasn't a recognized qualifier, bail out.
710     if (II) return;
711   }
712 }
713 
714 ///   objc-type-name:
715 ///     '(' objc-type-qualifiers[opt] type-name ')'
716 ///     '(' objc-type-qualifiers[opt] ')'
717 ///
718 Parser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
719   assert(Tok.is(tok::l_paren) && "expected (");
720 
721   SourceLocation LParenLoc = ConsumeParen();
722   SourceLocation TypeStartLoc = Tok.getLocation();
723 
724   // Parse type qualifiers, in, inout, etc.
725   ParseObjCTypeQualifierList(DS);
726 
727   TypeTy *Ty = 0;
728   if (isTypeSpecifierQualifier()) {
729     TypeResult TypeSpec = ParseTypeName();
730     if (!TypeSpec.isInvalid())
731       Ty = TypeSpec.get();
732   }
733 
734   if (Tok.is(tok::r_paren))
735     ConsumeParen();
736   else if (Tok.getLocation() == TypeStartLoc) {
737     // If we didn't eat any tokens, then this isn't a type.
738     Diag(Tok, diag::err_expected_type);
739     SkipUntil(tok::r_paren);
740   } else {
741     // Otherwise, we found *something*, but didn't get a ')' in the right
742     // place.  Emit an error then return what we have as the type.
743     MatchRHSPunctuation(tok::r_paren, LParenLoc);
744   }
745   return Ty;
746 }
747 
748 ///   objc-method-decl:
749 ///     objc-selector
750 ///     objc-keyword-selector objc-parmlist[opt]
751 ///     objc-type-name objc-selector
752 ///     objc-type-name objc-keyword-selector objc-parmlist[opt]
753 ///
754 ///   objc-keyword-selector:
755 ///     objc-keyword-decl
756 ///     objc-keyword-selector objc-keyword-decl
757 ///
758 ///   objc-keyword-decl:
759 ///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
760 ///     objc-selector ':' objc-keyword-attributes[opt] identifier
761 ///     ':' objc-type-name objc-keyword-attributes[opt] identifier
762 ///     ':' objc-keyword-attributes[opt] identifier
763 ///
764 ///   objc-parmlist:
765 ///     objc-parms objc-ellipsis[opt]
766 ///
767 ///   objc-parms:
768 ///     objc-parms , parameter-declaration
769 ///
770 ///   objc-ellipsis:
771 ///     , ...
772 ///
773 ///   objc-keyword-attributes:         [OBJC2]
774 ///     __attribute__((unused))
775 ///
776 Parser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
777                                               tok::TokenKind mType,
778                                               DeclPtrTy IDecl,
779                                           tok::ObjCKeywordKind MethodImplKind) {
780   ParsingDeclRAIIObject PD(*this);
781 
782   if (Tok.is(tok::code_completion)) {
783     Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
784                                        /*ReturnType=*/0, IDecl);
785     ConsumeCodeCompletionToken();
786   }
787 
788   // Parse the return type if present.
789   TypeTy *ReturnType = 0;
790   ObjCDeclSpec DSRet;
791   if (Tok.is(tok::l_paren))
792     ReturnType = ParseObjCTypeName(DSRet);
793 
794   // If attributes exist before the method, parse them.
795   llvm::OwningPtr<AttributeList> MethodAttrs;
796   if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
797     MethodAttrs.reset(ParseGNUAttributes());
798 
799   if (Tok.is(tok::code_completion)) {
800     Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
801                                        ReturnType, IDecl);
802     ConsumeCodeCompletionToken();
803   }
804 
805   // Now parse the selector.
806   SourceLocation selLoc;
807   IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
808 
809   // An unnamed colon is valid.
810   if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
811     Diag(Tok, diag::err_expected_selector_for_method)
812       << SourceRange(mLoc, Tok.getLocation());
813     // Skip until we get a ; or {}.
814     SkipUntil(tok::r_brace);
815     return DeclPtrTy();
816   }
817 
818   llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
819   if (Tok.isNot(tok::colon)) {
820     // If attributes exist after the method, parse them.
821     if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
822       MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
823                                           ParseGNUAttributes()));
824 
825     Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
826     DeclPtrTy Result
827          = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
828                                           mType, IDecl, DSRet, ReturnType, Sel,
829                                           0,
830                                           CParamInfo.data(), CParamInfo.size(),
831                                           MethodAttrs.get(),
832                                           MethodImplKind);
833     PD.complete(Result);
834     return Result;
835   }
836 
837   llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
838   llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
839 
840   while (1) {
841     Action::ObjCArgInfo ArgInfo;
842 
843     // Each iteration parses a single keyword argument.
844     if (Tok.isNot(tok::colon)) {
845       Diag(Tok, diag::err_expected_colon);
846       break;
847     }
848     ConsumeToken(); // Eat the ':'.
849 
850     ArgInfo.Type = 0;
851     if (Tok.is(tok::l_paren)) // Parse the argument type if present.
852       ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
853 
854     // If attributes exist before the argument name, parse them.
855     ArgInfo.ArgAttrs = 0;
856     if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
857       ArgInfo.ArgAttrs = ParseGNUAttributes();
858 
859     // Code completion for the next piece of the selector.
860     if (Tok.is(tok::code_completion)) {
861       ConsumeCodeCompletionToken();
862       KeyIdents.push_back(SelIdent);
863       Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
864                                                  mType == tok::minus,
865                                                  /*AtParameterName=*/true,
866                                                  ReturnType,
867                                                  KeyIdents.data(),
868                                                  KeyIdents.size());
869       KeyIdents.pop_back();
870       break;
871     }
872 
873     if (Tok.isNot(tok::identifier)) {
874       Diag(Tok, diag::err_expected_ident); // missing argument name.
875       break;
876     }
877 
878     ArgInfo.Name = Tok.getIdentifierInfo();
879     ArgInfo.NameLoc = Tok.getLocation();
880     ConsumeToken(); // Eat the identifier.
881 
882     ArgInfos.push_back(ArgInfo);
883     KeyIdents.push_back(SelIdent);
884 
885     // Code completion for the next piece of the selector.
886     if (Tok.is(tok::code_completion)) {
887       ConsumeCodeCompletionToken();
888       Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
889                                                  mType == tok::minus,
890                                                  /*AtParameterName=*/false,
891                                                  ReturnType,
892                                                  KeyIdents.data(),
893                                                  KeyIdents.size());
894       break;
895     }
896 
897     // Check for another keyword selector.
898     SourceLocation Loc;
899     SelIdent = ParseObjCSelectorPiece(Loc);
900     if (!SelIdent && Tok.isNot(tok::colon))
901       break;
902     // We have a selector or a colon, continue parsing.
903   }
904 
905   bool isVariadic = false;
906 
907   // Parse the (optional) parameter list.
908   while (Tok.is(tok::comma)) {
909     ConsumeToken();
910     if (Tok.is(tok::ellipsis)) {
911       isVariadic = true;
912       ConsumeToken();
913       break;
914     }
915     DeclSpec DS;
916     ParseDeclarationSpecifiers(DS);
917     // Parse the declarator.
918     Declarator ParmDecl(DS, Declarator::PrototypeContext);
919     ParseDeclarator(ParmDecl);
920     IdentifierInfo *ParmII = ParmDecl.getIdentifier();
921     DeclPtrTy Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
922     CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
923                                                     ParmDecl.getIdentifierLoc(),
924                                                     Param,
925                                                    0));
926 
927   }
928 
929   // FIXME: Add support for optional parmameter list...
930   // If attributes exist after the method, parse them.
931   if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
932     MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
933                                         ParseGNUAttributes()));
934 
935   if (KeyIdents.size() == 0)
936     return DeclPtrTy();
937   Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
938                                                    &KeyIdents[0]);
939   DeclPtrTy Result
940        = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
941                                         mType, IDecl, DSRet, ReturnType, Sel,
942                                         &ArgInfos[0],
943                                         CParamInfo.data(), CParamInfo.size(),
944                                         MethodAttrs.get(),
945                                         MethodImplKind, isVariadic);
946   PD.complete(Result);
947 
948   // Delete referenced AttributeList objects.
949   for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
950        I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
951     delete I->ArgAttrs;
952 
953   return Result;
954 }
955 
956 ///   objc-protocol-refs:
957 ///     '<' identifier-list '>'
958 ///
959 bool Parser::
960 ParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
961                             llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
962                             bool WarnOnDeclarations,
963                             SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
964   assert(Tok.is(tok::less) && "expected <");
965 
966   LAngleLoc = ConsumeToken(); // the "<"
967 
968   llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
969 
970   while (1) {
971     if (Tok.is(tok::code_completion)) {
972       Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
973                                                  ProtocolIdents.size());
974       ConsumeCodeCompletionToken();
975     }
976 
977     if (Tok.isNot(tok::identifier)) {
978       Diag(Tok, diag::err_expected_ident);
979       SkipUntil(tok::greater);
980       return true;
981     }
982     ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
983                                        Tok.getLocation()));
984     ProtocolLocs.push_back(Tok.getLocation());
985     ConsumeToken();
986 
987     if (Tok.isNot(tok::comma))
988       break;
989     ConsumeToken();
990   }
991 
992   // Consume the '>'.
993   if (Tok.isNot(tok::greater)) {
994     Diag(Tok, diag::err_expected_greater);
995     return true;
996   }
997 
998   EndLoc = ConsumeAnyToken();
999 
1000   // Convert the list of protocols identifiers into a list of protocol decls.
1001   Actions.FindProtocolDeclaration(WarnOnDeclarations,
1002                                   &ProtocolIdents[0], ProtocolIdents.size(),
1003                                   Protocols);
1004   return false;
1005 }
1006 
1007 ///   objc-class-instance-variables:
1008 ///     '{' objc-instance-variable-decl-list[opt] '}'
1009 ///
1010 ///   objc-instance-variable-decl-list:
1011 ///     objc-visibility-spec
1012 ///     objc-instance-variable-decl ';'
1013 ///     ';'
1014 ///     objc-instance-variable-decl-list objc-visibility-spec
1015 ///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
1016 ///     objc-instance-variable-decl-list ';'
1017 ///
1018 ///   objc-visibility-spec:
1019 ///     @private
1020 ///     @protected
1021 ///     @public
1022 ///     @package [OBJC2]
1023 ///
1024 ///   objc-instance-variable-decl:
1025 ///     struct-declaration
1026 ///
1027 void Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
1028                                              tok::ObjCKeywordKind visibility,
1029                                              SourceLocation atLoc) {
1030   assert(Tok.is(tok::l_brace) && "expected {");
1031   llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
1032 
1033   ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
1034 
1035   SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
1036 
1037   // While we still have something to read, read the instance variables.
1038   while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1039     // Each iteration of this loop reads one objc-instance-variable-decl.
1040 
1041     // Check for extraneous top-level semicolon.
1042     if (Tok.is(tok::semi)) {
1043       Diag(Tok, diag::ext_extra_ivar_semi)
1044         << FixItHint::CreateRemoval(Tok.getLocation());
1045       ConsumeToken();
1046       continue;
1047     }
1048 
1049     // Set the default visibility to private.
1050     if (Tok.is(tok::at)) { // parse objc-visibility-spec
1051       ConsumeToken(); // eat the @ sign
1052 
1053       if (Tok.is(tok::code_completion)) {
1054         Actions.CodeCompleteObjCAtVisibility(getCurScope());
1055         ConsumeCodeCompletionToken();
1056       }
1057 
1058       switch (Tok.getObjCKeywordID()) {
1059       case tok::objc_private:
1060       case tok::objc_public:
1061       case tok::objc_protected:
1062       case tok::objc_package:
1063         visibility = Tok.getObjCKeywordID();
1064         ConsumeToken();
1065         continue;
1066       default:
1067         Diag(Tok, diag::err_objc_illegal_visibility_spec);
1068         continue;
1069       }
1070     }
1071 
1072     if (Tok.is(tok::code_completion)) {
1073       Actions.CodeCompleteOrdinaryName(getCurScope(),
1074                                        Action::CCC_ObjCInstanceVariableList);
1075       ConsumeCodeCompletionToken();
1076     }
1077 
1078     struct ObjCIvarCallback : FieldCallback {
1079       Parser &P;
1080       DeclPtrTy IDecl;
1081       tok::ObjCKeywordKind visibility;
1082       llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1083 
1084       ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1085                        llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1086         P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1087       }
1088 
1089       DeclPtrTy invoke(FieldDeclarator &FD) {
1090         // Install the declarator into the interface decl.
1091         DeclPtrTy Field
1092           = P.Actions.ActOnIvar(P.getCurScope(),
1093                                 FD.D.getDeclSpec().getSourceRange().getBegin(),
1094                                 IDecl, FD.D, FD.BitfieldSize, visibility);
1095         if (Field)
1096           AllIvarDecls.push_back(Field);
1097         return Field;
1098       }
1099     } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1100 
1101     // Parse all the comma separated declarators.
1102     DeclSpec DS;
1103     ParseStructDeclaration(DS, Callback);
1104 
1105     if (Tok.is(tok::semi)) {
1106       ConsumeToken();
1107     } else {
1108       Diag(Tok, diag::err_expected_semi_decl_list);
1109       // Skip to end of block or statement
1110       SkipUntil(tok::r_brace, true, true);
1111     }
1112   }
1113   SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1114   // Call ActOnFields() even if we don't have any decls. This is useful
1115   // for code rewriting tools that need to be aware of the empty list.
1116   Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1117                       AllIvarDecls.data(), AllIvarDecls.size(),
1118                       LBraceLoc, RBraceLoc, 0);
1119   return;
1120 }
1121 
1122 ///   objc-protocol-declaration:
1123 ///     objc-protocol-definition
1124 ///     objc-protocol-forward-reference
1125 ///
1126 ///   objc-protocol-definition:
1127 ///     @protocol identifier
1128 ///       objc-protocol-refs[opt]
1129 ///       objc-interface-decl-list
1130 ///     @end
1131 ///
1132 ///   objc-protocol-forward-reference:
1133 ///     @protocol identifier-list ';'
1134 ///
1135 ///   "@protocol identifier ;" should be resolved as "@protocol
1136 ///   identifier-list ;": objc-interface-decl-list may not start with a
1137 ///   semicolon in the first alternative if objc-protocol-refs are omitted.
1138 Parser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1139                                                       AttributeList *attrList) {
1140   assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
1141          "ParseObjCAtProtocolDeclaration(): Expected @protocol");
1142   ConsumeToken(); // the "protocol" identifier
1143 
1144   if (Tok.is(tok::code_completion)) {
1145     Actions.CodeCompleteObjCProtocolDecl(getCurScope());
1146     ConsumeCodeCompletionToken();
1147   }
1148 
1149   if (Tok.isNot(tok::identifier)) {
1150     Diag(Tok, diag::err_expected_ident); // missing protocol name.
1151     return DeclPtrTy();
1152   }
1153   // Save the protocol name, then consume it.
1154   IdentifierInfo *protocolName = Tok.getIdentifierInfo();
1155   SourceLocation nameLoc = ConsumeToken();
1156 
1157   if (Tok.is(tok::semi)) { // forward declaration of one protocol.
1158     IdentifierLocPair ProtoInfo(protocolName, nameLoc);
1159     ConsumeToken();
1160     return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
1161                                                    attrList);
1162   }
1163 
1164   if (Tok.is(tok::comma)) { // list of forward declarations.
1165     llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
1166     ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
1167 
1168     // Parse the list of forward declarations.
1169     while (1) {
1170       ConsumeToken(); // the ','
1171       if (Tok.isNot(tok::identifier)) {
1172         Diag(Tok, diag::err_expected_ident);
1173         SkipUntil(tok::semi);
1174         return DeclPtrTy();
1175       }
1176       ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
1177                                                Tok.getLocation()));
1178       ConsumeToken(); // the identifier
1179 
1180       if (Tok.isNot(tok::comma))
1181         break;
1182     }
1183     // Consume the ';'.
1184     if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1185       return DeclPtrTy();
1186 
1187     return Actions.ActOnForwardProtocolDeclaration(AtLoc,
1188                                                    &ProtocolRefs[0],
1189                                                    ProtocolRefs.size(),
1190                                                    attrList);
1191   }
1192 
1193   // Last, and definitely not least, parse a protocol declaration.
1194   SourceLocation LAngleLoc, EndProtoLoc;
1195 
1196   llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
1197   llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1198   if (Tok.is(tok::less) &&
1199       ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
1200                                   LAngleLoc, EndProtoLoc))
1201     return DeclPtrTy();
1202 
1203   DeclPtrTy ProtoType =
1204     Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1205                                         ProtocolRefs.data(),
1206                                         ProtocolRefs.size(),
1207                                         ProtocolLocs.data(),
1208                                         EndProtoLoc, attrList);
1209   ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1210   return ProtoType;
1211 }
1212 
1213 ///   objc-implementation:
1214 ///     objc-class-implementation-prologue
1215 ///     objc-category-implementation-prologue
1216 ///
1217 ///   objc-class-implementation-prologue:
1218 ///     @implementation identifier objc-superclass[opt]
1219 ///       objc-class-instance-variables[opt]
1220 ///
1221 ///   objc-category-implementation-prologue:
1222 ///     @implementation identifier ( identifier )
1223 Parser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
1224   SourceLocation atLoc) {
1225   assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1226          "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1227   ConsumeToken(); // the "implementation" identifier
1228 
1229   // Code completion after '@implementation'.
1230   if (Tok.is(tok::code_completion)) {
1231     Actions.CodeCompleteObjCImplementationDecl(getCurScope());
1232     ConsumeCodeCompletionToken();
1233   }
1234 
1235   if (Tok.isNot(tok::identifier)) {
1236     Diag(Tok, diag::err_expected_ident); // missing class or category name.
1237     return DeclPtrTy();
1238   }
1239   // We have a class or category name - consume it.
1240   IdentifierInfo *nameId = Tok.getIdentifierInfo();
1241   SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1242 
1243   if (Tok.is(tok::l_paren)) {
1244     // we have a category implementation.
1245     SourceLocation lparenLoc = ConsumeParen();
1246     SourceLocation categoryLoc, rparenLoc;
1247     IdentifierInfo *categoryId = 0;
1248 
1249     if (Tok.is(tok::code_completion)) {
1250       Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
1251       ConsumeCodeCompletionToken();
1252     }
1253 
1254     if (Tok.is(tok::identifier)) {
1255       categoryId = Tok.getIdentifierInfo();
1256       categoryLoc = ConsumeToken();
1257     } else {
1258       Diag(Tok, diag::err_expected_ident); // missing category name.
1259       return DeclPtrTy();
1260     }
1261     if (Tok.isNot(tok::r_paren)) {
1262       Diag(Tok, diag::err_expected_rparen);
1263       SkipUntil(tok::r_paren, false); // don't stop at ';'
1264       return DeclPtrTy();
1265     }
1266     rparenLoc = ConsumeParen();
1267     DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
1268                                     atLoc, nameId, nameLoc, categoryId,
1269                                     categoryLoc);
1270     ObjCImpDecl = ImplCatType;
1271     PendingObjCImpDecl.push_back(ObjCImpDecl);
1272     return DeclPtrTy();
1273   }
1274   // We have a class implementation
1275   SourceLocation superClassLoc;
1276   IdentifierInfo *superClassId = 0;
1277   if (Tok.is(tok::colon)) {
1278     // We have a super class
1279     ConsumeToken();
1280     if (Tok.isNot(tok::identifier)) {
1281       Diag(Tok, diag::err_expected_ident); // missing super class name.
1282       return DeclPtrTy();
1283     }
1284     superClassId = Tok.getIdentifierInfo();
1285     superClassLoc = ConsumeToken(); // Consume super class name
1286   }
1287   DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
1288                                   atLoc, nameId, nameLoc,
1289                                   superClassId, superClassLoc);
1290 
1291   if (Tok.is(tok::l_brace)) // we have ivars
1292     ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
1293                                     tok::objc_private, atLoc);
1294   ObjCImpDecl = ImplClsType;
1295   PendingObjCImpDecl.push_back(ObjCImpDecl);
1296 
1297   return DeclPtrTy();
1298 }
1299 
1300 Parser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1301   assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1302          "ParseObjCAtEndDeclaration(): Expected @end");
1303   DeclPtrTy Result = ObjCImpDecl;
1304   ConsumeToken(); // the "end" identifier
1305   if (ObjCImpDecl) {
1306     Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
1307     ObjCImpDecl = DeclPtrTy();
1308     PendingObjCImpDecl.pop_back();
1309   }
1310   else {
1311     // missing @implementation
1312     Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1313   }
1314   return Result;
1315 }
1316 
1317 Parser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
1318   if (PendingObjCImpDecl.empty())
1319     return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
1320   DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1321   Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
1322   return Actions.ConvertDeclToDeclGroup(ImpDecl);
1323 }
1324 
1325 ///   compatibility-alias-decl:
1326 ///     @compatibility_alias alias-name  class-name ';'
1327 ///
1328 Parser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1329   assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1330          "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1331   ConsumeToken(); // consume compatibility_alias
1332   if (Tok.isNot(tok::identifier)) {
1333     Diag(Tok, diag::err_expected_ident);
1334     return DeclPtrTy();
1335   }
1336   IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1337   SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1338   if (Tok.isNot(tok::identifier)) {
1339     Diag(Tok, diag::err_expected_ident);
1340     return DeclPtrTy();
1341   }
1342   IdentifierInfo *classId = Tok.getIdentifierInfo();
1343   SourceLocation classLoc = ConsumeToken(); // consume class-name;
1344   if (Tok.isNot(tok::semi)) {
1345     Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1346     return DeclPtrTy();
1347   }
1348   return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1349                                         classId, classLoc);
1350 }
1351 
1352 ///   property-synthesis:
1353 ///     @synthesize property-ivar-list ';'
1354 ///
1355 ///   property-ivar-list:
1356 ///     property-ivar
1357 ///     property-ivar-list ',' property-ivar
1358 ///
1359 ///   property-ivar:
1360 ///     identifier
1361 ///     identifier '=' identifier
1362 ///
1363 Parser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1364   assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1365          "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1366   SourceLocation loc = ConsumeToken(); // consume synthesize
1367 
1368   while (true) {
1369     if (Tok.is(tok::code_completion)) {
1370       Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
1371       ConsumeCodeCompletionToken();
1372     }
1373 
1374     if (Tok.isNot(tok::identifier)) {
1375       Diag(Tok, diag::err_synthesized_property_name);
1376       SkipUntil(tok::semi);
1377       return DeclPtrTy();
1378     }
1379 
1380     IdentifierInfo *propertyIvar = 0;
1381     IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1382     SourceLocation propertyLoc = ConsumeToken(); // consume property name
1383     if (Tok.is(tok::equal)) {
1384       // property '=' ivar-name
1385       ConsumeToken(); // consume '='
1386 
1387       if (Tok.is(tok::code_completion)) {
1388         Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
1389                                                        ObjCImpDecl);
1390         ConsumeCodeCompletionToken();
1391       }
1392 
1393       if (Tok.isNot(tok::identifier)) {
1394         Diag(Tok, diag::err_expected_ident);
1395         break;
1396       }
1397       propertyIvar = Tok.getIdentifierInfo();
1398       ConsumeToken(); // consume ivar-name
1399     }
1400     Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
1401                                   propertyId, propertyIvar);
1402     if (Tok.isNot(tok::comma))
1403       break;
1404     ConsumeToken(); // consume ','
1405   }
1406   if (Tok.isNot(tok::semi)) {
1407     Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1408     SkipUntil(tok::semi);
1409   }
1410   else
1411     ConsumeToken(); // consume ';'
1412   return DeclPtrTy();
1413 }
1414 
1415 ///   property-dynamic:
1416 ///     @dynamic  property-list
1417 ///
1418 ///   property-list:
1419 ///     identifier
1420 ///     property-list ',' identifier
1421 ///
1422 Parser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1423   assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1424          "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1425   SourceLocation loc = ConsumeToken(); // consume dynamic
1426   while (true) {
1427     if (Tok.is(tok::code_completion)) {
1428       Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
1429       ConsumeCodeCompletionToken();
1430     }
1431 
1432     if (Tok.isNot(tok::identifier)) {
1433       Diag(Tok, diag::err_expected_ident);
1434       SkipUntil(tok::semi);
1435       return DeclPtrTy();
1436     }
1437 
1438     IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1439     SourceLocation propertyLoc = ConsumeToken(); // consume property name
1440     Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
1441                                   propertyId, 0);
1442 
1443     if (Tok.isNot(tok::comma))
1444       break;
1445     ConsumeToken(); // consume ','
1446   }
1447   if (Tok.isNot(tok::semi)) {
1448     Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
1449     SkipUntil(tok::semi);
1450   }
1451   else
1452     ConsumeToken(); // consume ';'
1453   return DeclPtrTy();
1454 }
1455 
1456 ///  objc-throw-statement:
1457 ///    throw expression[opt];
1458 ///
1459 Parser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
1460   OwningExprResult Res(Actions);
1461   ConsumeToken(); // consume throw
1462   if (Tok.isNot(tok::semi)) {
1463     Res = ParseExpression();
1464     if (Res.isInvalid()) {
1465       SkipUntil(tok::semi);
1466       return StmtError();
1467     }
1468   }
1469   // consume ';'
1470   ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
1471   return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), getCurScope());
1472 }
1473 
1474 /// objc-synchronized-statement:
1475 ///   @synchronized '(' expression ')' compound-statement
1476 ///
1477 Parser::OwningStmtResult
1478 Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1479   ConsumeToken(); // consume synchronized
1480   if (Tok.isNot(tok::l_paren)) {
1481     Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
1482     return StmtError();
1483   }
1484   ConsumeParen();  // '('
1485   OwningExprResult Res(ParseExpression());
1486   if (Res.isInvalid()) {
1487     SkipUntil(tok::semi);
1488     return StmtError();
1489   }
1490   if (Tok.isNot(tok::r_paren)) {
1491     Diag(Tok, diag::err_expected_lbrace);
1492     return StmtError();
1493   }
1494   ConsumeParen();  // ')'
1495   if (Tok.isNot(tok::l_brace)) {
1496     Diag(Tok, diag::err_expected_lbrace);
1497     return StmtError();
1498   }
1499   // Enter a scope to hold everything within the compound stmt.  Compound
1500   // statements can always hold declarations.
1501   ParseScope BodyScope(this, Scope::DeclScope);
1502 
1503   OwningStmtResult SynchBody(ParseCompoundStatementBody());
1504 
1505   BodyScope.Exit();
1506   if (SynchBody.isInvalid())
1507     SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1508   return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
1509 }
1510 
1511 ///  objc-try-catch-statement:
1512 ///    @try compound-statement objc-catch-list[opt]
1513 ///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1514 ///
1515 ///  objc-catch-list:
1516 ///    @catch ( parameter-declaration ) compound-statement
1517 ///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1518 ///  catch-parameter-declaration:
1519 ///     parameter-declaration
1520 ///     '...' [OBJC2]
1521 ///
1522 Parser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1523   bool catch_or_finally_seen = false;
1524 
1525   ConsumeToken(); // consume try
1526   if (Tok.isNot(tok::l_brace)) {
1527     Diag(Tok, diag::err_expected_lbrace);
1528     return StmtError();
1529   }
1530   StmtVector CatchStmts(Actions);
1531   OwningStmtResult FinallyStmt(Actions);
1532   ParseScope TryScope(this, Scope::DeclScope);
1533   OwningStmtResult TryBody(ParseCompoundStatementBody());
1534   TryScope.Exit();
1535   if (TryBody.isInvalid())
1536     TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1537 
1538   while (Tok.is(tok::at)) {
1539     // At this point, we need to lookahead to determine if this @ is the start
1540     // of an @catch or @finally.  We don't want to consume the @ token if this
1541     // is an @try or @encode or something else.
1542     Token AfterAt = GetLookAheadToken(1);
1543     if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
1544         !AfterAt.isObjCAtKeyword(tok::objc_finally))
1545       break;
1546 
1547     SourceLocation AtCatchFinallyLoc = ConsumeToken();
1548     if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1549       DeclPtrTy FirstPart;
1550       ConsumeToken(); // consume catch
1551       if (Tok.is(tok::l_paren)) {
1552         ConsumeParen();
1553         ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1554         if (Tok.isNot(tok::ellipsis)) {
1555           DeclSpec DS;
1556           ParseDeclarationSpecifiers(DS);
1557           // For some odd reason, the name of the exception variable is
1558           // optional. As a result, we need to use "PrototypeContext", because
1559           // we must accept either 'declarator' or 'abstract-declarator' here.
1560           Declarator ParmDecl(DS, Declarator::PrototypeContext);
1561           ParseDeclarator(ParmDecl);
1562 
1563           // Inform the actions module about the declarator, so it
1564           // gets added to the current scope.
1565           FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
1566         } else
1567           ConsumeToken(); // consume '...'
1568 
1569         SourceLocation RParenLoc;
1570 
1571         if (Tok.is(tok::r_paren))
1572           RParenLoc = ConsumeParen();
1573         else // Skip over garbage, until we get to ')'.  Eat the ')'.
1574           SkipUntil(tok::r_paren, true, false);
1575 
1576         OwningStmtResult CatchBody(Actions, true);
1577         if (Tok.is(tok::l_brace))
1578           CatchBody = ParseCompoundStatementBody();
1579         else
1580           Diag(Tok, diag::err_expected_lbrace);
1581         if (CatchBody.isInvalid())
1582           CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
1583 
1584         OwningStmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
1585                                                               RParenLoc,
1586                                                               FirstPart,
1587                                                               move(CatchBody));
1588         if (!Catch.isInvalid())
1589           CatchStmts.push_back(Catch.release());
1590 
1591       } else {
1592         Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
1593           << "@catch clause";
1594         return StmtError();
1595       }
1596       catch_or_finally_seen = true;
1597     } else {
1598       assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
1599       ConsumeToken(); // consume finally
1600       ParseScope FinallyScope(this, Scope::DeclScope);
1601 
1602       OwningStmtResult FinallyBody(Actions, true);
1603       if (Tok.is(tok::l_brace))
1604         FinallyBody = ParseCompoundStatementBody();
1605       else
1606         Diag(Tok, diag::err_expected_lbrace);
1607       if (FinallyBody.isInvalid())
1608         FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
1609       FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
1610                                                    move(FinallyBody));
1611       catch_or_finally_seen = true;
1612       break;
1613     }
1614   }
1615   if (!catch_or_finally_seen) {
1616     Diag(atLoc, diag::err_missing_catch_finally);
1617     return StmtError();
1618   }
1619 
1620   return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody),
1621                                     move_arg(CatchStmts),
1622                                     move(FinallyStmt));
1623 }
1624 
1625 ///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1626 ///
1627 Parser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1628   DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
1629 
1630   PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
1631                                         PP.getSourceManager(),
1632                                         "parsing Objective-C method");
1633 
1634   // parse optional ';'
1635   if (Tok.is(tok::semi)) {
1636     if (ObjCImpDecl) {
1637       Diag(Tok, diag::warn_semicolon_before_method_body)
1638         << FixItHint::CreateRemoval(Tok.getLocation());
1639     }
1640     ConsumeToken();
1641   }
1642 
1643   // We should have an opening brace now.
1644   if (Tok.isNot(tok::l_brace)) {
1645     Diag(Tok, diag::err_expected_method_body);
1646 
1647     // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1648     SkipUntil(tok::l_brace, true, true);
1649 
1650     // If we didn't find the '{', bail out.
1651     if (Tok.isNot(tok::l_brace))
1652       return DeclPtrTy();
1653   }
1654   SourceLocation BraceLoc = Tok.getLocation();
1655 
1656   // Enter a scope for the method body.
1657   ParseScope BodyScope(this,
1658                        Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
1659 
1660   // Tell the actions module that we have entered a method definition with the
1661   // specified Declarator for the method.
1662   Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
1663 
1664   OwningStmtResult FnBody(ParseCompoundStatementBody());
1665 
1666   // If the function body could not be parsed, make a bogus compoundstmt.
1667   if (FnBody.isInvalid())
1668     FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1669                                        MultiStmtArg(Actions), false);
1670 
1671   // TODO: Pass argument information.
1672   Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
1673 
1674   // Leave the function body scope.
1675   BodyScope.Exit();
1676 
1677   return MDecl;
1678 }
1679 
1680 Parser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
1681   if (Tok.is(tok::code_completion)) {
1682     Actions.CodeCompleteObjCAtStatement(getCurScope());
1683     ConsumeCodeCompletionToken();
1684     return StmtError();
1685   }
1686 
1687   if (Tok.isObjCAtKeyword(tok::objc_try))
1688     return ParseObjCTryStmt(AtLoc);
1689 
1690   if (Tok.isObjCAtKeyword(tok::objc_throw))
1691     return ParseObjCThrowStmt(AtLoc);
1692 
1693   if (Tok.isObjCAtKeyword(tok::objc_synchronized))
1694     return ParseObjCSynchronizedStmt(AtLoc);
1695 
1696   OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
1697   if (Res.isInvalid()) {
1698     // If the expression is invalid, skip ahead to the next semicolon. Not
1699     // doing this opens us up to the possibility of infinite loops if
1700     // ParseExpression does not consume any tokens.
1701     SkipUntil(tok::semi);
1702     return StmtError();
1703   }
1704 
1705   // Otherwise, eat the semicolon.
1706   ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1707   return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
1708 }
1709 
1710 Parser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
1711   switch (Tok.getKind()) {
1712   case tok::code_completion:
1713     Actions.CodeCompleteObjCAtExpression(getCurScope());
1714     ConsumeCodeCompletionToken();
1715     return ExprError();
1716 
1717   case tok::string_literal:    // primary-expression: string-literal
1718   case tok::wide_string_literal:
1719     return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1720   default:
1721     if (Tok.getIdentifierInfo() == 0)
1722       return ExprError(Diag(AtLoc, diag::err_unexpected_at));
1723 
1724     switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
1725     case tok::objc_encode:
1726       return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
1727     case tok::objc_protocol:
1728       return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
1729     case tok::objc_selector:
1730       return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
1731     default:
1732       return ExprError(Diag(AtLoc, diag::err_unexpected_at));
1733     }
1734   }
1735 }
1736 
1737 /// \brirg Parse the receiver of an Objective-C++ message send.
1738 ///
1739 /// This routine parses the receiver of a message send in
1740 /// Objective-C++ either as a type or as an expression. Note that this
1741 /// routine must not be called to parse a send to 'super', since it
1742 /// has no way to return such a result.
1743 ///
1744 /// \param IsExpr Whether the receiver was parsed as an expression.
1745 ///
1746 /// \param TypeOrExpr If the receiver was parsed as an expression (\c
1747 /// IsExpr is true), the parsed expression. If the receiver was parsed
1748 /// as a type (\c IsExpr is false), the parsed type.
1749 ///
1750 /// \returns True if an error occurred during parsing or semantic
1751 /// analysis, in which case the arguments do not have valid
1752 /// values. Otherwise, returns false for a successful parse.
1753 ///
1754 ///   objc-receiver: [C++]
1755 ///     'super' [not parsed here]
1756 ///     expression
1757 ///     simple-type-specifier
1758 ///     typename-specifier
1759 bool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
1760   if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
1761       Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
1762     TryAnnotateTypeOrScopeToken();
1763 
1764   if (!isCXXSimpleTypeSpecifier()) {
1765     //   objc-receiver:
1766     //     expression
1767     OwningExprResult Receiver = ParseExpression();
1768     if (Receiver.isInvalid())
1769       return true;
1770 
1771     IsExpr = true;
1772     TypeOrExpr = Receiver.take();
1773     return false;
1774   }
1775 
1776   // objc-receiver:
1777   //   typename-specifier
1778   //   simple-type-specifier
1779   //   expression (that starts with one of the above)
1780   DeclSpec DS;
1781   ParseCXXSimpleTypeSpecifier(DS);
1782 
1783   if (Tok.is(tok::l_paren)) {
1784     // If we see an opening parentheses at this point, we are
1785     // actually parsing an expression that starts with a
1786     // function-style cast, e.g.,
1787     //
1788     //   postfix-expression:
1789     //     simple-type-specifier ( expression-list [opt] )
1790     //     typename-specifier ( expression-list [opt] )
1791     //
1792     // Parse the remainder of this case, then the (optional)
1793     // postfix-expression suffix, followed by the (optional)
1794     // right-hand side of the binary expression. We have an
1795     // instance method.
1796     OwningExprResult Receiver = ParseCXXTypeConstructExpression(DS);
1797     if (!Receiver.isInvalid())
1798       Receiver = ParsePostfixExpressionSuffix(move(Receiver));
1799     if (!Receiver.isInvalid())
1800       Receiver = ParseRHSOfBinaryExpression(move(Receiver), prec::Comma);
1801     if (Receiver.isInvalid())
1802       return true;
1803 
1804     IsExpr = true;
1805     TypeOrExpr = Receiver.take();
1806     return false;
1807   }
1808 
1809   // We have a class message. Turn the simple-type-specifier or
1810   // typename-specifier we parsed into a type and parse the
1811   // remainder of the class message.
1812   Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1813   TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
1814   if (Type.isInvalid())
1815     return true;
1816 
1817   IsExpr = false;
1818   TypeOrExpr = Type.get();
1819   return false;
1820 }
1821 
1822 /// \brief Determine whether the parser is currently referring to a an
1823 /// Objective-C message send, using a simplified heuristic to avoid overhead.
1824 ///
1825 /// This routine will only return true for a subset of valid message-send
1826 /// expressions.
1827 bool Parser::isSimpleObjCMessageExpression() {
1828   assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
1829          "Incorrect start for isSimpleObjCMessageExpression");
1830   return GetLookAheadToken(1).is(tok::identifier) &&
1831          GetLookAheadToken(2).is(tok::identifier);
1832 }
1833 
1834 ///   objc-message-expr:
1835 ///     '[' objc-receiver objc-message-args ']'
1836 ///
1837 ///   objc-receiver: [C]
1838 ///     'super'
1839 ///     expression
1840 ///     class-name
1841 ///     type-name
1842 ///
1843 Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
1844   assert(Tok.is(tok::l_square) && "'[' expected");
1845   SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1846 
1847   if (Tok.is(tok::code_completion)) {
1848     Actions.CodeCompleteObjCMessageReceiver(getCurScope());
1849     ConsumeCodeCompletionToken();
1850     SkipUntil(tok::r_square);
1851     return ExprError();
1852   }
1853 
1854   if (getLang().CPlusPlus) {
1855     // We completely separate the C and C++ cases because C++ requires
1856     // more complicated (read: slower) parsing.
1857 
1858     // Handle send to super.
1859     // FIXME: This doesn't benefit from the same typo-correction we
1860     // get in Objective-C.
1861     if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
1862         NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
1863       return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
1864                                             ExprArg(Actions));
1865 
1866     // Parse the receiver, which is either a type or an expression.
1867     bool IsExpr;
1868     void *TypeOrExpr;
1869     if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
1870       SkipUntil(tok::r_square);
1871       return ExprError();
1872     }
1873 
1874     if (IsExpr)
1875       return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0,
1876                                          OwningExprResult(Actions, TypeOrExpr));
1877 
1878     return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1879                                           TypeOrExpr, ExprArg(Actions));
1880   }
1881 
1882   if (Tok.is(tok::identifier)) {
1883     IdentifierInfo *Name = Tok.getIdentifierInfo();
1884     SourceLocation NameLoc = Tok.getLocation();
1885     TypeTy *ReceiverType;
1886     switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
1887                                        Name == Ident_super,
1888                                        NextToken().is(tok::period),
1889                                        ReceiverType)) {
1890     case Action::ObjCSuperMessage:
1891       return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
1892                                             ExprArg(Actions));
1893 
1894     case Action::ObjCClassMessage:
1895       if (!ReceiverType) {
1896         SkipUntil(tok::r_square);
1897         return ExprError();
1898       }
1899 
1900       ConsumeToken(); // the type name
1901 
1902       return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1903                                             ReceiverType,
1904                                             ExprArg(Actions));
1905 
1906     case Action::ObjCInstanceMessage:
1907       // Fall through to parse an expression.
1908       break;
1909     }
1910   }
1911 
1912   // Otherwise, an arbitrary expression can be the receiver of a send.
1913   OwningExprResult Res(ParseExpression());
1914   if (Res.isInvalid()) {
1915     SkipUntil(tok::r_square);
1916     return move(Res);
1917   }
1918 
1919   return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0,
1920                                         move(Res));
1921 }
1922 
1923 /// \brief Parse the remainder of an Objective-C message following the
1924 /// '[' objc-receiver.
1925 ///
1926 /// This routine handles sends to super, class messages (sent to a
1927 /// class name), and instance messages (sent to an object), and the
1928 /// target is represented by \p SuperLoc, \p ReceiverType, or \p
1929 /// ReceiverExpr, respectively. Only one of these parameters may have
1930 /// a valid value.
1931 ///
1932 /// \param LBracLoc The location of the opening '['.
1933 ///
1934 /// \param SuperLoc If this is a send to 'super', the location of the
1935 /// 'super' keyword that indicates a send to the superclass.
1936 ///
1937 /// \param ReceiverType If this is a class message, the type of the
1938 /// class we are sending a message to.
1939 ///
1940 /// \param ReceiverExpr If this is an instance message, the expression
1941 /// used to compute the receiver object.
1942 ///
1943 ///   objc-message-args:
1944 ///     objc-selector
1945 ///     objc-keywordarg-list
1946 ///
1947 ///   objc-keywordarg-list:
1948 ///     objc-keywordarg
1949 ///     objc-keywordarg-list objc-keywordarg
1950 ///
1951 ///   objc-keywordarg:
1952 ///     selector-name[opt] ':' objc-keywordexpr
1953 ///
1954 ///   objc-keywordexpr:
1955 ///     nonempty-expr-list
1956 ///
1957 ///   nonempty-expr-list:
1958 ///     assignment-expression
1959 ///     nonempty-expr-list , assignment-expression
1960 ///
1961 Parser::OwningExprResult
1962 Parser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1963                                        SourceLocation SuperLoc,
1964                                        TypeTy *ReceiverType,
1965                                        ExprArg ReceiverExpr) {
1966   if (Tok.is(tok::code_completion)) {
1967     if (SuperLoc.isValid())
1968       Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0);
1969     else if (ReceiverType)
1970       Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0);
1971     else
1972       Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr.get(),
1973                                               0, 0);
1974     ConsumeCodeCompletionToken();
1975   }
1976 
1977   // Parse objc-selector
1978   SourceLocation Loc;
1979   IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
1980 
1981   SourceLocation SelectorLoc = Loc;
1982 
1983   llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1984   ExprVector KeyExprs(Actions);
1985 
1986   if (Tok.is(tok::colon)) {
1987     while (1) {
1988       // Each iteration parses a single keyword argument.
1989       KeyIdents.push_back(selIdent);
1990 
1991       if (Tok.isNot(tok::colon)) {
1992         Diag(Tok, diag::err_expected_colon);
1993         // We must manually skip to a ']', otherwise the expression skipper will
1994         // stop at the ']' when it skips to the ';'.  We want it to skip beyond
1995         // the enclosing expression.
1996         SkipUntil(tok::r_square);
1997         return ExprError();
1998       }
1999 
2000       ConsumeToken(); // Eat the ':'.
2001       ///  Parse the expression after ':'
2002       OwningExprResult Res(ParseAssignmentExpression());
2003       if (Res.isInvalid()) {
2004         // We must manually skip to a ']', otherwise the expression skipper will
2005         // stop at the ']' when it skips to the ';'.  We want it to skip beyond
2006         // the enclosing expression.
2007         SkipUntil(tok::r_square);
2008         return move(Res);
2009       }
2010 
2011       // We have a valid expression.
2012       KeyExprs.push_back(Res.release());
2013 
2014       // Code completion after each argument.
2015       if (Tok.is(tok::code_completion)) {
2016         if (SuperLoc.isValid())
2017           Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
2018                                                KeyIdents.data(),
2019                                                KeyIdents.size());
2020         else if (ReceiverType)
2021           Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2022                                                KeyIdents.data(),
2023                                                KeyIdents.size());
2024         else
2025           Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr.get(),
2026                                                   KeyIdents.data(),
2027                                                   KeyIdents.size());
2028         ConsumeCodeCompletionToken();
2029       }
2030 
2031       // Check for another keyword selector.
2032       selIdent = ParseObjCSelectorPiece(Loc);
2033       if (!selIdent && Tok.isNot(tok::colon))
2034         break;
2035       // We have a selector or a colon, continue parsing.
2036     }
2037     // Parse the, optional, argument list, comma separated.
2038     while (Tok.is(tok::comma)) {
2039       ConsumeToken(); // Eat the ','.
2040       ///  Parse the expression after ','
2041       OwningExprResult Res(ParseAssignmentExpression());
2042       if (Res.isInvalid()) {
2043         // We must manually skip to a ']', otherwise the expression skipper will
2044         // stop at the ']' when it skips to the ';'.  We want it to skip beyond
2045         // the enclosing expression.
2046         SkipUntil(tok::r_square);
2047         return move(Res);
2048       }
2049 
2050       // We have a valid expression.
2051       KeyExprs.push_back(Res.release());
2052     }
2053   } else if (!selIdent) {
2054     Diag(Tok, diag::err_expected_ident); // missing selector name.
2055 
2056     // We must manually skip to a ']', otherwise the expression skipper will
2057     // stop at the ']' when it skips to the ';'.  We want it to skip beyond
2058     // the enclosing expression.
2059     SkipUntil(tok::r_square);
2060     return ExprError();
2061   }
2062 
2063   if (Tok.isNot(tok::r_square)) {
2064     if (Tok.is(tok::identifier))
2065       Diag(Tok, diag::err_expected_colon);
2066     else
2067       Diag(Tok, diag::err_expected_rsquare);
2068     // We must manually skip to a ']', otherwise the expression skipper will
2069     // stop at the ']' when it skips to the ';'.  We want it to skip beyond
2070     // the enclosing expression.
2071     SkipUntil(tok::r_square);
2072     return ExprError();
2073   }
2074 
2075   SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
2076 
2077   unsigned nKeys = KeyIdents.size();
2078   if (nKeys == 0)
2079     KeyIdents.push_back(selIdent);
2080   Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
2081 
2082   if (SuperLoc.isValid())
2083     return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
2084                                      LBracLoc, SelectorLoc, RBracLoc,
2085                                      Action::MultiExprArg(Actions,
2086                                                           KeyExprs.take(),
2087                                                           KeyExprs.size()));
2088   else if (ReceiverType)
2089     return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
2090                                      LBracLoc, SelectorLoc, RBracLoc,
2091                                      Action::MultiExprArg(Actions,
2092                                                           KeyExprs.take(),
2093                                                           KeyExprs.size()));
2094   return Actions.ActOnInstanceMessage(getCurScope(), move(ReceiverExpr), Sel,
2095                                       LBracLoc, SelectorLoc, RBracLoc,
2096                                       Action::MultiExprArg(Actions,
2097                                                            KeyExprs.take(),
2098                                                            KeyExprs.size()));
2099 }
2100 
2101 Parser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
2102   OwningExprResult Res(ParseStringLiteralExpression());
2103   if (Res.isInvalid()) return move(Res);
2104 
2105   // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
2106   // expressions.  At this point, we know that the only valid thing that starts
2107   // with '@' is an @"".
2108   llvm::SmallVector<SourceLocation, 4> AtLocs;
2109   ExprVector AtStrings(Actions);
2110   AtLocs.push_back(AtLoc);
2111   AtStrings.push_back(Res.release());
2112 
2113   while (Tok.is(tok::at)) {
2114     AtLocs.push_back(ConsumeToken()); // eat the @.
2115 
2116     // Invalid unless there is a string literal.
2117     if (!isTokenStringLiteral())
2118       return ExprError(Diag(Tok, diag::err_objc_concat_string));
2119 
2120     OwningExprResult Lit(ParseStringLiteralExpression());
2121     if (Lit.isInvalid())
2122       return move(Lit);
2123 
2124     AtStrings.push_back(Lit.release());
2125   }
2126 
2127   return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
2128                                               AtStrings.size()));
2129 }
2130 
2131 ///    objc-encode-expression:
2132 ///      @encode ( type-name )
2133 Parser::OwningExprResult
2134 Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
2135   assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
2136 
2137   SourceLocation EncLoc = ConsumeToken();
2138 
2139   if (Tok.isNot(tok::l_paren))
2140     return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
2141 
2142   SourceLocation LParenLoc = ConsumeParen();
2143 
2144   TypeResult Ty = ParseTypeName();
2145 
2146   SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2147 
2148   if (Ty.isInvalid())
2149     return ExprError();
2150 
2151   return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
2152                                                  Ty.get(), RParenLoc));
2153 }
2154 
2155 ///     objc-protocol-expression
2156 ///       @protocol ( protocol-name )
2157 Parser::OwningExprResult
2158 Parser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
2159   SourceLocation ProtoLoc = ConsumeToken();
2160 
2161   if (Tok.isNot(tok::l_paren))
2162     return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
2163 
2164   SourceLocation LParenLoc = ConsumeParen();
2165 
2166   if (Tok.isNot(tok::identifier))
2167     return ExprError(Diag(Tok, diag::err_expected_ident));
2168 
2169   IdentifierInfo *protocolId = Tok.getIdentifierInfo();
2170   ConsumeToken();
2171 
2172   SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2173 
2174   return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2175                                                    LParenLoc, RParenLoc));
2176 }
2177 
2178 ///     objc-selector-expression
2179 ///       @selector '(' objc-keyword-selector ')'
2180 Parser::OwningExprResult
2181 Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
2182   SourceLocation SelectorLoc = ConsumeToken();
2183 
2184   if (Tok.isNot(tok::l_paren))
2185     return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
2186 
2187   llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
2188   SourceLocation LParenLoc = ConsumeParen();
2189   SourceLocation sLoc;
2190   IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
2191   if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
2192     return ExprError(Diag(Tok, diag::err_expected_ident));
2193 
2194   KeyIdents.push_back(SelIdent);
2195   unsigned nColons = 0;
2196   if (Tok.isNot(tok::r_paren)) {
2197     while (1) {
2198       if (Tok.isNot(tok::colon))
2199         return ExprError(Diag(Tok, diag::err_expected_colon));
2200 
2201       nColons++;
2202       ConsumeToken(); // Eat the ':'.
2203       if (Tok.is(tok::r_paren))
2204         break;
2205       // Check for another keyword selector.
2206       SourceLocation Loc;
2207       SelIdent = ParseObjCSelectorPiece(Loc);
2208       KeyIdents.push_back(SelIdent);
2209       if (!SelIdent && Tok.isNot(tok::colon))
2210         break;
2211     }
2212   }
2213   SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2214   Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
2215   return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2216                                                    LParenLoc, RParenLoc));
2217  }
2218