ParseObjc.cpp revision 6940972c33891ff5d2189fbf232dcdeee0e9daf8
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);
457f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_interface: {
460b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
4795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
4895ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
497f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
507f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_protocol: {
510b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
52bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
537f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
545ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
55849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return ParseObjCAtImplementationDeclaration(AtLoc);
565ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
57140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    return ParseObjCAtEndDeclaration(AtLoc);
585ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
5995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
6095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
615ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
6295ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCPropertySynthesize(AtLoc);
6395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
645ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
6595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCPropertyDynamic(AtLoc);
6695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
6732ad2ee2618745ce3da51c2ae066ed5f21157c07Ted Kremenek  case tok::objc___experimental_modules_import:
684e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().Modules)
6994ad28b31433058445a27db722f60402ee820beaDouglas Gregor      return ParseModuleImport(AtLoc);
7094ad28b31433058445a27db722f60402ee820beaDouglas Gregor
7194ad28b31433058445a27db722f60402ee820beaDouglas Gregor    // Fall through
7294ad28b31433058445a27db722f60402ee820beaDouglas Gregor
735ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
745ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
755ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
7695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = 0;
7795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  return Actions.ConvertDeclToDeclGroup(SingleDecl);
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// objc-class-declaration:
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
8695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::DeclGroupPtrTy
8795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
895f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 8> ClassNames;
905f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ClassLocs;
91c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek
921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
94df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
9795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian      return Actions.ConvertDeclToDeclGroup(0);
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
100c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek    ClassLocs.push_back(Tok.getLocation());
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
103df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1065f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
11195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    return Actions.ConvertDeclToDeclGroup(0);
1121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
114c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassLocs.data(),
115c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassNames.size());
1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
118d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggenvoid Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
119d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen{
120d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
121d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  if (ock == Sema::OCK_None)
122d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    return;
123d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
124849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Decl *Decl = Actions.getObjCDeclContext();
125849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (CurParsedObjCImpl) {
126849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    CurParsedObjCImpl->finish(AtLoc);
127849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  } else {
128849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    Actions.ActOnAtEnd(getCurScope(), AtLoc);
129849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
130d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Diag(AtLoc, diag::err_objc_missing_end)
131d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      << FixItHint::CreateInsertion(AtLoc, "@end\n");
132d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  if (Decl)
133d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(Decl->getLocStart(), diag::note_objc_container_start)
134d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << (int) ock;
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
164b2f6820773aabff3c5c9e0dbb1cbbbda0d80c41fPatrick Beard///     __attribute__((objc_root_class))
165dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
166d64251fd56577dd5c78903454632361e094c6dc1Erik VerbruggenDecl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
1677f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                              ParsedAttributes &attrs) {
168861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
169dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
170d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
171dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1733b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@interface'.
1743b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
17523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
1767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1777d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
1783b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
1793b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
180df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
181dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
182d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
183dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
18463e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
185dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1867ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
187dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
1885512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  if (Tok.is(tok::l_paren) &&
1895512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
1904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
1924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
1934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    SourceLocation categoryLoc;
195dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
19633ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
19723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
1987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
20033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
20133ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
202527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
203df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
204dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
205dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
20605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
2074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    else if (!getLangOpts().ObjC2) {
208527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
209d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
210dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
2114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
2134a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
214d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
21513d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
21613d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor    if (!attrs.empty()) { // categories don't support attributes.
21713d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      Diag(nameLoc, diag::err_objc_no_attributes_on_category);
21813d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      attrs.clear();
219dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
22013d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
2215512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    // Next, we need to check for any protocol references.
2225512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    SourceLocation LAngleLoc, EndProtoLoc;
2235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<Decl *, 8> ProtocolRefs;
2245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 8> ProtocolLocs;
2255512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::less) &&
2265512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
22771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
228d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
22905511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian
230d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *CategoryType =
231d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Actions.ActOnStartCategoryInterface(AtLoc,
2325512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        nameId, nameLoc,
2335512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        categoryId, categoryLoc,
2345512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.data(),
2355512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.size(),
2365512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolLocs.data(),
2375512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        EndProtoLoc);
238e6f07f538fd0eddd6c087fcc01d4e4ff19129c71Fariborz Jahanian
239a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    if (Tok.is(tok::l_brace))
240d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
241a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
2422f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian    ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
2435512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    return CategoryType;
244dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
245dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
246dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
247dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
2487ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
249df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
250dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
2513b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
2523b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
2533b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
25423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
2557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
2567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
2573b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2583b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
259df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
260dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
261d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
262dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
263dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
264dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
265dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
266dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
2675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
2685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
26971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
27006036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
27171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
27271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
273d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
2741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
275d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ClsType =
276d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
27706036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
278beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
27918df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                     ProtocolLocs.data(),
2807f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                     EndProtoLoc, attrs.getList());
2811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
282df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
283d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
284dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
2852f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
2865512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  return ClsType;
287dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
288dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
289d0014540005f2a5ab837365db6bd40f479406758John McCall/// The Objective-C property callback.  This should be defined where
290d0014540005f2a5ab837365db6bd40f479406758John McCall/// it's used, but instead it's been lifted to here to support VS2005.
291d0014540005f2a5ab837365db6bd40f479406758John McCallstruct Parser::ObjCPropertyCallback : FieldCallback {
29299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikieprivate:
29399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
29499ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikiepublic:
295d0014540005f2a5ab837365db6bd40f479406758John McCall  Parser &P;
2965f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVectorImpl<Decl *> &Props;
297d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCDeclSpec &OCDS;
298d0014540005f2a5ab837365db6bd40f479406758John McCall  SourceLocation AtLoc;
29977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian  SourceLocation LParenLoc;
300d0014540005f2a5ab837365db6bd40f479406758John McCall  tok::ObjCKeywordKind MethodImplKind;
301d0014540005f2a5ab837365db6bd40f479406758John McCall
302a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  ObjCPropertyCallback(Parser &P,
3035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &Props,
304d0014540005f2a5ab837365db6bd40f479406758John McCall                       ObjCDeclSpec &OCDS, SourceLocation AtLoc,
30577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                       SourceLocation LParenLoc,
306d0014540005f2a5ab837365db6bd40f479406758John McCall                       tok::ObjCKeywordKind MethodImplKind) :
30777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian    P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
308d0014540005f2a5ab837365db6bd40f479406758John McCall    MethodImplKind(MethodImplKind) {
309d0014540005f2a5ab837365db6bd40f479406758John McCall  }
310d0014540005f2a5ab837365db6bd40f479406758John McCall
311dcdff46dd8e6d749283fe6c43adfcfe780c1d562Eli Friedman  void invoke(ParsingFieldDeclarator &FD) {
312d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.D.getIdentifier() == 0) {
313d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
314d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
315dcdff46dd8e6d749283fe6c43adfcfe780c1d562Eli Friedman      return;
316d0014540005f2a5ab837365db6bd40f479406758John McCall    }
317d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.BitfieldSize) {
318d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_bitfield)
319d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
320dcdff46dd8e6d749283fe6c43adfcfe780c1d562Eli Friedman      return;
321d0014540005f2a5ab837365db6bd40f479406758John McCall    }
322d0014540005f2a5ab837365db6bd40f479406758John McCall
323d0014540005f2a5ab837365db6bd40f479406758John McCall    // Install the property declarator into interfaceDecl.
324d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SelName =
325d0014540005f2a5ab837365db6bd40f479406758John McCall      OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
326d0014540005f2a5ab837365db6bd40f479406758John McCall
327d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector GetterSel =
328d0014540005f2a5ab837365db6bd40f479406758John McCall      P.PP.getSelectorTable().getNullarySelector(SelName);
329d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SetterName = OCDS.getSetterName();
330d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector SetterSel;
331d0014540005f2a5ab837365db6bd40f479406758John McCall    if (SetterName)
332d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
333d0014540005f2a5ab837365db6bd40f479406758John McCall    else
334d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
335d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     P.PP.getSelectorTable(),
336d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     FD.D.getIdentifier());
337d0014540005f2a5ab837365db6bd40f479406758John McCall    bool isOverridingProperty = false;
338d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Property =
33977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
34077bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                              FD, OCDS,
341a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                              GetterSel, SetterSel,
342d0014540005f2a5ab837365db6bd40f479406758John McCall                              &isOverridingProperty,
343d0014540005f2a5ab837365db6bd40f479406758John McCall                              MethodImplKind);
344d0014540005f2a5ab837365db6bd40f479406758John McCall    if (!isOverridingProperty)
345d0014540005f2a5ab837365db6bd40f479406758John McCall      Props.push_back(Property);
346d0014540005f2a5ab837365db6bd40f479406758John McCall
347f66a0dda541cd859a928193efba6dc5d7ba8fe54Eli Friedman    FD.complete(Property);
348d0014540005f2a5ab837365db6bd40f479406758John McCall  }
349d0014540005f2a5ab837365db6bd40f479406758John McCall};
350d0014540005f2a5ab837365db6bd40f479406758John McCall
35199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid Parser::ObjCPropertyCallback::anchor() {
35299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie}
35399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
354dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
355dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
356dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
357294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
3583536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
359dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
360dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
361dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
362294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
363294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
364294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
365294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
3662f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanianvoid Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
3672f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian                                        Decl *CDecl) {
3685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> allMethods;
3695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 16> allProperties;
3705f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclGroupPtrTy, 8> allTUVariables;
37100933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
3721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
373782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  SourceRange AtEnd;
3742f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian
375294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
376e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
377df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
378d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian      if (Decl *methodPrototype =
379d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian          ParseObjCMethodPrototype(MethodImplKind, false))
380d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian        allMethods.push_back(methodPrototype);
3813536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
3823536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
3830db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis      if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
3840db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
3850db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
3860db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        if (Tok.is(tok::semi))
3870db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis          ConsumeToken();
3880db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis      }
389294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
390294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
39105511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    if (Tok.is(tok::l_paren)) {
39205511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      Diag(Tok, diag::err_expected_minus_or_plus);
393d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ParseObjCMethodDecl(Tok.getLocation(),
394d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          tok::minus,
39590ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                          MethodImplKind, false);
39605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      continue;
39705511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
398e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
399e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
400294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
401e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
402e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
4031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
405e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
406e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
408b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    // Code completion within an Objective-C interface.
409b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    if (Tok.is(tok::code_completion)) {
41023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
411849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                            CurParsedObjCImpl? Sema::PCC_ObjCImplementation
412f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             : Sema::PCC_ObjCInterface);
4137d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
414b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    }
415b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor
416e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
417e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
4181fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
4191fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
4201fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
4211fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
4221fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
4232edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt      ParsedAttributesWithRange attrs(AttrFactory);
4247f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
425e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
426e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
4271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
428e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
429e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
430c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    if (Tok.is(tok::code_completion)) {
431a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCAtDirective(getCurScope());
4327d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
433c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    }
434c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
435a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
437a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
438782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setBegin(AtLoc);
439782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setEnd(Tok.getLocation());
440e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
441c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor    } else if (DirectiveKind == tok::objc_not_keyword) {
442c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      Diag(Tok, diag::err_objc_unknown_at);
443c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      SkipUntil(tok::semi);
444c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      continue;
445bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
447bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
448bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
449bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
450a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
451a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
452bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
453bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
454bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
455bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
456f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
457bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
458a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
459a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
46046d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
46146d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian    case tok::objc_implementation:
462df81c2c6ce37f4ae3c45dd01093b6274fa0b6692Fariborz Jahanian    case tok::objc_interface:
463d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      Diag(AtLoc, diag::err_objc_missing_end)
464d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen          << FixItHint::CreateInsertion(AtLoc, "@end\n");
465d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      Diag(CDecl->getLocStart(), diag::note_objc_container_start)
466d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen          << (int) Actions.getObjCContainerKind();
46746d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      ConsumeToken();
46846d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      break;
46946d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
470a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
471a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
472a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
473bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
474e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
475bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
476a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
477bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
478a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
480a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
4814e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!getLangOpts().ObjC2)
482b321c0c0ba957d78475e72cebde4028fdaa00f8fChris Lattner        Diag(AtLoc, diag::err_objc_properties_require_objc2);
483f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
484e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
48577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      SourceLocation LParenLoc;
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
48777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      if (Tok.is(tok::l_paren)) {
48877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        LParenLoc = Tok.getLocation();
489a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        ParseObjCPropertyAttribute(OCDS);
49077bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      }
4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
492a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      ObjCPropertyCallback Callback(*this, allProperties,
49377bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                    OCDS, AtLoc, LParenLoc, MethodImplKind);
494bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
495e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
496f66a0dda541cd859a928193efba6dc5d7ba8fe54Eli Friedman      ParsingDeclSpec DS(*this);
497bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
4981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4997da19ea50d4161fcda40d135735bcf450cabeb50John McCall      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
500a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
501f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
502294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
503bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
504bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
505bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
506c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
507a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.CodeCompleteObjCAtDirective(getCurScope());
5087d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return cutOffParsing();
509d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
510bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
511d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  } else {
512d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(Tok, diag::err_objc_missing_end)
513d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
514d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(CDecl->getLocStart(), diag::note_objc_container_start)
515d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << (int) Actions.getObjCContainerKind();
516d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    AtEnd.setBegin(Tok.getLocation());
517d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    AtEnd.setEnd(Tok.getLocation());
518d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  }
5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
520a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
521bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
522a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnAtEnd(getCurScope(), AtEnd,
5231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
524beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
525beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
526294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
527294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
528d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
529d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
530d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
531d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
532d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
533d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
534d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
535d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
536d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
537d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
538d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
539d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
540d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
541d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
542d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
543f85e193739c953358c865005855253af4f68a497John McCall///     atomic
544f85e193739c953358c865005855253af4f68a497John McCall///     strong
545f85e193739c953358c865005855253af4f68a497John McCall///     weak
546f85e193739c953358c865005855253af4f68a497John McCall///     unsafe_unretained
547d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
548a28948f34817476d02412fa204cae038e228c827Fariborz Jahanianvoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
5498ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
5504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
5514a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
5521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
553cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
554ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
55523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
5567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
557ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
558d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
5591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
560f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
561f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
5624a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
563f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
564f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
566156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
569e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
57092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
571e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
572f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("unsafe_unretained"))
573f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
57492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
575e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
57692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
577e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
578f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("strong"))
579f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
58092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
581e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
58292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
583e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
58445937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian    else if (II->isStr("atomic"))
58545937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
586f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("weak"))
587f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
58892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
58942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      bool IsSetter = II->getNameStart()[0] == 's';
59042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
591e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
59242499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
59342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        diag::err_objc_expected_equal_for_getter;
59442499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
59542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
596dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5984ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      if (Tok.is(tok::code_completion)) {
59942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        if (IsSetter)
600a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertySetter(getCurScope());
6014ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        else
602a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertyGetter(getCurScope());
6037d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
6044ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      }
6054ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
60642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
60742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      SourceLocation SelLoc;
60842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
60942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
61042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (!SelIdent) {
61142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
61242499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson          << IsSetter;
6138ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
6148ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
6158ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
6161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (IsSetter) {
6188ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
61942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setSetterName(SelIdent);
6201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
621e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian        if (ExpectAndConsume(tok::colon,
622e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian                             diag::err_expected_colon_after_setter_name, "",
623156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
6248ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
6258ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
6268ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
62742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setGetterName(SelIdent);
6288ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
629e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
630a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
631cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
632cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
633cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
635156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
636156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
638156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
639d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
642d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
643d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
6443536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
6451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
6463536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
647294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
648294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
649294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
650294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6514985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
6524985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
6534985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
654a28948f34817476d02412fa204cae038e228c827Fariborz JahanianDecl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
65590ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                       bool MethodDefinition) {
656df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
657294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
659bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
660a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
66190ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                    MethodDefinition);
6623536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
6632bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
664f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
665294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
666294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
667294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
668294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
669294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
670294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
671294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
672294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
673294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
674294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6752fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
676be74740cc246ce08d42804a684385a42eb814edbFariborz Jahanian
677ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
678ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
679ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
680afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampamp:
681afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampequal:
682afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::amp:
683afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipe:
684afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::tilde:
685afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaim:
686afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaimequal:
687afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipepipe:
688afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipeequal:
689afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caret:
690afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caretequal: {
6913846ca29a8cc1d376a4b695194c29952dbbfb544Fariborz Jahanian    std::string ThisTok(PP.getSpelling(Tok));
692afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    if (isalpha(ThisTok[0])) {
693afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
694afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      Tok.setKind(tok::identifier);
695afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      SelectorLoc = ConsumeToken();
696afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      return II;
697afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    }
698afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    return 0;
699afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  }
700afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian
701ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
702ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
703ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
7049298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
705ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
706ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
707ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
708ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
709ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
710ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
711ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
712ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
713ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
714ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
715ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
716ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
717ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
718ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
719ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
720ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
721ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
722ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
723ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
724ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
725ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
726ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
727ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
728ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
729ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
730ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
731ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
732ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
733ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
734ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
735ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
736ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
737ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
738ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
739ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
740ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
741ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
742ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
743ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
744ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
745ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
746ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
747ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
748ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
749ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
750ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
751ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
752ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
753ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
754ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
755ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
756ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
757ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
758ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
759ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
760ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
761ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
762ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
763ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
764ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
765ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
766ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
767ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
768ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
769ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
770ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
7714b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
772ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
773d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
774294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
775294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
7760196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
7770196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
778335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
7793ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
7803ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
7813ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
7824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
7835ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
7840196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
7850196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
786a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
787e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
788e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
789294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
790294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
791294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
792294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
793294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
794b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregorvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
795cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                        Declarator::TheContext Context) {
796cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert(Context == Declarator::ObjCParameterContext ||
797cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall         Context == Declarator::ObjCResultContext);
798cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
799e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
800d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    if (Tok.is(tok::code_completion)) {
801b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor      Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
802cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                          Context == Declarator::ObjCParameterContext);
8037d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
804d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    }
805d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor
806cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
807e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
8081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
809e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
810e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
811a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
812e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
8131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
814a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
815e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
816b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      default: llvm_unreachable("Unknown decl qualifier");
817a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
818a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
819a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
820a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
821a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
822a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
823e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
824a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
825e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
826e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
827e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
828e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
8291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
830e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
831e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
832e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
833e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
834e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
835cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// Take all the decl attributes out of the given list and add
836cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// them to the given attribute set.
837cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCallstatic void takeDeclAttributes(ParsedAttributes &attrs,
838cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                               AttributeList *list) {
839cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  while (list) {
840cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    AttributeList *cur = list;
841cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    list = cur->getNext();
842cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
843cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    if (!cur->isUsedAsTypeAttr()) {
844cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // Clear out the next pointer.  We're really completely
845cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // destroying the internal invariants of the declarator here,
846cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // but it doesn't matter because we're done with it.
847cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      cur->setNext(0);
848cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      attrs.add(cur);
849cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    }
850cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  }
851cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall}
852cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
853cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// takeDeclAttributes - Take all the decl attributes from the given
854cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// declarator and add them to the given list.
855cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCallstatic void takeDeclAttributes(ParsedAttributes &attrs,
856cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                               Declarator &D) {
857cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // First, take ownership of all attributes.
858cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  attrs.getPool().takeAllFrom(D.getAttributePool());
859cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
860cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
861cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // Now actually move the attributes over.
862cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
863cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  takeDeclAttributes(attrs, D.getAttributes());
864cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
865cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    takeDeclAttributes(attrs,
866cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                  const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
867cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall}
868cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
869e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
870e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
871e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
872e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
873b77cab97f17f946744c920629ca17271308d8ebfDouglas GregorParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
874cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                     Declarator::TheContext context,
875cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                     ParsedAttributes *paramAttrs) {
876cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert(context == Declarator::ObjCParameterContext ||
877cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall         context == Declarator::ObjCResultContext);
878cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
879cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
880df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8824a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
8834a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
8844a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
885e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
8869735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  ObjCDeclContextSwitch ObjCDC(*this);
8879735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian
88819d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
889cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  ParseObjCTypeQualifierList(DS, context);
8904fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
891b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType Ty;
892809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
893cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // Parse an abstract declarator.
894cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    DeclSpec declSpec(AttrFactory);
895cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    declSpec.setObjCQualifiers(&DS);
896cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ParseSpecifierQualifierList(declSpec);
8976d1de1b8663fe652903bda2927b885d1ec0f2613Fariborz Jahanian    declSpec.SetRangeEnd(Tok.getLocation());
898cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    Declarator declarator(declSpec, context);
899cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ParseDeclarator(declarator);
900cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
901cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // If that's not invalid, extract a type.
902cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    if (!declarator.isInvalidType()) {
903cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
904cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      if (!type.isInvalid())
905cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall        Ty = type.get();
906cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
907cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // If we're parsing a parameter, steal all the decl attributes
908cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // and add them to the decl spec.
909cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      if (context == Declarator::ObjCParameterContext)
910cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall        takeDeclAttributes(*paramAttrs, declarator);
911cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    }
912cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  } else if (context == Declarator::ObjCResultContext &&
913cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall             Tok.is(tok::identifier)) {
914e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (!Ident_instancetype)
915e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ident_instancetype = PP.getIdentifierInfo("instancetype");
916e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
917e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (Tok.getIdentifierInfo() == Ident_instancetype) {
918e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
919e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      ConsumeToken();
920e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    }
921809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
922e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
9234a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
9244a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
9254a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
926e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
9271ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
9284a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
9294a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
9304a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
9314a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
9324a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
933294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
934f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
935294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
936294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
937294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
938294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
9394985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
940294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
9414985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
942294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
943294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
9441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
945294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
946294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
947294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
9487ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
9497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
9507ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
9517ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
952294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9534985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
9544985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
955294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9564985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
9574985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
958294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9594985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
960294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
961294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9627ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
9637ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
9647ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
965d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
9662ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor                                  tok::TokenKind mType,
96790ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  tok::ObjCKeywordKind MethodImplKind,
96890ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  bool MethodDefinition) {
9699257664568bf375b7790131a84d9a4fa30a5b7e3John McCall  ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
97054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
971e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
97223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
973a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       /*ReturnType=*/ ParsedType());
9747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
9757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
976e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
977e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
978e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
979b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType ReturnType;
980a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
981df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
982cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
9831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9849e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // If attributes exist before the method, parse them.
9850b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributes methodAttrs(AttrFactory);
9864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC2)
9870b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
9889e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek
989e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
99023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
991a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       ReturnType);
9927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
9937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
994e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
995e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
9969e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // Now parse the selector.
997bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
9982fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
999e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
100084c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
100184c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
10021ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
10031ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
1004d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian    // Skip until we get a ; or @.
1005d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian    SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
1006d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1007e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
10081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10095f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
1010df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
1011ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
10124e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjC2)
10130b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(methodAttrs);
10141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1015ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
1016d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Result
10177f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian         = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1018a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                          mType, DSRet, ReturnType,
1019926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                          selLoc, Sel, 0,
10204f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          CParamInfo.data(), CParamInfo.size(),
10210b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          methodAttrs.getList(), MethodImplKind,
10220b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          false, MethodDefinition);
102354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
102454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
1025ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
1026f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
10275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
102811d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> KeyLocs;
10295f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
10307f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian  ParseScope PrototypeScope(this,
10317f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian                            Scope::FunctionPrototypeScope|Scope::DeclScope);
10320b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
10330b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  AttributePool allParamAttrs(AttrFactory);
10347f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1035ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
10360b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes paramAttrs(AttrFactory);
1037f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Sema::ObjCArgInfo ArgInfo;
10381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1039ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
1040df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
1041ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
1042ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
1043ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
1044ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
10451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1046b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ArgInfo.Type = ParsedType();
1047e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
1048cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1049cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                       Declarator::ObjCParameterContext,
1050cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                       &paramAttrs);
1051e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
1052ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
1053cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // Regardless, collect all the attributes we've parsed so far.
1054e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
10554e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjC2) {
10560b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(paramAttrs);
10570b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      ArgInfo.ArgAttrs = paramAttrs.getList();
10587f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
10597ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
106040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    // Code completion for the next piece of the selector.
106140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    if (Tok.is(tok::code_completion)) {
106240ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.push_back(SelIdent);
106340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
106440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 mType == tok::minus,
106540ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/true,
106640ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 ReturnType,
106740ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.data(),
106840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.size());
10697d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
10707d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
107140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    }
107240ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor
1073df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1074ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
1075ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
10764985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
10771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1078e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
1079e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
1080ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
10811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1082e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
1083e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
108411d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis    KeyLocs.push_back(selLoc);
1085e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
10860b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    // Make sure the attributes persist.
10870b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    allParamAttrs.takeAllFrom(paramAttrs.getPool());
10880b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
10891f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    // Code completion for the next piece of the selector.
10901f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    if (Tok.is(tok::code_completion)) {
10911f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
10921f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 mType == tok::minus,
109340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/false,
10941f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 ReturnType,
10951f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.data(),
10961f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.size());
10977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
10987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
10991f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    }
11001f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor
1101ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
110211d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis    SelIdent = ParseObjCSelectorPiece(selLoc);
1103df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
1104ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
1105ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
1106ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
11071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1108335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
110956242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian  bool cStyleParamWarned = false;
1110ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
1111df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
1112ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
1113df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
1114335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
11154985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
1116ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
11174985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
111856242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian    if (!cStyleParamWarned) {
111956242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian      Diag(Tok, diag::warn_cstyle_param);
112056242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian      cStyleParamWarned = true;
112156242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian    }
11220b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
1123ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
1125ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
1126ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
11274f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1128d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
11294f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
11304f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    ParmDecl.getIdentifierLoc(),
11314f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    Param,
11324f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                   0));
11334985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11359c4bb2c08989265411925a04252fd4f93c26e3b1Cameron Esfahani  // FIXME: Add support for optional parameter list...
1136e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
11374e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC2)
11380b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
11397f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1140a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  if (KeyIdents.size() == 0)
1141d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
11427f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1143ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1144ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
1145d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Result
11467f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian       = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1147a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                        mType, DSRet, ReturnType,
114811d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis                                        KeyLocs, Sel, &ArgInfos[0],
11494f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        CParamInfo.data(), CParamInfo.size(),
11500b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                        methodAttrs.getList(),
115190ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                        MethodImplKind, isVariadic, MethodDefinition);
11527f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
115354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
115454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
1155294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
1156294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
1157dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
1158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
1159dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
11607caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
11615f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
11625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                            SmallVectorImpl<SourceLocation> &ProtocolLocs,
116371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
116471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
1165e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
11661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierLocPair, 8> ProtocolIdents;
11701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1171e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
117255385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
117355385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
117455385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
11757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
11767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return true;
117755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
117855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
1179e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
1180e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
1181e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
1182e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
1183e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
1184e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1185e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
118671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
1187e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
11881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1189e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
1190e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
1191e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
1192e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1194e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
1195e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
1196e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
1197e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
1198e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1200d78ef5b941ce2937228b010e8443f92025f9d683Douglas Gregor  EndLoc = ConsumeToken();
12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1202e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
1203e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
1204e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
1205e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
1206e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
1207e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
1208e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
12099bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// \brief Parse the Objective-C protocol qualifiers that follow a typename
12109bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// in a decl-specifier-seq, starting at the '<'.
121146f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregorbool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
12129bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
12134e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
12149bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  SourceLocation LAngleLoc, EndProtoLoc;
12155f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolDecl;
12165f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
121746f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
121846f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor                                            LAngleLoc, EndProtoLoc);
12199bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
12209bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor                           ProtocolLocs.data(), LAngleLoc);
12219bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  if (EndProtoLoc.isValid())
12229bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(EndProtoLoc);
122346f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  return Result;
12249bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor}
12259bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
12269bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
1227dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
1228dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
1229dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1230dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
1231dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
1232dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
1233dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
1234dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
1235dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
1236dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
1237dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1238dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
1239dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
1240dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
1241dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
1242ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
1243dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1244dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
12451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
1246dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1247d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
124883c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                             tok::ObjCKeywordKind visibility,
124960fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
1250df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
12515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> AllIvarDecls;
1252a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
12531a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
12543a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis  ObjCDeclContextSwitch ObjCDC(*this);
125572de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
12564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_brace);
12574a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1259ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
1260df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1261ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
12621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1263ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
1264df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
12654b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu      ConsumeExtraSemi(InstanceVariableList);
1266ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
1267ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1269ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
1270df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
1271ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
1272c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1273c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
127423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(getCurScope());
12757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
1276c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
1277c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1278861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
1279ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
1280ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
1281ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
1282ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
1283861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
1284ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
12851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
1286ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
1287ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1288ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
1289ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1290ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
12911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1292c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
129323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
1294f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                       Sema::PCC_ObjCInstanceVariableList);
12957d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
1296c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1297c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1298bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1299bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1300d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *IDecl;
1301bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
13025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<Decl *> &AllIvarDecls;
1303bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1304d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
13055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &AllIvarDecls) :
1306bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1307bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1308bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1309dcdff46dd8e6d749283fe6c43adfcfe780c1d562Eli Friedman      void invoke(ParsingFieldDeclarator &FD) {
1310a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        P.Actions.ActOnObjCContainerStartDefinition(IDecl);
1311bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1312d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        Decl *Field
131323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          = P.Actions.ActOnIvar(P.getCurScope(),
1314bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1315a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                FD.D, FD.BitfieldSize, visibility);
131610af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian        P.Actions.ActOnObjCContainerFinishDefinition();
13170bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian        if (Field)
13180bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian          AllIvarDecls.push_back(Field);
1319f66a0dda541cd859a928193efba6dc5d7ba8fe54Eli Friedman        FD.complete(Field);
1320bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1321bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1322d097be8f81fbf4ed96ac10bae18562dd8202666bFariborz Jahanian
1323e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
1324f66a0dda541cd859a928193efba6dc5d7ba8fe54Eli Friedman    ParsingDeclSpec DS(*this);
1325bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1327df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1328ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1329ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1330ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1331ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1332ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1333ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1334ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
13354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
13364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1337a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
13384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
133910af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian  Actions.ActOnObjCContainerFinishDefinition();
13408749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
13418749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
134223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
134377b6de07be9186063c12928d2e9785a5d4eecbf6David Blaikie                      AllIvarDecls,
13444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                      T.getOpenLocation(), T.getCloseLocation(), 0);
1345ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
13465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1347dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1348dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1349dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1350dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1351dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1352dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
135317d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///     \@protocol identifier
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
13551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
135617d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///     \@end
1357dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1358dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
135917d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///     \@protocol identifier-list ';'
1360dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
136117d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///   "\@protocol identifier ;" should be resolved as "\@protocol
13623536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1363dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1364bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas GregorParser::DeclGroupPtrTy
1365bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas GregorParser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1366bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor                                       ParsedAttributes &attrs) {
1367861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
13687ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
13697ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
13701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1371083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
137223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(getCurScope());
13737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1374bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
1375083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1376083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1377df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
13787ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1379bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
13807ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
13817ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
13827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
13837ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
13841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1385df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
13867caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
13877ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
13881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
13897f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
13907ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
13911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
139290ec96f3026480fa41057b05d58f338aed585f62Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
139390ec96f3026480fa41057b05d58f338aed585f62Erik Verbruggen
1394df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
13955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<IdentifierLocPair, 8> ProtocolRefs;
13967caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
13977caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
13987ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
13997ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
14007ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1401df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
14027ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
14037ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1404bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor        return DeclGroupPtrTy();
14057ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
14067caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
14077caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
14087ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1410df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
14117ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
14127ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
14137ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
14147ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1415bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor      return DeclGroupPtrTy();
14161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1417e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
14181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1419bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
14207f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
14217caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
14221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14237ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
142471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
14257caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
14265f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
14275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
14287caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
142971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
143071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1431bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
14321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1433d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ProtoType =
1434e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1435beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1436beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
143718df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
14387f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                        EndProtoLoc, attrs.getList());
1439a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
14402f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
1441bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor  return Actions.ConvertDeclToDeclGroup(ProtoType);
1442dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1443dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1444dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1445dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1446dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1447dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1448dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1449dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1450dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1451dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1452dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1453dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1454849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::DeclGroupPtrTy
1455849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
1456ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1457ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1458d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
1459ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
14601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14613b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
14623b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
146323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(getCurScope());
14647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1465849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return DeclGroupPtrTy();
14663b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
14673b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1468df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1469ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1470849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return DeclGroupPtrTy();
1471ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1472ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1473ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1474ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1475849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Decl *ObjCImpDecl = 0;
14761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1478ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1479dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeParen();
1480ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1481ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
148333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
148423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
14857d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1486849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
148733ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
148833ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1489df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1490ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1491ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1492ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1493ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1494849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
14951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1496df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1497ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1498ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1499849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
1500ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1501ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1502849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
1503d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen                                    AtLoc, nameId, nameLoc, categoryId,
15048f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1505a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1506849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  } else {
1507849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    // We have a class implementation
1508849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    SourceLocation superClassLoc;
1509849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    IdentifierInfo *superClassId = 0;
1510849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (Tok.is(tok::colon)) {
1511849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      // We have a super class
1512849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ConsumeToken();
1513849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      if (Tok.isNot(tok::identifier)) {
1514849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        Diag(Tok, diag::err_expected_ident); // missing super class name.
1515849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        return DeclGroupPtrTy();
1516849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      }
1517849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      superClassId = Tok.getIdentifierInfo();
1518849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      superClassLoc = ConsumeToken(); // Consume super class name
1519ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1520849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImpDecl = Actions.ActOnStartClassImplementation(
1521849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                                    AtLoc, nameId, nameLoc,
1522849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                                    superClassId, superClassLoc);
1523849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1524849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (Tok.is(tok::l_brace)) // we have ivars
1525849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
1526ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1527849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(ObjCImpDecl);
15281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1529849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  SmallVector<Decl *, 8> DeclsInGroup;
1530a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1531849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  {
1532849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1533849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1534849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ParsedAttributesWithRange attrs(AttrFactory);
1535849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      MaybeParseCXX0XAttributes(attrs);
1536849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      MaybeParseMicrosoftAttributes(attrs);
1537849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1538849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        DeclGroupRef DG = DGP.get();
1539849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        DeclsInGroup.append(DG.begin(), DG.end());
1540849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      }
1541849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    }
1542849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1543849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1544644af7b50bd0541468b45c197f5b637e934d48a0Argyrios Kyrtzidis  return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
15455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
154660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1547140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::DeclGroupPtrTy
1548140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1549ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1550ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1551ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1552849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (CurParsedObjCImpl)
1553849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    CurParsedObjCImpl->finish(atEnd);
15548697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  else
1555782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1556d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(atEnd.getBegin(), diag::err_expected_objc_container);
1557849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  return DeclGroupPtrTy();
15585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1559e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
1560849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1561849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (!Finished) {
1562849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    finish(P.Tok.getLocation());
1563849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (P.Tok.is(tok::eof)) {
1564849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      P.Diag(P.Tok, diag::err_objc_missing_end)
1565849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis          << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1566849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1567849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis          << Sema::OCK_Implementation;
1568849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    }
1569849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1570849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.CurParsedObjCImpl = 0;
1571849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(LateParsedObjCMethods.empty());
1572849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis}
1573d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
1574849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidisvoid Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1575849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(!Finished);
1576849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1577849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
15786c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian    P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
15796c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                               true/*Methods*/);
1580d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
1581849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
158263e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
15836c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  if (HasCFunction)
15846c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian    for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
15856c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian      P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
15866c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                                 false/*c-functions*/);
15876c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
1588849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  /// \brief Clear and free the cached objc methods.
15892fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  for (LateParsedObjCMethodContainer::iterator
15902fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis         I = LateParsedObjCMethods.begin(),
15912fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis         E = LateParsedObjCMethods.end(); I != E; ++I)
15922fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis    delete *I;
15932fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  LateParsedObjCMethods.clear();
1594849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1595849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Finished = true;
15962fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis}
15972fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis
1598e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1599e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1600e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1601d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1602e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1603e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1604e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1605df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1606e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1607d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1608e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1609243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1610243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1611df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1612e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1613d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1614e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1615243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1616243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1617e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1618e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor                   "@compatibility_alias");
1619de01b7a6b4df27555ff7b25e51c0d3df29e3a0cfRichard Smith  return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1620de01b7a6b4df27555ff7b25e51c0d3df29e3a0cfRichard Smith                                         classId, classLoc);
16215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1623ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1624ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1625ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1626ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1627ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1628ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1629ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1630ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1631ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1632ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1633ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1634d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1635ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1636ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1637dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume synthesize
16381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1639b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1640322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
1641a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
16427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1644322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1645322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1646b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1647b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1648b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1649d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1650b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1651b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1652f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1653f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1654f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1655a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor    SourceLocation propertyIvarLoc;
1656df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1657ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1658ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1659322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1660322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
1661a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
16627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
16637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return 0;
1664322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1665322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1666df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1667ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1668ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1669ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1670f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1671a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor      propertyIvarLoc = ConsumeToken(); // consume ivar-name
1672ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1673a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
1674a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, propertyIvar, propertyIvarLoc);
1675df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1676ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1677ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1678ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1679e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
1680d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1681ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1682ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1683ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1684ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1685ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1686ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1687ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1688ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1689ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1690d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1691ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1692ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1693dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume dynamic
1694424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1695424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
1696a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
16977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1699424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1700424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1701424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1702424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1703424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1704d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1705424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1706424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1707c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1708c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1709a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
1710a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, 0, SourceLocation());
1711c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1712df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1713ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1714ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1715ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1716e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
1717d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1718ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
17191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1720397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1721397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1722397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
172360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
172460d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res;
1725397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1726df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
172739f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
17280e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1729397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
173043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1731397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1732397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
173302418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  // consume ';'
173402418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
17359ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
1736397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1737397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1738c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
173978a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1740c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
174160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
174243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1743fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1744fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
17451ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
174643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1747fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
174807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
174907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // The operand is surrounded with parentheses.
1750fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
175107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ExprResult operand(ParseExpression());
175207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
175307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (Tok.is(tok::r_paren)) {
175407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    ConsumeParen();  // ')'
175507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  } else {
175607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
175707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_rparen);
175807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
175907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    // Skip forward until we see a left brace, but don't consume it.
176007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    SkipUntil(tok::l_brace, true, true);
1761fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
176207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
176307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Require a compound statement.
176478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
176507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
176607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_lbrace);
176743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
176878a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
17693ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
177007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Check the @synchronized operand now.
177107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (!operand.isInvalid())
177207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
177307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
177407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Parse the compound statement within a new scope.
177507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ParseScope bodyScope(this, Scope::DeclScope);
177607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  StmtResult body(ParseCompoundStatementBody());
177707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  bodyScope.Exit();
177807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
177907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // If there was a semantic or parse error earlier with the
178007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // operand, fail now.
178107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (operand.isInvalid())
178207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    return StmtError();
178307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
178407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (body.isInvalid())
178507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    body = Actions.ActOnNullStmt(Tok.getLocation());
17860e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
178707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
1788c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1789c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1790397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1791397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1792397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1793397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1794397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1795397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1796397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1797397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1798397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1799397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1800397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
180160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1802397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
180343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1804397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1805df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
18061ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
180743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1808397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
18098f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  StmtVector CatchStmts(Actions);
181060d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult FinallyStmt;
18118935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
181260d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult TryBody(ParseCompoundStatementBody());
18138935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
18140e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1815bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1816a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1817df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
18186b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
18196b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
18206b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
18216b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
18226b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
18236b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
18246b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
18250e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1826161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1827cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1828d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *FirstPart = 0;
18293b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1830df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1831397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1832e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1833df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
18340b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall          DeclSpec DS(AttrFactory);
1835397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
183617b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis          Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
18377ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
18387ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
18394e6c0d19b7c072758922cf80525a81aeefc6e64bDouglas Gregor          // Inform the actions module about the declarator, so it
18407ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
184123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
184264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1843397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
18441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
184593a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
18461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
184793a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
184893a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
184993a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
185093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
185193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
185260d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult CatchBody(true);
1853c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1854c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1855c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1856c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
18570e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
18583b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
18598f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
186060d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
18618f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              RParenLoc,
18628f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              FirstPart,
18639ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                              CatchBody.take());
18648f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        if (!Catch.isInvalid())
18658f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor          CatchStmts.push_back(Catch.release());
18668f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
186764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
18681ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
18691ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
187043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1871397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1872397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
18736b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
18746b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
187564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
18768935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
18770e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
187860d7b3a319d84d688752be3870615ac0f111fb16John McCall      StmtResult FinallyBody(true);
1879c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1880c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1881c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1882c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
18830e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1884161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
18850e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
18869ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                   FinallyBody.take());
1887397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1888397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1889397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1890397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1891bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1892397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
189343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1894bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
18958f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
18969ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
18978f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                    move_arg(CatchStmts),
18989ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                    FinallyStmt.take());
1899397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1900ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1901f85e193739c953358c865005855253af4f68a497John McCall/// objc-autoreleasepool-statement:
1902f85e193739c953358c865005855253af4f68a497John McCall///   @autoreleasepool compound-statement
1903f85e193739c953358c865005855253af4f68a497John McCall///
1904f85e193739c953358c865005855253af4f68a497John McCallStmtResult
1905f85e193739c953358c865005855253af4f68a497John McCallParser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1906f85e193739c953358c865005855253af4f68a497John McCall  ConsumeToken(); // consume autoreleasepool
1907f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isNot(tok::l_brace)) {
1908f85e193739c953358c865005855253af4f68a497John McCall    Diag(Tok, diag::err_expected_lbrace);
1909f85e193739c953358c865005855253af4f68a497John McCall    return StmtError();
1910f85e193739c953358c865005855253af4f68a497John McCall  }
1911f85e193739c953358c865005855253af4f68a497John McCall  // Enter a scope to hold everything within the compound stmt.  Compound
1912f85e193739c953358c865005855253af4f68a497John McCall  // statements can always hold declarations.
1913f85e193739c953358c865005855253af4f68a497John McCall  ParseScope BodyScope(this, Scope::DeclScope);
1914f85e193739c953358c865005855253af4f68a497John McCall
1915f85e193739c953358c865005855253af4f68a497John McCall  StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1916f85e193739c953358c865005855253af4f68a497John McCall
1917f85e193739c953358c865005855253af4f68a497John McCall  BodyScope.Exit();
1918f85e193739c953358c865005855253af4f68a497John McCall  if (AutoreleasePoolBody.isInvalid())
1919f85e193739c953358c865005855253af4f68a497John McCall    AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1920f85e193739c953358c865005855253af4f68a497John McCall  return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1921f85e193739c953358c865005855253af4f68a497John McCall                                                AutoreleasePoolBody.take());
1922f85e193739c953358c865005855253af4f68a497John McCall}
1923f85e193739c953358c865005855253af4f68a497John McCall
19246c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian/// StashAwayMethodOrFunctionBodyTokens -  Consume the tokens and store them
19256c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian/// for later parsing.
19266c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanianvoid Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
19276c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  LexedMethod* LM = new LexedMethod(this, MDecl);
19286c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
19296c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  CachedTokens &Toks = LM->Toks;
19306c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // Begin by storing the '{' token.
19316c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  Toks.push_back(Tok);
19326c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  ConsumeBrace();
19336c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // Consume everything up to (and including) the matching right brace.
19346c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
19356c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian}
19366c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
19373536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1938ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1939d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDefinition() {
1940a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodPrototype();
19411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1942f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1943f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      "parsing Objective-C method");
19441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1945ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1946209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1947849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (CurParsedObjCImpl) {
1948496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
1949849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1950496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1951ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1952209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1953ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1954409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1955df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1956da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
19571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1958409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1959409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
19601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1961409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1962409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1963d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1964ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1965849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1966849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (!MDecl) {
1967849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ConsumeBrace();
1968849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1969849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return 0;
1970849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1971849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1972140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Allow the rest of sema to find private method decl implementations.
1973849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Actions.AddAnyMethodToGlobalPool(MDecl);
1974c9b970996b8af41754a99a9cec15a9d401962ba1Fariborz Jahanian  assert (CurParsedObjCImpl
1975c9b970996b8af41754a99a9cec15a9d401962ba1Fariborz Jahanian          && "ParseObjCMethodDefinition - Method out of @implementation");
1976c9b970996b8af41754a99a9cec15a9d401962ba1Fariborz Jahanian  // Consume the tokens and store them for later parsing.
1977c9b970996b8af41754a99a9cec15a9d401962ba1Fariborz Jahanian  StashAwayMethodOrFunctionBodyTokens(MDecl);
197871c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
19795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19805508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
198160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
19829a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
198323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtStatement(getCurScope());
19847d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
19859a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
19865d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
19875d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19885d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
19896b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
19905d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19915d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
199264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
19935d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19945d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
199564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
1996f85e193739c953358c865005855253af4f68a497John McCall
1997f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1998f85e193739c953358c865005855253af4f68a497John McCall    return ParseObjCAutoreleasePoolStmt(AtLoc);
19995d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
200060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
20010e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
200264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
200364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
200464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
200564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
200643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
200764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
20085d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
200964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
20109ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
20119ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
201264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
201364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
201460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
20155508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
20169a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
201723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtExpression(getCurScope());
20187d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
20199a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
20209a0c85e640a08174569a303db22981612f05d385Douglas Gregor
2021ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::minus:
2022ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::plus: {
2023ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    tok::TokenKind Kind = Tok.getKind();
2024ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    SourceLocation OpLoc = ConsumeToken();
2025ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2026ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (!Tok.is(tok::numeric_constant)) {
2027ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      const char *Symbol = 0;
2028ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      switch (Kind) {
2029ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      case tok::minus: Symbol = "-"; break;
2030ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      case tok::plus: Symbol = "+"; break;
2031ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      default: llvm_unreachable("missing unary operator case");
2032ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      }
2033ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2034ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        << Symbol;
2035ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return ExprError();
2036ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2037ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2038ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2039ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Lit.isInvalid()) {
2040ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return move(Lit);
2041ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
20428b8d9534f185743fb72c59d4e735794e6e0f4335Benjamin Kramer    ConsumeToken(); // Consume the literal token.
2043ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2044ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2045ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Lit.isInvalid())
2046ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return move(Lit);
2047ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2048ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(
2049ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek             Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2050ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
2051ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2052b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
2053b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
20541d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
2055ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2056ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::char_constant:
2057ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2058ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2059ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::numeric_constant:
2060ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2061ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2062ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::kw_true:  // Objective-C++, etc.
2063ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2064ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2065ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::kw_false: // Objective-C++, etc.
2066ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2067ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2068ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2069ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::l_square:
2070ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Objective-C array literal
2071ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2072ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2073ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::l_brace:
2074ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Objective-C dictionary literal
2075ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2076ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2077eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  case tok::l_paren:
2078eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard    // Objective-C boxed expression
2079eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard    return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2080eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard
2081b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
20824fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
20831d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
20842f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
20854fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
20864fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
20871d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
20884fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
20891d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
20904fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
20911d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
20926749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian      default: {
20936749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        const char *str = 0;
20946749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        if (GetLookAheadToken(1).is(tok::l_brace)) {
20956749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          char ch = Tok.getIdentifierInfo()->getNameStart()[0];
20966749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          str =
20976749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian            ch == 't' ? "try"
20986749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian                      : (ch == 'f' ? "finally"
20996749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian                                   : (ch == 'a' ? "autoreleasepool" : 0));
21006749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        }
21016749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        if (str) {
21026749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          SourceLocation kwLoc = Tok.getLocation();
21036749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
21046749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian                             FixItHint::CreateReplacement(kwLoc, str));
21056749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        }
21066749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        else
21076749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          return ExprError(Diag(AtLoc, diag::err_unexpected_at));
21086749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian      }
21094fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
21105508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
21115508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
21125508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
21136aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brirg Parse the receiver of an Objective-C++ message send.
21146aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
21156aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// This routine parses the receiver of a message send in
21166aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// Objective-C++ either as a type or as an expression. Note that this
21176aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// routine must not be called to parse a send to 'super', since it
21186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// has no way to return such a result.
21196aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
21206aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param IsExpr Whether the receiver was parsed as an expression.
21216aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
21226aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param TypeOrExpr If the receiver was parsed as an expression (\c
21236aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// IsExpr is true), the parsed expression. If the receiver was parsed
21246aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// as a type (\c IsExpr is false), the parsed type.
21256aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
21266aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \returns True if an error occurred during parsing or semantic
21276aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// analysis, in which case the arguments do not have valid
21286aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// values. Otherwise, returns false for a successful parse.
21296aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
21306aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///   objc-receiver: [C++]
21316aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     'super' [not parsed here]
21326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     expression
21336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     simple-type-specifier
21346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     typename-specifier
21356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
21360fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
21370fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
21386aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
21396aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
21406aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TryAnnotateTypeOrScopeToken();
21416aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21427bf33401acf506b0039222834d7259acb80f6311Kaelyn Uhrain  if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
21436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   objc-receiver:
21446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     expression
214560d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseExpression();
21466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
21476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
21486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
21506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
21516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
21526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
21536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // objc-receiver:
21556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   typename-specifier
21566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   simple-type-specifier
21576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   expression (that starts with one of the above)
21580b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
21596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  ParseCXXSimpleTypeSpecifier(DS);
21606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::l_paren)) {
21626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // If we see an opening parentheses at this point, we are
21636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // actually parsing an expression that starts with a
21646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // function-style cast, e.g.,
21656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
21666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   postfix-expression:
21676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     simple-type-specifier ( expression-list [opt] )
21686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     typename-specifier ( expression-list [opt] )
21696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
21706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the remainder of this case, then the (optional)
21716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // postfix-expression suffix, followed by the (optional)
21726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // right-hand side of the binary expression. We have an
21736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // instance method.
217460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
21756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
21769ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParsePostfixExpressionSuffix(Receiver.take());
21776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
21789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
21796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
21806aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
21816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
21836aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
21846aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
21856aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
21866aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21876aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // We have a class message. Turn the simple-type-specifier or
21886aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // typename-specifier we parsed into a type and parse the
21896aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // remainder of the class message.
21906aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
219123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
21926aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Type.isInvalid())
21936aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
21946aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21956aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  IsExpr = false;
2196b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  TypeOrExpr = Type.get().getAsOpaquePtr();
21976aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
21986aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
21996aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22001b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// \brief Determine whether the parser is currently referring to a an
22011b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// Objective-C message send, using a simplified heuristic to avoid overhead.
22021b730e847ded503f2e615154035c083c4f94a067Douglas Gregor///
22031b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// This routine will only return true for a subset of valid message-send
22041b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// expressions.
22051b730e847ded503f2e615154035c083c4f94a067Douglas Gregorbool Parser::isSimpleObjCMessageExpression() {
22064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
22071b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         "Incorrect start for isSimpleObjCMessageExpression");
22081b730e847ded503f2e615154035c083c4f94a067Douglas Gregor  return GetLookAheadToken(1).is(tok::identifier) &&
22091b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         GetLookAheadToken(2).is(tok::identifier);
22101b730e847ded503f2e615154035c083c4f94a067Douglas Gregor}
22111b730e847ded503f2e615154035c083c4f94a067Douglas Gregor
22129497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregorbool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
22134e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
22149497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      InMessageExpression)
22159497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
22169497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22179497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22189497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  ParsedType Type;
22199497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22209497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (Tok.is(tok::annot_typename))
22219497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = getTypeAnnotation(Tok);
22229497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else if (Tok.is(tok::identifier))
22239497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
22249497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor                               getCurScope());
22259497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else
22269497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
22279497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22289497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
22299497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    const Token &AfterNext = GetLookAheadToken(2);
22309497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
22319497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      if (Tok.is(tok::identifier))
22329497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor        TryAnnotateTypeOrScopeToken();
22339497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22349497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      return Tok.is(tok::annot_typename);
22359497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    }
22369497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  }
22379497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22389497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  return false;
22399497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor}
22409497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
22420ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
22430ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22442725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///   objc-receiver: [C]
2245eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner///     'super'
22460ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
22470ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
22480ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
22496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
225060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCMessageExpression() {
2251699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
2252699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2253699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
22548e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  if (Tok.is(tok::code_completion)) {
225523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMessageReceiver(getCurScope());
22567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
22578e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    return ExprError();
22588e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  }
22598e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor
22600fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
22610fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
22624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
22636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // We completely separate the C and C++ cases because C++ requires
22646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // more complicated (read: slower) parsing.
22656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Handle send to super.
22676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: This doesn't benefit from the same typo-correction we
22686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // get in Objective-C.
22696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
227023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
2271b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2272b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
22736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the receiver, which is either a type or an expression.
22756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    bool IsExpr;
2276304b752a450c0fc5968c20ba25446d0bb7c6f68dNick Lewycky    void *TypeOrExpr = NULL;
22776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
22786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      SkipUntil(tok::r_square);
22796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ExprError();
22806aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    }
22816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (IsExpr)
2283b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2284b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(),
22859ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            static_cast<Expr*>(TypeOrExpr));
22866aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22876aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2288b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                              ParsedType::getFromOpaquePtr(TypeOrExpr),
2289b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          0);
2290c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  }
2291c59cb38810c63a806270385f79ea84e0203754eaChris Lattner
2292c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  if (Tok.is(tok::identifier)) {
22931dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    IdentifierInfo *Name = Tok.getIdentifierInfo();
22941dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    SourceLocation NameLoc = Tok.getLocation();
2295b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType ReceiverType;
229623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
22971dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor                                       Name == Ident_super,
22981569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       NextToken().is(tok::period),
22991569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       ReceiverType)) {
2300f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCSuperMessage:
2301b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2302b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
23032725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
2304f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCClassMessage:
23051569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      if (!ReceiverType) {
23062725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        SkipUntil(tok::r_square);
23072725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        return ExprError();
23082725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      }
23092725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
23101569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      ConsumeToken(); // the type name
23111569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
23121569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
23139ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            ReceiverType, 0);
23141dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor
2315f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCInstanceMessage:
23162725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      // Fall through to parse an expression.
23171dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor      break;
2318d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
2319699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
2320eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
2321eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  // Otherwise, an arbitrary expression can be the receiver of a send.
232260d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpression());
23230e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
23245c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
23251d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
2326699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
23271d922960e083906a586609ac6978678147250177Sebastian Redl
2328b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2329b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType(), Res.take());
2330699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
23311d922960e083906a586609ac6978678147250177Sebastian Redl
23322725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Parse the remainder of an Objective-C message following the
23332725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// '[' objc-receiver.
23342725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
23352725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine handles sends to super, class messages (sent to a
23362725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class name), and instance messages (sent to an object), and the
23372725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// target is represented by \p SuperLoc, \p ReceiverType, or \p
23382725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// ReceiverExpr, respectively. Only one of these parameters may have
23392725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// a valid value.
23402725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
23412725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening '['.
23422725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
23432725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc If this is a send to 'super', the location of the
23442725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 'super' keyword that indicates a send to the superclass.
23452725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
23462725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType If this is a class message, the type of the
23472725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class we are sending a message to.
23482725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
23492725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverExpr If this is an instance message, the expression
23502725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// used to compute the receiver object.
23511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
23520ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
23530ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
23540ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
23550ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
23560ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
23570ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
23580ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
23590ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
23601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
23610ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
23620ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
23630ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
23640ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
23650ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
23660ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
23670ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
23680ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
23691d922960e083906a586609ac6978678147250177Sebastian Redl///
237060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2371699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
23722725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                       SourceLocation SuperLoc,
2373b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                       ParsedType ReceiverType,
23741d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
23750fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
23760fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
2377c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
23782725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    if (SuperLoc.isValid())
237970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
238070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
23812725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    else if (ReceiverType)
238270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
238370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
2384c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
23859ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
238670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                              0, 0, false);
23877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
23887d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return ExprError();
2389c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
2390d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
2391a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
23924b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
23932fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
239468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
23955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2396951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> KeyLocs;
2397a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
239868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
2399df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
2400a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
2401a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
240268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
2403951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis      KeyLocs.push_back(Loc);
240437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
2405df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
2406a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
24074fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
24084fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
24094fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
24104fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
24111d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
2412a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
24131d922960e083906a586609ac6978678147250177Sebastian Redl
241468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
24151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
241670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
241770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      if (Tok.is(tok::code_completion)) {
241870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        if (SuperLoc.isValid())
241970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
242070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
242170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
242270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
242370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else if (ReceiverType)
242470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
242570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
242670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
242770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
242870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else
242970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
243070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.data(),
243170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
243270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  /*AtArgumentEpression=*/true);
243370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
24347d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
243570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
243670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      }
243770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
243860d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
24390e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
24404fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
24414fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
24424fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
24434fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
24441d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
244537387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
24461d922960e083906a586609ac6978678147250177Sebastian Redl
244737387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
2448effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
24491d922960e083906a586609ac6978678147250177Sebastian Redl
2450d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
2451d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
24522725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        if (SuperLoc.isValid())
245323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
24542725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                               KeyIdents.data(),
245570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
245670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
24572725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        else if (ReceiverType)
245823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2459d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
246070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
246170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
2462d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
24639ae2f076ca5ab1feb3ba95629099ec2319833701John McCall          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2464d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
246570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
246670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                /*AtArgumentEpression=*/false);
24677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
246870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
2469d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
2470d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
247137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
24722fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
2473df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
2474a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
2475a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
2476a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2477a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
2478df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
247918df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian      SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
24801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
248160d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
24820e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
248318df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian        if (Tok.is(tok::colon)) {
248418df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian          Diag(commaLoc, diag::note_extra_comma_message_arg) <<
248518df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian            FixItHint::CreateRemoval(commaLoc);
248618df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian        }
24874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
24884fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
24894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
24904fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
24911d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
249249f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
24930e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
249449f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
2495effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
2496a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2497a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
2498a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
24991d922960e083906a586609ac6978678147250177Sebastian Redl
25004fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
25014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
25024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
25034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
25041d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2505a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
2506809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian
2507df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
2508809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    if (Tok.is(tok::identifier))
2509809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_colon);
2510809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    else
2511809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_rsquare);
25124fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
25134fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
25144fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
25154fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
25161d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2517a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
25181d922960e083906a586609ac6978678147250177Sebastian Redl
2519699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
25201d922960e083906a586609ac6978678147250177Sebastian Redl
252129238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
2522951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  if (nKeys == 0) {
2523ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
2524951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis    KeyLocs.push_back(Loc);
2525951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  }
2526ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
25271d922960e083906a586609ac6978678147250177Sebastian Redl
25282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
252923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
2530951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                     LBracLoc, KeyLocs, RBracLoc,
2531f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2532f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2533f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
25342725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  else if (ReceiverType)
253523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
2536951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                     LBracLoc, KeyLocs, RBracLoc,
2537f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2538f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2539f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
25409ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
2541951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                      LBracLoc, KeyLocs, RBracLoc,
2542f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      MultiExprArg(Actions,
2543f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.take(),
2544f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.size()));
25450ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
25460ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
254760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
254860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseStringLiteralExpression());
25491d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
25501d922960e083906a586609ac6978678147250177Sebastian Redl
2551b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
2552b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
2553b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
25545f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 4> AtLocs;
2555a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
2556b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
2557effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
25580e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2559b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
2560b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
2561b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
256215faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
256397cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
256497cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
2565b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
256660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Lit(ParseStringLiteralExpression());
25670e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
25681d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
25690e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2570effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
2571b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
25721d922960e083906a586609ac6978678147250177Sebastian Redl
25731d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
25741d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
25755508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
2576f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
2577ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// ParseObjCBooleanLiteral -
2578ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// objc-scalar-literal : '@' boolean-keyword
2579ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                        ;
2580ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2581ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                        ;
2582ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2583ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                           bool ArgValue) {
2584ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  SourceLocation EndLoc = ConsumeToken();             // consume the keyword.
2585ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2586ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2587ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2588ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// ParseObjCCharacterLiteral -
2589ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// objc-scalar-literal : '@' character-literal
2590ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                        ;
2591ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2592ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2593ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (Lit.isInvalid()) {
2594ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return move(Lit);
2595ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
25968b8d9534f185743fb72c59d4e735794e6e0f4335Benjamin Kramer  ConsumeToken(); // Consume the literal token.
2597ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2598ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2599ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2600ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// ParseObjCNumericLiteral -
2601ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// objc-scalar-literal : '@' scalar-literal
2602ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                        ;
2603ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// scalar-literal : | numeric-constant			/* any numeric constant. */
2604ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                    ;
2605ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2606ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2607ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (Lit.isInvalid()) {
2608ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return move(Lit);
2609ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
26108b8d9534f185743fb72c59d4e735794e6e0f4335Benjamin Kramer  ConsumeToken(); // Consume the literal token.
2611ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return Owned(Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2612ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2613ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2614eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// ParseObjCBoxedExpr -
2615eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// objc-box-expression:
2616eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard///       @( assignment-expression )
2617eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardExprResult
2618eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardParser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2619eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  if (Tok.isNot(tok::l_paren))
2620eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2621eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard
2622eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  BalancedDelimiterTracker T(*this, tok::l_paren);
2623eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  T.consumeOpen();
2624eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  ExprResult ValueExpr(ParseAssignmentExpression());
2625eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  if (T.consumeClose())
2626eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard    return ExprError();
2627edd27602f9cede4fee2c81c785b8f295c9d28752Argyrios Kyrtzidis
2628edd27602f9cede4fee2c81c785b8f295c9d28752Argyrios Kyrtzidis  if (ValueExpr.isInvalid())
2629edd27602f9cede4fee2c81c785b8f295c9d28752Argyrios Kyrtzidis    return ExprError();
2630edd27602f9cede4fee2c81c785b8f295c9d28752Argyrios Kyrtzidis
2631eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  // Wrap the sub-expression in a parenthesized expression, to distinguish
2632eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  // a boxed expression from a literal.
2633eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2634eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
2635eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  return Owned(Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2636eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard                                          ValueExpr.take()));
2637eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard}
2638eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard
2639ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
2640ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ExprVector ElementExprs(Actions);                   // array elements.
2641ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ConsumeBracket(); // consume the l_square.
2642ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2643ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  while (Tok.isNot(tok::r_square)) {
2644ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Parse list of array element expressions (all must be id types).
2645ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExprResult Res(ParseAssignmentExpression());
2646ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Res.isInvalid()) {
2647ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // We must manually skip to a ']', otherwise the expression skipper will
2648ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // stop at the ']' when it skips to the ';'.  We want it to skip beyond
2649ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // the enclosing expression.
2650ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      SkipUntil(tok::r_square);
2651ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return move(Res);
2652ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2653ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2654ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Parse the ellipsis that indicates a pack expansion.
2655ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Tok.is(tok::ellipsis))
2656ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2657ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Res.isInvalid())
2658ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return true;
2659ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2660ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ElementExprs.push_back(Res.release());
2661ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2662ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Tok.is(tok::comma))
2663ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ConsumeToken(); // Eat the ','.
2664ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    else if (Tok.isNot(tok::r_square))
2665ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek     return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2666ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
2667ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  SourceLocation EndLoc = ConsumeBracket(); // location of ']'
2668ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  MultiExprArg Args(Actions, ElementExprs.take(), ElementExprs.size());
2669ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return Owned(Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args));
2670ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2671ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2672ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2673ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2674ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ConsumeBrace(); // consume the l_square.
2675ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  while (Tok.isNot(tok::r_brace)) {
2676ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Parse the comma separated key : value expressions.
2677ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExprResult KeyExpr;
2678ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    {
2679ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ColonProtectionRAIIObject X(*this);
2680ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      KeyExpr = ParseAssignmentExpression();
2681ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      if (KeyExpr.isInvalid()) {
2682ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        // We must manually skip to a '}', otherwise the expression skipper will
2683ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        // stop at the '}' when it skips to the ';'.  We want it to skip beyond
2684ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        // the enclosing expression.
2685ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        SkipUntil(tok::r_brace);
2686ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        return move(KeyExpr);
2687ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      }
2688ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2689ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2690ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Tok.is(tok::colon)) {
2691ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ConsumeToken();
2692ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    } else {
2693ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return ExprError(Diag(Tok, diag::err_expected_colon));
2694ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2695ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2696ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExprResult ValueExpr(ParseAssignmentExpression());
2697ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (ValueExpr.isInvalid()) {
2698ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // We must manually skip to a '}', otherwise the expression skipper will
2699ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // stop at the '}' when it skips to the ';'.  We want it to skip beyond
2700ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // the enclosing expression.
2701ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      SkipUntil(tok::r_brace);
2702ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return move(ValueExpr);
2703ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2704ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2705ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Parse the ellipsis that designates this as a pack expansion.
2706ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    SourceLocation EllipsisLoc;
27074e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
2708ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      EllipsisLoc = ConsumeToken();
2709ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2710ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // We have a valid expression. Collect it in a vector so we can
2711ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // build the argument list.
2712ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ObjCDictionaryElement Element = {
2713ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      KeyExpr.get(), ValueExpr.get(), EllipsisLoc, llvm::Optional<unsigned>()
2714ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    };
2715ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    Elements.push_back(Element);
2716ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2717ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Tok.is(tok::comma))
2718ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ConsumeToken(); // Eat the ','.
2719ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    else if (Tok.isNot(tok::r_brace))
2720ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2721ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
2722ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  SourceLocation EndLoc = ConsumeBrace();
2723ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2724ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Create the ObjCDictionaryLiteral.
2725ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return Owned(Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2726ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                  Elements.data(),
2727ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                                  Elements.size()));
2728ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2729ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2730f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
2731f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
273260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
27331d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
2734861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
27351d922960e083906a586609ac6978678147250177Sebastian Redl
2736f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
27371d922960e083906a586609ac6978678147250177Sebastian Redl
27384fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
27391d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
27401d922960e083906a586609ac6978678147250177Sebastian Redl
27414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
27424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
27431d922960e083906a586609ac6978678147250177Sebastian Redl
2744809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
27451d922960e083906a586609ac6978678147250177Sebastian Redl
27464a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
27471d922960e083906a586609ac6978678147250177Sebastian Redl
2748809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2749809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2750809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
27514a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
27524a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                 T.getOpenLocation(), Ty.get(),
27534a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                 T.getCloseLocation()));
2754f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
275529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
275629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
275717d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///       \@protocol ( protocol-name )
275860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
27591d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
276029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
27611d922960e083906a586609ac6978678147250177Sebastian Redl
27624fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
27631d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
27641d922960e083906a586609ac6978678147250177Sebastian Redl
27654a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
27664a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
27671d922960e083906a586609ac6978678147250177Sebastian Redl
27684fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
27691d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
27701d922960e083906a586609ac6978678147250177Sebastian Redl
2771390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
27727d24e289bea2accaaed79c6ca3e5cdd0c204ddc1Argyrios Kyrtzidis  SourceLocation ProtoIdLoc = ConsumeToken();
27731d922960e083906a586609ac6978678147250177Sebastian Redl
27744a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
277529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
27761d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
27774a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getOpenLocation(),
27787d24e289bea2accaaed79c6ca3e5cdd0c204ddc1Argyrios Kyrtzidis                                                   ProtoIdLoc,
27794a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getCloseLocation()));
278029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
2781a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
2782a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
2783a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
278460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
2785a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
27861d922960e083906a586609ac6978678147250177Sebastian Redl
27874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
27881d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
27891d922960e083906a586609ac6978678147250177Sebastian Redl
27905f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2791a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
2792458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
27934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
27944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
27954a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2796458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  if (Tok.is(tok::code_completion)) {
2797458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2798458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                     KeyIdents.size());
27997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
2800458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    return ExprError();
2801458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  }
2802458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
28032fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
28045add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner  if (!SelIdent &&  // missing selector name.
28055add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
28061d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
28071d922960e083906a586609ac6978678147250177Sebastian Redl
2808b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
2809887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
2810887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
2811a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
28125add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
28135add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        ++nColons;
28145add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        KeyIdents.push_back(0);
28155add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      } else if (Tok.isNot(tok::colon))
28161d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
28171d922960e083906a586609ac6978678147250177Sebastian Redl
28185add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      ++nColons;
28193b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      ConsumeToken(); // Eat the ':' or '::'.
2820a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
2821a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2822458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2823458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      if (Tok.is(tok::code_completion)) {
2824458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2825458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                         KeyIdents.size());
28267d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2827458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        return ExprError();
2828458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      }
2829458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2830a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
2831a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
28322fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
2833b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
28343b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
2835a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2836a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
2837887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
28384a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
2839887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
28401d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
28414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getOpenLocation(),
28424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getCloseLocation()));
284358065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
2844140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
28456c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanianvoid Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
28466c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // MCDecl might be null due to error in method or c-function  prototype, etc.
28476c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  Decl *MCDecl = LM.D;
28486c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  bool skip = MCDecl &&
28496c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian              ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
28506c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian              (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
28516c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  if (skip)
28526c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian    return;
28536c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
2854a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  // Save the current token position.
2855a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  SourceLocation OrigLoc = Tok.getLocation();
2856a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2857140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2858140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Append the current token at the end of the new token stream so that it
2859140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // doesn't get lost.
2860140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LM.Toks.push_back(Tok);
2861140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2862140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2863140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume the previously pushed token.
2864140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ConsumeAnyToken();
2865140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2866140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
28676c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // Enter a scope for the method or c-fucntion body.
2868140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ParseScope BodyScope(this,
28696c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                       parseMethod
28706c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                       ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
28716c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                       : Scope::FnScope|Scope::DeclScope);
2872140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
28736c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // Tell the actions module that we have entered a method or c-function definition
28746c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // with the specified Declarator for the method/function.
28758c6cb462946b8be19167fad7ff21d4f9fbaebfefFariborz Jahanian  if (parseMethod)
28768c6cb462946b8be19167fad7ff21d4f9fbaebfefFariborz Jahanian    Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
28778c6cb462946b8be19167fad7ff21d4f9fbaebfefFariborz Jahanian  else
28788c6cb462946b8be19167fad7ff21d4f9fbaebfefFariborz Jahanian    Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
28796940972c33891ff5d2189fbf232dcdeee0e9daf8Fariborz Jahanian
28806940972c33891ff5d2189fbf232dcdeee0e9daf8Fariborz Jahanian  MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
28816940972c33891ff5d2189fbf232dcdeee0e9daf8Fariborz Jahanian
2882a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  if (Tok.getLocation() != OrigLoc) {
2883a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // Due to parsing error, we either went over the cached tokens or
2884a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // there are still cached tokens left. If it's the latter case skip the
2885a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // leftover tokens.
2886a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // Since this is an uncommon situation that should be avoided, use the
2887a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // expensive isBeforeInTranslationUnit call.
2888a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2889a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis                                                     OrigLoc))
2890a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis      while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2891a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis        ConsumeAnyToken();
2892a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  }
2893a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
28946c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  return;
2895140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian}
2896