ParseObjc.cpp revision 13d05ac08974ccb41f7da7595d769c158f58fbd6
1a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the Objective-C portions of the Parser interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
1519510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Parse/Parser.h"
160fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor#include "RAIIObjectsForParser.h"
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
18f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
1919510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Scope.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24891dca671a80c865c6def7259f170ba785e4faaaChris Lattner/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       external-declaration: [C99 6.9]
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
2791fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-class-declaration
2891fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-alias-declaration
2991fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-protocol-definition
3091fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-method-definition
3191fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  '@' 'end'
3295ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation AtLoc = ConsumeToken(); // the "@"
341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
36a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.CodeCompleteObjCAtDirective(getCurScope());
377d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
387d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
39c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  }
4095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian
4195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  Decl *SingleDecl = 0;
42861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  switch (Tok.getObjCKeywordID()) {
435ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_class:
445ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtClassDeclaration(AtLoc);
4595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
467f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_interface: {
470b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
4895ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
4995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
507f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
517f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_protocol: {
520b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
5395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtProtocolDeclaration(AtLoc, attrs);
5495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
557f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
565ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
5795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtImplementationDeclaration(AtLoc);
5895ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
595ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
60140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    return ParseObjCAtEndDeclaration(AtLoc);
6195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
625ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
6395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
6495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
655ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
6695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCPropertySynthesize(AtLoc);
6795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
685ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
6995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCPropertyDynamic(AtLoc);
7095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
715ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
725ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
735ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
7495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = 0;
7595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
7795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  return Actions.ConvertDeclToDeclGroup(SingleDecl);
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// objc-class-declaration:
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
8495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::DeclGroupPtrTy
8595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 8> ClassNames;
885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ClassLocs;
89c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek
901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
92df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
9595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian      return Actions.ConvertDeclToDeclGroup(0);
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
98c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek    ClassLocs.push_back(Tok.getLocation());
995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
101df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1045f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1055f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
10995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    return Actions.ConvertDeclToDeclGroup(0);
1101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
112c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassLocs.data(),
113c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassNames.size());
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
116dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
117dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
118dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
119dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
120dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
1221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier objc-superclass[opt]
123dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-class-instance-variables[opt]
125dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
127dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
128dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier '(' identifier[opt] ')'
130dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
131dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
132dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
133dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
134dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
135dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
136dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
137dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
138dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
139dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
140dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
141dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
142dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
143dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1447f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallDecl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
1457f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                              ParsedAttributes &attrs) {
146861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1503b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@interface'.
1513b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
15223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
1537d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
1553b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
1563b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
157df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
159d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
160dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
16163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
162dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1637ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
164dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
1655512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  if (Tok.is(tok::l_paren) &&
1665512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
16713d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor    SourceLocation LParenLoc = ConsumeParen();
168dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation categoryLoc, rparenLoc;
169dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
17033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
17123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
1727d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
17433ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
17533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
176527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
177df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
178dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
179dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
18005511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
18105511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    else if (!getLang().ObjC2) {
182527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
183d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
184dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
18513d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
18613d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor    rparenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
18713d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor    if (rparenLoc.isInvalid())
188d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
18913d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
19013d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor    if (!attrs.empty()) { // categories don't support attributes.
19113d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      Diag(nameLoc, diag::err_objc_no_attributes_on_category);
19213d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      attrs.clear();
193dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
19413d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
1955512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    // Next, we need to check for any protocol references.
1965512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    SourceLocation LAngleLoc, EndProtoLoc;
1975f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<Decl *, 8> ProtocolRefs;
1985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 8> ProtocolLocs;
1995512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::less) &&
2005512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
20171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
202d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
20305511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian
204d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *CategoryType =
2055512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    Actions.ActOnStartCategoryInterface(atLoc,
2065512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        nameId, nameLoc,
2075512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        categoryId, categoryLoc,
2085512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.data(),
2095512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.size(),
2105512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolLocs.data(),
2115512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        EndProtoLoc);
212e6f07f538fd0eddd6c087fcc01d4e4ff19129c71Fariborz Jahanian
213a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    if (Tok.is(tok::l_brace))
214a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, atLoc);
215a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
2162f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian    ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
2175512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    return CategoryType;
218dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
219dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
220dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
221dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
2227ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
223df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
224dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
2253b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
2263b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
2273b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
22823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
2297d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
2307d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
2313b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2323b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
233df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
234dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
235d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
236dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
237dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
238dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
239dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
240dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
2415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
2425f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
24371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
24406036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
24571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
24671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
247d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
2481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
249d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ClsType =
2501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
25106036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
252beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
25318df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                     ProtocolLocs.data(),
2547f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                     EndProtoLoc, attrs.getList());
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
256df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
25783c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian    ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
258dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
2592f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
2605512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  return ClsType;
261dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
262dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
263d0014540005f2a5ab837365db6bd40f479406758John McCall/// The Objective-C property callback.  This should be defined where
264d0014540005f2a5ab837365db6bd40f479406758John McCall/// it's used, but instead it's been lifted to here to support VS2005.
265d0014540005f2a5ab837365db6bd40f479406758John McCallstruct Parser::ObjCPropertyCallback : FieldCallback {
266d0014540005f2a5ab837365db6bd40f479406758John McCall  Parser &P;
2675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVectorImpl<Decl *> &Props;
268d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCDeclSpec &OCDS;
269d0014540005f2a5ab837365db6bd40f479406758John McCall  SourceLocation AtLoc;
270d0014540005f2a5ab837365db6bd40f479406758John McCall  tok::ObjCKeywordKind MethodImplKind;
271d0014540005f2a5ab837365db6bd40f479406758John McCall
272a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  ObjCPropertyCallback(Parser &P,
2735f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &Props,
274d0014540005f2a5ab837365db6bd40f479406758John McCall                       ObjCDeclSpec &OCDS, SourceLocation AtLoc,
275d0014540005f2a5ab837365db6bd40f479406758John McCall                       tok::ObjCKeywordKind MethodImplKind) :
276a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
277d0014540005f2a5ab837365db6bd40f479406758John McCall    MethodImplKind(MethodImplKind) {
278d0014540005f2a5ab837365db6bd40f479406758John McCall  }
279d0014540005f2a5ab837365db6bd40f479406758John McCall
280d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *invoke(FieldDeclarator &FD) {
281d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.D.getIdentifier() == 0) {
282d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
283d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
284d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
285d0014540005f2a5ab837365db6bd40f479406758John McCall    }
286d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.BitfieldSize) {
287d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_bitfield)
288d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
289d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
290d0014540005f2a5ab837365db6bd40f479406758John McCall    }
291d0014540005f2a5ab837365db6bd40f479406758John McCall
292d0014540005f2a5ab837365db6bd40f479406758John McCall    // Install the property declarator into interfaceDecl.
293d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SelName =
294d0014540005f2a5ab837365db6bd40f479406758John McCall      OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
295d0014540005f2a5ab837365db6bd40f479406758John McCall
296d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector GetterSel =
297d0014540005f2a5ab837365db6bd40f479406758John McCall      P.PP.getSelectorTable().getNullarySelector(SelName);
298d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SetterName = OCDS.getSetterName();
299d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector SetterSel;
300d0014540005f2a5ab837365db6bd40f479406758John McCall    if (SetterName)
301d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
302d0014540005f2a5ab837365db6bd40f479406758John McCall    else
303d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
304d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     P.PP.getSelectorTable(),
305d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     FD.D.getIdentifier());
306d0014540005f2a5ab837365db6bd40f479406758John McCall    bool isOverridingProperty = false;
307d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Property =
30823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
309a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                              GetterSel, SetterSel,
310d0014540005f2a5ab837365db6bd40f479406758John McCall                              &isOverridingProperty,
311d0014540005f2a5ab837365db6bd40f479406758John McCall                              MethodImplKind);
312d0014540005f2a5ab837365db6bd40f479406758John McCall    if (!isOverridingProperty)
313d0014540005f2a5ab837365db6bd40f479406758John McCall      Props.push_back(Property);
314d0014540005f2a5ab837365db6bd40f479406758John McCall
315d0014540005f2a5ab837365db6bd40f479406758John McCall    return Property;
316d0014540005f2a5ab837365db6bd40f479406758John McCall  }
317d0014540005f2a5ab837365db6bd40f479406758John McCall};
318d0014540005f2a5ab837365db6bd40f479406758John McCall
319dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
320dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
321dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
322294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
3233536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
324dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
325dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
326dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
327294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
328294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
329294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
330294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
3312f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanianvoid Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
3322f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian                                        Decl *CDecl) {
3335f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> allMethods;
3345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 16> allProperties;
3355f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclGroupPtrTy, 8> allTUVariables;
33600933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
3371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
338782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  SourceRange AtEnd;
3392f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  Actions.ActOnObjCContainerStartDefinition(CDecl);
3402f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian
341294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
342e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
343df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
344d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *methodPrototype =
345a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        ParseObjCMethodPrototype(MethodImplKind, false);
34625e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
3473536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
3483536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
349b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
350b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner                       "", tok::semi);
351294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
352294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
35305511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    if (Tok.is(tok::l_paren)) {
35405511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      Diag(Tok, diag::err_expected_minus_or_plus);
355d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ParseObjCMethodDecl(Tok.getLocation(),
356d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          tok::minus,
35790ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                          MethodImplKind, false);
35805511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      continue;
35905511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
360e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
361e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
362294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
363e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
364e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
366bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
367e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
368e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
370b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    // Code completion within an Objective-C interface.
371b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    if (Tok.is(tok::code_completion)) {
37223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
373f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                  ObjCImpDecl? Sema::PCC_ObjCImplementation
374f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             : Sema::PCC_ObjCInterface);
3757d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
376b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    }
377b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor
378e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
379e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
3801fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
3811fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
3821fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
3831fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
3841fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
3850b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      ParsedAttributes attrs(AttrFactory);
3867f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
387e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
388e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
390e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
391e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
392c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    if (Tok.is(tok::code_completion)) {
393a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCAtDirective(getCurScope());
3947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
395c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor      break;
396c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    }
397c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
398a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
3991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
400a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
401782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setBegin(AtLoc);
402782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setEnd(Tok.getLocation());
403e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
404c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor    } else if (DirectiveKind == tok::objc_not_keyword) {
405c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      Diag(Tok, diag::err_objc_unknown_at);
406c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      SkipUntil(tok::semi);
407c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      continue;
408bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
4091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
410bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
411bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
412bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
413a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
414a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
415bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
416bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
417bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
418bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
419f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
420bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
421a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
422a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
42346d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
42446d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian    case tok::objc_implementation:
425df81c2c6ce37f4ae3c45dd01093b6274fa0b6692Fariborz Jahanian    case tok::objc_interface:
42646d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      Diag(Tok, diag::err_objc_missing_end);
42746d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      ConsumeToken();
42846d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      break;
42946d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
430a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
431a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
432a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
433bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
434e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
435bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
436a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
437bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
438a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
440a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
441f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
442b321c0c0ba957d78475e72cebde4028fdaa00f8fChris Lattner        Diag(AtLoc, diag::err_objc_properties_require_objc2);
443f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
444e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
4468ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.is(tok::l_paren))
447a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        ParseObjCPropertyAttribute(OCDS);
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
449a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      ObjCPropertyCallback Callback(*this, allProperties,
450d0014540005f2a5ab837365db6bd40f479406758John McCall                                    OCDS, AtLoc, MethodImplKind);
451bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
452e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
4530b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      DeclSpec DS(AttrFactory);
454bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
4551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4567da19ea50d4161fcda40d135735bcf450cabeb50John McCall      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
457a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
458f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
459294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
460bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
461bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
462bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
463c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
464a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.CodeCompleteObjCAtDirective(getCurScope());
4657d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return cutOffParsing();
466c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  } else if (Tok.isObjCAtKeyword(tok::objc_end))
467bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
468bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  else
469bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    Diag(Tok, diag::err_objc_missing_end);
4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
471a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
472bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
473a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnAtEnd(getCurScope(), AtEnd,
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
475beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
476beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
477294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
478294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
479d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
480d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
481d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
482d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
483d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
484d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
485d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
486d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
487d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
488d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
489d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
490d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
491d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
492d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
493d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
494f85e193739c953358c865005855253af4f68a497John McCall///     atomic
495f85e193739c953358c865005855253af4f68a497John McCall///     strong
496f85e193739c953358c865005855253af4f68a497John McCall///     weak
497f85e193739c953358c865005855253af4f68a497John McCall///     unsafe_unretained
498d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
499a28948f34817476d02412fa204cae038e228c827Fariborz Jahanianvoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
5008ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
501dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  SourceLocation LHSLoc = ConsumeParen(); // consume '('
5021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
503cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
504ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
50523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
5067d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
507ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
508d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
510f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
511f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
512f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      MatchRHSPunctuation(tok::r_paren, LHSLoc);
513f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
514f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
516156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
51892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
519e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
52092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
521e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
522f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("unsafe_unretained"))
523f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
52492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
525e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
52692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
527e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
528f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("strong"))
529f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
53092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
531e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
53292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
533e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
53445937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian    else if (II->isStr("atomic"))
53545937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
536f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("weak"))
537f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
53892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
53942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      bool IsSetter = II->getNameStart()[0] == 's';
54042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
541e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
54242499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
54342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        diag::err_objc_expected_equal_for_getter;
54442499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
54542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
546dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
5471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5484ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      if (Tok.is(tok::code_completion)) {
54942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        if (IsSetter)
550a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertySetter(getCurScope());
5514ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        else
552a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertyGetter(getCurScope());
5537d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
5544ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      }
5554ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
55642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
55742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      SourceLocation SelLoc;
55842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
55942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
56042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (!SelIdent) {
56142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
56242499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson          << IsSetter;
5638ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
5648ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
5658ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
56742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (IsSetter) {
5688ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
56942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setSetterName(SelIdent);
5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
571e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian        if (ExpectAndConsume(tok::colon,
572e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian                             diag::err_expected_colon_after_setter_name, "",
573156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
5748ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
5758ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
5768ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
57742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setGetterName(SelIdent);
5788ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
579e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
580a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
581cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
582cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
583cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
585156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
586156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
588156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
589d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
591156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner  MatchRHSPunctuation(tok::r_paren, LHSLoc);
592d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
593d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
5943536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
5951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
5963536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
597294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
598294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
599294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
600294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6014985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
6024985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
6034985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
604a28948f34817476d02412fa204cae038e228c827Fariborz JahanianDecl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
60590ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                       bool MethodDefinition) {
606df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
607294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
609bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
610a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
61190ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                    MethodDefinition);
6123536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
6132bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
614f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
615294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
616294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
617294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
618294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
619294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
620294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
621294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
622294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
623294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
624294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6252fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
626be74740cc246ce08d42804a684385a42eb814edbFariborz Jahanian
627ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
628ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
629ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
630afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampamp:
631afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampequal:
632afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::amp:
633afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipe:
634afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::tilde:
635afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaim:
636afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaimequal:
637afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipepipe:
638afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipeequal:
639afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caret:
640afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caretequal: {
6413846ca29a8cc1d376a4b695194c29952dbbfb544Fariborz Jahanian    std::string ThisTok(PP.getSpelling(Tok));
642afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    if (isalpha(ThisTok[0])) {
643afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
644afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      Tok.setKind(tok::identifier);
645afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      SelectorLoc = ConsumeToken();
646afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      return II;
647afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    }
648afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    return 0;
649afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  }
650afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian
651ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
652ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
653ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
6549298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
655ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
656ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
657ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
658ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
659ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
660ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
661ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
662ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
663ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
664ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
665ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
666ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
667ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
668ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
669ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
670ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
671ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
672ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
673ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
674ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
675ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
676ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
677ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
678ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
679ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
680ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
681ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
682ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
683ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
684ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
685ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
686ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
687ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
688ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
689ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
690ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
691ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
692ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
693ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
694ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
695ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
696ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
697ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
698ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
699ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
700ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
701ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
702ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
703ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
704ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
705ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
706ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
707ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
708ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
709ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
710ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
711ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
712ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
713ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
714ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
715ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
716ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
717ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
718ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
719ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
720ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
7214b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
722ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
723d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
724294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
725294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
7260196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
7270196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
728335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
7293ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
7303ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
7313ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
7321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
7335ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
7340196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
7350196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
736a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
737e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
738e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
739294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
740294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
741294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
742294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
743294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
744b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregorvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
745b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor                                        ObjCTypeNameContext Context) {
746e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
747d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    if (Tok.is(tok::code_completion)) {
748b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor      Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
749b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor                                          Context == OTN_ParameterType);
7507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
751d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    }
752d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor
753cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
754e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
756e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
757e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
758a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
759e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
761a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
762e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
763b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      default: llvm_unreachable("Unknown decl qualifier");
764a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
765a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
766a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
767a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
768a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
769a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
770e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
771a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
772e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
773e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
774e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
775e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
777e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
778e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
779e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
780e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
781e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
782e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
783e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
784e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
785e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
786b77cab97f17f946744c920629ca17271308d8ebfDouglas GregorParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
787b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor                                     ObjCTypeNameContext Context) {
788df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
7891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7904a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  SourceLocation LParenLoc = ConsumeParen();
791e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
7929735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  ObjCDeclContextSwitch ObjCDC(*this);
7939735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian
79419d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
795b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor  ParseObjCTypeQualifierList(DS, Context);
7964fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
797b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType Ty;
798809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
799f85e193739c953358c865005855253af4f68a497John McCall    TypeResult TypeSpec =
800f85e193739c953358c865005855253af4f68a497John McCall      ParseTypeName(0, Declarator::ObjCPrototypeContext, &DS);
801809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (!TypeSpec.isInvalid())
802809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      Ty = TypeSpec.get();
803e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor  } else if (Context == OTN_ResultType && Tok.is(tok::identifier)) {
804e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (!Ident_instancetype)
805e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ident_instancetype = PP.getIdentifierInfo("instancetype");
806e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
807e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (Tok.getIdentifierInfo() == Ident_instancetype) {
808e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
809e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      ConsumeToken();
810e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    }
811809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
812e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
8134a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
8144a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    ConsumeParen();
8154a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
816e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
8171ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
8184a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
8194a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
8204a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
8214a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
8224a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    MatchRHSPunctuation(tok::r_paren, LParenLoc);
823294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
824f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
825294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
826294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
827294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
828294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
8294985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
830294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
8314985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
832294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
833294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
8341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
835294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
836294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
837294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
8387ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
8397ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
8407ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
8417ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
842294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
8434985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
8444985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
845294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
8464985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
8474985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
848294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
8494985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
850294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
851294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
8527ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
8537ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
8547ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
855d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
8562ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor                                  tok::TokenKind mType,
85790ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  tok::ObjCKeywordKind MethodImplKind,
85890ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  bool MethodDefinition) {
85954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
86054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
861e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
86223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
863a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       /*ReturnType=*/ ParsedType());
8647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
8657d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
866e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
867e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
868e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
869b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType ReturnType;
870a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
871df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
872b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor    ReturnType = ParseObjCTypeName(DSRet, OTN_ResultType);
8731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8749e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // If attributes exist before the method, parse them.
8750b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributes methodAttrs(AttrFactory);
8767f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (getLang().ObjC2)
8770b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
8789e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek
879e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
88023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
881a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       ReturnType);
8827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
8837d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
884e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
885e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
8869e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // Now parse the selector.
887bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
8882fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
889e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
89084c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
89184c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
8921ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
8931ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
894e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
895e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
896d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
897e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
900df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
901ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
9027f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (getLang().ObjC2)
9030b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(methodAttrs);
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
905ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
906d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Result
9077f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian         = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
908a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                          mType, DSRet, ReturnType,
909926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                          selLoc, Sel, 0,
9104f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          CParamInfo.data(), CParamInfo.size(),
9110b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          methodAttrs.getList(), MethodImplKind,
9120b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          false, MethodDefinition);
91354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
91454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
915ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
916f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
9175f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
9185f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
9197f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian  ParseScope PrototypeScope(this,
9207f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian                            Scope::FunctionPrototypeScope|Scope::DeclScope);
9210b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
9220b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  AttributePool allParamAttrs(AttrFactory);
9237f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
924ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
9250b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes paramAttrs(AttrFactory);
926f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Sema::ObjCArgInfo ArgInfo;
9271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
928ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
929df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
930ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
931ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
932ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
933ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
935b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ArgInfo.Type = ParsedType();
936e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
937b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, OTN_ParameterType);
938e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
939ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
940e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
9417f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (getLang().ObjC2) {
9420b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(paramAttrs);
9430b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      ArgInfo.ArgAttrs = paramAttrs.getList();
9447f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
9457ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
94640ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    // Code completion for the next piece of the selector.
94740ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    if (Tok.is(tok::code_completion)) {
94840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.push_back(SelIdent);
94940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
95040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 mType == tok::minus,
95140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/true,
95240ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 ReturnType,
95340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.data(),
95440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.size());
9557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
9567d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
95740ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    }
95840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor
959df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
960ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
961ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
9624985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
9631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
964e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
965e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
966ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
968e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
969e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
970e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
9710b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    // Make sure the attributes persist.
9720b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    allParamAttrs.takeAllFrom(paramAttrs.getPool());
9730b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
9741f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    // Code completion for the next piece of the selector.
9751f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    if (Tok.is(tok::code_completion)) {
9761f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
9771f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 mType == tok::minus,
97840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/false,
9791f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 ReturnType,
9801f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.data(),
9811f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.size());
9827d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
9837d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
9841f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    }
9851f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor
986ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
9874b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
9882fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner    SelIdent = ParseObjCSelectorPiece(Loc);
989df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
990ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
991ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
992ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
9931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
994335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
996ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
997df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
998ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
999df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
1000335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
10014985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
1002ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
10034985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
10040b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
1005ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
1007ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
1008ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
10094f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1010d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
10114f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
10124f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    ParmDecl.getIdentifierLoc(),
10134f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    Param,
10144f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                   0));
10154f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian
10164985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
10171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10189c4bb2c08989265411925a04252fd4f93c26e3b1Cameron Esfahani  // FIXME: Add support for optional parameter list...
1019e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
10207f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (getLang().ObjC2)
10210b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
10227f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1023a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  if (KeyIdents.size() == 0)
1024d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
10257f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1026ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1027ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
1028d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Result
10297f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian       = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1030a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                        mType, DSRet, ReturnType,
1031926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                        selLoc, Sel, &ArgInfos[0],
10324f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        CParamInfo.data(), CParamInfo.size(),
10330b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                        methodAttrs.getList(),
103490ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                        MethodImplKind, isVariadic, MethodDefinition);
10357f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
103654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
103754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
1038294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
1039294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
1040dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
1041dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
1042dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
10437caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
10445f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
10455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                            SmallVectorImpl<SourceLocation> &ProtocolLocs,
104671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
104771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
1048e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
10491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
105071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
10511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierLocPair, 8> ProtocolIdents;
10531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1054e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
105555385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
105655385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
105755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
10587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
10597d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return true;
106055385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
106155385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
1062e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
1063e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
1064e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
1065e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
1066e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
1067e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1068e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
106971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
1070e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1072e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
1073e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
1074e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
1075e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1077e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
1078e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
1079e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
1080e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
1081e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
10821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1083e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
10841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1085e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
1086e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
1087e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
1088e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
1089e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
1090e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
1091e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
10929bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// \brief Parse the Objective-C protocol qualifiers that follow a typename
10939bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// in a decl-specifier-seq, starting at the '<'.
109446f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregorbool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
10959bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
10969bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
10979bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  SourceLocation LAngleLoc, EndProtoLoc;
10985f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolDecl;
10995f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
110046f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
110146f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor                                            LAngleLoc, EndProtoLoc);
11029bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
11039bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor                           ProtocolLocs.data(), LAngleLoc);
11049bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  if (EndProtoLoc.isValid())
11059bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(EndProtoLoc);
110646f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  return Result;
11079bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor}
11089bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
11099bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
1110dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
1111dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
1112dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1113dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
1114dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
1115dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
1116dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
1117dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
1118dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
1119dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
1120dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
1122dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
1123dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
1124dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
1125ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
1126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1127dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
11281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
1129dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1130d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
113183c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                             tok::ObjCKeywordKind visibility,
113260fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
1133df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
11345f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> AllIvarDecls;
1135a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
11361a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
113772de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
1138ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1140ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
1141df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1142ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
11431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1144ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
1145df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1146f13ca06e57ac094ed05ea08c26a499af1ba0ce88Douglas Gregor      Diag(Tok, diag::ext_extra_ivar_semi)
1147849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1148ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1149ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
1150ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
11511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1152ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
1153df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
1154ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
1155c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1156c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
115723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(getCurScope());
11587d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
1159c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
1160c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1161861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
1162ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
1163ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
1164ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
1165ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
1166861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
1167ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
1169ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
1170ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1171ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
1172ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1173ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
11741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1175c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
117623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
1177f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                       Sema::PCC_ObjCInstanceVariableList);
11787d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
1179c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1180c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1181bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1182bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1183d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *IDecl;
1184bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
11855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<Decl *> &AllIvarDecls;
1186bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1187d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
11885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &AllIvarDecls) :
1189bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1190bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1191bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1192d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *invoke(FieldDeclarator &FD) {
1193a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        P.Actions.ActOnObjCContainerStartDefinition(IDecl);
1194bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1195d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        Decl *Field
119623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          = P.Actions.ActOnIvar(P.getCurScope(),
1197bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1198a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                FD.D, FD.BitfieldSize, visibility);
119910af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian        P.Actions.ActOnObjCContainerFinishDefinition();
12000bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian        if (Field)
12010bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian          AllIvarDecls.push_back(Field);
1202bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
1203bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1204bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1205d097be8f81fbf4ed96ac10bae18562dd8202666bFariborz Jahanian
1206e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
12070b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
1208bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
12091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1210df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1211ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1212ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1213ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1214ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1215ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1216ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1217ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
121860fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1219a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
1220a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnLastBitfield(RBraceLoc, AllIvarDecls);
122110af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian  Actions.ActOnObjCContainerFinishDefinition();
12228749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
12238749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
122423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
122577b6de07be9186063c12928d2e9785a5d4eecbf6David Blaikie                      AllIvarDecls,
12261bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
1227ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
12285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1229dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1230dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1231dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1232dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1233dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1234dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
12361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
12371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1238dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1239dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1240dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1241dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1242dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1243dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
12443536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1245dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1246d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
12477f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                             ParsedAttributes &attrs) {
1248861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
12497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
12507ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
12511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1252083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
125323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(getCurScope());
12547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
12557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
1256083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1257083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1258df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
12597ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1260d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
12617ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
12627ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
12637ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
12647ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
12651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1266df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
12677caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
12687ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
12691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
12707f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
12717ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
12721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1273df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
12745f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<IdentifierLocPair, 8> ProtocolRefs;
12757caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
12767caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
12777ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
12787ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
12797ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1280df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
12817ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
12827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1283d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        return 0;
12847ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
12857caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
12867caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
12877ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1289df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
12907ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
12917ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
12927ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
12937ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1294d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
12951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1296e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
12971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1298bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
12997f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
13007caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13027ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
130371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
13047caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
13055f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
13065f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
13077caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
130871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
130971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1310d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1312d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ProtoType =
1313e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1314beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1315beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
131618df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
13177f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                        EndProtoLoc, attrs.getList());
1318a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
13192f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
1320bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1321dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1322dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1323dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1324dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1325dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1326dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1327dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1328dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1329dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1330dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1331dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1332dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1333d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtImplementationDeclaration(
1334ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1335ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1336ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1337ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
13381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13393b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
13403b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
134123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(getCurScope());
13427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
13437d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
13443b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
13453b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1346df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1347ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1348d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1349ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1350ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1351ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1352ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
13531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1355ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1356dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeParen();
1357ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1358ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
13591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
136033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
136123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
13627d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
13637d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
136433ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
136533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1366df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1367ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1368ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1369ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1370ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1371d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
13721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1373df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1374ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1375ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1376d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1377ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1378ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1379d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
13801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    atLoc, nameId, nameLoc, categoryId,
13818f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1382a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1383a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnObjCContainerStartDefinition(ImplCatType);
1384a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
138563e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.push_back(ObjCImpDecl);
1386d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1387ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1388ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1389ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1390ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1391df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1392ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1393ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1394df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1395ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1396d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1397ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1398ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1399ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1400ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1401d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ImplClsType = Actions.ActOnStartClassImplementation(
1402cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1403ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
14041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
140560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
1406a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    ParseObjCClassInstanceVariables(ImplClsType, tok::objc_private, atLoc);
1407a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1408a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnObjCContainerStartDefinition(ImplClsType);
1409a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
141063e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  PendingObjCImpDecl.push_back(ObjCImpDecl);
1411d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
14125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
141360fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1414140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::DeclGroupPtrTy
1415140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1416ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1417ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1418ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1419140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  SmallVector<Decl *, 8> DeclsInGroup;
14208697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  Actions.DefaultSynthesizeProperties(getCurScope(), ObjCImpDecl);
1421140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i) {
1422140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    Decl *D = ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i]);
1423140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    DeclsInGroup.push_back(D);
1424140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  }
1425140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  DeclsInGroup.push_back(ObjCImpDecl);
14268697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian
1427a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
1428a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnAtEnd(getCurScope(), atEnd);
142963e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.pop_back();
1430a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
14318697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  else
1432782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
143364089cece350472c04b420c497ae391443353325Fariborz Jahanian    Diag(atEnd.getBegin(), diag::err_expected_implementation);
14348697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian
14358697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  LateParsedObjCMethods.clear();
14368697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  ObjCImpDecl = 0;
1437140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  return Actions.BuildDeclaratorGroup(
1438140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian           DeclsInGroup.data(), DeclsInGroup.size(), false);
14395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1440e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
14413fe104154dd2e8ffb351142d74f308938b5c99bfFariborz JahanianParser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
14423fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian  Actions.DiagnoseUseOfUnimplementedSelectors();
144363e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  if (PendingObjCImpDecl.empty())
1444d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return Actions.ConvertDeclToDeclGroup(0);
1445d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
1446a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnAtEnd(getCurScope(), SourceRange());
144763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  return Actions.ConvertDeclToDeclGroup(ImpDecl);
144863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian}
144963e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1450e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1451e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1452e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1453d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1454e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1455e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1456e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1457df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1458e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1459d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1460e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1461243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1462243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1463df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1464e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1465d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1466e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1467243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1468243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1469e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1470e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor                   "@compatibility_alias");
1471b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1472b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
14735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1475ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1476ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1477ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1478ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1479ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1480ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1481ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1482ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1483ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1484ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1485ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1486d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1487ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1488ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1489dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume synthesize
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1491b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1492322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
1493a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
14947d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
14957d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1496322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1497322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1498b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1499b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1500b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1501d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1502b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1503b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1504f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1505f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1506f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1507a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor    SourceLocation propertyIvarLoc;
1508df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1509ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1510ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1511322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1512322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
1513a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
15147d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
15157d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return 0;
1516322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1517322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1518df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1519ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1520ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1521ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1522f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1523a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor      propertyIvarLoc = ConsumeToken(); // consume ivar-name
1524ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1525a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
1526a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, propertyIvar, propertyIvarLoc);
1527df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1528ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1529ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1530ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1531e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
1532d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1533ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1534ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1535ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1536ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1537ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1538ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1539ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1540ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1541ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1542d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1543ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1544ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1545dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume dynamic
1546424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1547424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
1548a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
15497d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
15507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1551424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1552424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1553424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1554424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1555424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1556d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1557424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1558424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1559c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1560c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1561a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
1562a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, 0, SourceLocation());
1563c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1564df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1565ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1566ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1567ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1568e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
1569d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1570ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1572397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1573397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1574397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
157560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
157660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res;
1577397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1578df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
157939f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
15800e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1581397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
158243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1583397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1584397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
158502418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  // consume ';'
158602418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
15879ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
1588397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1589397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1590c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
159178a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1592c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
159360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
159443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1595fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1596fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
15971ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
159843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1599fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
160007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
160107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // The operand is surrounded with parentheses.
1602fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
160307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ExprResult operand(ParseExpression());
160407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
160507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (Tok.is(tok::r_paren)) {
160607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    ConsumeParen();  // ')'
160707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  } else {
160807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
160907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_rparen);
161007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
161107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    // Skip forward until we see a left brace, but don't consume it.
161207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    SkipUntil(tok::l_brace, true, true);
1613fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
161407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
161507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Require a compound statement.
161678a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
161707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
161807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_lbrace);
161943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
162078a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
16213ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
162207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Check the @synchronized operand now.
162307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (!operand.isInvalid())
162407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
162507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
162607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Parse the compound statement within a new scope.
162707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ParseScope bodyScope(this, Scope::DeclScope);
162807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  StmtResult body(ParseCompoundStatementBody());
162907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  bodyScope.Exit();
163007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
163107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // If there was a semantic or parse error earlier with the
163207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // operand, fail now.
163307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (operand.isInvalid())
163407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    return StmtError();
163507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
163607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (body.isInvalid())
163707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    body = Actions.ActOnNullStmt(Tok.getLocation());
16380e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
163907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
1640c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1641c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1642397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1643397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1644397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1645397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1646397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1647397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1648397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1649397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1650397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1651397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1652397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
165360d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1654397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
165543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1656397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1657df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
16581ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
165943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1660397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
16618f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  StmtVector CatchStmts(Actions);
166260d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult FinallyStmt;
16638935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
166460d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult TryBody(ParseCompoundStatementBody());
16658935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
16660e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1667bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1668a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1669df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
16706b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
16716b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
16726b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
16736b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
16746b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
16756b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
16766b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
16770e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1678161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1679cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1680d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *FirstPart = 0;
16813b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1682df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1683397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1684e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1685df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
16860b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall          DeclSpec DS(AttrFactory);
1687397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
168817b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis          Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
16897ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
16907ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
16914e6c0d19b7c072758922cf80525a81aeefc6e64bDouglas Gregor          // Inform the actions module about the declarator, so it
16927ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
169323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
169464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1695397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
16961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
169793a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
169993a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
170093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
170193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
170293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
170393a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
170460d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult CatchBody(true);
1705c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1706c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1707c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1708c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
17090e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
17103b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
17118f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
171260d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
17138f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              RParenLoc,
17148f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              FirstPart,
17159ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                              CatchBody.take());
17168f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        if (!Catch.isInvalid())
17178f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor          CatchStmts.push_back(Catch.release());
17188f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
171964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
17201ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
17211ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
172243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1723397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1724397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
17256b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
17266b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
172764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
17288935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
17290e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
173060d7b3a319d84d688752be3870615ac0f111fb16John McCall      StmtResult FinallyBody(true);
1731c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1732c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1733c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1734c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
17350e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1736161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
17370e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
17389ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                   FinallyBody.take());
1739397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1740397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1741397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1742397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1743bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1744397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
174543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1746bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
17478f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
17489ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
17498f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                    move_arg(CatchStmts),
17509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                    FinallyStmt.take());
1751397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1752ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1753f85e193739c953358c865005855253af4f68a497John McCall/// objc-autoreleasepool-statement:
1754f85e193739c953358c865005855253af4f68a497John McCall///   @autoreleasepool compound-statement
1755f85e193739c953358c865005855253af4f68a497John McCall///
1756f85e193739c953358c865005855253af4f68a497John McCallStmtResult
1757f85e193739c953358c865005855253af4f68a497John McCallParser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1758f85e193739c953358c865005855253af4f68a497John McCall  ConsumeToken(); // consume autoreleasepool
1759f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isNot(tok::l_brace)) {
1760f85e193739c953358c865005855253af4f68a497John McCall    Diag(Tok, diag::err_expected_lbrace);
1761f85e193739c953358c865005855253af4f68a497John McCall    return StmtError();
1762f85e193739c953358c865005855253af4f68a497John McCall  }
1763f85e193739c953358c865005855253af4f68a497John McCall  // Enter a scope to hold everything within the compound stmt.  Compound
1764f85e193739c953358c865005855253af4f68a497John McCall  // statements can always hold declarations.
1765f85e193739c953358c865005855253af4f68a497John McCall  ParseScope BodyScope(this, Scope::DeclScope);
1766f85e193739c953358c865005855253af4f68a497John McCall
1767f85e193739c953358c865005855253af4f68a497John McCall  StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1768f85e193739c953358c865005855253af4f68a497John McCall
1769f85e193739c953358c865005855253af4f68a497John McCall  BodyScope.Exit();
1770f85e193739c953358c865005855253af4f68a497John McCall  if (AutoreleasePoolBody.isInvalid())
1771f85e193739c953358c865005855253af4f68a497John McCall    AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1772f85e193739c953358c865005855253af4f68a497John McCall  return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1773f85e193739c953358c865005855253af4f68a497John McCall                                                AutoreleasePoolBody.take());
1774f85e193739c953358c865005855253af4f68a497John McCall}
1775f85e193739c953358c865005855253af4f68a497John McCall
17763536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1777ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1778d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDefinition() {
1779a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodPrototype();
17801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1781f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1782f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      "parsing Objective-C method");
17831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1784ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1785209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1786496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    if (ObjCImpDecl) {
1787496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
1788849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1789496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1790ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1791209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1792ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1793409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1794df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1795da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
17961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1797409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1798409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
17991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1800409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1801409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1802d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1803ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1804140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Allow the rest of sema to find private method decl implementations.
1805140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  if (MDecl)
1806140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    Actions.AddAnyMethodToGlobalPool(MDecl);
1807140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
1808140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume the tokens and store them for later parsing.
1809140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LexedMethod* LM = new LexedMethod(this, MDecl);
1810140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LateParsedObjCMethods.push_back(LM);
1811140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  CachedTokens &Toks = LM->Toks;
1812140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Begin by storing the '{' token.
1813140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  Toks.push_back(Tok);
1814140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ConsumeBrace();
1815140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume everything up to (and including) the matching right brace.
1816140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
181771c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
18185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
18195508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
182060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
18219a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
182223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtStatement(getCurScope());
18237d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
18249a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
18255d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
18265d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
18275d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
18286b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
18295d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
18305d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
183164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
18325d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
18335d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
183464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
1835f85e193739c953358c865005855253af4f68a497John McCall
1836f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
1837f85e193739c953358c865005855253af4f68a497John McCall    return ParseObjCAutoreleasePoolStmt(AtLoc);
18385d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
183960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
18400e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
184164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
184264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
184364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
184464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
184543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
184664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
18475d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
184864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
18499ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
18509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
185164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
185264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
185360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
18545508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
18559a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
185623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtExpression(getCurScope());
18577d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
18589a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
18599a0c85e640a08174569a303db22981612f05d385Douglas Gregor
1860b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1861b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
18621d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1863b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
18644fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
18651d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
18662f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
18674fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
18684fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
18691d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
18704fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
18711d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
18724fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
18731d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
18744fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
18751d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
18764fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
18775508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
18785508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
18795508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
18806aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brirg Parse the receiver of an Objective-C++ message send.
18816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// This routine parses the receiver of a message send in
18836aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// Objective-C++ either as a type or as an expression. Note that this
18846aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// routine must not be called to parse a send to 'super', since it
18856aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// has no way to return such a result.
18866aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18876aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param IsExpr Whether the receiver was parsed as an expression.
18886aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18896aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param TypeOrExpr If the receiver was parsed as an expression (\c
18906aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// IsExpr is true), the parsed expression. If the receiver was parsed
18916aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// as a type (\c IsExpr is false), the parsed type.
18926aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18936aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \returns True if an error occurred during parsing or semantic
18946aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// analysis, in which case the arguments do not have valid
18956aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// values. Otherwise, returns false for a successful parse.
18966aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18976aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///   objc-receiver: [C++]
18986aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     'super' [not parsed here]
18996aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     expression
19006aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     simple-type-specifier
19016aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     typename-specifier
19026aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
19030fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
19040fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
19056aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
19066aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
19076aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TryAnnotateTypeOrScopeToken();
19086aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19096aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (!isCXXSimpleTypeSpecifier()) {
19106aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   objc-receiver:
19116aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     expression
191260d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseExpression();
19136aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
19146aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
19156aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19166aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
19176aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
19186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
19196aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
19206aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19216aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // objc-receiver:
19226aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   typename-specifier
19236aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   simple-type-specifier
19246aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   expression (that starts with one of the above)
19250b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
19266aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  ParseCXXSimpleTypeSpecifier(DS);
19276aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19286aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::l_paren)) {
19296aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // If we see an opening parentheses at this point, we are
19306aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // actually parsing an expression that starts with a
19316aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // function-style cast, e.g.,
19326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
19336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   postfix-expression:
19346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     simple-type-specifier ( expression-list [opt] )
19356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     typename-specifier ( expression-list [opt] )
19366aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
19376aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the remainder of this case, then the (optional)
19386aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // postfix-expression suffix, followed by the (optional)
19396aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // right-hand side of the binary expression. We have an
19406aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // instance method.
194160d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
19426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
19439ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParsePostfixExpressionSuffix(Receiver.take());
19446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
19459ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
19466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
19476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
19486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
19506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
19516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
19526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
19536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // We have a class message. Turn the simple-type-specifier or
19556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // typename-specifier we parsed into a type and parse the
19566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // remainder of the class message.
19576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
195823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
19596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Type.isInvalid())
19606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
19616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  IsExpr = false;
1963b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  TypeOrExpr = Type.get().getAsOpaquePtr();
19646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
19656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
19666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19671b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// \brief Determine whether the parser is currently referring to a an
19681b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// Objective-C message send, using a simplified heuristic to avoid overhead.
19691b730e847ded503f2e615154035c083c4f94a067Douglas Gregor///
19701b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// This routine will only return true for a subset of valid message-send
19711b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// expressions.
19721b730e847ded503f2e615154035c083c4f94a067Douglas Gregorbool Parser::isSimpleObjCMessageExpression() {
1973c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
19741b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         "Incorrect start for isSimpleObjCMessageExpression");
19751b730e847ded503f2e615154035c083c4f94a067Douglas Gregor  return GetLookAheadToken(1).is(tok::identifier) &&
19761b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         GetLookAheadToken(2).is(tok::identifier);
19771b730e847ded503f2e615154035c083c4f94a067Douglas Gregor}
19781b730e847ded503f2e615154035c083c4f94a067Douglas Gregor
19799497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregorbool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
19809497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
19819497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      InMessageExpression)
19829497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
19839497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19849497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19859497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  ParsedType Type;
19869497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19879497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (Tok.is(tok::annot_typename))
19889497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = getTypeAnnotation(Tok);
19899497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else if (Tok.is(tok::identifier))
19909497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
19919497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor                               getCurScope());
19929497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else
19939497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
19949497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19959497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
19969497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    const Token &AfterNext = GetLookAheadToken(2);
19979497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
19989497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      if (Tok.is(tok::identifier))
19999497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor        TryAnnotateTypeOrScopeToken();
20009497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
20019497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      return Tok.is(tok::annot_typename);
20029497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    }
20039497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  }
20049497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
20059497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  return false;
20069497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor}
20079497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
20081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
20090ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
20100ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
20112725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///   objc-receiver: [C]
2012eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner///     'super'
20130ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
20140ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
20150ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
20166aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
201760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCMessageExpression() {
2018699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
2019699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2020699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
20218e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  if (Tok.is(tok::code_completion)) {
202223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMessageReceiver(getCurScope());
20237d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
20248e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    return ExprError();
20258e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  }
20268e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor
20270fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
20280fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
20296aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (getLang().CPlusPlus) {
20306aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // We completely separate the C and C++ cases because C++ requires
20316aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // more complicated (read: slower) parsing.
20326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Handle send to super.
20346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: This doesn't benefit from the same typo-correction we
20356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // get in Objective-C.
20366aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
203723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
2038b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2039b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
20406aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20416aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the receiver, which is either a type or an expression.
20426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    bool IsExpr;
2043304b752a450c0fc5968c20ba25446d0bb7c6f68dNick Lewycky    void *TypeOrExpr = NULL;
20446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
20456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      SkipUntil(tok::r_square);
20466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ExprError();
20476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    }
20486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (IsExpr)
2050b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2051b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(),
20529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            static_cast<Expr*>(TypeOrExpr));
20536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
20546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2055b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                              ParsedType::getFromOpaquePtr(TypeOrExpr),
2056b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          0);
2057c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  }
2058c59cb38810c63a806270385f79ea84e0203754eaChris Lattner
2059c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  if (Tok.is(tok::identifier)) {
20601dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    IdentifierInfo *Name = Tok.getIdentifierInfo();
20611dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    SourceLocation NameLoc = Tok.getLocation();
2062b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType ReceiverType;
206323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
20641dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor                                       Name == Ident_super,
20651569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       NextToken().is(tok::period),
20661569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       ReceiverType)) {
2067f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCSuperMessage:
2068b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2069b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
20702725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
2071f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCClassMessage:
20721569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      if (!ReceiverType) {
20732725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        SkipUntil(tok::r_square);
20742725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        return ExprError();
20752725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      }
20762725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
20771569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      ConsumeToken(); // the type name
20781569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
20791569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
20809ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            ReceiverType, 0);
20811dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor
2082f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCInstanceMessage:
20832725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      // Fall through to parse an expression.
20841dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor      break;
2085d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
2086699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
2087eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
2088eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  // Otherwise, an arbitrary expression can be the receiver of a send.
208960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpression());
20900e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
20915c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
20921d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
2093699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
20941d922960e083906a586609ac6978678147250177Sebastian Redl
2095b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2096b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType(), Res.take());
2097699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
20981d922960e083906a586609ac6978678147250177Sebastian Redl
20992725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Parse the remainder of an Objective-C message following the
21002725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// '[' objc-receiver.
21012725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
21022725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine handles sends to super, class messages (sent to a
21032725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class name), and instance messages (sent to an object), and the
21042725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// target is represented by \p SuperLoc, \p ReceiverType, or \p
21052725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// ReceiverExpr, respectively. Only one of these parameters may have
21062725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// a valid value.
21072725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
21082725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening '['.
21092725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
21102725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc If this is a send to 'super', the location of the
21112725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 'super' keyword that indicates a send to the superclass.
21122725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
21132725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType If this is a class message, the type of the
21142725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class we are sending a message to.
21152725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
21162725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverExpr If this is an instance message, the expression
21172725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// used to compute the receiver object.
21181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
21190ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
21200ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
21210ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
21220ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
21230ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
21240ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
21250ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
21260ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
21271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
21280ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
21290ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
21300ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
21310ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
21320ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
21330ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
21340ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
21350ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
21361d922960e083906a586609ac6978678147250177Sebastian Redl///
213760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2138699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
21392725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                       SourceLocation SuperLoc,
2140b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                       ParsedType ReceiverType,
21411d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
21420fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
21430fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
2144c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
21452725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    if (SuperLoc.isValid())
214670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
214770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
21482725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    else if (ReceiverType)
214970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
215070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
2151c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
21529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
215370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                              0, 0, false);
21547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
21557d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return ExprError();
2156c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
2157d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
2158a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
21594b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
21602fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
216168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
2162ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson  SourceLocation SelectorLoc = Loc;
21631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21645f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2165a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
216668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
2167df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
2168a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
2169a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
217068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
217137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
2172df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
2173a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
21744fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
21754fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
21764fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
21774fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
21781d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
2179a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
21801d922960e083906a586609ac6978678147250177Sebastian Redl
218168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
21821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
218370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
218470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      if (Tok.is(tok::code_completion)) {
218570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        if (SuperLoc.isValid())
218670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
218770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
218870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
218970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
219070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else if (ReceiverType)
219170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
219270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
219370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
219470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
219570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else
219670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
219770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.data(),
219870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
219970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  /*AtArgumentEpression=*/true);
220070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
22017d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
220270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
220370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      }
220470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
220560d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
22060e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
22074fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
22084fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
22094fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
22104fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
22111d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
221237387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
22131d922960e083906a586609ac6978678147250177Sebastian Redl
221437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
2215effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
22161d922960e083906a586609ac6978678147250177Sebastian Redl
2217d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
2218d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
22192725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        if (SuperLoc.isValid())
222023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
22212725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                               KeyIdents.data(),
222270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
222370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
22242725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        else if (ReceiverType)
222523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2226d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
222770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
222870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
2229d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
22309ae2f076ca5ab1feb3ba95629099ec2319833701John McCall          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2231d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
223270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
223370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                /*AtArgumentEpression=*/false);
22347d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
223570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
2236d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
2237d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
223837387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
22392fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
2240df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
2241a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
2242a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
2243a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2244a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
2245df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
224649f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
22471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
224860d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
22490e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
22504fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
22514fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
22524fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
22534fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
22541d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
225549f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
22560e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
225749f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
2258effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
2259a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2260a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
2261a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
22621d922960e083906a586609ac6978678147250177Sebastian Redl
22634fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
22644fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
22654fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
22664fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
22671d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2268a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
2269809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian
2270df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
2271809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    if (Tok.is(tok::identifier))
2272809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_colon);
2273809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    else
2274809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_rsquare);
22754fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
22764fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
22774fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
22784fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
22791d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2280a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
22811d922960e083906a586609ac6978678147250177Sebastian Redl
2282699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
22831d922960e083906a586609ac6978678147250177Sebastian Redl
228429238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
2285ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
2286ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
2287ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
22881d922960e083906a586609ac6978678147250177Sebastian Redl
22892725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
229023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
22912725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                     LBracLoc, SelectorLoc, RBracLoc,
2292f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2293f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2294f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
22952725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  else if (ReceiverType)
229623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
22972725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                     LBracLoc, SelectorLoc, RBracLoc,
2298f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2299f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2300f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
23019ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
23022725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                      LBracLoc, SelectorLoc, RBracLoc,
2303f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      MultiExprArg(Actions,
2304f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.take(),
2305f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.size()));
23060ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
23070ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
230860d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
230960d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseStringLiteralExpression());
23101d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
23111d922960e083906a586609ac6978678147250177Sebastian Redl
2312b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
2313b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
2314b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
23155f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 4> AtLocs;
2316a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
2317b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
2318effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
23190e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2320b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
2321b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
2322b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
232315faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
232497cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
232597cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
2326b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
232760d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Lit(ParseStringLiteralExpression());
23280e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
23291d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
23300e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2331effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
2332b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
23331d922960e083906a586609ac6978678147250177Sebastian Redl
23341d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
23351d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
23365508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
2337f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
2338f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
2339f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
234060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
23411d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
2342861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
23431d922960e083906a586609ac6978678147250177Sebastian Redl
2344f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
23451d922960e083906a586609ac6978678147250177Sebastian Redl
23464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
23471d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
23481d922960e083906a586609ac6978678147250177Sebastian Redl
2349f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
23501d922960e083906a586609ac6978678147250177Sebastian Redl
2351809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
23521d922960e083906a586609ac6978678147250177Sebastian Redl
23534988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
23541d922960e083906a586609ac6978678147250177Sebastian Redl
2355809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2356809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2357809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
23581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
2359809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                                 Ty.get(), RParenLoc));
2360f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
236129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
236229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
236329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
236460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
23651d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
236629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
23671d922960e083906a586609ac6978678147250177Sebastian Redl
23684fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
23691d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
23701d922960e083906a586609ac6978678147250177Sebastian Redl
237129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
23721d922960e083906a586609ac6978678147250177Sebastian Redl
23734fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
23741d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
23751d922960e083906a586609ac6978678147250177Sebastian Redl
2376390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
237729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
23781d922960e083906a586609ac6978678147250177Sebastian Redl
23794988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
238029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
23811d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
23821d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
238329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
2384a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
2385a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
2386a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
238760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
2388a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
23891d922960e083906a586609ac6978678147250177Sebastian Redl
23904fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
23911d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
23921d922960e083906a586609ac6978678147250177Sebastian Redl
23935f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2394a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
2395a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
2396458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2397458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  if (Tok.is(tok::code_completion)) {
2398458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2399458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                     KeyIdents.size());
24007d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
2401458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    return ExprError();
2402458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  }
2403458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
24042fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
24055add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner  if (!SelIdent &&  // missing selector name.
24065add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
24071d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
24081d922960e083906a586609ac6978678147250177Sebastian Redl
2409b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
2410887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
2411887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
2412a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
24135add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
24145add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        ++nColons;
24155add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        KeyIdents.push_back(0);
24165add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      } else if (Tok.isNot(tok::colon))
24171d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
24181d922960e083906a586609ac6978678147250177Sebastian Redl
24195add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      ++nColons;
24203b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      ConsumeToken(); // Eat the ':' or '::'.
2421a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
2422a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2423458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2424458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      if (Tok.is(tok::code_completion)) {
2425458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2426458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                         KeyIdents.size());
24277d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2428458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        return ExprError();
2429458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      }
2430458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2431a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
2432a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
24332fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
2434b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
24353b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
2436a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2437a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
2438887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
2439a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2440887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
24411d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
24421d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
244358065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
2444140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2445140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianDecl *Parser::ParseLexedObjCMethodDefs(LexedMethod &LM) {
2446140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2447140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2448140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Append the current token at the end of the new token stream so that it
2449140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // doesn't get lost.
2450140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LM.Toks.push_back(Tok);
2451140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2452140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2453140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // MDecl might be null due to error in method prototype, etc.
2454140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  Decl *MDecl = LM.D;
2455140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume the previously pushed token.
2456140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ConsumeAnyToken();
2457140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2458140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(Tok.is(tok::l_brace) && "Inline objective-c method not starting with '{'");
2459140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  SourceLocation BraceLoc = Tok.getLocation();
2460140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Enter a scope for the method body.
2461140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ParseScope BodyScope(this,
2462140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian                       Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
2463140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2464140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Tell the actions module that we have entered a method definition with the
2465140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // specified Declarator for the method.
2466140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
2467140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2468140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  if (PP.isCodeCompletionEnabled()) {
2469140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian      if (trySkippingFunctionBodyForCodeCompletion()) {
2470140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian          BodyScope.Exit();
2471140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian          return Actions.ActOnFinishFunctionBody(MDecl, 0);
2472140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian      }
2473140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  }
2474140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2475140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  StmtResult FnBody(ParseCompoundStatementBody());
2476140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2477140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // If the function body could not be parsed, make a bogus compoundstmt.
2478140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  if (FnBody.isInvalid())
2479140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
2480140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian                                       MultiStmtArg(Actions), false);
2481140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2482140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Leave the function body scope.
2483140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  BodyScope.Exit();
2484140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2485140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  return Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
2486140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian}
2487