ParseObjc.cpp revision 77bfb8b43ec3f7dee3a71f6e854b03ad29dab84f
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;
675948ae1021122164b22f74353bb7fe325a64f616Douglas Gregor  case tok::objc_import:
6894ad28b31433058445a27db722f60402ee820beaDouglas Gregor    if (getLang().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
164dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
165d64251fd56577dd5c78903454632361e094c6dc1Erik VerbruggenDecl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
1667f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                              ParsedAttributes &attrs) {
167861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
168dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
169d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
170dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1723b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@interface'.
1733b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
17423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
1757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
1773b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
1783b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
179df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
181d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
182dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
18363e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
184dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1857ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
186dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
1875512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  if (Tok.is(tok::l_paren) &&
1885512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
1894a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1904a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
1914a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
1924a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    SourceLocation categoryLoc;
194dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
19533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
19623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
1977d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
19933ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
20033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
201527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
202df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
203dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
204dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
20505511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
20605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    else if (!getLang().ObjC2) {
207527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
208d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
209dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
2104a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
2124a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
213d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
21413d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
21513d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor    if (!attrs.empty()) { // categories don't support attributes.
21613d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      Diag(nameLoc, diag::err_objc_no_attributes_on_category);
21713d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      attrs.clear();
218dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
21913d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
2205512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    // Next, we need to check for any protocol references.
2215512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    SourceLocation LAngleLoc, EndProtoLoc;
2225f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<Decl *, 8> ProtocolRefs;
2235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 8> ProtocolLocs;
2245512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::less) &&
2255512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
22671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
227d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
22805511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian
229d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *CategoryType =
230d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Actions.ActOnStartCategoryInterface(AtLoc,
2315512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        nameId, nameLoc,
2325512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        categoryId, categoryLoc,
2335512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.data(),
2345512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.size(),
2355512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolLocs.data(),
2365512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        EndProtoLoc);
237e6f07f538fd0eddd6c087fcc01d4e4ff19129c71Fariborz Jahanian
238a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    if (Tok.is(tok::l_brace))
239d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
240a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
2412f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian    ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
2425512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    return CategoryType;
243dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
244dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
245dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
246dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
2477ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
248df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
249dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
2503b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
2513b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
2523b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
25323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
2547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
2557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
2563b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2573b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
258df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
259dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
260d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
261dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
262dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
263dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
264dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
265dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
2665f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
2675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
26871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
26906036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
27071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
27171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
272d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
2731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
274d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ClsType =
275d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
27606036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
277beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
27818df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                     ProtocolLocs.data(),
2797f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                     EndProtoLoc, attrs.getList());
2801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
281df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
282d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
283dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
2842f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
2855512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  return ClsType;
286dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
287dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
288d0014540005f2a5ab837365db6bd40f479406758John McCall/// The Objective-C property callback.  This should be defined where
289d0014540005f2a5ab837365db6bd40f479406758John McCall/// it's used, but instead it's been lifted to here to support VS2005.
290d0014540005f2a5ab837365db6bd40f479406758John McCallstruct Parser::ObjCPropertyCallback : FieldCallback {
29199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikieprivate:
29299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
29399ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikiepublic:
294d0014540005f2a5ab837365db6bd40f479406758John McCall  Parser &P;
2955f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVectorImpl<Decl *> &Props;
296d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCDeclSpec &OCDS;
297d0014540005f2a5ab837365db6bd40f479406758John McCall  SourceLocation AtLoc;
29877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian  SourceLocation LParenLoc;
299d0014540005f2a5ab837365db6bd40f479406758John McCall  tok::ObjCKeywordKind MethodImplKind;
300d0014540005f2a5ab837365db6bd40f479406758John McCall
301a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  ObjCPropertyCallback(Parser &P,
3025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &Props,
303d0014540005f2a5ab837365db6bd40f479406758John McCall                       ObjCDeclSpec &OCDS, SourceLocation AtLoc,
30477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                       SourceLocation LParenLoc,
305d0014540005f2a5ab837365db6bd40f479406758John McCall                       tok::ObjCKeywordKind MethodImplKind) :
30677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian    P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
307d0014540005f2a5ab837365db6bd40f479406758John McCall    MethodImplKind(MethodImplKind) {
308d0014540005f2a5ab837365db6bd40f479406758John McCall  }
309d0014540005f2a5ab837365db6bd40f479406758John McCall
310d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *invoke(FieldDeclarator &FD) {
311d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.D.getIdentifier() == 0) {
312d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
313d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
314d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
315d0014540005f2a5ab837365db6bd40f479406758John McCall    }
316d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.BitfieldSize) {
317d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_bitfield)
318d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
319d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
320d0014540005f2a5ab837365db6bd40f479406758John McCall    }
321d0014540005f2a5ab837365db6bd40f479406758John McCall
322d0014540005f2a5ab837365db6bd40f479406758John McCall    // Install the property declarator into interfaceDecl.
323d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SelName =
324d0014540005f2a5ab837365db6bd40f479406758John McCall      OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
325d0014540005f2a5ab837365db6bd40f479406758John McCall
326d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector GetterSel =
327d0014540005f2a5ab837365db6bd40f479406758John McCall      P.PP.getSelectorTable().getNullarySelector(SelName);
328d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SetterName = OCDS.getSetterName();
329d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector SetterSel;
330d0014540005f2a5ab837365db6bd40f479406758John McCall    if (SetterName)
331d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
332d0014540005f2a5ab837365db6bd40f479406758John McCall    else
333d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
334d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     P.PP.getSelectorTable(),
335d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     FD.D.getIdentifier());
336d0014540005f2a5ab837365db6bd40f479406758John McCall    bool isOverridingProperty = false;
337d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Property =
33877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
33977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                              FD, OCDS,
340a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                              GetterSel, SetterSel,
341d0014540005f2a5ab837365db6bd40f479406758John McCall                              &isOverridingProperty,
342d0014540005f2a5ab837365db6bd40f479406758John McCall                              MethodImplKind);
343d0014540005f2a5ab837365db6bd40f479406758John McCall    if (!isOverridingProperty)
344d0014540005f2a5ab837365db6bd40f479406758John McCall      Props.push_back(Property);
345d0014540005f2a5ab837365db6bd40f479406758John McCall
346d0014540005f2a5ab837365db6bd40f479406758John McCall    return Property;
347d0014540005f2a5ab837365db6bd40f479406758John McCall  }
348d0014540005f2a5ab837365db6bd40f479406758John McCall};
349d0014540005f2a5ab837365db6bd40f479406758John McCall
35099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid Parser::ObjCPropertyCallback::anchor() {
35199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie}
35299ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
353dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
354dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
355dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
356294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
3573536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
358dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
359dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
360dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
361294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
362294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
363294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
364294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
3652f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanianvoid Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
3662f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian                                        Decl *CDecl) {
3675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> allMethods;
3685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 16> allProperties;
3695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclGroupPtrTy, 8> allTUVariables;
37000933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
3711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
372782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  SourceRange AtEnd;
3732f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian
374294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
375e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
376df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
377d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *methodPrototype =
378a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        ParseObjCMethodPrototype(MethodImplKind, false);
37925e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
3803536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
3813536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
3820db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis      if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
3830db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
3840db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
3850db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        if (Tok.is(tok::semi))
3860db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis          ConsumeToken();
3870db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis      }
388294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
389294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
39005511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    if (Tok.is(tok::l_paren)) {
39105511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      Diag(Tok, diag::err_expected_minus_or_plus);
392d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ParseObjCMethodDecl(Tok.getLocation(),
393d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          tok::minus,
39490ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                          MethodImplKind, false);
39505511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      continue;
39605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
397e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
398e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
399294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
400e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
401e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
403bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
404e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
405e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
407b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    // Code completion within an Objective-C interface.
408b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    if (Tok.is(tok::code_completion)) {
40923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
410849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                            CurParsedObjCImpl? Sema::PCC_ObjCImplementation
411f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             : Sema::PCC_ObjCInterface);
4127d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
413b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    }
414b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor
415e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
416e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
4171fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
4181fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
4191fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
4201fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
4211fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
4220b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      ParsedAttributes attrs(AttrFactory);
4237f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
424e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
425e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
427e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
428e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
429c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    if (Tok.is(tok::code_completion)) {
430a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCAtDirective(getCurScope());
4317d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
432c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    }
433c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
434a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
436a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
437782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setBegin(AtLoc);
438782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setEnd(Tok.getLocation());
439e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
440c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor    } else if (DirectiveKind == tok::objc_not_keyword) {
441c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      Diag(Tok, diag::err_objc_unknown_at);
442c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      SkipUntil(tok::semi);
443c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      continue;
444bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
446bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
447bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
448bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
449a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
450a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
451bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
452bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
453bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
454bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
455f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
456bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
457a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
458a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
45946d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
46046d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian    case tok::objc_implementation:
461df81c2c6ce37f4ae3c45dd01093b6274fa0b6692Fariborz Jahanian    case tok::objc_interface:
462d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      Diag(AtLoc, diag::err_objc_missing_end)
463d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen          << FixItHint::CreateInsertion(AtLoc, "@end\n");
464d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      Diag(CDecl->getLocStart(), diag::note_objc_container_start)
465d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen          << (int) Actions.getObjCContainerKind();
46646d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      ConsumeToken();
46746d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      break;
46846d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
469a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
470a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
471a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
472bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
473e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
474bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
475a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
476bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
477a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
479a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
480f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
481b321c0c0ba957d78475e72cebde4028fdaa00f8fChris Lattner        Diag(AtLoc, diag::err_objc_properties_require_objc2);
482f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
483e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
48477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      SourceLocation LParenLoc;
4851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
48677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      if (Tok.is(tok::l_paren)) {
48777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        LParenLoc = Tok.getLocation();
488a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        ParseObjCPropertyAttribute(OCDS);
48977bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      }
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
491a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      ObjCPropertyCallback Callback(*this, allProperties,
49277bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                    OCDS, AtLoc, LParenLoc, MethodImplKind);
493bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
494e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
4950b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      DeclSpec DS(AttrFactory);
496bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4987da19ea50d4161fcda40d135735bcf450cabeb50John McCall      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
499a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
500f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
501294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
502bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
503bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
504bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
505c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
506a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.CodeCompleteObjCAtDirective(getCurScope());
5077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return cutOffParsing();
508d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
509bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
510d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  } else {
511d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(Tok, diag::err_objc_missing_end)
512d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
513d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(CDecl->getLocStart(), diag::note_objc_container_start)
514d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << (int) Actions.getObjCContainerKind();
515d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    AtEnd.setBegin(Tok.getLocation());
516d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    AtEnd.setEnd(Tok.getLocation());
517d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  }
5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
519a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
520bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
521a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnAtEnd(getCurScope(), AtEnd,
5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
523beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
524beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
525294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
526294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
527d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
528d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
529d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
530d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
531d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
532d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
533d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
534d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
535d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
536d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
537d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
538d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
539d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
540d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
541d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
542f85e193739c953358c865005855253af4f68a497John McCall///     atomic
543f85e193739c953358c865005855253af4f68a497John McCall///     strong
544f85e193739c953358c865005855253af4f68a497John McCall///     weak
545f85e193739c953358c865005855253af4f68a497John McCall///     unsafe_unretained
546d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
547a28948f34817476d02412fa204cae038e228c827Fariborz Jahanianvoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
5488ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
5494a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
5504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
5511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
552cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
553ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
55423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
5557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
556ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
557d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
5581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
559f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
560f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
5614a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
562f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
563f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
565156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56792e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
568e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
56992e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
570e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
571f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("unsafe_unretained"))
572f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
57392e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
574e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
57592e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
576e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
577f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("strong"))
578f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
57992e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
580e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
58192e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
582e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
58345937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian    else if (II->isStr("atomic"))
58445937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
585f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("weak"))
586f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
58792e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
58842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      bool IsSetter = II->getNameStart()[0] == 's';
58942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
590e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
59142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
59242499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        diag::err_objc_expected_equal_for_getter;
59342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
59442499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
595dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5974ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      if (Tok.is(tok::code_completion)) {
59842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        if (IsSetter)
599a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertySetter(getCurScope());
6004ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        else
601a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertyGetter(getCurScope());
6027d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
6034ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      }
6044ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
60542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
60642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      SourceLocation SelLoc;
60742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
60842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
60942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (!SelIdent) {
61042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
61142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson          << IsSetter;
6128ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
6138ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
6148ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
61642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (IsSetter) {
6178ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
61842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setSetterName(SelIdent);
6191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
620e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian        if (ExpectAndConsume(tok::colon,
621e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian                             diag::err_expected_colon_after_setter_name, "",
622156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
6238ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
6248ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
6258ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
62642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setGetterName(SelIdent);
6278ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
628e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
629a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
630cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
631cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
632cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
6331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
634156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
635156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
637156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
638d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
6391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
641d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
642d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
6433536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
6441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
6453536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
646294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
647294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
648294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
649294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6504985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
6514985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
6524985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
653a28948f34817476d02412fa204cae038e228c827Fariborz JahanianDecl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
65490ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                       bool MethodDefinition) {
655df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
656294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
658bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
659a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
66090ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                    MethodDefinition);
6613536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
6622bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
663f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
664294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
665294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
666294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
667294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
668294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
669294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
670294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
671294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
672294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
673294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6742fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
675be74740cc246ce08d42804a684385a42eb814edbFariborz Jahanian
676ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
677ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
678ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
679afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampamp:
680afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampequal:
681afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::amp:
682afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipe:
683afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::tilde:
684afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaim:
685afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaimequal:
686afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipepipe:
687afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipeequal:
688afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caret:
689afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caretequal: {
6903846ca29a8cc1d376a4b695194c29952dbbfb544Fariborz Jahanian    std::string ThisTok(PP.getSpelling(Tok));
691afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    if (isalpha(ThisTok[0])) {
692afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
693afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      Tok.setKind(tok::identifier);
694afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      SelectorLoc = ConsumeToken();
695afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      return II;
696afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    }
697afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    return 0;
698afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  }
699afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian
700ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
701ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
702ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
7039298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
704ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
705ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
706ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
707ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
708ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
709ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
710ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
711ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
712ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
713ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
714ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
715ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
716ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
717ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
718ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
719ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
720ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
721ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
722ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
723ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
724ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
725ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
726ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
727ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
728ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
729ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
730ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
731ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
732ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
733ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
734ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
735ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
736ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
737ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
738ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
739ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
740ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
741ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
742ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
743ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
744ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
745ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
746ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
747ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
748ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
749ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
750ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
751ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
752ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
753ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
754ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
755ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
756ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
757ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
758ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
759ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
760ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
761ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
762ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
763ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
764ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
765ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
766ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
767ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
768ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
769ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
7704b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
771ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
772d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
773294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
774294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
7750196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
7760196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
777335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
7783ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
7793ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
7803ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
7811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
7825ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
7830196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
7840196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
785a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
786e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
787e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
788294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
789294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
790294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
791294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
792294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
793b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregorvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
794cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                        Declarator::TheContext Context) {
795cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert(Context == Declarator::ObjCParameterContext ||
796cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall         Context == Declarator::ObjCResultContext);
797cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
798e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
799d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    if (Tok.is(tok::code_completion)) {
800b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor      Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
801cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                          Context == Declarator::ObjCParameterContext);
8027d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
803d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    }
804d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor
805cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
806e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
808e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
809e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
810a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
811e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
8121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
813a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
814e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
815b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      default: llvm_unreachable("Unknown decl qualifier");
816a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
817a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
818a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
819a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
820a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
821a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
822e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
823a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
824e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
825e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
826e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
827e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
829e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
830e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
831e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
832e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
833e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
834cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// Take all the decl attributes out of the given list and add
835cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// them to the given attribute set.
836cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCallstatic void takeDeclAttributes(ParsedAttributes &attrs,
837cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                               AttributeList *list) {
838cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  while (list) {
839cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    AttributeList *cur = list;
840cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    list = cur->getNext();
841cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
842cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    if (!cur->isUsedAsTypeAttr()) {
843cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // Clear out the next pointer.  We're really completely
844cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // destroying the internal invariants of the declarator here,
845cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // but it doesn't matter because we're done with it.
846cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      cur->setNext(0);
847cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      attrs.add(cur);
848cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    }
849cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  }
850cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall}
851cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
852cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// takeDeclAttributes - Take all the decl attributes from the given
853cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// declarator and add them to the given list.
854cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCallstatic void takeDeclAttributes(ParsedAttributes &attrs,
855cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                               Declarator &D) {
856cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // First, take ownership of all attributes.
857cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  attrs.getPool().takeAllFrom(D.getAttributePool());
858cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
859cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
860cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // Now actually move the attributes over.
861cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
862cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  takeDeclAttributes(attrs, D.getAttributes());
863cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
864cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    takeDeclAttributes(attrs,
865cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                  const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
866cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall}
867cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
868e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
869e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
870e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
871e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
872b77cab97f17f946744c920629ca17271308d8ebfDouglas GregorParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
873cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                     Declarator::TheContext context,
874cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                     ParsedAttributes *paramAttrs) {
875cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert(context == Declarator::ObjCParameterContext ||
876cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall         context == Declarator::ObjCResultContext);
877cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
878cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
879df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8814a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
8824a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
8834a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
884e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
8859735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  ObjCDeclContextSwitch ObjCDC(*this);
8869735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian
88719d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
888cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  ParseObjCTypeQualifierList(DS, context);
8894fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
890b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType Ty;
891809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
892cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // Parse an abstract declarator.
893cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    DeclSpec declSpec(AttrFactory);
894cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    declSpec.setObjCQualifiers(&DS);
895cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ParseSpecifierQualifierList(declSpec);
896cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    Declarator declarator(declSpec, context);
897cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ParseDeclarator(declarator);
898cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
899cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // If that's not invalid, extract a type.
900cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    if (!declarator.isInvalidType()) {
901cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
902cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      if (!type.isInvalid())
903cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall        Ty = type.get();
904cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
905cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // If we're parsing a parameter, steal all the decl attributes
906cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // and add them to the decl spec.
907cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      if (context == Declarator::ObjCParameterContext)
908cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall        takeDeclAttributes(*paramAttrs, declarator);
909cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    }
910cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  } else if (context == Declarator::ObjCResultContext &&
911cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall             Tok.is(tok::identifier)) {
912e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (!Ident_instancetype)
913e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ident_instancetype = PP.getIdentifierInfo("instancetype");
914e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
915e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (Tok.getIdentifierInfo() == Ident_instancetype) {
916e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
917e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      ConsumeToken();
918e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    }
919809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
920e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
9214a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
9224a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
9234a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
924e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
9251ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
9264a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
9274a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
9284a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
9294a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
9304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
931294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
932f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
933294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
934294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
935294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
936294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
9374985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
938294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
9394985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
940294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
941294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
943294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
944294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
945294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
9467ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
9477ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
9487ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
9497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
950294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9514985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
9524985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
953294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9544985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
9554985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
956294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9574985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
958294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
959294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9607ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
9617ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
9627ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
963d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
9642ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor                                  tok::TokenKind mType,
96590ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  tok::ObjCKeywordKind MethodImplKind,
96690ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  bool MethodDefinition) {
96754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
96854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
969e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
97023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
971a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       /*ReturnType=*/ ParsedType());
9727d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
9737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
974e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
975e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
976e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
977b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType ReturnType;
978a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
979df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
980cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9829e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // If attributes exist before the method, parse them.
9830b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributes methodAttrs(AttrFactory);
9847f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (getLang().ObjC2)
9850b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
9869e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek
987e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
98823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
989a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       ReturnType);
9907d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
9917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
992e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
993e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
9949e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // Now parse the selector.
995bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
9962fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
997e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
99884c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
99984c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
10001ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
10011ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
1002e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
1003e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
1004d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1005e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10075f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
1008df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
1009ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
10107f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (getLang().ObjC2)
10110b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(methodAttrs);
10121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1013ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
1014d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Result
10157f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian         = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1016a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                          mType, DSRet, ReturnType,
1017926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                          selLoc, Sel, 0,
10184f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          CParamInfo.data(), CParamInfo.size(),
10190b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          methodAttrs.getList(), MethodImplKind,
10200b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          false, MethodDefinition);
102154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
102254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
1023ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
1024f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
10255f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
102611d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> KeyLocs;
10275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
10287f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian  ParseScope PrototypeScope(this,
10297f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian                            Scope::FunctionPrototypeScope|Scope::DeclScope);
10300b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
10310b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  AttributePool allParamAttrs(AttrFactory);
10327f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1033ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
10340b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes paramAttrs(AttrFactory);
1035f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Sema::ObjCArgInfo ArgInfo;
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1037ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
1038df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
1039ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
1040ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
1041ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
1042ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1044b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ArgInfo.Type = ParsedType();
1045e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
1046cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1047cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                       Declarator::ObjCParameterContext,
1048cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                       &paramAttrs);
1049e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
1050ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
1051cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // Regardless, collect all the attributes we've parsed so far.
1052e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
10537f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (getLang().ObjC2) {
10540b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(paramAttrs);
10550b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      ArgInfo.ArgAttrs = paramAttrs.getList();
10567f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
10577ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
105840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    // Code completion for the next piece of the selector.
105940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    if (Tok.is(tok::code_completion)) {
106040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.push_back(SelIdent);
106140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
106240ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 mType == tok::minus,
106340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/true,
106440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 ReturnType,
106540ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.data(),
106640ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.size());
10677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
10687d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
106940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    }
107040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor
1071df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1072ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
1073ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
10744985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
10751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1076e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
1077e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
1078ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
10791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1080e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
1081e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
108211d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis    KeyLocs.push_back(selLoc);
1083e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
10840b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    // Make sure the attributes persist.
10850b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    allParamAttrs.takeAllFrom(paramAttrs.getPool());
10860b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
10871f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    // Code completion for the next piece of the selector.
10881f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    if (Tok.is(tok::code_completion)) {
10891f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
10901f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 mType == tok::minus,
109140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/false,
10921f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 ReturnType,
10931f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.data(),
10941f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.size());
10957d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
10967d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
10971f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    }
10981f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor
1099ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
110011d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis    SelIdent = ParseObjCSelectorPiece(selLoc);
1101df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
1102ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
1103ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
1104ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
11051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1106335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
11071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1108ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
1109df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
1110ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
1111df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
1112335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
11134985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
1114ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
11154985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
11160b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
1117ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
1119ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
1120ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
11214f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1122d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
11234f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
11244f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    ParmDecl.getIdentifierLoc(),
11254f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    Param,
11264f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                   0));
11274f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian
11284985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11309c4bb2c08989265411925a04252fd4f93c26e3b1Cameron Esfahani  // FIXME: Add support for optional parameter list...
1131e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
11327f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (getLang().ObjC2)
11330b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
11347f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1135a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  if (KeyIdents.size() == 0)
1136d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
11377f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1138ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1139ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
1140d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Result
11417f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian       = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1142a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                        mType, DSRet, ReturnType,
114311d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis                                        KeyLocs, Sel, &ArgInfos[0],
11444f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        CParamInfo.data(), CParamInfo.size(),
11450b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                        methodAttrs.getList(),
114690ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                        MethodImplKind, isVariadic, MethodDefinition);
11477f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
114854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
114954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
1150294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
1151294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
1152dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
1153dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
1154dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
11557caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
11565f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
11575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                            SmallVectorImpl<SourceLocation> &ProtocolLocs,
115871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
115971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
1160e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
11611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierLocPair, 8> ProtocolIdents;
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1166e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
116755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
116855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
116955385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
11707d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
11717d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return true;
117255385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
117355385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
1174e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
1175e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
1176e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
1177e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
1178e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
1179e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1180e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
118171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
1182e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1184e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
1185e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
1186e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
1187e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
11881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1189e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
1190e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
1191e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
1192e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
1193e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1195e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
11961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1197e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
1198e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
1199e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
1200e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
1201e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
1202e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
1203e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
12049bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// \brief Parse the Objective-C protocol qualifiers that follow a typename
12059bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// in a decl-specifier-seq, starting at the '<'.
120646f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregorbool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
12079bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
12089bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
12099bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  SourceLocation LAngleLoc, EndProtoLoc;
12105f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolDecl;
12115f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
121246f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
121346f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor                                            LAngleLoc, EndProtoLoc);
12149bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
12159bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor                           ProtocolLocs.data(), LAngleLoc);
12169bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  if (EndProtoLoc.isValid())
12179bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(EndProtoLoc);
121846f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  return Result;
12199bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor}
12209bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
12219bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
1222dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
1223dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
1224dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1225dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
1226dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
1227dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
1228dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
1229dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
1230dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
1231dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
1232dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1233dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
1234dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
1235dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
1236dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
1237ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
1238dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1239dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
12401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
1241dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1242d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
124383c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                             tok::ObjCKeywordKind visibility,
124460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
1245df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
12465f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> AllIvarDecls;
1247a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
12481a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
12493a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis  ObjCDeclContextSwitch ObjCDC(*this);
125072de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
12514a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_brace);
12524a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1254ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
1255df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1256ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1258ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
1259df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1260f13ca06e57ac094ed05ea08c26a499af1ba0ce88Douglas Gregor      Diag(Tok, diag::ext_extra_ivar_semi)
1261849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1262ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1263ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
1264ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
12651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1266ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
1267df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
1268ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
1269c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1270c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
127123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(getCurScope());
12727d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
1273c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
1274c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1275861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
1276ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
1277ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
1278ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
1279ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
1280861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
1281ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
12821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
1283ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
1284ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1285ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
1286ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1287ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1289c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
129023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
1291f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                       Sema::PCC_ObjCInstanceVariableList);
12927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
1293c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1294c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1295bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1296bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1297d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *IDecl;
1298bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
12995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<Decl *> &AllIvarDecls;
1300bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1301d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
13025f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &AllIvarDecls) :
1303bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1304bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1305bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1306d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *invoke(FieldDeclarator &FD) {
1307a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        P.Actions.ActOnObjCContainerStartDefinition(IDecl);
1308bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1309d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        Decl *Field
131023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          = P.Actions.ActOnIvar(P.getCurScope(),
1311bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1312a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                FD.D, FD.BitfieldSize, visibility);
131310af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian        P.Actions.ActOnObjCContainerFinishDefinition();
13140bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian        if (Field)
13150bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian          AllIvarDecls.push_back(Field);
1316bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
1317bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1318bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1319d097be8f81fbf4ed96ac10bae18562dd8202666bFariborz Jahanian
1320e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
13210b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
1322bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1324df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1325ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1326ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1327ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1328ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1329ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1330ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1331ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
13324a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
13334a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
1334a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
13354a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
133610af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian  Actions.ActOnObjCContainerFinishDefinition();
13378749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
13388749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
133923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
134077b6de07be9186063c12928d2e9785a5d4eecbf6David Blaikie                      AllIvarDecls,
13414a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                      T.getOpenLocation(), T.getCloseLocation(), 0);
1342ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
13435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1344dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1345dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1346dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1347dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1348dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1349dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
13521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1353dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1354dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1355dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1356dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1357dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1358dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
13593536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1360dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1361bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas GregorParser::DeclGroupPtrTy
1362bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas GregorParser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1363bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor                                       ParsedAttributes &attrs) {
1364861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
13657ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
13667ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1368083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
136923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(getCurScope());
13707d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1371bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
1372083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1373083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1374df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
13757ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1376bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
13777ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
13787ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
13797ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
13807ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
13811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1382df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
13837caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
13847ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
13851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
13867f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
13877ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
13881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
138990ec96f3026480fa41057b05d58f338aed585f62Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
139090ec96f3026480fa41057b05d58f338aed585f62Erik Verbruggen
1391df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
13925f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<IdentifierLocPair, 8> ProtocolRefs;
13937caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
13947caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
13957ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
13967ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
13977ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1398df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
13997ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
14007ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1401bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor        return DeclGroupPtrTy();
14027ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
14037caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
14047caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
14057ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
14061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1407df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
14087ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
14097ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
14107ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
14117ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1412bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor      return DeclGroupPtrTy();
14131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1414e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
14151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1416bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
14177f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
14187caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
14191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14207ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
142171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
14227caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
14235f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
14245f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
14257caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
142671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
142771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1428bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
14291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1430d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ProtoType =
1431e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1432beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1433beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
143418df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
14357f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                        EndProtoLoc, attrs.getList());
1436a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
14372f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
1438bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor  return Actions.ConvertDeclToDeclGroup(ProtoType);
1439dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1440dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1441dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1442dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1443dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1444dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1445dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1446dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1447dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1448dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1449dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1450dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1451849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::DeclGroupPtrTy
1452849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
1453ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1454ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1455d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
1456ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14583b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
14593b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
146023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(getCurScope());
14617d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1462849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return DeclGroupPtrTy();
14633b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
14643b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1465df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1466ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1467849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return DeclGroupPtrTy();
1468ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1469ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1470ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1471ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1472849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Decl *ObjCImpDecl = 0;
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1475ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1476dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeParen();
1477ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1478ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
148033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
148123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
14827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1483849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
148433ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
148533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1486df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1487ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1488ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1489ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1490ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1491849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
14921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1493df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1494ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1495ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1496849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
1497ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1498ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1499849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
1500d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen                                    AtLoc, nameId, nameLoc, categoryId,
15018f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1502a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1503849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  } else {
1504849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    // We have a class implementation
1505849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    SourceLocation superClassLoc;
1506849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    IdentifierInfo *superClassId = 0;
1507849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (Tok.is(tok::colon)) {
1508849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      // We have a super class
1509849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ConsumeToken();
1510849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      if (Tok.isNot(tok::identifier)) {
1511849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        Diag(Tok, diag::err_expected_ident); // missing super class name.
1512849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        return DeclGroupPtrTy();
1513849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      }
1514849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      superClassId = Tok.getIdentifierInfo();
1515849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      superClassLoc = ConsumeToken(); // Consume super class name
1516ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1517849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImpDecl = Actions.ActOnStartClassImplementation(
1518849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                                    AtLoc, nameId, nameLoc,
1519849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                                    superClassId, superClassLoc);
1520849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1521849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (Tok.is(tok::l_brace)) // we have ivars
1522849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
1523ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1524849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(ObjCImpDecl);
15251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1526849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  SmallVector<Decl *, 8> DeclsInGroup;
1527a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1528849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  {
1529849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1530849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1531849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ParsedAttributesWithRange attrs(AttrFactory);
1532849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      MaybeParseCXX0XAttributes(attrs);
1533849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      MaybeParseMicrosoftAttributes(attrs);
1534849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1535849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        DeclGroupRef DG = DGP.get();
1536849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        DeclsInGroup.append(DG.begin(), DG.end());
1537849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      }
1538849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    }
1539849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1540849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1541644af7b50bd0541468b45c197f5b637e934d48a0Argyrios Kyrtzidis  return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
15425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
154360fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1544140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::DeclGroupPtrTy
1545140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1546ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1547ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1548ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1549849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (CurParsedObjCImpl)
1550849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    CurParsedObjCImpl->finish(atEnd);
15518697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  else
1552782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1553d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(atEnd.getBegin(), diag::err_expected_objc_container);
1554849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  return DeclGroupPtrTy();
15555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1556e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
1557849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1558849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (!Finished) {
1559849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    finish(P.Tok.getLocation());
1560849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (P.Tok.is(tok::eof)) {
1561849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      P.Diag(P.Tok, diag::err_objc_missing_end)
1562849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis          << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1563849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1564849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis          << Sema::OCK_Implementation;
1565849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    }
1566849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1567849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.CurParsedObjCImpl = 0;
1568849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(LateParsedObjCMethods.empty());
1569849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis}
1570d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
1571849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidisvoid Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1572849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(!Finished);
1573849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1574849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
1575849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1576d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
1577849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
157863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1579849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  /// \brief Clear and free the cached objc methods.
15802fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  for (LateParsedObjCMethodContainer::iterator
15812fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis         I = LateParsedObjCMethods.begin(),
15822fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis         E = LateParsedObjCMethods.end(); I != E; ++I)
15832fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis    delete *I;
15842fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  LateParsedObjCMethods.clear();
1585849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1586849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Finished = true;
15872fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis}
15882fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis
1589e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1590e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1591e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1592d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1593e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1594e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1595e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1596df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1597e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1598d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1599e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1600243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1601243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1602df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1603e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1604d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1605e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1606243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1607243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1608e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1609e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor                   "@compatibility_alias");
1610b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1611b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
16125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1614ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1615ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1616ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1617ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1618ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1619ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1620ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1621ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1622ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1623ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1624ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1625d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1626ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1627ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1628dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume synthesize
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1630b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1631322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
1632a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
16337d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16347d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1635322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1636322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1637b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1638b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1639b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1640d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1641b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1642b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1643f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1644f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1645f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1646a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor    SourceLocation propertyIvarLoc;
1647df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1648ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1649ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1650322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1651322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
1652a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
16537d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
16547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return 0;
1655322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1656322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1657df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1658ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1659ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1660ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1661f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1662a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor      propertyIvarLoc = ConsumeToken(); // consume ivar-name
1663ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1664a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
1665a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, propertyIvar, propertyIvarLoc);
1666df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1667ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1668ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1669ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1670e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
1671d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1672ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1673ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1674ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1675ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1676ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1677ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1678ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1679ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1680ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1681d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1682ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1683ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1684dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume dynamic
1685424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1686424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
1687a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
16887d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16897d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1690424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1691424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1692424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1693424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1694424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1695d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1696424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1697424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1698c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1699c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1700a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
1701a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, 0, SourceLocation());
1702c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1703df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1704ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1705ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1706ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1707e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
1708d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1709ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
17101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1711397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1712397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1713397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
171460d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
171560d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res;
1716397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1717df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
171839f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
17190e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1720397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
172143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1722397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1723397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
172402418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  // consume ';'
172502418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
17269ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
1727397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1728397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1729c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
173078a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1731c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
173260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
173343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1734fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1735fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
17361ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
173743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1738fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
173907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
174007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // The operand is surrounded with parentheses.
1741fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
174207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ExprResult operand(ParseExpression());
174307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
174407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (Tok.is(tok::r_paren)) {
174507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    ConsumeParen();  // ')'
174607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  } else {
174707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
174807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_rparen);
174907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
175007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    // Skip forward until we see a left brace, but don't consume it.
175107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    SkipUntil(tok::l_brace, true, true);
1752fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
175307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
175407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Require a compound statement.
175578a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
175607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
175707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_lbrace);
175843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
175978a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
17603ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
176107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Check the @synchronized operand now.
176207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (!operand.isInvalid())
176307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
176407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
176507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Parse the compound statement within a new scope.
176607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ParseScope bodyScope(this, Scope::DeclScope);
176707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  StmtResult body(ParseCompoundStatementBody());
176807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  bodyScope.Exit();
176907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
177007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // If there was a semantic or parse error earlier with the
177107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // operand, fail now.
177207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (operand.isInvalid())
177307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    return StmtError();
177407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
177507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (body.isInvalid())
177607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    body = Actions.ActOnNullStmt(Tok.getLocation());
17770e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
177807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
1779c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1780c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1781397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1782397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1783397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1784397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1785397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1786397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1787397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1788397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1789397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1790397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1791397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
179260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1793397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
179443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1795397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1796df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
17971ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
179843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1799397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
18008f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  StmtVector CatchStmts(Actions);
180160d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult FinallyStmt;
18028935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
180360d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult TryBody(ParseCompoundStatementBody());
18048935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
18050e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1806bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1807a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1808df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
18096b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
18106b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
18116b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
18126b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
18136b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
18146b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
18156b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
18160e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1817161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1818cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1819d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *FirstPart = 0;
18203b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1821df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1822397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1823e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1824df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
18250b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall          DeclSpec DS(AttrFactory);
1826397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
182717b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis          Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
18287ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
18297ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
18304e6c0d19b7c072758922cf80525a81aeefc6e64bDouglas Gregor          // Inform the actions module about the declarator, so it
18317ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
183223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
183364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1834397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
18351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
183693a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
18371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
183893a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
183993a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
184093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
184193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
184293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
184360d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult CatchBody(true);
1844c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1845c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1846c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1847c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
18480e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
18493b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
18508f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
185160d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
18528f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              RParenLoc,
18538f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              FirstPart,
18549ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                              CatchBody.take());
18558f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        if (!Catch.isInvalid())
18568f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor          CatchStmts.push_back(Catch.release());
18578f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
185864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
18591ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
18601ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
186143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1862397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1863397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
18646b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
18656b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
186664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
18678935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
18680e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
186960d7b3a319d84d688752be3870615ac0f111fb16John McCall      StmtResult FinallyBody(true);
1870c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1871c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1872c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1873c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
18740e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1875161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
18760e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
18779ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                   FinallyBody.take());
1878397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1879397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1880397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1881397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1882bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1883397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
188443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1885bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
18868f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
18879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
18888f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                    move_arg(CatchStmts),
18899ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                    FinallyStmt.take());
1890397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1891ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1892f85e193739c953358c865005855253af4f68a497John McCall/// objc-autoreleasepool-statement:
1893f85e193739c953358c865005855253af4f68a497John McCall///   @autoreleasepool compound-statement
1894f85e193739c953358c865005855253af4f68a497John McCall///
1895f85e193739c953358c865005855253af4f68a497John McCallStmtResult
1896f85e193739c953358c865005855253af4f68a497John McCallParser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1897f85e193739c953358c865005855253af4f68a497John McCall  ConsumeToken(); // consume autoreleasepool
1898f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isNot(tok::l_brace)) {
1899f85e193739c953358c865005855253af4f68a497John McCall    Diag(Tok, diag::err_expected_lbrace);
1900f85e193739c953358c865005855253af4f68a497John McCall    return StmtError();
1901f85e193739c953358c865005855253af4f68a497John McCall  }
1902f85e193739c953358c865005855253af4f68a497John McCall  // Enter a scope to hold everything within the compound stmt.  Compound
1903f85e193739c953358c865005855253af4f68a497John McCall  // statements can always hold declarations.
1904f85e193739c953358c865005855253af4f68a497John McCall  ParseScope BodyScope(this, Scope::DeclScope);
1905f85e193739c953358c865005855253af4f68a497John McCall
1906f85e193739c953358c865005855253af4f68a497John McCall  StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1907f85e193739c953358c865005855253af4f68a497John McCall
1908f85e193739c953358c865005855253af4f68a497John McCall  BodyScope.Exit();
1909f85e193739c953358c865005855253af4f68a497John McCall  if (AutoreleasePoolBody.isInvalid())
1910f85e193739c953358c865005855253af4f68a497John McCall    AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1911f85e193739c953358c865005855253af4f68a497John McCall  return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1912f85e193739c953358c865005855253af4f68a497John McCall                                                AutoreleasePoolBody.take());
1913f85e193739c953358c865005855253af4f68a497John McCall}
1914f85e193739c953358c865005855253af4f68a497John McCall
19153536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1916ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1917d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDefinition() {
1918a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodPrototype();
19191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1920f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1921f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      "parsing Objective-C method");
19221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1923ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1924209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1925849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (CurParsedObjCImpl) {
1926496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
1927849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1928496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1929ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1930209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1931ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1932409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1933df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1934da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
19351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1936409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1937409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
19381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1939409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1940409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1941d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1942ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1943849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1944849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (!MDecl) {
1945849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ConsumeBrace();
1946849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1947849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return 0;
1948849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1949849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1950140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Allow the rest of sema to find private method decl implementations.
1951849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Actions.AddAnyMethodToGlobalPool(MDecl);
1952849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1953849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (CurParsedObjCImpl) {
1954849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    // Consume the tokens and store them for later parsing.
1955849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    LexedMethod* LM = new LexedMethod(this, MDecl);
1956849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
1957849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    CachedTokens &Toks = LM->Toks;
1958849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    // Begin by storing the '{' token.
1959849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    Toks.push_back(Tok);
1960849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ConsumeBrace();
1961849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    // Consume everything up to (and including) the matching right brace.
1962849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
1963849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1964849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  } else {
1965849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ConsumeBrace();
1966849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
1967849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1968849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
196971c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
19705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
19715508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
197260d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
19739a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
197423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtStatement(getCurScope());
19757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
19769a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
19775d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
19785d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19795d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
19806b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
19815d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19825d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
198364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
19845d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
19855d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
198664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
1987f85e193739c953358c865005855253af4f68a497John McCall
1988f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1989f85e193739c953358c865005855253af4f68a497John McCall    return ParseObjCAutoreleasePoolStmt(AtLoc);
19905d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
199160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
19920e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
199364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
199464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
199564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
199664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
199743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
199864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
19995d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
200064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
20019ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
20029ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
200364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
200464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
200560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
20065508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
20079a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
200823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtExpression(getCurScope());
20097d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
20109a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
20119a0c85e640a08174569a303db22981612f05d385Douglas Gregor
2012b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
2013b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
20141d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
2015b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
20164fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
20171d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
20182f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
20194fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
20204fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
20211d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
20224fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
20231d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
20244fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
20251d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
20264fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
20271d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
20284fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
20295508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
20305508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
20315508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
20326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brirg Parse the receiver of an Objective-C++ message send.
20336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// This routine parses the receiver of a message send in
20356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// Objective-C++ either as a type or as an expression. Note that this
20366aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// routine must not be called to parse a send to 'super', since it
20376aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// has no way to return such a result.
20386aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20396aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param IsExpr Whether the receiver was parsed as an expression.
20406aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20416aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param TypeOrExpr If the receiver was parsed as an expression (\c
20426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// IsExpr is true), the parsed expression. If the receiver was parsed
20436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// as a type (\c IsExpr is false), the parsed type.
20446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \returns True if an error occurred during parsing or semantic
20466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// analysis, in which case the arguments do not have valid
20476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// values. Otherwise, returns false for a successful parse.
20486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
20496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///   objc-receiver: [C++]
20506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     'super' [not parsed here]
20516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     expression
20526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     simple-type-specifier
20536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     typename-specifier
20546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
20550fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
20560fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
20576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
20586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
20596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TryAnnotateTypeOrScopeToken();
20606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (!isCXXSimpleTypeSpecifier()) {
20626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   objc-receiver:
20636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     expression
206460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseExpression();
20656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
20666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
20676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
20696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
20706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
20716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
20726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // objc-receiver:
20746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   typename-specifier
20756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   simple-type-specifier
20766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   expression (that starts with one of the above)
20770b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
20786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  ParseCXXSimpleTypeSpecifier(DS);
20796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20806aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::l_paren)) {
20816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // If we see an opening parentheses at this point, we are
20826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // actually parsing an expression that starts with a
20836aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // function-style cast, e.g.,
20846aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
20856aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   postfix-expression:
20866aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     simple-type-specifier ( expression-list [opt] )
20876aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     typename-specifier ( expression-list [opt] )
20886aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
20896aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the remainder of this case, then the (optional)
20906aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // postfix-expression suffix, followed by the (optional)
20916aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // right-hand side of the binary expression. We have an
20926aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // instance method.
209360d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
20946aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
20959ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParsePostfixExpressionSuffix(Receiver.take());
20966aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
20979ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
20986aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
20996aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
21006aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21016aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
21026aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
21036aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
21046aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
21056aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21066aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // We have a class message. Turn the simple-type-specifier or
21076aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // typename-specifier we parsed into a type and parse the
21086aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // remainder of the class message.
21096aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
211023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
21116aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Type.isInvalid())
21126aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
21136aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21146aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  IsExpr = false;
2115b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  TypeOrExpr = Type.get().getAsOpaquePtr();
21166aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
21176aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
21186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21191b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// \brief Determine whether the parser is currently referring to a an
21201b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// Objective-C message send, using a simplified heuristic to avoid overhead.
21211b730e847ded503f2e615154035c083c4f94a067Douglas Gregor///
21221b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// This routine will only return true for a subset of valid message-send
21231b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// expressions.
21241b730e847ded503f2e615154035c083c4f94a067Douglas Gregorbool Parser::isSimpleObjCMessageExpression() {
2125c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
21261b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         "Incorrect start for isSimpleObjCMessageExpression");
21271b730e847ded503f2e615154035c083c4f94a067Douglas Gregor  return GetLookAheadToken(1).is(tok::identifier) &&
21281b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         GetLookAheadToken(2).is(tok::identifier);
21291b730e847ded503f2e615154035c083c4f94a067Douglas Gregor}
21301b730e847ded503f2e615154035c083c4f94a067Douglas Gregor
21319497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregorbool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
21329497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
21339497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      InMessageExpression)
21349497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
21359497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21369497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21379497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  ParsedType Type;
21389497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21399497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (Tok.is(tok::annot_typename))
21409497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = getTypeAnnotation(Tok);
21419497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else if (Tok.is(tok::identifier))
21429497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
21439497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor                               getCurScope());
21449497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else
21459497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
21469497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21479497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
21489497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    const Token &AfterNext = GetLookAheadToken(2);
21499497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
21509497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      if (Tok.is(tok::identifier))
21519497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor        TryAnnotateTypeOrScopeToken();
21529497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21539497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      return Tok.is(tok::annot_typename);
21549497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    }
21559497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  }
21569497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21579497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  return false;
21589497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor}
21599497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
21601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
21610ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
21620ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
21632725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///   objc-receiver: [C]
2164eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner///     'super'
21650ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
21660ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
21670ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
21686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
216960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCMessageExpression() {
2170699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
2171699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2172699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
21738e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  if (Tok.is(tok::code_completion)) {
217423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMessageReceiver(getCurScope());
21757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
21768e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    return ExprError();
21778e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  }
21788e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor
21790fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
21800fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
21816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (getLang().CPlusPlus) {
21826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // We completely separate the C and C++ cases because C++ requires
21836aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // more complicated (read: slower) parsing.
21846aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21856aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Handle send to super.
21866aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: This doesn't benefit from the same typo-correction we
21876aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // get in Objective-C.
21886aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
218923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
2190b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2191b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
21926aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
21936aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the receiver, which is either a type or an expression.
21946aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    bool IsExpr;
2195304b752a450c0fc5968c20ba25446d0bb7c6f68dNick Lewycky    void *TypeOrExpr = NULL;
21966aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
21976aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      SkipUntil(tok::r_square);
21986aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ExprError();
21996aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    }
22006aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22016aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (IsExpr)
2202b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2203b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(),
22049ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            static_cast<Expr*>(TypeOrExpr));
22056aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22066aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2207b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                              ParsedType::getFromOpaquePtr(TypeOrExpr),
2208b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          0);
2209c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  }
2210c59cb38810c63a806270385f79ea84e0203754eaChris Lattner
2211c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  if (Tok.is(tok::identifier)) {
22121dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    IdentifierInfo *Name = Tok.getIdentifierInfo();
22131dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    SourceLocation NameLoc = Tok.getLocation();
2214b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType ReceiverType;
221523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
22161dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor                                       Name == Ident_super,
22171569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       NextToken().is(tok::period),
22181569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       ReceiverType)) {
2219f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCSuperMessage:
2220b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2221b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
22222725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
2223f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCClassMessage:
22241569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      if (!ReceiverType) {
22252725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        SkipUntil(tok::r_square);
22262725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        return ExprError();
22272725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      }
22282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
22291569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      ConsumeToken(); // the type name
22301569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
22311569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
22329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            ReceiverType, 0);
22331dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor
2234f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCInstanceMessage:
22352725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      // Fall through to parse an expression.
22361dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor      break;
2237d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
2238699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
2239eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
2240eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  // Otherwise, an arbitrary expression can be the receiver of a send.
224160d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpression());
22420e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
22435c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
22441d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
2245699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
22461d922960e083906a586609ac6978678147250177Sebastian Redl
2247b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2248b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType(), Res.take());
2249699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
22501d922960e083906a586609ac6978678147250177Sebastian Redl
22512725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Parse the remainder of an Objective-C message following the
22522725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// '[' objc-receiver.
22532725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22542725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine handles sends to super, class messages (sent to a
22552725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class name), and instance messages (sent to an object), and the
22562725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// target is represented by \p SuperLoc, \p ReceiverType, or \p
22572725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// ReceiverExpr, respectively. Only one of these parameters may have
22582725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// a valid value.
22592725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22602725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening '['.
22612725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22622725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc If this is a send to 'super', the location of the
22632725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 'super' keyword that indicates a send to the superclass.
22642725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22652725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType If this is a class message, the type of the
22662725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class we are sending a message to.
22672725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
22682725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverExpr If this is an instance message, the expression
22692725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// used to compute the receiver object.
22701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
22710ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
22720ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
22730ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
22740ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22750ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
22760ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
22770ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
22780ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
22800ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
22810ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22820ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
22830ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
22840ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
22850ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
22860ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
22870ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
22881d922960e083906a586609ac6978678147250177Sebastian Redl///
228960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2290699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
22912725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                       SourceLocation SuperLoc,
2292b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                       ParsedType ReceiverType,
22931d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
22940fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
22950fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
2296c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
22972725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    if (SuperLoc.isValid())
229870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
229970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
23002725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    else if (ReceiverType)
230170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
230270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
2303c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
23049ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
230570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                              0, 0, false);
23067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
23077d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return ExprError();
2308c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
2309d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
2310a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
23114b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
23122fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
231368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
23145f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2315951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> KeyLocs;
2316a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
231768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
2318df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
2319a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
2320a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
232168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
2322951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis      KeyLocs.push_back(Loc);
232337387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
2324df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
2325a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
23264fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
23274fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
23284fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
23294fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
23301d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
2331a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
23321d922960e083906a586609ac6978678147250177Sebastian Redl
233368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
23341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
233570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
233670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      if (Tok.is(tok::code_completion)) {
233770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        if (SuperLoc.isValid())
233870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
233970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
234070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
234170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
234270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else if (ReceiverType)
234370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
234470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
234570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
234670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
234770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else
234870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
234970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.data(),
235070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
235170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  /*AtArgumentEpression=*/true);
235270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
23537d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
235470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
235570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      }
235670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
235760d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
23580e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
23594fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
23604fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
23614fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
23624fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
23631d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
236437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
23651d922960e083906a586609ac6978678147250177Sebastian Redl
236637387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
2367effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
23681d922960e083906a586609ac6978678147250177Sebastian Redl
2369d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
2370d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
23712725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        if (SuperLoc.isValid())
237223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
23732725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                               KeyIdents.data(),
237470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
237570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
23762725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        else if (ReceiverType)
237723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2378d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
237970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
238070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
2381d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
23829ae2f076ca5ab1feb3ba95629099ec2319833701John McCall          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2383d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
238470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
238570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                /*AtArgumentEpression=*/false);
23867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
238770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
2388d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
2389d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
239037387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
23912fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
2392df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
2393a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
2394a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
2395a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2396a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
2397df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
239849f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
23991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
240060d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
24010e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
24024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
24034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
24044fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
24054fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
24061d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
240749f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
24080e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
240949f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
2410effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
2411a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2412a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
2413a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
24141d922960e083906a586609ac6978678147250177Sebastian Redl
24154fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
24164fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
24174fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
24184fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
24191d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2420a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
2421809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian
2422df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
2423809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    if (Tok.is(tok::identifier))
2424809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_colon);
2425809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    else
2426809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_rsquare);
24274fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
24284fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
24294fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
24304fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
24311d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2432a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
24331d922960e083906a586609ac6978678147250177Sebastian Redl
2434699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
24351d922960e083906a586609ac6978678147250177Sebastian Redl
243629238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
2437951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  if (nKeys == 0) {
2438ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
2439951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis    KeyLocs.push_back(Loc);
2440951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  }
2441ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
24421d922960e083906a586609ac6978678147250177Sebastian Redl
24432725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
244423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
2445951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                     LBracLoc, KeyLocs, RBracLoc,
2446f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2447f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2448f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
24492725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  else if (ReceiverType)
245023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
2451951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                     LBracLoc, KeyLocs, RBracLoc,
2452f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2453f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2454f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
24559ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
2456951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis                                      LBracLoc, KeyLocs, RBracLoc,
2457f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      MultiExprArg(Actions,
2458f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.take(),
2459f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.size()));
24600ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
24610ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
246260d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
246360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseStringLiteralExpression());
24641d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
24651d922960e083906a586609ac6978678147250177Sebastian Redl
2466b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
2467b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
2468b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
24695f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 4> AtLocs;
2470a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
2471b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
2472effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
24730e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2474b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
2475b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
2476b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
247715faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
247897cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
247997cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
2480b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
248160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Lit(ParseStringLiteralExpression());
24820e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
24831d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
24840e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2485effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
2486b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
24871d922960e083906a586609ac6978678147250177Sebastian Redl
24881d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
24891d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
24905508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
2491f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
2492f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
2493f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
249460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
24951d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
2496861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
24971d922960e083906a586609ac6978678147250177Sebastian Redl
2498f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
24991d922960e083906a586609ac6978678147250177Sebastian Redl
25004fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
25011d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
25021d922960e083906a586609ac6978678147250177Sebastian Redl
25034a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
25044a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
25051d922960e083906a586609ac6978678147250177Sebastian Redl
2506809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
25071d922960e083906a586609ac6978678147250177Sebastian Redl
25084a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
25091d922960e083906a586609ac6978678147250177Sebastian Redl
2510809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2511809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2512809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
25134a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc,
25144a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                 T.getOpenLocation(), Ty.get(),
25154a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                 T.getCloseLocation()));
2516f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
251729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
251829b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
251929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
252060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
25211d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
252229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
25231d922960e083906a586609ac6978678147250177Sebastian Redl
25244fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
25251d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
25261d922960e083906a586609ac6978678147250177Sebastian Redl
25274a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
25284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
25291d922960e083906a586609ac6978678147250177Sebastian Redl
25304fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
25311d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
25321d922960e083906a586609ac6978678147250177Sebastian Redl
2533390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
253429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
25351d922960e083906a586609ac6978678147250177Sebastian Redl
25364a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
253729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
25381d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
25394a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getOpenLocation(),
25404a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getCloseLocation()));
254129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
2542a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
2543a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
2544a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
254560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
2546a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
25471d922960e083906a586609ac6978678147250177Sebastian Redl
25484fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
25491d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
25501d922960e083906a586609ac6978678147250177Sebastian Redl
25515f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2552a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
2553458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
25544a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
25554a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
25564a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2557458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  if (Tok.is(tok::code_completion)) {
2558458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2559458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                     KeyIdents.size());
25607d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
2561458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    return ExprError();
2562458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  }
2563458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
25642fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
25655add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner  if (!SelIdent &&  // missing selector name.
25665add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
25671d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
25681d922960e083906a586609ac6978678147250177Sebastian Redl
2569b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
2570887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
2571887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
2572a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
25735add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
25745add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        ++nColons;
25755add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        KeyIdents.push_back(0);
25765add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      } else if (Tok.isNot(tok::colon))
25771d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
25781d922960e083906a586609ac6978678147250177Sebastian Redl
25795add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      ++nColons;
25803b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      ConsumeToken(); // Eat the ':' or '::'.
2581a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
2582a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2583458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2584458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      if (Tok.is(tok::code_completion)) {
2585458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2586458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                         KeyIdents.size());
25877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2588458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        return ExprError();
2589458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      }
2590458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2591a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
2592a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
25932fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
2594b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
25953b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
2596a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2597a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
2598887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
25994a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
2600887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
26011d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
26024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getOpenLocation(),
26034a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor                                                   T.getCloseLocation()));
260458065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
2605140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2606140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianDecl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2607a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2608a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  // Save the current token position.
2609a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  SourceLocation OrigLoc = Tok.getLocation();
2610a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2611140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2612140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Append the current token at the end of the new token stream so that it
2613140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // doesn't get lost.
2614140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LM.Toks.push_back(Tok);
2615140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2616140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2617140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // MDecl might be null due to error in method prototype, etc.
2618140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  Decl *MDecl = LM.D;
2619140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume the previously pushed token.
2620140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ConsumeAnyToken();
2621140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2622140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2623140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  SourceLocation BraceLoc = Tok.getLocation();
2624140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Enter a scope for the method body.
2625140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ParseScope BodyScope(this,
2626140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian                       Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2627140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2628140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Tell the actions module that we have entered a method definition with the
2629140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // specified Declarator for the method.
2630140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2631140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2632140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  if (PP.isCodeCompletionEnabled()) {
2633140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian      if (trySkippingFunctionBodyForCodeCompletion()) {
2634140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian          BodyScope.Exit();
2635140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian          return Actions.ActOnFinishFunctionBody(MDecl, 0);
2636140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian      }
2637140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  }
2638140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2639140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  StmtResult FnBody(ParseCompoundStatementBody());
2640140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2641140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // If the function body could not be parsed, make a bogus compoundstmt.
2642625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  if (FnBody.isInvalid()) {
2643625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko    Sema::CompoundScopeRAII CompoundScope(Actions);
2644140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2645140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian                                       MultiStmtArg(Actions), false);
2646625bb569df0c34feec0d52c0ec5215f21ef2e054Dmitri Gribenko  }
2647140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2648140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Leave the function body scope.
2649140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  BodyScope.Exit();
2650140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2651a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  MDecl = Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2652a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2653a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  if (Tok.getLocation() != OrigLoc) {
2654a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // Due to parsing error, we either went over the cached tokens or
2655a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // there are still cached tokens left. If it's the latter case skip the
2656a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // leftover tokens.
2657a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // Since this is an uncommon situation that should be avoided, use the
2658a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // expensive isBeforeInTranslationUnit call.
2659a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2660a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis                                                     OrigLoc))
2661a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis      while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2662a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis        ConsumeAnyToken();
2663a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  }
2664a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2665a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  return MDecl;
2666140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian}
2667