ParseObjc.cpp revision 94ad28b31433058445a27db722f60402ee820bea
1a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the Objective-C portions of the Parser interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
1519510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Parse/Parser.h"
160fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor#include "RAIIObjectsForParser.h"
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
18f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
1919510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Scope.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24891dca671a80c865c6def7259f170ba785e4faaaChris Lattner/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       external-declaration: [C99 6.9]
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
2791fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-class-declaration
2891fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-alias-declaration
2991fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-protocol-definition
3091fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-method-definition
3191fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  '@' 'end'
3295ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation AtLoc = ConsumeToken(); // the "@"
341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
36a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.CodeCompleteObjCAtDirective(getCurScope());
377d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
387d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
39c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  }
4095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian
4195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  Decl *SingleDecl = 0;
42861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  switch (Tok.getObjCKeywordID()) {
435ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_class:
445ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtClassDeclaration(AtLoc);
4595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
467f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_interface: {
470b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
4895ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
4995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
507f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
517f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_protocol: {
520b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
53bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
547f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
555ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
5695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
5795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
585ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
59140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    return ParseObjCAtEndDeclaration(AtLoc);
605ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
6195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
6295ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
635ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
6495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCPropertySynthesize(AtLoc);
6595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
665ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
6795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCPropertyDynamic(AtLoc);
6895ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
695948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  case tok::objc_import:
7094ad28b31433058445a27db722f60402ee820beaDouglas Gregor    if (getLang().Modules)
7194ad28b31433058445a27db722f60402ee820beaDouglas Gregor      return ParseModuleImport(AtLoc);
7294ad28b31433058445a27db722f60402ee820beaDouglas Gregor
7394ad28b31433058445a27db722f60402ee820beaDouglas Gregor    // Fall through
7494ad28b31433058445a27db722f60402ee820beaDouglas Gregor
755ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
765ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
775ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
7895ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = 0;
7995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
8195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  return Actions.ConvertDeclToDeclGroup(SingleDecl);
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// objc-class-declaration:
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
8895ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::DeclGroupPtrTy
8995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
915f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 8> ClassNames;
925f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ClassLocs;
93c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek
941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
96df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
9995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian      return Actions.ConvertDeclToDeclGroup(0);
1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
102c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek    ClassLocs.push_back(Tok.getLocation());
1035f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
105df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
11395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    return Actions.ConvertDeclToDeclGroup(0);
1141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
116c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassLocs.data(),
117c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassNames.size());
1185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
120d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggenvoid Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
121d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen{
122d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
123d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  if (ock == Sema::OCK_None)
124d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    return;
125d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
126d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Decl *Decl = Actions.ActOnAtEnd(getCurScope(), AtLoc);
127d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Diag(AtLoc, diag::err_objc_missing_end)
128d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      << FixItHint::CreateInsertion(AtLoc, "@end\n");
129d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  if (Decl)
130d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(Decl->getLocStart(), diag::note_objc_container_start)
131d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << (int) ock;
132d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  if (!PendingObjCImpDecl.empty())
133d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    PendingObjCImpDecl.pop_back();
134d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  ObjCImpDecl = 0;
135d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen}
136d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
137dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
138dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
139dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
140dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
141dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
142dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier objc-superclass[opt]
144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-class-instance-variables[opt]
146dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
149dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
1501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier '(' identifier[opt] ')'
151dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
152dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
153dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
154dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
155dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
156dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
157dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
159dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
160dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
161dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
162dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
163dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
164dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
165d64251fd56577dd5c78903454632361e094c6dc1Erik VerbruggenDecl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
1667f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                              ParsedAttributes &attrs) {
167861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
168dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
169d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
170dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1723b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@interface'.
1733b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
17423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
1757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
1773b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
1783b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
179df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
181d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
182dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
18363e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
184dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1857ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
186dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
1875512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  if (Tok.is(tok::l_paren) &&
1885512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
1894a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
1914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
1924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    SourceLocation categoryLoc;
194dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
19533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
19623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
1977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
19933ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
20033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
201527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
202df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
203dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
204dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
20505511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
20605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    else if (!getLang().ObjC2) {
207527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
208d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
209dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
2104a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
2124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
213d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
21413d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
21513d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor    if (!attrs.empty()) { // categories don't support attributes.
21613d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      Diag(nameLoc, diag::err_objc_no_attributes_on_category);
21713d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      attrs.clear();
218dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
21913d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
2205512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    // Next, we need to check for any protocol references.
2215512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    SourceLocation LAngleLoc, EndProtoLoc;
2225f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<Decl *, 8> ProtocolRefs;
2235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 8> ProtocolLocs;
2245512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::less) &&
2255512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
22671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
227d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
22805511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian
229d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *CategoryType =
230d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Actions.ActOnStartCategoryInterface(AtLoc,
2315512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        nameId, nameLoc,
2325512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        categoryId, categoryLoc,
2335512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.data(),
2345512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.size(),
2355512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolLocs.data(),
2365512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        EndProtoLoc);
237e6f07f538fd0eddd6c087fcc01d4e4ff19129c71Fariborz Jahanian
238a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    if (Tok.is(tok::l_brace))
239d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
240a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
2412f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian    ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
2425512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    return CategoryType;
243dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
244dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
245dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
246dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
2477ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
248df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
249dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
2503b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
2513b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
2523b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
25323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
2547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
2557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
2563b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2573b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
258df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
259dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
260d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
261dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
262dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
263dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
264dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
265dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
2665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
2675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
26871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
26906036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
27071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
27171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
272d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
2731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
274d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ClsType =
275d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
27606036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
277beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
27818df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                     ProtocolLocs.data(),
2797f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                     EndProtoLoc, attrs.getList());
2801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
281df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
282d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
283dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
2842f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
2855512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  return ClsType;
286dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
287dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
288d0014540005f2a5ab837365db6bd40f479406758John McCall/// The Objective-C property callback.  This should be defined where
289d0014540005f2a5ab837365db6bd40f479406758John McCall/// it's used, but instead it's been lifted to here to support VS2005.
290d0014540005f2a5ab837365db6bd40f479406758John McCallstruct Parser::ObjCPropertyCallback : FieldCallback {
29199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikieprivate:
29299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
29399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikiepublic:
294d0014540005f2a5ab837365db6bd40f479406758John McCall  Parser &P;
2955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVectorImpl<Decl *> &Props;
296d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCDeclSpec &OCDS;
297d0014540005f2a5ab837365db6bd40f479406758John McCall  SourceLocation AtLoc;
298d0014540005f2a5ab837365db6bd40f479406758John McCall  tok::ObjCKeywordKind MethodImplKind;
299d0014540005f2a5ab837365db6bd40f479406758John McCall
300a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  ObjCPropertyCallback(Parser &P,
3015f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &Props,
302d0014540005f2a5ab837365db6bd40f479406758John McCall                       ObjCDeclSpec &OCDS, SourceLocation AtLoc,
303d0014540005f2a5ab837365db6bd40f479406758John McCall                       tok::ObjCKeywordKind MethodImplKind) :
304a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
305d0014540005f2a5ab837365db6bd40f479406758John McCall    MethodImplKind(MethodImplKind) {
306d0014540005f2a5ab837365db6bd40f479406758John McCall  }
307d0014540005f2a5ab837365db6bd40f479406758John McCall
308d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *invoke(FieldDeclarator &FD) {
309d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.D.getIdentifier() == 0) {
310d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
311d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
312d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
313d0014540005f2a5ab837365db6bd40f479406758John McCall    }
314d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.BitfieldSize) {
315d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_bitfield)
316d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
317d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
318d0014540005f2a5ab837365db6bd40f479406758John McCall    }
319d0014540005f2a5ab837365db6bd40f479406758John McCall
320d0014540005f2a5ab837365db6bd40f479406758John McCall    // Install the property declarator into interfaceDecl.
321d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SelName =
322d0014540005f2a5ab837365db6bd40f479406758John McCall      OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
323d0014540005f2a5ab837365db6bd40f479406758John McCall
324d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector GetterSel =
325d0014540005f2a5ab837365db6bd40f479406758John McCall      P.PP.getSelectorTable().getNullarySelector(SelName);
326d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SetterName = OCDS.getSetterName();
327d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector SetterSel;
328d0014540005f2a5ab837365db6bd40f479406758John McCall    if (SetterName)
329d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
330d0014540005f2a5ab837365db6bd40f479406758John McCall    else
331d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
332d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     P.PP.getSelectorTable(),
333d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     FD.D.getIdentifier());
334d0014540005f2a5ab837365db6bd40f479406758John McCall    bool isOverridingProperty = false;
335d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Property =
33623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
337a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                              GetterSel, SetterSel,
338d0014540005f2a5ab837365db6bd40f479406758John McCall                              &isOverridingProperty,
339d0014540005f2a5ab837365db6bd40f479406758John McCall                              MethodImplKind);
340d0014540005f2a5ab837365db6bd40f479406758John McCall    if (!isOverridingProperty)
341d0014540005f2a5ab837365db6bd40f479406758John McCall      Props.push_back(Property);
342d0014540005f2a5ab837365db6bd40f479406758John McCall
343d0014540005f2a5ab837365db6bd40f479406758John McCall    return Property;
344d0014540005f2a5ab837365db6bd40f479406758John McCall  }
345d0014540005f2a5ab837365db6bd40f479406758John McCall};
346d0014540005f2a5ab837365db6bd40f479406758John McCall
34799ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid Parser::ObjCPropertyCallback::anchor() {
34899ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie}
34999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
350dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
351dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
352dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
353294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
3543536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
355dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
356dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
357dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
358294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
359294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
360294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
361294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
3622f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanianvoid Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
3632f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian                                        Decl *CDecl) {
3645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> allMethods;
3655f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 16> allProperties;
3665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclGroupPtrTy, 8> allTUVariables;
36700933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
3681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
369782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  SourceRange AtEnd;
3702f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian
371294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
372e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
373df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
374d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *methodPrototype =
375a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        ParseObjCMethodPrototype(MethodImplKind, false);
37625e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
3773536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
3783536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
3790db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis      if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
3800db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
3810db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
3820db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        if (Tok.is(tok::semi))
3830db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis          ConsumeToken();
3840db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis      }
385294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
386294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
38705511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    if (Tok.is(tok::l_paren)) {
38805511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      Diag(Tok, diag::err_expected_minus_or_plus);
389d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ParseObjCMethodDecl(Tok.getLocation(),
390d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          tok::minus,
39190ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                          MethodImplKind, false);
39205511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      continue;
39305511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
394e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
395e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
396294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
397e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
398e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
400bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
401e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
402e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
4031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    // Code completion within an Objective-C interface.
405b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    if (Tok.is(tok::code_completion)) {
40623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
407f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                  ObjCImpDecl? Sema::PCC_ObjCImplementation
408f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             : Sema::PCC_ObjCInterface);
4097d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
410b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    }
411b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor
412e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
413e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
4141fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
4151fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
4161fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
4171fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
4181fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
4190b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      ParsedAttributes attrs(AttrFactory);
4207f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
421e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
422e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
4231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
424e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
425e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
426c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    if (Tok.is(tok::code_completion)) {
427a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCAtDirective(getCurScope());
4287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
429c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor      break;
430c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    }
431c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
432a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
434a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
435782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setBegin(AtLoc);
436782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setEnd(Tok.getLocation());
437e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
438c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor    } else if (DirectiveKind == tok::objc_not_keyword) {
439c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      Diag(Tok, diag::err_objc_unknown_at);
440c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      SkipUntil(tok::semi);
441c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      continue;
442bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
4431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
444bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
445bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
446bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
447a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
448a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
449bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
450bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
451bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
452bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
453f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
454bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
455a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
456a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
45746d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
45846d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian    case tok::objc_implementation:
459df81c2c6ce37f4ae3c45dd01093b6274fa0b6692Fariborz Jahanian    case tok::objc_interface:
460d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      Diag(AtLoc, diag::err_objc_missing_end)
461d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen          << FixItHint::CreateInsertion(AtLoc, "@end\n");
462d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      Diag(CDecl->getLocStart(), diag::note_objc_container_start)
463d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen          << (int) Actions.getObjCContainerKind();
46446d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      ConsumeToken();
46546d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      break;
46646d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
467a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
468a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
469a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
470bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
471e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
472bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
473a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
474bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
475a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
477a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
478f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
479b321c0c0ba957d78475e72cebde4028fdaa00f8fChris Lattner        Diag(AtLoc, diag::err_objc_properties_require_objc2);
480f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
481e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
4821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
4838ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.is(tok::l_paren))
484a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        ParseObjCPropertyAttribute(OCDS);
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
486a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      ObjCPropertyCallback Callback(*this, allProperties,
487d0014540005f2a5ab837365db6bd40f479406758John McCall                                    OCDS, AtLoc, MethodImplKind);
488bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
489e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
4900b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      DeclSpec DS(AttrFactory);
491bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4937da19ea50d4161fcda40d135735bcf450cabeb50John McCall      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
494a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
495f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
496294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
497bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
498bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
499bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
500c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
501a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.CodeCompleteObjCAtDirective(getCurScope());
5027d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return cutOffParsing();
503d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
504bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
505d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  } else {
506d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(Tok, diag::err_objc_missing_end)
507d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
508d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(CDecl->getLocStart(), diag::note_objc_container_start)
509d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << (int) Actions.getObjCContainerKind();
510d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    AtEnd.setBegin(Tok.getLocation());
511d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    AtEnd.setEnd(Tok.getLocation());
512d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  }
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
514a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
515bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
516a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnAtEnd(getCurScope(), AtEnd,
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
518beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
519beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
520294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
521294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
522d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
523d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
524d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
525d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
526d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
527d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
528d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
529d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
530d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
531d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
532d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
533d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
534d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
535d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
536d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
537f85e193739c953358c865005855253af4f68a497John McCall///     atomic
538f85e193739c953358c865005855253af4f68a497John McCall///     strong
539f85e193739c953358c865005855253af4f68a497John McCall///     weak
540f85e193739c953358c865005855253af4f68a497John McCall///     unsafe_unretained
541d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
542a28948f34817476d02412fa204cae038e228c827Fariborz Jahanianvoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
5438ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
5444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
5454a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
547cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
548ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
54923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
5507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
551ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
552d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
5531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
554f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
555f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
5564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
557f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
558f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
5591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
560156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
5611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
563e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
56492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
565e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
566f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("unsafe_unretained"))
567f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
56892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
569e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
57092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
571e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
572f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("strong"))
573f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
57492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
575e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
57692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
577e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
57845937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian    else if (II->isStr("atomic"))
57945937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
580f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("weak"))
581f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
58292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
58342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      bool IsSetter = II->getNameStart()[0] == 's';
58442499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
585e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
58642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
58742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        diag::err_objc_expected_equal_for_getter;
58842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
58942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
590dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
5911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5924ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      if (Tok.is(tok::code_completion)) {
59342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        if (IsSetter)
594a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertySetter(getCurScope());
5954ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        else
596a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertyGetter(getCurScope());
5977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
5984ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      }
5994ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
60042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
60142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      SourceLocation SelLoc;
60242499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
60342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
60442499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (!SelIdent) {
60542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
60642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson          << IsSetter;
6078ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
6088ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
6098ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (IsSetter) {
6128ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
61342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setSetterName(SelIdent);
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
615e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian        if (ExpectAndConsume(tok::colon,
616e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian                             diag::err_expected_colon_after_setter_name, "",
617156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
6188ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
6198ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
6208ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
62142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setGetterName(SelIdent);
6228ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
623e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
624a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
625cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
626cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
627cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
6281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
629156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
630156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
632156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
633d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
636d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
637d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
6383536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
6391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
6403536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
641294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
642294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
643294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
644294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6454985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
6464985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
6474985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
648a28948f34817476d02412fa204cae038e228c827Fariborz JahanianDecl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
64990ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                       bool MethodDefinition) {
650df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
651294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
653bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
654a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
65590ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                    MethodDefinition);
6563536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
6572bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
658f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
659294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
660294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
661294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
662294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
663294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
664294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
665294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
666294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
667294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
668294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6692fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
670be74740cc246ce08d42804a684385a42eb814edbFariborz Jahanian
671ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
672ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
673ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
674afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampamp:
675afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampequal:
676afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::amp:
677afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipe:
678afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::tilde:
679afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaim:
680afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaimequal:
681afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipepipe:
682afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipeequal:
683afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caret:
684afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caretequal: {
6853846ca29a8cc1d376a4b695194c29952dbbfb544Fariborz Jahanian    std::string ThisTok(PP.getSpelling(Tok));
686afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    if (isalpha(ThisTok[0])) {
687afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
688afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      Tok.setKind(tok::identifier);
689afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      SelectorLoc = ConsumeToken();
690afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      return II;
691afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    }
692afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    return 0;
693afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  }
694afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian
695ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
696ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
697ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
6989298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
699ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
700ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
701ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
702ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
703ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
704ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
705ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
706ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
707ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
708ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
709ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
710ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
711ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
712ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
713ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
714ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
715ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
716ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
717ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
718ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
719ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
720ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
721ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
722ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
723ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
724ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
725ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
726ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
727ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
728ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
729ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
730ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
731ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
732ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
733ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
734ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
735ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
736ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
737ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
738ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
739ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
740ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
741ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
742ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
743ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
744ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
745ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
746ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
747ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
748ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
749ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
750ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
751ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
752ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
753ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
754ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
755ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
756ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
757ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
758ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
759ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
760ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
761ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
762ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
763ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
764ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
7654b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
766ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
767d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
768294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
769294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
7700196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
7710196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
772335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
7733ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
7743ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
7753ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
7775ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
7780196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
7790196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
780a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
781e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
782e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
783294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
784294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
785294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
786294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
787294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
788b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregorvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
789cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                        Declarator::TheContext Context) {
790cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert(Context == Declarator::ObjCParameterContext ||
791cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall         Context == Declarator::ObjCResultContext);
792cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
793e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
794d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    if (Tok.is(tok::code_completion)) {
795b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor      Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
796cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                          Context == Declarator::ObjCParameterContext);
7977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
798d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    }
799d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor
800cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
801e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
8021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
803e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
804e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
805a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
806e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
808a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
809e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
810b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      default: llvm_unreachable("Unknown decl qualifier");
811a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
812a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
813a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
814a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
815a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
816a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
817e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
818a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
819e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
820e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
821e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
822e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
8231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
824e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
825e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
826e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
827e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
828e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
829cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// Take all the decl attributes out of the given list and add
830cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// them to the given attribute set.
831cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCallstatic void takeDeclAttributes(ParsedAttributes &attrs,
832cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                               AttributeList *list) {
833cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  while (list) {
834cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    AttributeList *cur = list;
835cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    list = cur->getNext();
836cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
837cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    if (!cur->isUsedAsTypeAttr()) {
838cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // Clear out the next pointer.  We're really completely
839cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // destroying the internal invariants of the declarator here,
840cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // but it doesn't matter because we're done with it.
841cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      cur->setNext(0);
842cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      attrs.add(cur);
843cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    }
844cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  }
845cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall}
846cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
847cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// takeDeclAttributes - Take all the decl attributes from the given
848cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// declarator and add them to the given list.
849cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCallstatic void takeDeclAttributes(ParsedAttributes &attrs,
850cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                               Declarator &D) {
851cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // First, take ownership of all attributes.
852cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  attrs.getPool().takeAllFrom(D.getAttributePool());
853cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
854cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
855cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // Now actually move the attributes over.
856cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
857cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  takeDeclAttributes(attrs, D.getAttributes());
858cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
859cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    takeDeclAttributes(attrs,
860cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                  const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
861cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall}
862cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
863e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
864e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
865e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
866e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
867b77cab97f17f946744c920629ca17271308d8ebfDouglas GregorParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
868cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                     Declarator::TheContext context,
869cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                     ParsedAttributes *paramAttrs) {
870cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert(context == Declarator::ObjCParameterContext ||
871cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall         context == Declarator::ObjCResultContext);
872cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
873cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
874df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8764a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
8774a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
8784a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
879e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
8809735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  ObjCDeclContextSwitch ObjCDC(*this);
8819735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian
88219d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
883cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  ParseObjCTypeQualifierList(DS, context);
8844fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
885b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType Ty;
886809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
887cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // Parse an abstract declarator.
888cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    DeclSpec declSpec(AttrFactory);
889cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    declSpec.setObjCQualifiers(&DS);
890cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ParseSpecifierQualifierList(declSpec);
891cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    Declarator declarator(declSpec, context);
892cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ParseDeclarator(declarator);
893cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
894cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // If that's not invalid, extract a type.
895cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    if (!declarator.isInvalidType()) {
896cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
897cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      if (!type.isInvalid())
898cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall        Ty = type.get();
899cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
900cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // If we're parsing a parameter, steal all the decl attributes
901cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // and add them to the decl spec.
902cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      if (context == Declarator::ObjCParameterContext)
903cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall        takeDeclAttributes(*paramAttrs, declarator);
904cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    }
905cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  } else if (context == Declarator::ObjCResultContext &&
906cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall             Tok.is(tok::identifier)) {
907e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (!Ident_instancetype)
908e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ident_instancetype = PP.getIdentifierInfo("instancetype");
909e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
910e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (Tok.getIdentifierInfo() == Ident_instancetype) {
911e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
912e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      ConsumeToken();
913e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    }
914809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
915e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
9164a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
9174a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
9184a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
919e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
9201ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
9214a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
9224a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
9234a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
9244a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
9254a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
926294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
927f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
928294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
929294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
930294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
931294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
9324985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
933294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
9344985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
935294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
936294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
9371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
938294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
939294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
940294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
9417ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
9427ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
9437ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
9447ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
945294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9464985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
9474985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
948294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9494985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
9504985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
951294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9524985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
953294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
954294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9557ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
9567ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
9577ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
958d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
9592ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor                                  tok::TokenKind mType,
96090ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  tok::ObjCKeywordKind MethodImplKind,
96190ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  bool MethodDefinition) {
96254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
96354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
964e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
96523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
966a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       /*ReturnType=*/ ParsedType());
9677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
9687d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
969e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
970e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
971e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
972b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType ReturnType;
973a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
974df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
975cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
9761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9779e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // If attributes exist before the method, parse them.
9780b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributes methodAttrs(AttrFactory);
9797f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (getLang().ObjC2)
9800b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
9819e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek
982e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
98323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
984a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       ReturnType);
9857d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
9867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
987e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
988e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
9899e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // Now parse the selector.
990bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
9912fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
992e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
99384c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
99484c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
9951ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
9961ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
997e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
998e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
999d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1000e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
1003df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
1004ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
10057f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (getLang().ObjC2)
10060b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(methodAttrs);
10071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1008ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
1009d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Result
10107f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian         = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1011a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                          mType, DSRet, ReturnType,
1012926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                          selLoc, Sel, 0,
10134f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          CParamInfo.data(), CParamInfo.size(),
10140b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          methodAttrs.getList(), MethodImplKind,
10150b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          false, MethodDefinition);
101654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
101754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
1018ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
1019f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
10205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
102111d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> KeyLocs;
10225f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
10237f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian  ParseScope PrototypeScope(this,
10247f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian                            Scope::FunctionPrototypeScope|Scope::DeclScope);
10250b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
10260b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  AttributePool allParamAttrs(AttrFactory);
10277f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1028ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
10290b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes paramAttrs(AttrFactory);
1030f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Sema::ObjCArgInfo ArgInfo;
10311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1032ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
1033df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
1034ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
1035ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
1036ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
1037ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
10381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1039b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ArgInfo.Type = ParsedType();
1040e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
1041cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1042cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                       Declarator::ObjCParameterContext,
1043cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                       &paramAttrs);
1044e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
1045ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
1046cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // Regardless, collect all the attributes we've parsed so far.
1047e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
10487f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (getLang().ObjC2) {
10490b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(paramAttrs);
10500b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      ArgInfo.ArgAttrs = paramAttrs.getList();
10517f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
10527ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
105340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    // Code completion for the next piece of the selector.
105440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    if (Tok.is(tok::code_completion)) {
105540ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.push_back(SelIdent);
105640ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
105740ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 mType == tok::minus,
105840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/true,
105940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 ReturnType,
106040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.data(),
106140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.size());
10627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
10637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
106440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    }
106540ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor
1066df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1067ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
1068ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
10694985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
10701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1071e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
1072e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
1073ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
10741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1075e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
1076e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
107711d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis    KeyLocs.push_back(selLoc);
1078e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
10790b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    // Make sure the attributes persist.
10800b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    allParamAttrs.takeAllFrom(paramAttrs.getPool());
10810b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
10821f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    // Code completion for the next piece of the selector.
10831f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    if (Tok.is(tok::code_completion)) {
10841f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
10851f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 mType == tok::minus,
108640ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/false,
10871f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 ReturnType,
10881f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.data(),
10891f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.size());
10907d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
10917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
10921f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    }
10931f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor
1094ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
109511d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis    SelIdent = ParseObjCSelectorPiece(selLoc);
1096df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
1097ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
1098ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
1099ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
11001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1101335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1103ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
1104df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
1105ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
1106df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
1107335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
11084985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
1109ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
11104985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
11110b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
1112ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
1114ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
1115ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
11164f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1117d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
11184f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
11194f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    ParmDecl.getIdentifierLoc(),
11204f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    Param,
11214f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                   0));
11224f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian
11234985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11259c4bb2c08989265411925a04252fd4f93c26e3b1Cameron Esfahani  // FIXME: Add support for optional parameter list...
1126e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
11277f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (getLang().ObjC2)
11280b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
11297f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1130a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  if (KeyIdents.size() == 0)
1131d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
11327f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1133ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1134ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
1135d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Result
11367f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian       = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1137a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                        mType, DSRet, ReturnType,
113811d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis                                        KeyLocs, Sel, &ArgInfos[0],
11394f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        CParamInfo.data(), CParamInfo.size(),
11400b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                        methodAttrs.getList(),
114190ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                        MethodImplKind, isVariadic, MethodDefinition);
11427f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
114354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
114454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
1145294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
1146294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
1147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
1148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
1149dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
11507caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
11515f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
11525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                            SmallVectorImpl<SourceLocation> &ProtocolLocs,
115371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
115471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
1155e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
11581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11595f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierLocPair, 8> ProtocolIdents;
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1161e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
116255385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
116355385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
116455385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
11657d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
11667d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return true;
116755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
116855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
1169e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
1170e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
1171e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
1172e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
1173e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
1174e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1175e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
117671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
1177e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1179e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
1180e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
1181e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
1182e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1184e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
1185e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
1186e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
1187e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
1188e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
11891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1190e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1192e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
1193e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
1194e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
1195e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
1196e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
1197e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
1198e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
11999bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// \brief Parse the Objective-C protocol qualifiers that follow a typename
12009bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// in a decl-specifier-seq, starting at the '<'.
120146f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregorbool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
12029bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
12039bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
12049bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  SourceLocation LAngleLoc, EndProtoLoc;
12055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolDecl;
12065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
120746f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
120846f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor                                            LAngleLoc, EndProtoLoc);
12099bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
12109bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor                           ProtocolLocs.data(), LAngleLoc);
12119bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  if (EndProtoLoc.isValid())
12129bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(EndProtoLoc);
121346f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  return Result;
12149bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor}
12159bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
12169bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
1217dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
1218dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
1219dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1220dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
1221dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
1222dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
1223dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
1224dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
1225dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
1226dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
1227dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1228dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
1229dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
1230dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
1231dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
1232ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
1233dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1234dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
1236dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1237d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
123883c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                             tok::ObjCKeywordKind visibility,
123960fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
1240df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
12415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> AllIvarDecls;
1242a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
12431a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
12443a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis  ObjCDeclContextSwitch ObjCDC(*this);
124572de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
12464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_brace);
12474a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1249ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
1250df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1251ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
12521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1253ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
1254df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1255f13ca06e57ac094ed05ea08c26a499af1ba0ce88Douglas Gregor      Diag(Tok, diag::ext_extra_ivar_semi)
1256849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1257ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1258ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
1259ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
12601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1261ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
1262df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
1263ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
1264c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1265c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
126623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(getCurScope());
12677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
1268c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
1269c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1270861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
1271ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
1272ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
1273ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
1274ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
1275861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
1276ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
12771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
1278ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
1279ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1280ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
1281ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1282ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1284c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
128523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
1286f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                       Sema::PCC_ObjCInstanceVariableList);
12877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
1288c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1289c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1290bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1291bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1292d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *IDecl;
1293bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
12945f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<Decl *> &AllIvarDecls;
1295bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1296d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
12975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &AllIvarDecls) :
1298bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1299bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1300bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1301d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *invoke(FieldDeclarator &FD) {
1302a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        P.Actions.ActOnObjCContainerStartDefinition(IDecl);
1303bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1304d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        Decl *Field
130523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          = P.Actions.ActOnIvar(P.getCurScope(),
1306bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1307a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                FD.D, FD.BitfieldSize, visibility);
130810af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian        P.Actions.ActOnObjCContainerFinishDefinition();
13090bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian        if (Field)
13100bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian          AllIvarDecls.push_back(Field);
1311bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
1312bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1313bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1314d097be8f81fbf4ed96ac10bae18562dd8202666bFariborz Jahanian
1315e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
13160b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
1317bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1319df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1320ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1321ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1322ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1323ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1324ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1325ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1326ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
13274a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
13284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1329a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
13304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
133110af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian  Actions.ActOnObjCContainerFinishDefinition();
13328749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
13338749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
133423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
133577b6de07be9186063c12928d2e9785a5d4eecbf6David Blaikie                      AllIvarDecls,
13364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                      T.getOpenLocation(), T.getCloseLocation(), 0);
1337ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
13385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1339dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1340dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1341dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1342dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1343dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1344dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
13451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
13471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1348dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1349dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1350dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1351dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1352dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1353dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
13543536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1355dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1356bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas GregorParser::DeclGroupPtrTy
1357bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas GregorParser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1358bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor                                       ParsedAttributes &attrs) {
1359861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
13607ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
13617ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
13621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1363083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
136423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(getCurScope());
13657d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1366bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
1367083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1368083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1369df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
13707ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1371bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
13727ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
13737ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
13747ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
13757ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
13761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1377df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
13787caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
13797ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
13801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
13817f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
13827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138490ec96f3026480fa41057b05d58f338aed585f62Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
138590ec96f3026480fa41057b05d58f338aed585f62Erik Verbruggen
1386df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
13875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<IdentifierLocPair, 8> ProtocolRefs;
13887caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
13897caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
13907ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
13917ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
13927ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1393df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
13947ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
13957ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1396bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor        return DeclGroupPtrTy();
13977ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
13987caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
13997caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
14007ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1402df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
14037ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
14047ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
14057ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
14067ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1407bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor      return DeclGroupPtrTy();
14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1409e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
14101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1411bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
14127f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
14137caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
14141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14157ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
141671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
14177caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
14185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
14195f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
14207caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
142171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
142271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1423bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1425d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ProtoType =
1426e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1427beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1428beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
142918df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
14307f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                        EndProtoLoc, attrs.getList());
1431a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
14322f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
1433bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor  return Actions.ConvertDeclToDeclGroup(ProtoType);
1434dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1435dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1436dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1437dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1438dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1439dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1440dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1441dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1442dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1443dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1444dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1445dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1446d64251fd56577dd5c78903454632361e094c6dc1Erik VerbruggenDecl *Parser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
1447ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1448ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1449d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
1450ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14523b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
14533b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
145423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(getCurScope());
14557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
14567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
14573b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
14583b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1459df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1460ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1461d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1462ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1463ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1464ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1465ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
14661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1468ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1469dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeParen();
1470ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1471ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
14721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
147333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
147423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
14757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
14767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
147733ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
147833ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1479df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1480ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1481ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1482ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1483ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1484d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
14851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1486df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1487ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1488ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1489d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1490ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1491ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1492d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
1493d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen                                    AtLoc, nameId, nameLoc, categoryId,
14948f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1495a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1496a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
149763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.push_back(ObjCImpDecl);
1498d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1499ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1500ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1501ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1502ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1503df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1504ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1505ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1506df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1507ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1508d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1509ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1510ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1511ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1512ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1513d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ImplClsType = Actions.ActOnStartClassImplementation(
1514d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen                                  AtLoc, nameId, nameLoc,
1515ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
15161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
151760fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
1518d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, AtLoc);
1519a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1520a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
152163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  PendingObjCImpDecl.push_back(ObjCImpDecl);
1522d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
15235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
152460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1525140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::DeclGroupPtrTy
1526140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1527ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1528ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1529ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1530140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  SmallVector<Decl *, 8> DeclsInGroup;
15318697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
1532140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1533140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
153435593a9add85353532519f8eba5ec513a611aac5Argyrios Kyrtzidis    if (D)
153535593a9add85353532519f8eba5ec513a611aac5Argyrios Kyrtzidis      DeclsInGroup.push_back(D);
1536140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  }
1537140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  DeclsInGroup.push_back(ObjCImpDecl);
15388697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian
1539a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
1540a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnAtEnd(getCurScope(), atEnd);
154163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.pop_back();
1542a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
15438697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  else
1544782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1545d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(atEnd.getBegin(), diag::err_expected_objc_container);
15468697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian
15472fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  clearLateParsedObjCMethods();
15488697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  ObjCImpDecl = 0;
1549140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  return Actions.BuildDeclaratorGroup(
1550140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian           DeclsInGroup.data(), DeclsInGroup.size(), false);
15515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1552e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
15533fe104154dd2e8ffb351142d74f308938b5c99bfFariborz JahanianParser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
15543fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian  Actions.DiagnoseUseOfUnimplementedSelectors();
155563e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  if (PendingObjCImpDecl.empty())
1556d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return Actions.ConvertDeclToDeclGroup(0);
1557d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
1558d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
1559d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Actions.ActOnAtEnd(getCurScope(), SourceRange(Tok.getLocation()));
1560d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Diag(Tok, diag::err_objc_missing_end)
1561d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
1562d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  if (ImpDecl)
1563d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(ImpDecl->getLocStart(), diag::note_objc_container_start)
1564d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << Sema::OCK_Implementation;
1565d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
156663e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  return Actions.ConvertDeclToDeclGroup(ImpDecl);
156763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian}
156863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
15692fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidisvoid Parser::clearLateParsedObjCMethods() {
15702fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  for (LateParsedObjCMethodContainer::iterator
15712fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis         I = LateParsedObjCMethods.begin(),
15722fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis         E = LateParsedObjCMethods.end(); I != E; ++I)
15732fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis    delete *I;
15742fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  LateParsedObjCMethods.clear();
15752fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis}
15762fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis
1577e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1578e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1579e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1580d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1581e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1582e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1583e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1584df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1585e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1586d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1587e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1588243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1589243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1590df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1591e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1592d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1593e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1594243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1595243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1596e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1597e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor                   "@compatibility_alias");
1598b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1599b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
16005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1602ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1603ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1604ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1605ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1606ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1607ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1608ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1609ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1610ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1611ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1612ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1613d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1614ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1615ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1616dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume synthesize
16171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1618b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1619322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
1620a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
16217d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16227d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1623322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1624322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1625b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1626b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1627b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1628d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1629b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1630b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1631f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1632f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1633f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1634a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor    SourceLocation propertyIvarLoc;
1635df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1636ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1637ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1638322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1639322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
1640a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
16417d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
16427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return 0;
1643322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1644322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1645df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1646ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1647ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1648ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1649f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1650a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor      propertyIvarLoc = ConsumeToken(); // consume ivar-name
1651ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1652a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
1653a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, propertyIvar, propertyIvarLoc);
1654df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1655ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1656ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1657ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1658e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
1659d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1660ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1661ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1662ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1663ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1664ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1665ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1666ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1667ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1668ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1669d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1670ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1671ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1672dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume dynamic
1673424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1674424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
1675a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
16767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16777d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1678424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1679424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1680424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1681424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1682424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1683d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1684424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1685424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1686c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1687c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1688a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
1689a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, 0, SourceLocation());
1690c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1691df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1692ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1693ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1694ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1695e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
1696d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1697ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1699397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1700397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1701397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
170260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
170360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res;
1704397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1705df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
170639f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
17070e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1708397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
170943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1710397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1711397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
171202418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  // consume ';'
171302418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
17149ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
1715397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1716397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1717c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
171878a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1719c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
172060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
172143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1722fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1723fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
17241ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
172543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1726fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
172707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
172807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // The operand is surrounded with parentheses.
1729fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
173007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ExprResult operand(ParseExpression());
173107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
173207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (Tok.is(tok::r_paren)) {
173307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    ConsumeParen();  // ')'
173407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  } else {
173507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
173607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_rparen);
173707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
173807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    // Skip forward until we see a left brace, but don't consume it.
173907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    SkipUntil(tok::l_brace, true, true);
1740fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
174107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
174207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Require a compound statement.
174378a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
174407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
174507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_lbrace);
174643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
174778a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
17483ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
174907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Check the @synchronized operand now.
175007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (!operand.isInvalid())
175107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
175207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
175307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Parse the compound statement within a new scope.
175407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ParseScope bodyScope(this, Scope::DeclScope);
175507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  StmtResult body(ParseCompoundStatementBody());
175607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  bodyScope.Exit();
175707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
175807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // If there was a semantic or parse error earlier with the
175907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // operand, fail now.
176007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (operand.isInvalid())
176107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    return StmtError();
176207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
176307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (body.isInvalid())
176407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    body = Actions.ActOnNullStmt(Tok.getLocation());
17650e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
176607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
1767c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1768c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1769397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1770397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1771397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1772397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1773397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1774397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1775397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1776397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1777397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1778397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1779397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
178060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1781397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
178243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1783397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1784df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
17851ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
178643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1787397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
17888f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  StmtVector CatchStmts(Actions);
178960d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult FinallyStmt;
17908935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
179160d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult TryBody(ParseCompoundStatementBody());
17928935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
17930e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1794bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1795a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1796df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
17976b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
17986b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
17996b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
18006b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
18016b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
18026b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
18036b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
18040e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1805161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1806cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1807d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *FirstPart = 0;
18083b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1809df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1810397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1811e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1812df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
18130b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall          DeclSpec DS(AttrFactory);
1814397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
181517b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis          Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
18167ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
18177ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
18184e6c0d19b7c072758922cf80525a81aeefc6e64bDouglas Gregor          // Inform the actions module about the declarator, so it
18197ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
182023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
182164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1822397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
18231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182493a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
18251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
182693a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
182793a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
182893a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
182993a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
183093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
183160d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult CatchBody(true);
1832c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1833c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1834c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1835c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
18360e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
18373b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
18388f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
183960d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
18408f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              RParenLoc,
18418f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              FirstPart,
18429ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                              CatchBody.take());
18438f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        if (!Catch.isInvalid())
18448f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor          CatchStmts.push_back(Catch.release());
18458f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
184664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
18471ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
18481ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
184943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1850397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1851397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
18526b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
18536b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
185464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
18558935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
18560e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
185760d7b3a319d84d688752be3870615ac0f111fb16John McCall      StmtResult FinallyBody(true);
1858c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1859c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1860c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1861c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
18620e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1863161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
18640e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
18659ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                   FinallyBody.take());
1866397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1867397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1868397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1869397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1870bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1871397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
187243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1873bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
18748f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
18759ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
18768f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                    move_arg(CatchStmts),
18779ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                    FinallyStmt.take());
1878397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1879ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1880f85e193739c953358c865005855253af4f68a497John McCall/// objc-autoreleasepool-statement:
1881f85e193739c953358c865005855253af4f68a497John McCall///   @autoreleasepool compound-statement
1882f85e193739c953358c865005855253af4f68a497John McCall///
1883f85e193739c953358c865005855253af4f68a497John McCallStmtResult
1884f85e193739c953358c865005855253af4f68a497John McCallParser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1885f85e193739c953358c865005855253af4f68a497John McCall  ConsumeToken(); // consume autoreleasepool
1886f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isNot(tok::l_brace)) {
1887f85e193739c953358c865005855253af4f68a497John McCall    Diag(Tok, diag::err_expected_lbrace);
1888f85e193739c953358c865005855253af4f68a497John McCall    return StmtError();
1889f85e193739c953358c865005855253af4f68a497John McCall  }
1890f85e193739c953358c865005855253af4f68a497John McCall  // Enter a scope to hold everything within the compound stmt.  Compound
1891f85e193739c953358c865005855253af4f68a497John McCall  // statements can always hold declarations.
1892f85e193739c953358c865005855253af4f68a497John McCall  ParseScope BodyScope(this, Scope::DeclScope);
1893f85e193739c953358c865005855253af4f68a497John McCall
1894f85e193739c953358c865005855253af4f68a497John McCall  StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1895f85e193739c953358c865005855253af4f68a497John McCall
1896f85e193739c953358c865005855253af4f68a497John McCall  BodyScope.Exit();
1897f85e193739c953358c865005855253af4f68a497John McCall  if (AutoreleasePoolBody.isInvalid())
1898f85e193739c953358c865005855253af4f68a497John McCall    AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1899f85e193739c953358c865005855253af4f68a497John McCall  return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1900f85e193739c953358c865005855253af4f68a497John McCall                                                AutoreleasePoolBody.take());
1901f85e193739c953358c865005855253af4f68a497John McCall}
1902f85e193739c953358c865005855253af4f68a497John McCall
19033536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1904ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1905d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDefinition() {
1906a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodPrototype();
19071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1908f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1909f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      "parsing Objective-C method");
19101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1911ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1912209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1913496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    if (ObjCImpDecl) {
1914496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
1915849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1916496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1917ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1918209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1919ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1920409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1921df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1922da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
19231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1924409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1925409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
19261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1927409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1928409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1929d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1930ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1931140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Allow the rest of sema to find private method decl implementations.
1932140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  if (MDecl)
1933140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    Actions.AddAnyMethodToGlobalPool(MDecl);
1934140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
1935140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume the tokens and store them for later parsing.
1936140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LexedMethod* LM = new LexedMethod(this, MDecl);
1937140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LateParsedObjCMethods.push_back(LM);
1938140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  CachedTokens &Toks = LM->Toks;
1939140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Begin by storing the '{' token.
1940140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  Toks.push_back(Tok);
1941140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ConsumeBrace();
1942140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume everything up to (and including) the matching right brace.
1943140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
194471c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
19455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19465508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
194760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
19489a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
194923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtStatement(getCurScope());
19507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
19519a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
19525d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
19535d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19545d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
19556b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
19565d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19575d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
195864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
19595d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19605d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
196164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
1962f85e193739c953358c865005855253af4f68a497John McCall
1963f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1964f85e193739c953358c865005855253af4f68a497John McCall    return ParseObjCAutoreleasePoolStmt(AtLoc);
19655d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
196660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
19670e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
196864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
196964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
197064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
197164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
197243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
197364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
19745d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
197564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
19769ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
19779ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
197864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
197964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
198060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
19815508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
19829a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
198323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtExpression(getCurScope());
19847d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
19859a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
19869a0c85e640a08174569a303db22981612f05d385Douglas Gregor
1987b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1988b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
19891d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1990b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
19914fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
19921d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
19932f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
19944fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
19954fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
19961d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
19974fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
19981d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
19994fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
20001d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
20014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
20021d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
20034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
20045508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
20055508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
20065508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
20076aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brirg Parse the receiver of an Objective-C++ message send.
20086aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20096aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// This routine parses the receiver of a message send in
20106aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// Objective-C++ either as a type or as an expression. Note that this
20116aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// routine must not be called to parse a send to 'super', since it
20126aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// has no way to return such a result.
20136aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20146aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param IsExpr Whether the receiver was parsed as an expression.
20156aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20166aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param TypeOrExpr If the receiver was parsed as an expression (\c
20176aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// IsExpr is true), the parsed expression. If the receiver was parsed
20186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// as a type (\c IsExpr is false), the parsed type.
20196aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20206aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \returns True if an error occurred during parsing or semantic
20216aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// analysis, in which case the arguments do not have valid
20226aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// values. Otherwise, returns false for a successful parse.
20236aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20246aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///   objc-receiver: [C++]
20256aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     'super' [not parsed here]
20266aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     expression
20276aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     simple-type-specifier
20286aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     typename-specifier
20296aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
20300fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
20310fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
20326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
20336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
20346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TryAnnotateTypeOrScopeToken();
20356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20366aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (!isCXXSimpleTypeSpecifier()) {
20376aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   objc-receiver:
20386aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     expression
203960d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseExpression();
20406aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
20416aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
20426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
20446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
20456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
20466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
20476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // objc-receiver:
20496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   typename-specifier
20506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   simple-type-specifier
20516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   expression (that starts with one of the above)
20520b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
20536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  ParseCXXSimpleTypeSpecifier(DS);
20546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::l_paren)) {
20566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // If we see an opening parentheses at this point, we are
20576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // actually parsing an expression that starts with a
20586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // function-style cast, e.g.,
20596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
20606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   postfix-expression:
20616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     simple-type-specifier ( expression-list [opt] )
20626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     typename-specifier ( expression-list [opt] )
20636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
20646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the remainder of this case, then the (optional)
20656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // postfix-expression suffix, followed by the (optional)
20666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // right-hand side of the binary expression. We have an
20676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // instance method.
206860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
20696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
20709ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParsePostfixExpressionSuffix(Receiver.take());
20716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
20729ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
20736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
20746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
20756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
20776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
20786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
20796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
20806aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // We have a class message. Turn the simple-type-specifier or
20826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // typename-specifier we parsed into a type and parse the
20836aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // remainder of the class message.
20846aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
208523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
20866aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Type.isInvalid())
20876aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
20886aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20896aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  IsExpr = false;
2090b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  TypeOrExpr = Type.get().getAsOpaquePtr();
20916aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
20926aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
20936aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20941b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// \brief Determine whether the parser is currently referring to a an
20951b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// Objective-C message send, using a simplified heuristic to avoid overhead.
20961b730e847ded503f2e615154035c083c4f94a067Douglas Gregor///
20971b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// This routine will only return true for a subset of valid message-send
20981b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// expressions.
20991b730e847ded503f2e615154035c083c4f94a067Douglas Gregorbool Parser::isSimpleObjCMessageExpression() {
2100c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
21011b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         "Incorrect start for isSimpleObjCMessageExpression");
21021b730e847ded503f2e615154035c083c4f94a067Douglas Gregor  return GetLookAheadToken(1).is(tok::identifier) &&
21031b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         GetLookAheadToken(2).is(tok::identifier);
21041b730e847ded503f2e615154035c083c4f94a067Douglas Gregor}
21051b730e847ded503f2e615154035c083c4f94a067Douglas Gregor
21069497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregorbool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
21079497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
21089497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      InMessageExpression)
21099497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
21109497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21119497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21129497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  ParsedType Type;
21139497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21149497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (Tok.is(tok::annot_typename))
21159497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = getTypeAnnotation(Tok);
21169497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else if (Tok.is(tok::identifier))
21179497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
21189497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor                               getCurScope());
21199497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else
21209497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
21219497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21229497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
21239497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    const Token &AfterNext = GetLookAheadToken(2);
21249497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
21259497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      if (Tok.is(tok::identifier))
21269497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor        TryAnnotateTypeOrScopeToken();
21279497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21289497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      return Tok.is(tok::annot_typename);
21299497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    }
21309497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  }
21319497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21329497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  return false;
21339497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor}
21349497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
21360ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
21370ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
21382725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///   objc-receiver: [C]
2139eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner///     'super'
21400ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
21410ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
21420ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
21436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
214460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCMessageExpression() {
2145699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
2146699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2147699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
21488e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  if (Tok.is(tok::code_completion)) {
214923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMessageReceiver(getCurScope());
21507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
21518e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    return ExprError();
21528e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  }
21538e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor
21540fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
21550fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
21566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (getLang().CPlusPlus) {
21576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // We completely separate the C and C++ cases because C++ requires
21586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // more complicated (read: slower) parsing.
21596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Handle send to super.
21616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: This doesn't benefit from the same typo-correction we
21626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // get in Objective-C.
21636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
216423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
2165b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2166b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
21676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the receiver, which is either a type or an expression.
21696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    bool IsExpr;
2170304b752a450c0fc5968c20ba25446d0bb7c6f68dNick Lewycky    void *TypeOrExpr = NULL;
21716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
21726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      SkipUntil(tok::r_square);
21736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ExprError();
21746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    }
21756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (IsExpr)
2177b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2178b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(),
21799ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            static_cast<Expr*>(TypeOrExpr));
21806aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2182b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                              ParsedType::getFromOpaquePtr(TypeOrExpr),
2183b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          0);
2184c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  }
2185c59cb38810c63a806270385f79ea84e0203754eaChris Lattner
2186c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  if (Tok.is(tok::identifier)) {
21871dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    IdentifierInfo *Name = Tok.getIdentifierInfo();
21881dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    SourceLocation NameLoc = Tok.getLocation();
2189b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType ReceiverType;
219023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
21911dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor                                       Name == Ident_super,
21921569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       NextToken().is(tok::period),
21931569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       ReceiverType)) {
2194f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCSuperMessage:
2195b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2196b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
21972725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
2198f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCClassMessage:
21991569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      if (!ReceiverType) {
22002725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        SkipUntil(tok::r_square);
22012725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        return ExprError();
22022725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      }
22032725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
22041569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      ConsumeToken(); // the type name
22051569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
22061569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
22079ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            ReceiverType, 0);
22081dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor
2209f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCInstanceMessage:
22102725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      // Fall through to parse an expression.
22111dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor      break;
2212d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
2213699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
2214eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
2215eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  // Otherwise, an arbitrary expression can be the receiver of a send.
221660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpression());
22170e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
22185c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
22191d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
2220699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
22211d922960e083906a586609ac6978678147250177Sebastian Redl
2222b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2223b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType(), Res.take());
2224699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
22251d922960e083906a586609ac6978678147250177Sebastian Redl
22262725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Parse the remainder of an Objective-C message following the
22272725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// '[' objc-receiver.
22282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22292725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine handles sends to super, class messages (sent to a
22302725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class name), and instance messages (sent to an object), and the
22312725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// target is represented by \p SuperLoc, \p ReceiverType, or \p
22322725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// ReceiverExpr, respectively. Only one of these parameters may have
22332725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// a valid value.
22342725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22352725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening '['.
22362725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22372725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc If this is a send to 'super', the location of the
22382725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 'super' keyword that indicates a send to the superclass.
22392725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22402725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType If this is a class message, the type of the
22412725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class we are sending a message to.
22422725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22432725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverExpr If this is an instance message, the expression
22442725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// used to compute the receiver object.
22451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
22460ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
22470ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
22480ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
22490ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22500ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
22510ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
22520ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
22530ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
22550ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
22560ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22570ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
22580ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
22590ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22600ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
22610ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
22620ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
22631d922960e083906a586609ac6978678147250177Sebastian Redl///
226460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2265699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
22662725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                       SourceLocation SuperLoc,
2267b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                       ParsedType ReceiverType,
22681d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
22690fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
22700fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
2271c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
22722725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    if (SuperLoc.isValid())
227370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
227470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
22752725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    else if (ReceiverType)
227670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
227770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
2278c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
22799ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
228070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                              0, 0, false);
22817d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
22827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return ExprError();
2283c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
2284d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
2285a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
22864b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
22872fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
228868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
22895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2290951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> KeyLocs;
2291a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
229268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
2293df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
2294a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
2295a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
229668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
2297951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis      KeyLocs.push_back(Loc);
229837387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
2299df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
2300a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
23014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
23024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
23034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
23044fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
23051d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
2306a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
23071d922960e083906a586609ac6978678147250177Sebastian Redl
230868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
23091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
231070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
231170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      if (Tok.is(tok::code_completion)) {
231270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        if (SuperLoc.isValid())
231370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
231470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
231570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
231670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
231770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else if (ReceiverType)
231870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
231970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
232070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
232170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
232270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else
232370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
232470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.data(),
232570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
232670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  /*AtArgumentEpression=*/true);
232770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
23287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
232970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
233070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      }
233170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
233260d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
23330e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
23344fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
23354fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
23364fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
23374fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
23381d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
233937387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
23401d922960e083906a586609ac6978678147250177Sebastian Redl
234137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
2342effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
23431d922960e083906a586609ac6978678147250177Sebastian Redl
2344d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
2345d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
23462725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        if (SuperLoc.isValid())
234723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
23482725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                               KeyIdents.data(),
234970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
235070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
23512725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        else if (ReceiverType)
235223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2353d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
235470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
235570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
2356d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
23579ae2f076ca5ab1feb3ba95629099ec2319833701John McCall          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2358d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
235970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
236070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                /*AtArgumentEpression=*/false);
23617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
236270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
2363d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
2364d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
236537387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
23662fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
2367df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
2368a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
2369a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
2370a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2371a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
2372df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
237349f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
23741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
237560d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
23760e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
23774fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
23784fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
23794fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
23804fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
23811d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
238249f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
23830e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
238449f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
2385effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
2386a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2387a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
2388a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
23891d922960e083906a586609ac6978678147250177Sebastian Redl
23904fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
23914fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
23924fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
23934fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
23941d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2395a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
2396809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian
2397df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
2398809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    if (Tok.is(tok::identifier))
2399809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_colon);
2400809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    else
2401809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_rsquare);
24024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
24034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
24044fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
24054fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
24061d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2407a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
24081d922960e083906a586609ac6978678147250177Sebastian Redl
2409699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
24101d922960e083906a586609ac6978678147250177Sebastian Redl
241129238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
2412951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  if (nKeys == 0) {
2413ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
2414951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis    KeyLocs.push_back(Loc);
2415951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  }
2416ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
24171d922960e083906a586609ac6978678147250177Sebastian Redl
24182725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
241923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
2420951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                     LBracLoc, KeyLocs, RBracLoc,
2421f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2422f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2423f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
24242725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  else if (ReceiverType)
242523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
2426951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                     LBracLoc, KeyLocs, RBracLoc,
2427f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2428f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2429f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
24309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
2431951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                      LBracLoc, KeyLocs, RBracLoc,
2432f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      MultiExprArg(Actions,
2433f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.take(),
2434f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.size()));
24350ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
24360ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
243760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
243860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseStringLiteralExpression());
24391d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
24401d922960e083906a586609ac6978678147250177Sebastian Redl
2441b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
2442b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
2443b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
24445f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 4> AtLocs;
2445a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
2446b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
2447effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
24480e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2449b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
2450b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
2451b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
245215faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
245397cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
245497cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
2455b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
245660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Lit(ParseStringLiteralExpression());
24570e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
24581d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
24590e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2460effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
2461b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
24621d922960e083906a586609ac6978678147250177Sebastian Redl
24631d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
24641d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
24655508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
2466f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
2467f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
2468f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
246960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
24701d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
2471861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
24721d922960e083906a586609ac6978678147250177Sebastian Redl
2473f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
24741d922960e083906a586609ac6978678147250177Sebastian Redl
24754fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
24761d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
24771d922960e083906a586609ac6978678147250177Sebastian Redl
24784a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
24794a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
24801d922960e083906a586609ac6978678147250177Sebastian Redl
2481809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
24821d922960e083906a586609ac6978678147250177Sebastian Redl
24834a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
24841d922960e083906a586609ac6978678147250177Sebastian Redl
2485809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2486809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2487809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
24884a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
24894a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                 T.getOpenLocation(), Ty.get(),
24904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                 T.getCloseLocation()));
2491f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
249229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
249329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
249429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
249560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
24961d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
249729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
24981d922960e083906a586609ac6978678147250177Sebastian Redl
24994fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
25001d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
25011d922960e083906a586609ac6978678147250177Sebastian Redl
25024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
25034a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
25041d922960e083906a586609ac6978678147250177Sebastian Redl
25054fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
25061d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
25071d922960e083906a586609ac6978678147250177Sebastian Redl
2508390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
250929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
25101d922960e083906a586609ac6978678147250177Sebastian Redl
25114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
251229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
25131d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
25144a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getOpenLocation(),
25154a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getCloseLocation()));
251629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
2517a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
2518a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
2519a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
252060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
2521a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
25221d922960e083906a586609ac6978678147250177Sebastian Redl
25234fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
25241d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
25251d922960e083906a586609ac6978678147250177Sebastian Redl
25265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2527a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
2528458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
25294a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
25304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
25314a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2532458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  if (Tok.is(tok::code_completion)) {
2533458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2534458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                     KeyIdents.size());
25357d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
2536458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    return ExprError();
2537458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  }
2538458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
25392fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
25405add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner  if (!SelIdent &&  // missing selector name.
25415add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
25421d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
25431d922960e083906a586609ac6978678147250177Sebastian Redl
2544b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
2545887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
2546887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
2547a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
25485add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
25495add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        ++nColons;
25505add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        KeyIdents.push_back(0);
25515add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      } else if (Tok.isNot(tok::colon))
25521d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
25531d922960e083906a586609ac6978678147250177Sebastian Redl
25545add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      ++nColons;
25553b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      ConsumeToken(); // Eat the ':' or '::'.
2556a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
2557a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2558458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2559458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      if (Tok.is(tok::code_completion)) {
2560458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2561458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                         KeyIdents.size());
25627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2563458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        return ExprError();
2564458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      }
2565458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2566a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
2567a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
25682fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
2569b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
25703b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
2571a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2572a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
2573887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
25744a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
2575887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
25761d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
25774a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getOpenLocation(),
25784a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getCloseLocation()));
257958065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
2580140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2581140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianDecl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2582a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2583a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  // Save the current token position.
2584a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  SourceLocation OrigLoc = Tok.getLocation();
2585a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2586140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2587140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Append the current token at the end of the new token stream so that it
2588140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // doesn't get lost.
2589140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LM.Toks.push_back(Tok);
2590140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2591140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2592140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // MDecl might be null due to error in method prototype, etc.
2593140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  Decl *MDecl = LM.D;
2594140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume the previously pushed token.
2595140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ConsumeAnyToken();
2596140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2597140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2598140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  SourceLocation BraceLoc = Tok.getLocation();
2599140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Enter a scope for the method body.
2600140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ParseScope BodyScope(this,
2601140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian                       Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2602140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2603140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Tell the actions module that we have entered a method definition with the
2604140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // specified Declarator for the method.
2605140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2606140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2607140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  if (PP.isCodeCompletionEnabled()) {
2608140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian      if (trySkippingFunctionBodyForCodeCompletion()) {
2609140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian          BodyScope.Exit();
2610140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian          return Actions.ActOnFinishFunctionBody(MDecl, 0);
2611140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian      }
2612140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  }
2613140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2614140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  StmtResult FnBody(ParseCompoundStatementBody());
2615140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2616140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // If the function body could not be parsed, make a bogus compoundstmt.
2617140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  if (FnBody.isInvalid())
2618140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2619140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian                                       MultiStmtArg(Actions), false);
2620140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2621140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Leave the function body scope.
2622140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  BodyScope.Exit();
2623140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2624a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2625a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2626a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  if (Tok.getLocation() != OrigLoc) {
2627a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // Due to parsing error, we either went over the cached tokens or
2628a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // there are still cached tokens left. If it's the latter case skip the
2629a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // leftover tokens.
2630a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // Since this is an uncommon situation that should be avoided, use the
2631a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // expensive isBeforeInTranslationUnit call.
2632a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2633a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis                                                     OrigLoc))
2634a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis      while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2635a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis        ConsumeAnyToken();
2636a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  }
2637a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2638a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  return MDecl;
2639140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian}
2640