ParseObjc.cpp revision 80e8ea92d6dcaa05165dcb4730485db82dcd4629
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
1419510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Parse/Parser.h"
150fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor#include "RAIIObjectsForParser.h"
163f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose#include "clang/Basic/CharInfo.h"
1755fc873017f10f6f566b182b70f6fc22aefa3464Chandler Carruth#include "clang/Parse/ParseDiagnostic.h"
1819510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
19f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
2019510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Scope.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
2208602359845e668194d111b794b8965358babfe6Fariborz Jahanian#include "llvm/ADT/StringExtras.h"
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
259f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber/// Skips attributes after an Objective-C @ directive. Emits a diagnostic.
26e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Webervoid Parser::MaybeSkipAttributes(tok::ObjCKeywordKind Kind) {
279f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber  ParsedAttributes attrs(AttrFactory);
289f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber  if (Tok.is(tok::kw___attribute)) {
29e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber    if (Kind == tok::objc_interface || Kind == tok::objc_protocol)
30e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber      Diag(Tok, diag::err_objc_postfix_attribute_hint)
31e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber          << (Kind == tok::objc_protocol);
32e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber    else
33e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber      Diag(Tok, diag::err_objc_postfix_attribute);
349f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber    ParseGNUAttributes(attrs);
359f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber  }
369f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber}
375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
38891dca671a80c865c6def7259f170ba785e4faaaChris Lattner/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       external-declaration: [C99 6.9]
405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
4191fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-class-declaration
4291fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-alias-declaration
4391fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-protocol-definition
4491fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-method-definition
4591fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  '@' 'end'
4695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::DeclGroupPtrTy Parser::ParseObjCAtDirectives() {
475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation AtLoc = ConsumeToken(); // the "@"
481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
50a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.CodeCompleteObjCAtDirective(getCurScope());
517d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
527d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return DeclGroupPtrTy();
53c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  }
5495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian
5595ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  Decl *SingleDecl = 0;
56861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  switch (Tok.getObjCKeywordID()) {
575ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_class:
585ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtClassDeclaration(AtLoc);
597f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_interface: {
600b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
6195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
6295ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
637f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
647f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_protocol: {
650b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes attrs(AttrFactory);
66bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
677f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
685ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
69849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return ParseObjCAtImplementationDeclaration(AtLoc);
705ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
71140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian    return ParseObjCAtEndDeclaration(AtLoc);
725ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
7395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCAtAliasDeclaration(AtLoc);
7495ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
755ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
7695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCPropertySynthesize(AtLoc);
7795ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
785ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
7995ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = ParseObjCPropertyDynamic(AtLoc);
8095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
811b257afbae854c6817f26b7d61c4fed8ff7aebadDouglas Gregor  case tok::objc_import:
824e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().Modules)
8394ad28b31433058445a27db722f60402ee820beaDouglas Gregor      return ParseModuleImport(AtLoc);
8494ad28b31433058445a27db722f60402ee820beaDouglas Gregor
8594ad28b31433058445a27db722f60402ee820beaDouglas Gregor    // Fall through
8694ad28b31433058445a27db722f60402ee820beaDouglas Gregor
875ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
885ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
895ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
9095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    SingleDecl = 0;
9195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    break;
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
9395ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian  return Actions.ConvertDeclToDeclGroup(SingleDecl);
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// objc-class-declaration:
985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
10095ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::DeclGroupPtrTy
10195ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz JahanianParser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
1035f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 8> ClassNames;
1045f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ClassLocs;
105c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek
1061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
108e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber    MaybeSkipAttributes(tok::objc_class);
109df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
11295ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian      return Actions.ConvertDeclToDeclGroup(0);
1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
115c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek    ClassLocs.push_back(Tok.getLocation());
1165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
118df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
1201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
1225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
1231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
1255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
12695ed7784a335aca53b0c6e952cf31a4cfb633360Fariborz Jahanian    return Actions.ConvertDeclToDeclGroup(0);
1271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
129c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassLocs.data(),
130c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassNames.size());
1315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
133d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggenvoid Parser::CheckNestedObjCContexts(SourceLocation AtLoc)
134d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen{
135d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Sema::ObjCContainerKind ock = Actions.getObjCContainerKind();
136d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  if (ock == Sema::OCK_None)
137d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    return;
138d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
139849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Decl *Decl = Actions.getObjCDeclContext();
140849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (CurParsedObjCImpl) {
141849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    CurParsedObjCImpl->finish(AtLoc);
142849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  } else {
143849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    Actions.ActOnAtEnd(getCurScope(), AtLoc);
144849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
145d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  Diag(AtLoc, diag::err_objc_missing_end)
146d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      << FixItHint::CreateInsertion(AtLoc, "@end\n");
147d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  if (Decl)
148d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(Decl->getLocStart(), diag::note_objc_container_start)
149d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << (int) ock;
150d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen}
151d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
152dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
153dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
154dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
155dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
156dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
157dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier objc-superclass[opt]
159dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
1601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-class-instance-variables[opt]
161dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
162dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
163dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
164dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
1651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier '(' identifier[opt] ')'
166dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
167dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
168dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
169dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
170dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
171dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
172dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
173dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
174dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
175dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
176dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
177dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
178dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
179b2f6820773aabff3c5c9e0dbb1cbbbda0d80c41fPatrick Beard///     __attribute__((objc_root_class))
180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
181d64251fd56577dd5c78903454632361e094c6dc1Erik VerbruggenDecl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation AtLoc,
1827f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                              ParsedAttributes &attrs) {
183861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
184dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
185d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
186dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1883b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@interface'.
1893b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
19023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
1917d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
1933b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
1943b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
195e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber  MaybeSkipAttributes(tok::objc_interface);
1969f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber
197df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
198dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
199d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
200dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
20163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
202dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
2037ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
204dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
2055512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  if (Tok.is(tok::l_paren) &&
2065512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
2074a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2084a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    BalancedDelimiterTracker T(*this, tok::l_paren);
2094a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeOpen();
2104a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2114a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    SourceLocation categoryLoc;
212dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
21333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
21423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
2157d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
2167d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
21733ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
21833ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
219527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
220df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
221dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
222dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
22305511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
2244e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    else if (!getLangOpts().ObjC2) {
225527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
226d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
227dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
2284a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2294a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
2304a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    if (T.getCloseLocation().isInvalid())
231d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
23213d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
23313d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor    if (!attrs.empty()) { // categories don't support attributes.
23413d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      Diag(nameLoc, diag::err_objc_no_attributes_on_category);
23513d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor      attrs.clear();
236dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
23713d05ac08974ccb41f7da7595d769c158f58fbd6Douglas Gregor
2385512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    // Next, we need to check for any protocol references.
2395512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    SourceLocation LAngleLoc, EndProtoLoc;
2405f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<Decl *, 8> ProtocolRefs;
2415f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<SourceLocation, 8> ProtocolLocs;
2425512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::less) &&
2435512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
24471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
245d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
24605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian
247d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *CategoryType =
248d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Actions.ActOnStartCategoryInterface(AtLoc,
2495512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        nameId, nameLoc,
2505512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        categoryId, categoryLoc,
2515512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.data(),
2525512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.size(),
2535512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolLocs.data(),
2545512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        EndProtoLoc);
255e6f07f538fd0eddd6c087fcc01d4e4ff19129c71Fariborz Jahanian
256a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    if (Tok.is(tok::l_brace))
257d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      ParseObjCClassInstanceVariables(CategoryType, tok::objc_private, AtLoc);
258a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
2592f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian    ParseObjCInterfaceDeclList(tok::objc_not_keyword, CategoryType);
2605512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    return CategoryType;
261dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
262dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
263dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
264dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
2657ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
266df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
267dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
2683b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
2693b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
2703b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
27123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
2727d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
2737d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
2743b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2753b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
276df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
277dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
278d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
279dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
280dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
281dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
282dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
283dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
2845f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
2855f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
28671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
28706036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
28871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
28971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
290d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
2911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
292d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ClsType =
293d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Actions.ActOnStartClassInterface(AtLoc, nameId, nameLoc,
29406036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
295beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
29618df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                     ProtocolLocs.data(),
2977f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                     EndProtoLoc, attrs.getList());
2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
299df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
300d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, AtLoc);
301dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
3022f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_interface, ClsType);
3035512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  return ClsType;
304dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
305dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
306d0014540005f2a5ab837365db6bd40f479406758John McCall/// The Objective-C property callback.  This should be defined where
307d0014540005f2a5ab837365db6bd40f479406758John McCall/// it's used, but instead it's been lifted to here to support VS2005.
308d0014540005f2a5ab837365db6bd40f479406758John McCallstruct Parser::ObjCPropertyCallback : FieldCallback {
30999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikieprivate:
31099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie  virtual void anchor();
31199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikiepublic:
312d0014540005f2a5ab837365db6bd40f479406758John McCall  Parser &P;
3135f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVectorImpl<Decl *> &Props;
314d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCDeclSpec &OCDS;
315d0014540005f2a5ab837365db6bd40f479406758John McCall  SourceLocation AtLoc;
31677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian  SourceLocation LParenLoc;
317d0014540005f2a5ab837365db6bd40f479406758John McCall  tok::ObjCKeywordKind MethodImplKind;
318d0014540005f2a5ab837365db6bd40f479406758John McCall
319a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  ObjCPropertyCallback(Parser &P,
3205f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &Props,
321d0014540005f2a5ab837365db6bd40f479406758John McCall                       ObjCDeclSpec &OCDS, SourceLocation AtLoc,
32277bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                       SourceLocation LParenLoc,
323d0014540005f2a5ab837365db6bd40f479406758John McCall                       tok::ObjCKeywordKind MethodImplKind) :
32477bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian    P(P), Props(Props), OCDS(OCDS), AtLoc(AtLoc), LParenLoc(LParenLoc),
325d0014540005f2a5ab837365db6bd40f479406758John McCall    MethodImplKind(MethodImplKind) {
326d0014540005f2a5ab837365db6bd40f479406758John McCall  }
327d0014540005f2a5ab837365db6bd40f479406758John McCall
328dcdff46dd8e6d749283fe6c43adfcfe780c1d562Eli Friedman  void invoke(ParsingFieldDeclarator &FD) {
329d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.D.getIdentifier() == 0) {
330d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
331d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
332dcdff46dd8e6d749283fe6c43adfcfe780c1d562Eli Friedman      return;
333d0014540005f2a5ab837365db6bd40f479406758John McCall    }
334d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.BitfieldSize) {
335d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_bitfield)
336d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
337dcdff46dd8e6d749283fe6c43adfcfe780c1d562Eli Friedman      return;
338d0014540005f2a5ab837365db6bd40f479406758John McCall    }
339d0014540005f2a5ab837365db6bd40f479406758John McCall
340d0014540005f2a5ab837365db6bd40f479406758John McCall    // Install the property declarator into interfaceDecl.
341d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SelName =
342d0014540005f2a5ab837365db6bd40f479406758John McCall      OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
343d0014540005f2a5ab837365db6bd40f479406758John McCall
344d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector GetterSel =
345d0014540005f2a5ab837365db6bd40f479406758John McCall      P.PP.getSelectorTable().getNullarySelector(SelName);
346d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SetterName = OCDS.getSetterName();
347d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector SetterSel;
348d0014540005f2a5ab837365db6bd40f479406758John McCall    if (SetterName)
349d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
350d0014540005f2a5ab837365db6bd40f479406758John McCall    else
35180e8ea92d6dcaa05165dcb4730485db82dcd4629Adrian Prantl      SetterSel =
35280e8ea92d6dcaa05165dcb4730485db82dcd4629Adrian Prantl        SelectorTable::constructSetterSelector(P.PP.getIdentifierTable(),
35380e8ea92d6dcaa05165dcb4730485db82dcd4629Adrian Prantl                                               P.PP.getSelectorTable(),
35480e8ea92d6dcaa05165dcb4730485db82dcd4629Adrian Prantl                                               FD.D.getIdentifier());
355d0014540005f2a5ab837365db6bd40f479406758John McCall    bool isOverridingProperty = false;
356d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Property =
35777bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      P.Actions.ActOnProperty(P.getCurScope(), AtLoc, LParenLoc,
35877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                              FD, OCDS,
359a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                              GetterSel, SetterSel,
360d0014540005f2a5ab837365db6bd40f479406758John McCall                              &isOverridingProperty,
361d0014540005f2a5ab837365db6bd40f479406758John McCall                              MethodImplKind);
362d0014540005f2a5ab837365db6bd40f479406758John McCall    if (!isOverridingProperty)
363d0014540005f2a5ab837365db6bd40f479406758John McCall      Props.push_back(Property);
364d0014540005f2a5ab837365db6bd40f479406758John McCall
365f66a0dda541cd859a928193efba6dc5d7ba8fe54Eli Friedman    FD.complete(Property);
366d0014540005f2a5ab837365db6bd40f479406758John McCall  }
367d0014540005f2a5ab837365db6bd40f479406758John McCall};
368d0014540005f2a5ab837365db6bd40f479406758John McCall
36999ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikievoid Parser::ObjCPropertyCallback::anchor() {
37099ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie}
37199ba9e3bd70671f3441fb974895f226a83ce0e66David Blaikie
372dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
373dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
374dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
375294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
3763536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
377dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
378dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
379dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
380294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
381294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
382294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
383294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
3842f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanianvoid Parser::ParseObjCInterfaceDeclList(tok::ObjCKeywordKind contextKey,
3852f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian                                        Decl *CDecl) {
3865f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> allMethods;
3875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 16> allProperties;
3885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclGroupPtrTy, 8> allTUVariables;
38900933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
3901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
391782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  SourceRange AtEnd;
3922f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian
393294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
394e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
395df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
396d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian      if (Decl *methodPrototype =
397d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian          ParseObjCMethodPrototype(MethodImplKind, false))
398d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian        allMethods.push_back(methodPrototype);
3993536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
4003536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
4010db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis      if (ExpectAndConsumeSemi(diag::err_expected_semi_after_method_proto)) {
4020db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        // We didn't find a semi and we error'ed out. Skip until a ';' or '@'.
4030db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        SkipUntil(tok::at, /*StopAtSemi=*/true, /*DontConsume=*/true);
4040db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis        if (Tok.is(tok::semi))
4050db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis          ConsumeToken();
4060db9f4dad563a335641f5b9d4a42504d638b6c85Argyrios Kyrtzidis      }
407294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
408294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
40905511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    if (Tok.is(tok::l_paren)) {
41005511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      Diag(Tok, diag::err_expected_minus_or_plus);
411d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ParseObjCMethodDecl(Tok.getLocation(),
412d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          tok::minus,
41390ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                          MethodImplKind, false);
41405511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      continue;
41505511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
416e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
417e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
418294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
419e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
420e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
4211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
422bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
423e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
424e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
4251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
426b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    // Code completion within an Objective-C interface.
427b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    if (Tok.is(tok::code_completion)) {
42823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
429849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                            CurParsedObjCImpl? Sema::PCC_ObjCImplementation
430f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             : Sema::PCC_ObjCInterface);
4317d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
432b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    }
433b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor
434e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
435e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
4361fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
4371fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
4381fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
4391fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
4401fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
4412edf0a2520313cde900799b1eb9bd11c9c776afeSean Hunt      ParsedAttributesWithRange attrs(AttrFactory);
4427f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
443e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
444e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
446e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
447e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
448c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    if (Tok.is(tok::code_completion)) {
449a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCAtDirective(getCurScope());
4507d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
451c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    }
452c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
453a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
4541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
455a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
456782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setBegin(AtLoc);
457782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setEnd(Tok.getLocation());
458e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
459c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor    } else if (DirectiveKind == tok::objc_not_keyword) {
460c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      Diag(Tok, diag::err_objc_unknown_at);
461c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      SkipUntil(tok::semi);
462c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      continue;
463bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
465bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
466bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
467bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
468a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
469a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
470bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
471bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
472bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
473bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
474f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
475bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
476a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
477a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
47846d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
47946d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian    case tok::objc_implementation:
480df81c2c6ce37f4ae3c45dd01093b6274fa0b6692Fariborz Jahanian    case tok::objc_interface:
481d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      Diag(AtLoc, diag::err_objc_missing_end)
482d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen          << FixItHint::CreateInsertion(AtLoc, "@end\n");
483d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen      Diag(CDecl->getLocStart(), diag::note_objc_container_start)
484d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen          << (int) Actions.getObjCContainerKind();
48546d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      ConsumeToken();
48646d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      break;
48746d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
488a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
489a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
490a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
491bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
492e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
493bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
494a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
495bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
496a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
498a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
4994e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie      if (!getLangOpts().ObjC2)
500b321c0c0ba957d78475e72cebde4028fdaa00f8fChris Lattner        Diag(AtLoc, diag::err_objc_properties_require_objc2);
501f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
502e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
50377bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      SourceLocation LParenLoc;
5041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
50577bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      if (Tok.is(tok::l_paren)) {
50677bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian        LParenLoc = Tok.getLocation();
507a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        ParseObjCPropertyAttribute(OCDS);
50877bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian      }
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
510a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      ObjCPropertyCallback Callback(*this, allProperties,
51177bfb8b43ec3f7dee3a71f6e854b03ad29dab84fFariborz Jahanian                                    OCDS, AtLoc, LParenLoc, MethodImplKind);
512bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
513e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
514f66a0dda541cd859a928193efba6dc5d7ba8fe54Eli Friedman      ParsingDeclSpec DS(*this);
515bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
5161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5177da19ea50d4161fcda40d135735bcf450cabeb50John McCall      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
518a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
519f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
520294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
521bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
522bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
523bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
524c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
525a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.CodeCompleteObjCAtDirective(getCurScope());
5267d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return cutOffParsing();
527d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  } else if (Tok.isObjCAtKeyword(tok::objc_end)) {
528bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
529d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  } else {
530d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(Tok, diag::err_objc_missing_end)
531d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << FixItHint::CreateInsertion(Tok.getLocation(), "\n@end\n");
532d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(CDecl->getLocStart(), diag::note_objc_container_start)
533d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen        << (int) Actions.getObjCContainerKind();
534d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    AtEnd.setBegin(Tok.getLocation());
535d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    AtEnd.setEnd(Tok.getLocation());
536d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  }
5371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
538a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
539bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
540a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Actions.ActOnAtEnd(getCurScope(), AtEnd,
5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
542beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
543beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
544294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
545294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
546d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
547d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
548d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
549d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
550d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
551d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
552d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
553d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
554d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
555d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
556d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
557d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
558d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
559d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
560d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
561f85e193739c953358c865005855253af4f68a497John McCall///     atomic
562f85e193739c953358c865005855253af4f68a497John McCall///     strong
563f85e193739c953358c865005855253af4f68a497John McCall///     weak
564f85e193739c953358c865005855253af4f68a497John McCall///     unsafe_unretained
565d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
566a28948f34817476d02412fa204cae038e228c827Fariborz Jahanianvoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
5678ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
5684a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
5694a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
5701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
571cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
572ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
57323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
5747d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
575ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
576d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
578f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
579f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
5804a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor      T.consumeClose();
581f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
582f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
5831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
584156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
58692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
587e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
58892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
589e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
590f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("unsafe_unretained"))
591f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_unsafe_unretained);
59292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
593e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
59492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
595e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
596f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("strong"))
597f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_strong);
59892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
599e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
60092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
601e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
60245937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian    else if (II->isStr("atomic"))
60345937ae10a0f70f74508165aab4f2b63e18ea747Fariborz Jahanian      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
604f85e193739c953358c865005855253af4f68a497John McCall    else if (II->isStr("weak"))
605f85e193739c953358c865005855253af4f68a497John McCall      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_weak);
60692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
60742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      bool IsSetter = II->getNameStart()[0] == 's';
60842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
609e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
61042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
61142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        diag::err_objc_expected_equal_for_getter;
61242499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
61342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
614dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6164ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      if (Tok.is(tok::code_completion)) {
61742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        if (IsSetter)
618a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertySetter(getCurScope());
6194ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        else
620a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian          Actions.CodeCompleteObjCPropertyGetter(getCurScope());
6217d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
6224ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      }
6234ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
62442499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
62542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      SourceLocation SelLoc;
62642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
62742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
62842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (!SelIdent) {
62942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
63042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson          << IsSetter;
6318ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
6328ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
6338ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
63542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (IsSetter) {
6368ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
63742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setSetterName(SelIdent);
6381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
639e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian        if (ExpectAndConsume(tok::colon,
640e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian                             diag::err_expected_colon_after_setter_name, "",
641156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
6428ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
6438ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
6448ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
64542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setGetterName(SelIdent);
6468ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
647e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
648a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
649cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
650cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
651cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
653156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
654156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
656156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
657d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6594a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
660d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
661d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
6623536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
6631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
6643536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
665294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
666294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
667294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
668294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6694985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
6704985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
6714985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
672a28948f34817476d02412fa204cae038e228c827Fariborz JahanianDecl *Parser::ParseObjCMethodPrototype(tok::ObjCKeywordKind MethodImplKind,
67390ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                       bool MethodDefinition) {
674df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
675294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
677bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
678a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, MethodImplKind,
67990ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                    MethodDefinition);
6803536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
6812bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
682f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
683294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
684294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
685294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
686294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
687294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
688294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
689294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
690294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
691294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
692294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6932fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
694be74740cc246ce08d42804a684385a42eb814edbFariborz Jahanian
695ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
696ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
697ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
698afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampamp:
699afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampequal:
700afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::amp:
701afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipe:
702afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::tilde:
703afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaim:
704afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaimequal:
705afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipepipe:
706afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipeequal:
707afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caret:
708afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caretequal: {
7093846ca29a8cc1d376a4b695194c29952dbbfb544Fariborz Jahanian    std::string ThisTok(PP.getSpelling(Tok));
7103f6f51e28231f65de9c2dd150a2d757b2162cfa3Jordan Rose    if (isLetter(ThisTok[0])) {
711afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
712afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      Tok.setKind(tok::identifier);
713afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      SelectorLoc = ConsumeToken();
714afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      return II;
715afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    }
716afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    return 0;
717afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  }
718afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian
719ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
720ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
721ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
7229298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
723ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
724ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
725ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
726ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
727ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
728ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
729ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
730ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
731ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
732ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
733ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
734ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
735ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
736ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
737ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
738ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
739ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
740ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
741ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
742ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
743ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
744ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
745ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
746ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
747ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
748ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
749ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
750ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
751ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
752ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
753ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
754ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
755ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
756ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
757ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
758ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
759ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
760ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
761ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
762ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
763ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
764ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
765ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
766ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
767ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
768ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
769ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
770ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
771ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
772ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
773ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
774ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
775ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
776ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
777ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
778ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
779ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
780ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
781ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
782ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
783ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
784ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
785ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
786ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
787ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
788ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
7894b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
790ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
791d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
792294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
793294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
7940196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
7950196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
796335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
7973ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
7983ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
7993ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
8004e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  return (getLangOpts().ObjC2 && Tok.is(tok::identifier) &&
8015ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
8020196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
8030196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
804a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
805e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
806e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
807294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
808294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
809294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
810294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
811294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
812b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregorvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS,
813cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                        Declarator::TheContext Context) {
814cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert(Context == Declarator::ObjCParameterContext ||
815cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall         Context == Declarator::ObjCResultContext);
816cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
817e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
818d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    if (Tok.is(tok::code_completion)) {
819b77cab97f17f946744c920629ca17271308d8ebfDouglas Gregor      Actions.CodeCompleteObjCPassingType(getCurScope(), DS,
820cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                          Context == Declarator::ObjCParameterContext);
8217d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
822d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    }
823d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor
824cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
825e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
8261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
827e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
828e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
829a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
830e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
832a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
833e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
834b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      default: llvm_unreachable("Unknown decl qualifier");
835a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
836a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
837a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
838a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
839a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
840a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
841e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
842a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
843e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
844e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
845e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
846e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
8471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
848e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
849e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
850e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
851e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
852e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
853cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// Take all the decl attributes out of the given list and add
854cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// them to the given attribute set.
855cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCallstatic void takeDeclAttributes(ParsedAttributes &attrs,
856cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                               AttributeList *list) {
857cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  while (list) {
858cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    AttributeList *cur = list;
859cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    list = cur->getNext();
860cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
861cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    if (!cur->isUsedAsTypeAttr()) {
862cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // Clear out the next pointer.  We're really completely
863cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // destroying the internal invariants of the declarator here,
864cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // but it doesn't matter because we're done with it.
865cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      cur->setNext(0);
866cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      attrs.add(cur);
867cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    }
868cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  }
869cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall}
870cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
871cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// takeDeclAttributes - Take all the decl attributes from the given
872cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall/// declarator and add them to the given list.
873cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCallstatic void takeDeclAttributes(ParsedAttributes &attrs,
874cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                               Declarator &D) {
875cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // First, take ownership of all attributes.
876cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  attrs.getPool().takeAllFrom(D.getAttributePool());
877cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  attrs.getPool().takeAllFrom(D.getDeclSpec().getAttributePool());
878cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
879cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  // Now actually move the attributes over.
880cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  takeDeclAttributes(attrs, D.getDeclSpec().getAttributes().getList());
881cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  takeDeclAttributes(attrs, D.getAttributes());
882cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
883cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    takeDeclAttributes(attrs,
884cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                  const_cast<AttributeList*>(D.getTypeObject(i).getAttrs()));
885cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall}
886cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
887e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
888e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
889e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
890e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
891b77cab97f17f946744c920629ca17271308d8ebfDouglas GregorParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS,
892cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                     Declarator::TheContext context,
893cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                     ParsedAttributes *paramAttrs) {
894cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert(context == Declarator::ObjCParameterContext ||
895cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall         context == Declarator::ObjCResultContext);
896cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  assert((paramAttrs != 0) == (context == Declarator::ObjCParameterContext));
897cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
898df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9004a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
9014a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
9024a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
903e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
9049735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian  ObjCDeclContextSwitch ObjCDC(*this);
9059735c5e60027b26a809df19677ff16a4d13f1321Fariborz Jahanian
90619d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
907cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  ParseObjCTypeQualifierList(DS, context);
9084fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
909b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType Ty;
910809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
911cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // Parse an abstract declarator.
912cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    DeclSpec declSpec(AttrFactory);
913cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    declSpec.setObjCQualifiers(&DS);
914cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ParseSpecifierQualifierList(declSpec);
9156d1de1b8663fe652903bda2927b885d1ec0f2613Fariborz Jahanian    declSpec.SetRangeEnd(Tok.getLocation());
916cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    Declarator declarator(declSpec, context);
917cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ParseDeclarator(declarator);
918cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
919cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // If that's not invalid, extract a type.
920cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    if (!declarator.isInvalidType()) {
921cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      TypeResult type = Actions.ActOnTypeName(getCurScope(), declarator);
922cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      if (!type.isInvalid())
923cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall        Ty = type.get();
924cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall
925cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // If we're parsing a parameter, steal all the decl attributes
926cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      // and add them to the decl spec.
927cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      if (context == Declarator::ObjCParameterContext)
928cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall        takeDeclAttributes(*paramAttrs, declarator);
929cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    }
930cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall  } else if (context == Declarator::ObjCResultContext &&
931cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall             Tok.is(tok::identifier)) {
932e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (!Ident_instancetype)
933e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ident_instancetype = PP.getIdentifierInfo("instancetype");
934e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
935e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    if (Tok.getIdentifierInfo() == Ident_instancetype) {
936e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      Ty = Actions.ActOnObjCInstanceType(Tok.getLocation());
937e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor      ConsumeToken();
938e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor    }
939809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
940e97179c675b341927807c718be215c8d1aab8acbDouglas Gregor
9414a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
9424a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
9434a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
944e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
9451ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
9464a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
9474a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
9484a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
9494a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
9504a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor    T.consumeClose();
951294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
952f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
953294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
954294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
955294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
956294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
9574985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
958294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
9594985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
960294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
961294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
9621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
963294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
964294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
965294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
9667ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
9677ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
9687ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
9697ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
970294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9714985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
9724985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
973294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9744985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
9754985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
976294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9774985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
978294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
979294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
9807ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
9817ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
9827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
983d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
9842ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor                                  tok::TokenKind mType,
98590ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  tok::ObjCKeywordKind MethodImplKind,
98690ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                  bool MethodDefinition) {
9879257664568bf375b7790131a84d9a4fa30a5b7e3John McCall  ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
98854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
989e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
99023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
991a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       /*ReturnType=*/ ParsedType());
9927d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
9937d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
994e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
995e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
996e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
997b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType ReturnType;
998a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
999df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
1000cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    ReturnType = ParseObjCTypeName(DSRet, Declarator::ObjCResultContext, 0);
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10029e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // If attributes exist before the method, parse them.
10030b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  ParsedAttributes methodAttrs(AttrFactory);
10044e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC2)
10050b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
10069e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek
1007e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
100823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
1009a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                       ReturnType);
10107d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
10117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return 0;
1012e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
1013e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
10149e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // Now parse the selector.
1015bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
10162fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
1017e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
101884c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
101984c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
10201ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
10211ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
1022d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian    // Skip until we get a ; or @.
1023d30ec7076fef736754206ac87a4f2c67251cc4d8Fariborz Jahanian    SkipUntil(tok::at, true /*StopAtSemi*/, true /*don't consume*/);
1024d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1025e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
10261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10275f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
1028df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
1029ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
10304e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjC2)
10310b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(methodAttrs);
10321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1033ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
1034d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Result
10357f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian         = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1036a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                          mType, DSRet, ReturnType,
1037926df6cfabf3eaa4afc990c097fa4619b76a9b57Douglas Gregor                                          selLoc, Sel, 0,
10384f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          CParamInfo.data(), CParamInfo.size(),
10390b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          methodAttrs.getList(), MethodImplKind,
10400b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                          false, MethodDefinition);
104154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
104254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
1043ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
1044f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
10455f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
104611d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> KeyLocs;
10475f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
10483a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith  ParseScope PrototypeScope(this, Scope::FunctionPrototypeScope |
10493a2b7a18a4504f39e3ded0d2b5749c5c80b8b9b5Richard Smith                            Scope::FunctionDeclarationScope | Scope::DeclScope);
10500b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
10510b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  AttributePool allParamAttrs(AttrFactory);
1052ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
10530b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    ParsedAttributes paramAttrs(AttrFactory);
1054f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Sema::ObjCArgInfo ArgInfo;
10551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1056ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
1057df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
1058ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
1059ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
1060ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
1061ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
10621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1063b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ArgInfo.Type = ParsedType();
1064e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
1065cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec,
1066cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                       Declarator::ObjCParameterContext,
1067cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall                                       &paramAttrs);
1068e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
1069ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
1070cdda47faab5c2c61c239491a1a091e071ed3e38eJohn McCall    // Regardless, collect all the attributes we've parsed so far.
1071e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
10724e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (getLangOpts().ObjC2) {
10730b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      MaybeParseGNUAttributes(paramAttrs);
10740b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall      ArgInfo.ArgAttrs = paramAttrs.getList();
10757f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
10767ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
107740ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    // Code completion for the next piece of the selector.
107840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    if (Tok.is(tok::code_completion)) {
107940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.push_back(SelIdent);
108040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
108140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 mType == tok::minus,
108240ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/true,
108340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 ReturnType,
108440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.data(),
108540ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.size());
10867d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
10877d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
108840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    }
108940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor
1090df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1091ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
1092ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
10934985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
10941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1095e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
1096e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
1097ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1099e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
1100e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
110111d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis    KeyLocs.push_back(selLoc);
1102e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
11030b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    // Make sure the attributes persist.
11040b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    allParamAttrs.takeAllFrom(paramAttrs.getPool());
11050b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall
11061f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    // Code completion for the next piece of the selector.
11071f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    if (Tok.is(tok::code_completion)) {
11081f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
11091f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 mType == tok::minus,
111040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/false,
11111f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 ReturnType,
11121f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.data(),
11131f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.size());
11147d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
11157d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
11161f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    }
11171f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor
1118ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
111911d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis    SelIdent = ParseObjCSelectorPiece(selLoc);
1120b648cf02f13ae74ec346f8d22933298f9977e4e0Ted Kremenek    if (!SelIdent && Tok.isNot(tok::colon))
1121b648cf02f13ae74ec346f8d22933298f9977e4e0Ted Kremenek      break;
112203ebd3b4af9e4ab8e14a3f136d8a28df463f54ecFariborz Jahanian    if (!SelIdent) {
112308602359845e668194d111b794b8965358babfe6Fariborz Jahanian      SourceLocation ColonLoc = Tok.getLocation();
112408602359845e668194d111b794b8965358babfe6Fariborz Jahanian      if (PP.getLocForEndOfToken(ArgInfo.NameLoc) == ColonLoc) {
112503ebd3b4af9e4ab8e14a3f136d8a28df463f54ecFariborz Jahanian        Diag(ArgInfo.NameLoc, diag::warn_missing_selector_name) << ArgInfo.Name;
112603ebd3b4af9e4ab8e14a3f136d8a28df463f54ecFariborz Jahanian        Diag(ArgInfo.NameLoc, diag::note_missing_selector_name) << ArgInfo.Name;
112703ebd3b4af9e4ab8e14a3f136d8a28df463f54ecFariborz Jahanian        Diag(ColonLoc, diag::note_force_empty_selector_name) << ArgInfo.Name;
112808602359845e668194d111b794b8965358babfe6Fariborz Jahanian      }
112908602359845e668194d111b794b8965358babfe6Fariborz Jahanian    }
1130ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
1131ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
11321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1133335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
113456242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian  bool cStyleParamWarned = false;
1135ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
1136df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
1137ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
1138df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
1139335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
11404985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
1141ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
11424985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
114356242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian    if (!cStyleParamWarned) {
114456242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian      Diag(Tok, diag::warn_cstyle_param);
114556242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian      cStyleParamWarned = true;
114656242baaae6c45b24fbdd5bc56fe4ee516fa9e6bFariborz Jahanian    }
11470b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    DeclSpec DS(AttrFactory);
1148ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
11491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
1150ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
1151ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
11524f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    IdentifierInfo *ParmII = ParmDecl.getIdentifier();
1153d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
11544f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
11554f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    ParmDecl.getIdentifierLoc(),
11564f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    Param,
11574f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                   0));
11584985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
11591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11609c4bb2c08989265411925a04252fd4f93c26e3b1Cameron Esfahani  // FIXME: Add support for optional parameter list...
1161e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
11624e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().ObjC2)
11630b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall    MaybeParseGNUAttributes(methodAttrs);
11647f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1165a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  if (KeyIdents.size() == 0)
1166d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
11677f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
1168ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
1169ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
1170d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Result
11717f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian       = Actions.ActOnMethodDeclaration(getCurScope(), mLoc, Tok.getLocation(),
1172a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                        mType, DSRet, ReturnType,
117311d77169555480ee0a04c6e5bc390d8fde41175dArgyrios Kyrtzidis                                        KeyLocs, Sel, &ArgInfos[0],
11744f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        CParamInfo.data(), CParamInfo.size(),
11750b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall                                        methodAttrs.getList(),
117690ba78c64d0c24cfbc1bf88728db9775d44d7f9fFariborz Jahanian                                        MethodImplKind, isVariadic, MethodDefinition);
11777f53253223b29d77f1e9c5c0ce93a19932761b77Fariborz Jahanian
117854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
117954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
1180294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
1181294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
1182dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
1183dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
1184dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
11857caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
11865f9e272e632e951b1efe824cd16acb4d96077930Chris LattnerParseObjCProtocolReferences(SmallVectorImpl<Decl *> &Protocols,
11875f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                            SmallVectorImpl<SourceLocation> &ProtocolLocs,
118871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
118971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
1190e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
119271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11945f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierLocPair, 8> ProtocolIdents;
11951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1196e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
119755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
119855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
119955385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
12007d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
12017d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return true;
120255385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
120355385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
1204e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
1205e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
1206e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
1207e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
1208e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
1209e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1210e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
121171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
1212e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
12131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1214e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
1215e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
1216e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
1217e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
12181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1219e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
1220b707a4762fcc47c12b5f487856ba0781c9399295Nico Weber  if (ParseGreaterThanInTemplateList(EndLoc, /*ConsumeLastToken=*/true))
1221e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1223e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
1224e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
1225e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
1226e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
1227e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
1228e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
1229e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
12309bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// \brief Parse the Objective-C protocol qualifiers that follow a typename
12319bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// in a decl-specifier-seq, starting at the '<'.
123246f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregorbool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
12339bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
12344e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(getLangOpts().ObjC1 && "Protocol qualifiers only exist in Objective-C");
12359bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  SourceLocation LAngleLoc, EndProtoLoc;
12365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolDecl;
12375f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
123846f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
123946f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor                                            LAngleLoc, EndProtoLoc);
12409bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
12419bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor                           ProtocolLocs.data(), LAngleLoc);
12429bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  if (EndProtoLoc.isValid())
12439bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(EndProtoLoc);
124446f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  return Result;
12459bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor}
12469bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
12474cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanianvoid Parser::HelperActionsForIvarDeclarations(Decl *interfaceDecl, SourceLocation atLoc,
12484cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian                                 BalancedDelimiterTracker &T,
12494cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian                                 SmallVectorImpl<Decl *> &AllIvarDecls,
12504cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian                                 bool RBraceMissing) {
12514cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian  if (!RBraceMissing)
12524cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian    T.consumeClose();
12534cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian
12544cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian  Actions.ActOnObjCContainerStartDefinition(interfaceDecl);
12554cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian  Actions.ActOnLastBitfield(T.getCloseLocation(), AllIvarDecls);
12564cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian  Actions.ActOnObjCContainerFinishDefinition();
12574cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian  // Call ActOnFields() even if we don't have any decls. This is useful
12584cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian  // for code rewriting tools that need to be aware of the empty list.
12594cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian  Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
12604cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian                      AllIvarDecls,
12614cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian                      T.getOpenLocation(), T.getCloseLocation(), 0);
12624cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian}
12639bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
1264dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
1265dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
1266dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1267dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
1268dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
1269dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
1270dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
1271dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
1272dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
1273dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
1274dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1275dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
1276dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
1277dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
1278dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
1279ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
1280dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1281dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
12821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
1283dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1284d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
128583c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                             tok::ObjCKeywordKind visibility,
128660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
1287df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
12885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 32> AllIvarDecls;
1289a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
12901a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
12913a387441ae339363ee5b254658f295e97bd9e913Argyrios Kyrtzidis  ObjCDeclContextSwitch ObjCDC(*this);
129272de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
12934a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_brace);
12944a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
1295ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
1296df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1297ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1299ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
1300df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
13014b0e6f1da341510c1ad83eaf4c836f3134d0156aRichard Trieu      ConsumeExtraSemi(InstanceVariableList);
1302ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
1303ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1305ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
1306df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
1307ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
1308c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1309c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
131023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(getCurScope());
13117d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return cutOffParsing();
1312c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
1313c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1314861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
1315ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
1316ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
1317ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
1318ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
1319861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
1320ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
1322695031cd78fe789efbb88504372413432ae2feaaFariborz Jahanian
1323695031cd78fe789efbb88504372413432ae2feaaFariborz Jahanian      case tok::objc_end:
1324695031cd78fe789efbb88504372413432ae2feaaFariborz Jahanian        Diag(Tok, diag::err_objc_unexpected_atend);
13254cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian        Tok.setLocation(Tok.getLocation().getLocWithOffset(-1));
13264cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian        Tok.setKind(tok::at);
13274cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian        Tok.setLength(1);
13284cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian        PP.EnterToken(Tok);
13294cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian        HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
13304cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian                                         T, AllIvarDecls, true);
13314cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian        return;
1332695031cd78fe789efbb88504372413432ae2feaaFariborz Jahanian
1333695031cd78fe789efbb88504372413432ae2feaaFariborz Jahanian      default:
1334695031cd78fe789efbb88504372413432ae2feaaFariborz Jahanian        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1335695031cd78fe789efbb88504372413432ae2feaaFariborz Jahanian        continue;
1336ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1337ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
13381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1339c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
134023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
1341f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                       Sema::PCC_ObjCInstanceVariableList);
13427d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return cutOffParsing();
1343c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1344c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1345bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1346bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1347d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *IDecl;
1348bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
13495f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner      SmallVectorImpl<Decl *> &AllIvarDecls;
1350bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1351d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
13525f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                       SmallVectorImpl<Decl *> &AllIvarDecls) :
1353bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1354bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1355bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1356dcdff46dd8e6d749283fe6c43adfcfe780c1d562Eli Friedman      void invoke(ParsingFieldDeclarator &FD) {
1357a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        P.Actions.ActOnObjCContainerStartDefinition(IDecl);
1358bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1359d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        Decl *Field
136023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          = P.Actions.ActOnIvar(P.getCurScope(),
1361bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1362a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian                                FD.D, FD.BitfieldSize, visibility);
136310af87932fe4bffad539b24d512a33a1803daeaeFariborz Jahanian        P.Actions.ActOnObjCContainerFinishDefinition();
13640bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian        if (Field)
13650bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian          AllIvarDecls.push_back(Field);
1366f66a0dda541cd859a928193efba6dc5d7ba8fe54Eli Friedman        FD.complete(Field);
1367bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1368bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1369d097be8f81fbf4ed96ac10bae18562dd8202666bFariborz Jahanian
1370e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
1371f66a0dda541cd859a928193efba6dc5d7ba8fe54Eli Friedman    ParsingDeclSpec DS(*this);
1372bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
13731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1374df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1375ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1376ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1377ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1378ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1379ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1380ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1381ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
13824cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian  HelperActionsForIvarDeclarations(interfaceDecl, atLoc,
13834cc0cf1a54541b26b258c45cd7a3c80c9401eaadFariborz Jahanian                                   T, AllIvarDecls, false);
1384ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
13855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1386dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1387dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1388dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1389dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1390dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1391dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
139217d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///     \@protocol identifier
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
13941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
139517d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///     \@end
1396dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1397dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
139817d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///     \@protocol identifier-list ';'
1399dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
140017d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///   "\@protocol identifier ;" should be resolved as "\@protocol
14013536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1402dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1403bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas GregorParser::DeclGroupPtrTy
1404bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas GregorParser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1405bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor                                       ParsedAttributes &attrs) {
1406861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
14077ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
14087ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1410083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
141123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(getCurScope());
14127d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1413bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
1414083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1415083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1416e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber  MaybeSkipAttributes(tok::objc_protocol);
14179f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber
1418df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
14197ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1420bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
14217ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
14227ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
14237ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
14247ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
14251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1426df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
14277caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
14287ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
14291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
14307f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
14317ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
14321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
143390ec96f3026480fa41057b05d58f338aed585f62Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
143490ec96f3026480fa41057b05d58f338aed585f62Erik Verbruggen
1435df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
14365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner    SmallVector<IdentifierLocPair, 8> ProtocolRefs;
14377caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
14387caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
14397ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
14407ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
14417ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1442df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
14437ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
14447ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1445bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor        return DeclGroupPtrTy();
14467ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
14477caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
14487caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
14497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1451df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
14527ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
14537ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
14547ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
14557ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1456bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor      return DeclGroupPtrTy();
14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1458e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1460bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
14617f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
14627caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
14631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14647ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
146571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
14667caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
14675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<Decl *, 8> ProtocolRefs;
14685f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 8> ProtocolLocs;
14697caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
147071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
147171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1472bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor    return DeclGroupPtrTy();
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1474d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ProtoType =
1475e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1476beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1477beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
147818df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
14797f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                        EndProtoLoc, attrs.getList());
1480a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
14812f64cfe19e8bf6b6ba1660e38da8c421440457feFariborz Jahanian  ParseObjCInterfaceDeclList(tok::objc_protocol, ProtoType);
1482bd9482d859a74bf2c45ef8b8aedec61c0e1c8374Douglas Gregor  return Actions.ConvertDeclToDeclGroup(ProtoType);
1483dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1484dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1485dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1486dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1487dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1488dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1489dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1490dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1491dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1492dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1493dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1494dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1495849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::DeclGroupPtrTy
1496849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::ParseObjCAtImplementationDeclaration(SourceLocation AtLoc) {
1497ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1498ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1499d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen  CheckNestedObjCContexts(AtLoc);
1500ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
15011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15023b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
15033b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
150423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(getCurScope());
15057d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
1506849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return DeclGroupPtrTy();
15073b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
15083b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1509e145bfd06c7eaf0cff0f7d825d1c6244127c26dfNico Weber  MaybeSkipAttributes(tok::objc_implementation);
15109f4f5f1ca1e1967daf64d5422717c9b1e75eeb9eNico Weber
1511df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1512ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1513849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return DeclGroupPtrTy();
1514ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1515ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1516ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1517ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1518849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Decl *ObjCImpDecl = 0;
15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1521ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1522dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin    ConsumeParen();
1523ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1524ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
15251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
152633ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
152723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
15287d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
1529849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
153033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
153133ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1532df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1533ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1534ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1535ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1536ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1537849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1539df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1540ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1541ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1542849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      return DeclGroupPtrTy();
1543ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1544ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1545e4bb74973cfdf0c82aca2088ccfae5e61727c846Fariborz Jahanian    if (Tok.is(tok::less)) { // we have illegal '<' try to recover
1546e4bb74973cfdf0c82aca2088ccfae5e61727c846Fariborz Jahanian      Diag(Tok, diag::err_unexpected_protocol_qualifier);
1547e4bb74973cfdf0c82aca2088ccfae5e61727c846Fariborz Jahanian      AttributeFactory attr;
1548e4bb74973cfdf0c82aca2088ccfae5e61727c846Fariborz Jahanian      DeclSpec DS(attr);
1549e4bb74973cfdf0c82aca2088ccfae5e61727c846Fariborz Jahanian      (void)ParseObjCProtocolQualifiers(DS);
1550e4bb74973cfdf0c82aca2088ccfae5e61727c846Fariborz Jahanian    }
1551849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImpDecl = Actions.ActOnStartCategoryImplementation(
1552d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen                                    AtLoc, nameId, nameLoc, categoryId,
15538f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1554a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1555849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  } else {
1556849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    // We have a class implementation
1557849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    SourceLocation superClassLoc;
1558849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    IdentifierInfo *superClassId = 0;
1559849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (Tok.is(tok::colon)) {
1560849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      // We have a super class
1561849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ConsumeToken();
1562849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      if (Tok.isNot(tok::identifier)) {
1563849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        Diag(Tok, diag::err_expected_ident); // missing super class name.
1564849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        return DeclGroupPtrTy();
1565849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      }
1566849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      superClassId = Tok.getIdentifierInfo();
1567849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      superClassLoc = ConsumeToken(); // Consume super class name
1568ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1569849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImpDecl = Actions.ActOnStartClassImplementation(
1570849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                                    AtLoc, nameId, nameLoc,
1571849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis                                    superClassId, superClassLoc);
1572849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1573849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (Tok.is(tok::l_brace)) // we have ivars
1574849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ParseObjCClassInstanceVariables(ObjCImpDecl, tok::objc_private, AtLoc);
157551c30afe7e2eb9273dd706229f42d87524fb8660Fariborz Jahanian    else if (Tok.is(tok::less)) { // we have illegal '<' try to recover
157651c30afe7e2eb9273dd706229f42d87524fb8660Fariborz Jahanian      Diag(Tok, diag::err_unexpected_protocol_qualifier);
157751c30afe7e2eb9273dd706229f42d87524fb8660Fariborz Jahanian      // try to recover.
157851c30afe7e2eb9273dd706229f42d87524fb8660Fariborz Jahanian      AttributeFactory attr;
157951c30afe7e2eb9273dd706229f42d87524fb8660Fariborz Jahanian      DeclSpec DS(attr);
158051c30afe7e2eb9273dd706229f42d87524fb8660Fariborz Jahanian      (void)ParseObjCProtocolQualifiers(DS);
158151c30afe7e2eb9273dd706229f42d87524fb8660Fariborz Jahanian    }
1582ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1583849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(ObjCImpDecl);
15841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1585849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  SmallVector<Decl *, 8> DeclsInGroup;
1586a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian
1587849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  {
1588849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ObjCImplParsingDataRAII ObjCImplParsing(*this, ObjCImpDecl);
1589849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    while (!ObjCImplParsing.isFinished() && Tok.isNot(tok::eof)) {
1590849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      ParsedAttributesWithRange attrs(AttrFactory);
15914e24f0f711e2c9fde79f19fa1c80deaab3f3b356Richard Smith      MaybeParseCXX11Attributes(attrs);
1592849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      MaybeParseMicrosoftAttributes(attrs);
1593849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      if (DeclGroupPtrTy DGP = ParseExternalDeclaration(attrs)) {
1594849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        DeclGroupRef DG = DGP.get();
1595849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis        DeclsInGroup.append(DG.begin(), DG.end());
1596849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      }
1597849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    }
1598849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1599849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1600644af7b50bd0541468b45c197f5b637e934d48a0Argyrios Kyrtzidis  return Actions.ActOnFinishObjCImplementation(ObjCImpDecl, DeclsInGroup);
16015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
160260fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1603140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::DeclGroupPtrTy
1604140ab234c23f392d5422691c5de1ee3c15026defFariborz JahanianParser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1605ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1606ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1607ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1608849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (CurParsedObjCImpl)
1609849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    CurParsedObjCImpl->finish(atEnd);
16108697d308c1bdd50e5c45757ac11be701c26e9e97Fariborz Jahanian  else
1611782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1612d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen    Diag(atEnd.getBegin(), diag::err_expected_objc_container);
1613849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  return DeclGroupPtrTy();
16145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1615e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
1616849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios KyrtzidisParser::ObjCImplParsingDataRAII::~ObjCImplParsingDataRAII() {
1617849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (!Finished) {
1618849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    finish(P.Tok.getLocation());
1619849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (P.Tok.is(tok::eof)) {
1620849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      P.Diag(P.Tok, diag::err_objc_missing_end)
1621849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis          << FixItHint::CreateInsertion(P.Tok.getLocation(), "\n@end\n");
1622849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis      P.Diag(Dcl->getLocStart(), diag::note_objc_container_start)
1623849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis          << Sema::OCK_Implementation;
1624849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    }
1625849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
1626849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.CurParsedObjCImpl = 0;
1627849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(LateParsedObjCMethods.empty());
1628849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis}
1629d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
1630849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidisvoid Parser::ObjCImplParsingDataRAII::finish(SourceRange AtEnd) {
1631849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  assert(!Finished);
1632849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.Actions.DefaultSynthesizeProperties(P.getCurScope(), Dcl);
1633849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
16346c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian    P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
16356c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                               true/*Methods*/);
1636d64251fd56577dd5c78903454632361e094c6dc1Erik Verbruggen
1637849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  P.Actions.ActOnAtEnd(P.getCurScope(), AtEnd);
163863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
16396c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  if (HasCFunction)
16406c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian    for (size_t i = 0; i < LateParsedObjCMethods.size(); ++i)
16416c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian      P.ParseLexedObjCMethodDefs(*LateParsedObjCMethods[i],
16426c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                                 false/*c-functions*/);
16436c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
1644849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  /// \brief Clear and free the cached objc methods.
16452fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  for (LateParsedObjCMethodContainer::iterator
16462fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis         I = LateParsedObjCMethods.begin(),
16472fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis         E = LateParsedObjCMethods.end(); I != E; ++I)
16482fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis    delete *I;
16492fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis  LateParsedObjCMethods.clear();
1650849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
1651849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Finished = true;
16522fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis}
16532fea2242fe7e7c37df1e96316616febeaf4e29ebArgyrios Kyrtzidis
1654e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1655e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1656e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1657d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1658e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1659e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1660e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1661df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1662e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1663d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1664e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1665243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1666243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1667df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1668e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1669d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1670e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1671243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1672243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1673e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after,
1674e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor                   "@compatibility_alias");
1675de01b7a6b4df27555ff7b25e51c0d3df29e3a0cfRichard Smith  return Actions.ActOnCompatibilityAlias(atLoc, aliasId, aliasLoc,
1676de01b7a6b4df27555ff7b25e51c0d3df29e3a0cfRichard Smith                                         classId, classLoc);
16775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1679ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1680ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1681ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1682ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1683ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1684ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1685ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1686ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1687ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1688ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1689ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1690d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1691ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1692df1fdfd093e4e8d07813b5d86c22bb50a82e6714Fariborz Jahanian         "ParseObjCPropertySynthesize(): Expected '@synthesize'");
1693dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume synthesize
16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1695b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1696322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
1697a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
16987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
16997d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1700322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1701322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1702b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1703b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1704b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1705d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1706b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1707b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1708f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1709f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1710f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1711a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor    SourceLocation propertyIvarLoc;
1712df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1713ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1714ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1715322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1716322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
1717a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian        Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId);
17187d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
17197d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        return 0;
1720322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1721322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1722df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1723ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1724ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1725ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1726f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1727a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor      propertyIvarLoc = ConsumeToken(); // consume ivar-name
1728ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1729a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true,
1730a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, propertyIvar, propertyIvarLoc);
1731df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1732ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1733ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1734ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1735e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@synthesize");
1736d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1737ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1738ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1739ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1740ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1741ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1742ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1743ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1744ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1745ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1746d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1747ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1748ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1749dec0984fce504a39a7f085774fb67cfd9957be58Jeffrey Yasskin  ConsumeToken(); // consume dynamic
1750424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1751424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
1752a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian      Actions.CodeCompleteObjCPropertyDefinition(getCurScope());
17537d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      cutOffParsing();
17547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis      return 0;
1755424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1756424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1757424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1758424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1759424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1760d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1761424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1762424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1763c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1764c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1765a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false,
1766a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, 0, SourceLocation());
1767c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1768df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1769ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1770ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1771ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1772e6bf90aec0044342ffccd13201b8a3a31a985a4bDouglas Gregor  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@dynamic");
1773d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1774ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
17751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1776397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1777397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1778397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
177960d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
178060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res;
1781397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1782df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
178339f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
17840e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1785397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
178643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1787397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1788397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
178902418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  // consume ';'
179002418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
17919ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
1792397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1793397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1794c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
179578a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1796c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
179760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
179843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1799fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1800fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
18011ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
180243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1803fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
180407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
180507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // The operand is surrounded with parentheses.
1806fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
180707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ExprResult operand(ParseExpression());
180807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
180907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (Tok.is(tok::r_paren)) {
181007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    ConsumeParen();  // ')'
181107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  } else {
181207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
181307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_rparen);
181407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
181507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    // Skip forward until we see a left brace, but don't consume it.
181607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    SkipUntil(tok::l_brace, true, true);
1817fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
181807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
181907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Require a compound statement.
182078a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
182107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    if (!operand.isInvalid())
182207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall      Diag(Tok, diag::err_expected_lbrace);
182343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
182478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
18253ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
182607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Check the @synchronized operand now.
182707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (!operand.isInvalid())
182807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    operand = Actions.ActOnObjCAtSynchronizedOperand(atLoc, operand.take());
182907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
183007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // Parse the compound statement within a new scope.
183107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  ParseScope bodyScope(this, Scope::DeclScope);
183207524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  StmtResult body(ParseCompoundStatementBody());
183307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  bodyScope.Exit();
183407524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
183507524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // If there was a semantic or parse error earlier with the
183607524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  // operand, fail now.
183707524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (operand.isInvalid())
183807524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    return StmtError();
183907524039dce5c820f111a1b3f772b4261f004b4aJohn McCall
184007524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  if (body.isInvalid())
184107524039dce5c820f111a1b3f772b4261f004b4aJohn McCall    body = Actions.ActOnNullStmt(Tok.getLocation());
18420e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
184307524039dce5c820f111a1b3f772b4261f004b4aJohn McCall  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, operand.get(), body.get());
1844c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1845c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1846397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1847397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1848397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1849397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1850397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1851397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1852397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1853397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1854397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1855397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1856397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
185760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1858397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
185943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1860397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1861df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
18621ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
186343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1864397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
18654e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer  StmtVector CatchStmts;
186660d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult FinallyStmt;
18678935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
186860d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult TryBody(ParseCompoundStatementBody());
18698935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
18700e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1871bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1872a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1873df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
18746b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
18756b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
18766b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
18776b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
18786b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
18796b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
18806b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
18810e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1882161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1883cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1884d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *FirstPart = 0;
18853b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1886df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1887397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1888e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1889df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
18900b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall          DeclSpec DS(AttrFactory);
1891397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
189217b6399f8461c5b7e1c6f367b0a0dde49f921240Argyrios Kyrtzidis          Declarator ParmDecl(DS, Declarator::ObjCCatchContext);
18937ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
18947ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
18954e6c0d19b7c072758922cf80525a81aeefc6e64bDouglas Gregor          // Inform the actions module about the declarator, so it
18967ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
189723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
189864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1899397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
19001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
190193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
19021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
190393a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
190493a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
190593a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
190693a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
190793a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
190860d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult CatchBody(true);
1909c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1910c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1911c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1912c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
19130e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
19143b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
19158f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
191660d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
19178f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              RParenLoc,
19188f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              FirstPart,
19199ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                              CatchBody.take());
19208f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        if (!Catch.isInvalid())
19218f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor          CatchStmts.push_back(Catch.release());
19228f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
192364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
19241ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
19251ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
192643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1927397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1928397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
19296b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
19306b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
193164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
19328935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
19330e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
193460d7b3a319d84d688752be3870615ac0f111fb16John McCall      StmtResult FinallyBody(true);
1935c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1936c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1937c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1938c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
19390e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1940161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
19410e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
19429ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                   FinallyBody.take());
1943397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1944397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1945397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1946397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1947bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1948397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
194943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1950bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
19518f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
19529ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
19533fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer                                    CatchStmts,
19549ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                    FinallyStmt.take());
1955397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1956ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1957f85e193739c953358c865005855253af4f68a497John McCall/// objc-autoreleasepool-statement:
1958f85e193739c953358c865005855253af4f68a497John McCall///   @autoreleasepool compound-statement
1959f85e193739c953358c865005855253af4f68a497John McCall///
1960f85e193739c953358c865005855253af4f68a497John McCallStmtResult
1961f85e193739c953358c865005855253af4f68a497John McCallParser::ParseObjCAutoreleasePoolStmt(SourceLocation atLoc) {
1962f85e193739c953358c865005855253af4f68a497John McCall  ConsumeToken(); // consume autoreleasepool
1963f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isNot(tok::l_brace)) {
1964f85e193739c953358c865005855253af4f68a497John McCall    Diag(Tok, diag::err_expected_lbrace);
1965f85e193739c953358c865005855253af4f68a497John McCall    return StmtError();
1966f85e193739c953358c865005855253af4f68a497John McCall  }
1967f85e193739c953358c865005855253af4f68a497John McCall  // Enter a scope to hold everything within the compound stmt.  Compound
1968f85e193739c953358c865005855253af4f68a497John McCall  // statements can always hold declarations.
1969f85e193739c953358c865005855253af4f68a497John McCall  ParseScope BodyScope(this, Scope::DeclScope);
1970f85e193739c953358c865005855253af4f68a497John McCall
1971f85e193739c953358c865005855253af4f68a497John McCall  StmtResult AutoreleasePoolBody(ParseCompoundStatementBody());
1972f85e193739c953358c865005855253af4f68a497John McCall
1973f85e193739c953358c865005855253af4f68a497John McCall  BodyScope.Exit();
1974f85e193739c953358c865005855253af4f68a497John McCall  if (AutoreleasePoolBody.isInvalid())
1975f85e193739c953358c865005855253af4f68a497John McCall    AutoreleasePoolBody = Actions.ActOnNullStmt(Tok.getLocation());
1976f85e193739c953358c865005855253af4f68a497John McCall  return Actions.ActOnObjCAutoreleasePoolStmt(atLoc,
1977f85e193739c953358c865005855253af4f68a497John McCall                                                AutoreleasePoolBody.take());
1978f85e193739c953358c865005855253af4f68a497John McCall}
1979f85e193739c953358c865005855253af4f68a497John McCall
19806c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian/// StashAwayMethodOrFunctionBodyTokens -  Consume the tokens and store them
19816c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian/// for later parsing.
19826c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanianvoid Parser::StashAwayMethodOrFunctionBodyTokens(Decl *MDecl) {
19836c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  LexedMethod* LM = new LexedMethod(this, MDecl);
19846c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  CurParsedObjCImpl->LateParsedObjCMethods.push_back(LM);
19856c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  CachedTokens &Toks = LM->Toks;
19869e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian  // Begin by storing the '{' or 'try' or ':' token.
19876c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  Toks.push_back(Tok);
19882eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  if (Tok.is(tok::kw_try)) {
19892eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian    ConsumeToken();
1990dbd6945b7f4397f44fe6a1f1acf62ab6671e84f5Fariborz Jahanian    if (Tok.is(tok::colon)) {
1991dbd6945b7f4397f44fe6a1f1acf62ab6671e84f5Fariborz Jahanian      Toks.push_back(Tok);
1992dbd6945b7f4397f44fe6a1f1acf62ab6671e84f5Fariborz Jahanian      ConsumeToken();
1993dbd6945b7f4397f44fe6a1f1acf62ab6671e84f5Fariborz Jahanian      while (Tok.isNot(tok::l_brace)) {
1994dbd6945b7f4397f44fe6a1f1acf62ab6671e84f5Fariborz Jahanian        ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
1995dbd6945b7f4397f44fe6a1f1acf62ab6671e84f5Fariborz Jahanian        ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
1996dbd6945b7f4397f44fe6a1f1acf62ab6671e84f5Fariborz Jahanian      }
1997dbd6945b7f4397f44fe6a1f1acf62ab6671e84f5Fariborz Jahanian    }
19989e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian    Toks.push_back(Tok); // also store '{'
19999e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian  }
20009e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian  else if (Tok.is(tok::colon)) {
20019e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian    ConsumeToken();
20029e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian    while (Tok.isNot(tok::l_brace)) {
20039e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian      ConsumeAndStoreUntil(tok::l_paren, Toks, /*StopAtSemi=*/false);
20049e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian      ConsumeAndStoreUntil(tok::r_paren, Toks, /*StopAtSemi=*/false);
20059e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian    }
20062eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian    Toks.push_back(Tok); // also store '{'
20072eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  }
20086c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  ConsumeBrace();
20096c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // Consume everything up to (and including) the matching right brace.
20106c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
20112eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  while (Tok.is(tok::kw_catch)) {
20122eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian    ConsumeAndStoreUntil(tok::l_brace, Toks, /*StopAtSemi=*/false);
20132eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian    ConsumeAndStoreUntil(tok::r_brace, Toks, /*StopAtSemi=*/false);
20142eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  }
20156c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian}
20166c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
20173536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
2018ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
2019d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDefinition() {
2020a28948f34817476d02412fa204cae038e228c827Fariborz Jahanian  Decl *MDecl = ParseObjCMethodPrototype();
20211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2022f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
2023f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      "parsing Objective-C method");
20241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2025ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
2026209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
2027849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    if (CurParsedObjCImpl) {
2028496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
2029849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
2030496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
2031ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
2032209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
2033ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
2034409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
2035df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
2036da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
20371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2038409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
2039409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
20401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2041409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
2042409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
2043d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
2044ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
2045849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
2046849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  if (!MDecl) {
2047849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    ConsumeBrace();
2048849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    SkipUntil(tok::r_brace, /*StopAtSemi=*/false);
2049849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis    return 0;
2050849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  }
2051849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis
2052140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Allow the rest of sema to find private method decl implementations.
2053849639d8b548519cc5a00c0c9253f0c0d525060dArgyrios Kyrtzidis  Actions.AddAnyMethodToGlobalPool(MDecl);
2054c9b970996b8af41754a99a9cec15a9d401962ba1Fariborz Jahanian  assert (CurParsedObjCImpl
2055c9b970996b8af41754a99a9cec15a9d401962ba1Fariborz Jahanian          && "ParseObjCMethodDefinition - Method out of @implementation");
2056c9b970996b8af41754a99a9cec15a9d401962ba1Fariborz Jahanian  // Consume the tokens and store them for later parsing.
2057c9b970996b8af41754a99a9cec15a9d401962ba1Fariborz Jahanian  StashAwayMethodOrFunctionBodyTokens(MDecl);
205871c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
20595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
20605508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
206160d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
20629a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
206323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtStatement(getCurScope());
20647d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
20659a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
20665d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
20675d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
20685d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
20696b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
20705d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
20715d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
207264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
20735d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
20745d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
207564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
2076f85e193739c953358c865005855253af4f68a497John McCall
2077f85e193739c953358c865005855253af4f68a497John McCall  if (Tok.isObjCAtKeyword(tok::objc_autoreleasepool))
2078f85e193739c953358c865005855253af4f68a497John McCall    return ParseObjCAutoreleasePoolStmt(AtLoc);
20795d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
208060d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
20810e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
208264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
208364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
208464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
208564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
208643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
208764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
20885d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
208964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
20909ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
2091419563768ef4929a622d7c2b066856e82901bb91Richard Smith  return Actions.ActOnExprStmt(Res);
209264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
209364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
209460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
20955508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
20969a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
209723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtExpression(getCurScope());
20987d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
20999a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
21009a0c85e640a08174569a303db22981612f05d385Douglas Gregor
2101ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::minus:
2102ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::plus: {
2103ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    tok::TokenKind Kind = Tok.getKind();
2104ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    SourceLocation OpLoc = ConsumeToken();
2105ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2106ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (!Tok.is(tok::numeric_constant)) {
2107ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      const char *Symbol = 0;
2108ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      switch (Kind) {
2109ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      case tok::minus: Symbol = "-"; break;
2110ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      case tok::plus: Symbol = "+"; break;
2111ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      default: llvm_unreachable("missing unary operator case");
2112ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      }
2113ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      Diag(Tok, diag::err_nsnumber_nonliteral_unary)
2114ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        << Symbol;
2115ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return ExprError();
2116ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2117ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2118ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2119ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Lit.isInvalid()) {
21203fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      return Lit;
2121ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
21228b8d9534f185743fb72c59d4e735794e6e0f4335Benjamin Kramer    ConsumeToken(); // Consume the literal token.
2123ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2124ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    Lit = Actions.ActOnUnaryOp(getCurScope(), OpLoc, Kind, Lit.take());
2125ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Lit.isInvalid())
21263fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      return Lit;
2127ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2128ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(
2129ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek             Actions.BuildObjCNumericLiteral(AtLoc, Lit.take()));
2130ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
2131ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2132b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
2133b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
21341d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
2135ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2136ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::char_constant:
2137ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCCharacterLiteral(AtLoc));
2138ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2139ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::numeric_constant:
2140ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCNumericLiteral(AtLoc));
2141ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2142ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::kw_true:  // Objective-C++, etc.
2143ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::kw___objc_yes: // c/c++/objc/objc++ __objc_yes
2144ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, true));
2145ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::kw_false: // Objective-C++, etc.
2146ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::kw___objc_no: // c/c++/objc/objc++ __objc_no
2147ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCBooleanLiteral(AtLoc, false));
2148ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2149ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::l_square:
2150ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Objective-C array literal
2151ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCArrayLiteral(AtLoc));
2152ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2153ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  case tok::l_brace:
2154ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Objective-C dictionary literal
2155ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    return ParsePostfixExpressionSuffix(ParseObjCDictionaryLiteral(AtLoc));
2156ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2157eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  case tok::l_paren:
2158eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard    // Objective-C boxed expression
2159eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard    return ParsePostfixExpressionSuffix(ParseObjCBoxedExpr(AtLoc));
2160eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard
2161b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
21624fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
21631d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
21642f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
21654fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
21664fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
21671d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
21684fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
21691d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
21704fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
21711d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
21726749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian      default: {
21736749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        const char *str = 0;
21746749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        if (GetLookAheadToken(1).is(tok::l_brace)) {
21756749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          char ch = Tok.getIdentifierInfo()->getNameStart()[0];
21766749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          str =
21776749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian            ch == 't' ? "try"
21786749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian                      : (ch == 'f' ? "finally"
21796749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian                                   : (ch == 'a' ? "autoreleasepool" : 0));
21806749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        }
21816749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        if (str) {
21826749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          SourceLocation kwLoc = Tok.getLocation();
21836749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          return ExprError(Diag(AtLoc, diag::err_unexpected_at) <<
21846749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian                             FixItHint::CreateReplacement(kwLoc, str));
21856749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        }
21866749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian        else
21876749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian          return ExprError(Diag(AtLoc, diag::err_unexpected_at));
21886749ae1dbcc9e45ade21b05c6542a6354903ad77Fariborz Jahanian      }
21894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
21905508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
21915508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
21925508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
2193e23fb90712233bdfa04387e48b54a7168e23cb3eDmitri Gribenko/// \brief Parse the receiver of an Objective-C++ message send.
21946aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
21956aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// This routine parses the receiver of a message send in
21966aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// Objective-C++ either as a type or as an expression. Note that this
21976aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// routine must not be called to parse a send to 'super', since it
21986aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// has no way to return such a result.
21996aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
22006aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param IsExpr Whether the receiver was parsed as an expression.
22016aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
22026aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param TypeOrExpr If the receiver was parsed as an expression (\c
22036aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// IsExpr is true), the parsed expression. If the receiver was parsed
22046aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// as a type (\c IsExpr is false), the parsed type.
22056aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
22066aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \returns True if an error occurred during parsing or semantic
22076aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// analysis, in which case the arguments do not have valid
22086aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// values. Otherwise, returns false for a successful parse.
22096aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
22106aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///   objc-receiver: [C++]
22116aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     'super' [not parsed here]
22126aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     expression
22136aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     simple-type-specifier
22146aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     typename-specifier
22156aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
22160fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
22170fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
22186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
22196aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
22206aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TryAnnotateTypeOrScopeToken();
22216aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22227bf33401acf506b0039222834d7259acb80f6311Kaelyn Uhrain  if (!Actions.isSimpleTypeSpecifier(Tok.getKind())) {
22236aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   objc-receiver:
22246aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     expression
222560d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseExpression();
22266aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
22276aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
22286aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22296aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
22306aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
22316aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
22326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
22336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // objc-receiver:
22356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   typename-specifier
22366aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   simple-type-specifier
22376aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   expression (that starts with one of the above)
22380b7e678a11ece4288dc01aebb5b17e5eef8f8d2dJohn McCall  DeclSpec DS(AttrFactory);
22396aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  ParseCXXSimpleTypeSpecifier(DS);
22406aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22416aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::l_paren)) {
22426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // If we see an opening parentheses at this point, we are
22436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // actually parsing an expression that starts with a
22446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // function-style cast, e.g.,
22456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
22466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   postfix-expression:
22476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     simple-type-specifier ( expression-list [opt] )
22486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     typename-specifier ( expression-list [opt] )
22496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
22506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the remainder of this case, then the (optional)
22516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // postfix-expression suffix, followed by the (optional)
22526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // right-hand side of the binary expression. We have an
22536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // instance method.
225460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
22556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
22569ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParsePostfixExpressionSuffix(Receiver.take());
22576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
22589ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
22596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
22606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
22616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
22636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
22646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
22656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
22666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // We have a class message. Turn the simple-type-specifier or
22686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // typename-specifier we parsed into a type and parse the
22696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // remainder of the class message.
22706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
227123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
22726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Type.isInvalid())
22736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
22746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  IsExpr = false;
2276b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  TypeOrExpr = Type.get().getAsOpaquePtr();
22776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
22786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
22796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
22801b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// \brief Determine whether the parser is currently referring to a an
22811b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// Objective-C message send, using a simplified heuristic to avoid overhead.
22821b730e847ded503f2e615154035c083c4f94a067Douglas Gregor///
22831b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// This routine will only return true for a subset of valid message-send
22841b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// expressions.
22851b730e847ded503f2e615154035c083c4f94a067Douglas Gregorbool Parser::isSimpleObjCMessageExpression() {
22864e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  assert(Tok.is(tok::l_square) && getLangOpts().ObjC1 &&
22871b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         "Incorrect start for isSimpleObjCMessageExpression");
22881b730e847ded503f2e615154035c083c4f94a067Douglas Gregor  return GetLookAheadToken(1).is(tok::identifier) &&
22891b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         GetLookAheadToken(2).is(tok::identifier);
22901b730e847ded503f2e615154035c083c4f94a067Douglas Gregor}
22911b730e847ded503f2e615154035c083c4f94a067Douglas Gregor
22929497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregorbool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
22934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (!getLangOpts().ObjC1 || !NextToken().is(tok::identifier) ||
22949497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      InMessageExpression)
22959497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
22969497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22979497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
22989497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  ParsedType Type;
22999497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
23009497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (Tok.is(tok::annot_typename))
23019497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = getTypeAnnotation(Tok);
23029497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else if (Tok.is(tok::identifier))
23039497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
23049497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor                               getCurScope());
23059497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else
23069497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
23079497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
23089497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
23099497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    const Token &AfterNext = GetLookAheadToken(2);
23109497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
23119497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      if (Tok.is(tok::identifier))
23129497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor        TryAnnotateTypeOrScopeToken();
23139497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
23149497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      return Tok.is(tok::annot_typename);
23159497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    }
23169497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  }
23179497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
23189497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  return false;
23199497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor}
23209497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
23211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
23220ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
23230ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
23242725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///   objc-receiver: [C]
2325eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner///     'super'
23260ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
23270ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
23280ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
23296aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
233060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCMessageExpression() {
2331699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
2332699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
2333699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
23348e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  if (Tok.is(tok::code_completion)) {
233523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMessageReceiver(getCurScope());
23367d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
23378e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    return ExprError();
23388e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  }
23398e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor
23400fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
23410fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
23424e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  if (getLangOpts().CPlusPlus) {
23436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // We completely separate the C and C++ cases because C++ requires
23446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // more complicated (read: slower) parsing.
23456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
23466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Handle send to super.
23476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: This doesn't benefit from the same typo-correction we
23486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // get in Objective-C.
23496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
235023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
2351b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2352b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
23536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
23546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the receiver, which is either a type or an expression.
23556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    bool IsExpr;
2356304b752a450c0fc5968c20ba25446d0bb7c6f68dNick Lewycky    void *TypeOrExpr = NULL;
23576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
23586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      SkipUntil(tok::r_square);
23596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ExprError();
23606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    }
23616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
23626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (IsExpr)
2363b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2364b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(),
23659ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            static_cast<Expr*>(TypeOrExpr));
23666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
23676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2368b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                              ParsedType::getFromOpaquePtr(TypeOrExpr),
2369b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          0);
2370c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  }
2371c59cb38810c63a806270385f79ea84e0203754eaChris Lattner
2372c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  if (Tok.is(tok::identifier)) {
23731dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    IdentifierInfo *Name = Tok.getIdentifierInfo();
23741dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    SourceLocation NameLoc = Tok.getLocation();
2375b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType ReceiverType;
237623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
23771dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor                                       Name == Ident_super,
23781569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       NextToken().is(tok::period),
23791569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       ReceiverType)) {
2380f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCSuperMessage:
2381b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
2382b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
23832725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
2384f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCClassMessage:
23851569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      if (!ReceiverType) {
23862725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        SkipUntil(tok::r_square);
23872725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        return ExprError();
23882725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      }
23892725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
23901569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      ConsumeToken(); // the type name
23911569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
23921569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
23939ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            ReceiverType, 0);
23941dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor
2395f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCInstanceMessage:
23962725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      // Fall through to parse an expression.
23971dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor      break;
2398d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
2399699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
2400eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
2401eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  // Otherwise, an arbitrary expression can be the receiver of a send.
240260d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpression());
24030e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
24045c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
24053fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Res;
2406699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
24071d922960e083906a586609ac6978678147250177Sebastian Redl
2408b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2409b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType(), Res.take());
2410699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
24111d922960e083906a586609ac6978678147250177Sebastian Redl
24122725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Parse the remainder of an Objective-C message following the
24132725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// '[' objc-receiver.
24142725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
24152725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine handles sends to super, class messages (sent to a
24162725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class name), and instance messages (sent to an object), and the
24172725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// target is represented by \p SuperLoc, \p ReceiverType, or \p
24182725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// ReceiverExpr, respectively. Only one of these parameters may have
24192725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// a valid value.
24202725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
24212725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening '['.
24222725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
24232725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc If this is a send to 'super', the location of the
24242725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 'super' keyword that indicates a send to the superclass.
24252725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
24262725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType If this is a class message, the type of the
24272725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class we are sending a message to.
24282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
24292725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverExpr If this is an instance message, the expression
24302725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// used to compute the receiver object.
24311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
24320ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
24330ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
24340ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
24350ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
24360ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
24370ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
24380ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
24390ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
24401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
24410ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
24420ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
24430ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
24440ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
24450ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
24460ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
24470ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
24480ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
24491d922960e083906a586609ac6978678147250177Sebastian Redl///
245060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2451699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
24522725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                       SourceLocation SuperLoc,
2453b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                       ParsedType ReceiverType,
24541d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
24550fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
24560fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
2457c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
24582725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    if (SuperLoc.isValid())
245970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
246070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
24612725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    else if (ReceiverType)
246270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
246370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
2464c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
24659ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
246670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                              0, 0, false);
24677d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
24687d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    return ExprError();
2469c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
2470d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
2471a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
24724b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
24732fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
247448f3cc2b2be1d32df14234904539b34e6e387e4aFariborz Jahanian
24755f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2476951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  SmallVector<SourceLocation, 12> KeyLocs;
24774e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer  ExprVector KeyExprs;
247868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
2479df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
2480a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
2481a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
248268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
2483951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis      KeyLocs.push_back(Loc);
248437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
2485df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
2486a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
24874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
24884fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
24894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
24904fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
24911d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
2492a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
24931d922960e083906a586609ac6978678147250177Sebastian Redl
249468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
24951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
249670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
249770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      if (Tok.is(tok::code_completion)) {
249870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        if (SuperLoc.isValid())
249970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
250070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
250170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
250270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
250370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else if (ReceiverType)
250470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
250570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
250670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
250770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
250870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else
250970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
251070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.data(),
251170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
251270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  /*AtArgumentEpression=*/true);
251370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
25147d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
251570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
251670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      }
251770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
25180c7102f070c10a261ea40700cfbb9102f642ad72Fariborz Jahanian      ExprResult Expr;
25190c7102f070c10a261ea40700cfbb9102f642ad72Fariborz Jahanian      if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
25200c7102f070c10a261ea40700cfbb9102f642ad72Fariborz Jahanian        Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
25210c7102f070c10a261ea40700cfbb9102f642ad72Fariborz Jahanian        Expr = ParseBraceInitializer();
25220c7102f070c10a261ea40700cfbb9102f642ad72Fariborz Jahanian      } else
25230c7102f070c10a261ea40700cfbb9102f642ad72Fariborz Jahanian        Expr = ParseAssignmentExpression();
25240c7102f070c10a261ea40700cfbb9102f642ad72Fariborz Jahanian
25250c7102f070c10a261ea40700cfbb9102f642ad72Fariborz Jahanian      ExprResult Res(Expr);
25260e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
25274fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
25284fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
25294fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
25304fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
25313fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer        return Res;
253237387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
25331d922960e083906a586609ac6978678147250177Sebastian Redl
253437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
2535effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
25361d922960e083906a586609ac6978678147250177Sebastian Redl
2537d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
2538d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
25392725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        if (SuperLoc.isValid())
254023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
25412725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                               KeyIdents.data(),
254270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
254370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
25442725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        else if (ReceiverType)
254523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2546d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
254770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
254870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
2549d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
25509ae2f076ca5ab1feb3ba95629099ec2319833701John McCall          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2551d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
255270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
255370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                /*AtArgumentEpression=*/false);
25547d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
255570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
2556d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
2557d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
255837387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
25592fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
2560df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
2561a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
2562a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
2563a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2564a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
2565df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
256618df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian      SourceLocation commaLoc = ConsumeToken(); // Eat the ','.
25671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
256860d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
25690e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
257018df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian        if (Tok.is(tok::colon)) {
257118df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian          Diag(commaLoc, diag::note_extra_comma_message_arg) <<
257218df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian            FixItHint::CreateRemoval(commaLoc);
257318df0eba838c7609d10edcb845ca6f35698e822cFariborz Jahanian        }
25744fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
25754fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
25764fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
25774fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
25783fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer        return Res;
257949f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
25800e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
258149f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
2582effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
2583a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2584a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
2585a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
25861d922960e083906a586609ac6978678147250177Sebastian Redl
25874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
25884fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
25894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
25904fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
25911d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2592a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
2593809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian
2594df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
2595809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    if (Tok.is(tok::identifier))
2596809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_colon);
2597809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    else
2598809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_rsquare);
25994fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
26004fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
26014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
26024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
26031d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2604a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
260548f3cc2b2be1d32df14234904539b34e6e387e4aFariborz Jahanian
2606699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
26071d922960e083906a586609ac6978678147250177Sebastian Redl
260829238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
2609951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  if (nKeys == 0) {
2610ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
2611951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis    KeyLocs.push_back(Loc);
2612951376242c076c3f62dd78bf672909fc011991dbArgyrios Kyrtzidis  }
2613ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
26141d922960e083906a586609ac6978678147250177Sebastian Redl
26152725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
261623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
26174e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer                                     LBracLoc, KeyLocs, RBracLoc, KeyExprs);
26182725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  else if (ReceiverType)
261923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
26204e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer                                     LBracLoc, KeyLocs, RBracLoc, KeyExprs);
26219ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
26224e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer                                      LBracLoc, KeyLocs, RBracLoc, KeyExprs);
26230ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
26240ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
262560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
262660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseStringLiteralExpression());
26273fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer  if (Res.isInvalid()) return Res;
26281d922960e083906a586609ac6978678147250177Sebastian Redl
2629b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
2630b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
2631b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
26325f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<SourceLocation, 4> AtLocs;
26334e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer  ExprVector AtStrings;
2634b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
2635effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
26360e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2637b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
2638b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
2639b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
264015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
264197cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
264297cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
2643b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
264460d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Lit(ParseStringLiteralExpression());
26450e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
26463fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      return Lit;
26470e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2648effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
2649b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
26501d922960e083906a586609ac6978678147250177Sebastian Redl
2651d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.data(),
2652d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber                                        AtStrings.size());
26535508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
2654f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
2655ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// ParseObjCBooleanLiteral -
2656ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// objc-scalar-literal : '@' boolean-keyword
2657ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                        ;
2658ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// boolean-keyword: 'true' | 'false' | '__objc_yes' | '__objc_no'
2659ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                        ;
2660ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCBooleanLiteral(SourceLocation AtLoc,
2661ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek                                           bool ArgValue) {
2662ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  SourceLocation EndLoc = ConsumeToken();             // consume the keyword.
2663ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  return Actions.ActOnObjCBoolLiteral(AtLoc, EndLoc, ArgValue);
2664ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2665ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2666ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// ParseObjCCharacterLiteral -
2667ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// objc-scalar-literal : '@' character-literal
2668ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                        ;
2669ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCCharacterLiteral(SourceLocation AtLoc) {
2670ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ExprResult Lit(Actions.ActOnCharacterConstant(Tok));
2671ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (Lit.isInvalid()) {
26723fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Lit;
2673ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
26748b8d9534f185743fb72c59d4e735794e6e0f4335Benjamin Kramer  ConsumeToken(); // Consume the literal token.
2675d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
2676ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2677ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2678ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// ParseObjCNumericLiteral -
2679ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// objc-scalar-literal : '@' scalar-literal
2680ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                        ;
2681ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek/// scalar-literal : | numeric-constant			/* any numeric constant. */
2682ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek///                    ;
2683ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCNumericLiteral(SourceLocation AtLoc) {
2684ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ExprResult Lit(Actions.ActOnNumericConstant(Tok));
2685ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  if (Lit.isInvalid()) {
26863fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer    return Lit;
2687ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
26888b8d9534f185743fb72c59d4e735794e6e0f4335Benjamin Kramer  ConsumeToken(); // Consume the literal token.
2689d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.BuildObjCNumericLiteral(AtLoc, Lit.take());
2690ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2691ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2692eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// ParseObjCBoxedExpr -
2693eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard/// objc-box-expression:
2694eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard///       @( assignment-expression )
2695eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardExprResult
2696eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick BeardParser::ParseObjCBoxedExpr(SourceLocation AtLoc) {
2697eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  if (Tok.isNot(tok::l_paren))
2698eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@");
2699eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard
2700eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  BalancedDelimiterTracker T(*this, tok::l_paren);
2701eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  T.consumeOpen();
2702eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  ExprResult ValueExpr(ParseAssignmentExpression());
2703eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  if (T.consumeClose())
2704eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard    return ExprError();
2705edd27602f9cede4fee2c81c785b8f295c9d28752Argyrios Kyrtzidis
2706edd27602f9cede4fee2c81c785b8f295c9d28752Argyrios Kyrtzidis  if (ValueExpr.isInvalid())
2707edd27602f9cede4fee2c81c785b8f295c9d28752Argyrios Kyrtzidis    return ExprError();
2708edd27602f9cede4fee2c81c785b8f295c9d28752Argyrios Kyrtzidis
2709eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  // Wrap the sub-expression in a parenthesized expression, to distinguish
2710eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  // a boxed expression from a literal.
2711eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  SourceLocation LPLoc = T.getOpenLocation(), RPLoc = T.getCloseLocation();
2712eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard  ValueExpr = Actions.ActOnParenExpr(LPLoc, RPLoc, ValueExpr.take());
2713d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.BuildObjCBoxedExpr(SourceRange(AtLoc, RPLoc),
2714d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber                                    ValueExpr.take());
2715eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard}
2716eb382ec1507cf2c8c12d7443d0b67c076223aec6Patrick Beard
2717ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCArrayLiteral(SourceLocation AtLoc) {
27184e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer  ExprVector ElementExprs;                   // array elements.
2719ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ConsumeBracket(); // consume the l_square.
2720ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2721ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  while (Tok.isNot(tok::r_square)) {
2722ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Parse list of array element expressions (all must be id types).
2723ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExprResult Res(ParseAssignmentExpression());
2724ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Res.isInvalid()) {
2725ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // We must manually skip to a ']', otherwise the expression skipper will
2726ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // stop at the ']' when it skips to the ';'.  We want it to skip beyond
2727ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // the enclosing expression.
2728ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      SkipUntil(tok::r_square);
27293fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      return Res;
2730ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2731ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2732ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Parse the ellipsis that indicates a pack expansion.
2733ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Tok.is(tok::ellipsis))
2734ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      Res = Actions.ActOnPackExpansion(Res.get(), ConsumeToken());
2735ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Res.isInvalid())
2736ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return true;
2737ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2738ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ElementExprs.push_back(Res.release());
2739ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2740ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Tok.is(tok::comma))
2741ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ConsumeToken(); // Eat the ','.
2742ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    else if (Tok.isNot(tok::r_square))
2743ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek     return ExprError(Diag(Tok, diag::err_expected_rsquare_or_comma));
2744ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
2745ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  SourceLocation EndLoc = ConsumeBracket(); // location of ']'
27464e28d9e2ba9ce237549b45cfd4136ec6536d1325Benjamin Kramer  MultiExprArg Args(ElementExprs);
2747d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.BuildObjCArrayLiteral(SourceRange(AtLoc, EndLoc), Args);
2748ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2749ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2750ebcb57a8d298862c65043e88b2429591ab3c58d3Ted KremenekExprResult Parser::ParseObjCDictionaryLiteral(SourceLocation AtLoc) {
2751ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  SmallVector<ObjCDictionaryElement, 4> Elements; // dictionary elements.
2752ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  ConsumeBrace(); // consume the l_square.
2753ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  while (Tok.isNot(tok::r_brace)) {
2754ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Parse the comma separated key : value expressions.
2755ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExprResult KeyExpr;
2756ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    {
2757ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ColonProtectionRAIIObject X(*this);
2758ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      KeyExpr = ParseAssignmentExpression();
2759ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      if (KeyExpr.isInvalid()) {
2760ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        // We must manually skip to a '}', otherwise the expression skipper will
2761ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        // stop at the '}' when it skips to the ';'.  We want it to skip beyond
2762ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        // the enclosing expression.
2763ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek        SkipUntil(tok::r_brace);
27643fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer        return KeyExpr;
2765ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      }
2766ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2767ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2768ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Tok.is(tok::colon)) {
2769ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ConsumeToken();
2770ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    } else {
27710ac0ee9be071c796b0f05392b473234b884c0ae8Fariborz Jahanian      Diag(Tok, diag::err_expected_colon);
27720ac0ee9be071c796b0f05392b473234b884c0ae8Fariborz Jahanian      SkipUntil(tok::r_brace);
27730ac0ee9be071c796b0f05392b473234b884c0ae8Fariborz Jahanian      return ExprError();
2774ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2775ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2776ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ExprResult ValueExpr(ParseAssignmentExpression());
2777ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (ValueExpr.isInvalid()) {
2778ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // We must manually skip to a '}', otherwise the expression skipper will
2779ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // stop at the '}' when it skips to the ';'.  We want it to skip beyond
2780ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      // the enclosing expression.
2781ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      SkipUntil(tok::r_brace);
27823fe198bf0d6118c7b080c17c3bb28d7c84e458b9Benjamin Kramer      return ValueExpr;
2783ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    }
2784ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2785ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // Parse the ellipsis that designates this as a pack expansion.
2786ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    SourceLocation EllipsisLoc;
27874e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    if (Tok.is(tok::ellipsis) && getLangOpts().CPlusPlus)
2788ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      EllipsisLoc = ConsumeToken();
2789ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2790ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // We have a valid expression. Collect it in a vector so we can
2791ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    // build the argument list.
2792ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    ObjCDictionaryElement Element = {
279366874fb18afbffb8b2ca05576851a64534be3352David Blaikie      KeyExpr.get(), ValueExpr.get(), EllipsisLoc, None
2794ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    };
2795ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    Elements.push_back(Element);
2796ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2797ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    if (Tok.is(tok::comma))
2798ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      ConsumeToken(); // Eat the ','.
2799ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek    else if (Tok.isNot(tok::r_brace))
2800ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek      return ExprError(Diag(Tok, diag::err_expected_rbrace_or_comma));
2801ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  }
2802ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  SourceLocation EndLoc = ConsumeBrace();
2803ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2804ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek  // Create the ObjCDictionaryLiteral.
2805d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.BuildObjCDictionaryLiteral(SourceRange(AtLoc, EndLoc),
2806d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber                                            Elements.data(), Elements.size());
2807ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek}
2808ebcb57a8d298862c65043e88b2429591ab3c58d3Ted Kremenek
2809f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
2810e23fb90712233bdfa04387e48b54a7168e23cb3eDmitri Gribenko///      \@encode ( type-name )
281160d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
28121d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
2813861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
28141d922960e083906a586609ac6978678147250177Sebastian Redl
2815f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
28161d922960e083906a586609ac6978678147250177Sebastian Redl
28174fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
28181d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
28191d922960e083906a586609ac6978678147250177Sebastian Redl
28204a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
28214a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
28221d922960e083906a586609ac6978678147250177Sebastian Redl
2823809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
28241d922960e083906a586609ac6978678147250177Sebastian Redl
28254a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
28261d922960e083906a586609ac6978678147250177Sebastian Redl
2827809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2828809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2829809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
2830d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, T.getOpenLocation(),
2831d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber                                           Ty.get(), T.getCloseLocation());
2832f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
283329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
283429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
283517d26a6c1f2cb921d0000c337b4967699dc928fdJames Dennett///       \@protocol ( protocol-name )
283660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
28371d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
283829b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
28391d922960e083906a586609ac6978678147250177Sebastian Redl
28404fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
28411d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
28421d922960e083906a586609ac6978678147250177Sebastian Redl
28434a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
28444a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
28451d922960e083906a586609ac6978678147250177Sebastian Redl
28464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
28471d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
28481d922960e083906a586609ac6978678147250177Sebastian Redl
2849390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
28507d24e289bea2accaaed79c6ca3e5cdd0c204ddc1Argyrios Kyrtzidis  SourceLocation ProtoIdLoc = ConsumeToken();
28511d922960e083906a586609ac6978678147250177Sebastian Redl
28524a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
285329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
2854d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
2855d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber                                             T.getOpenLocation(), ProtoIdLoc,
2856d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber                                             T.getCloseLocation());
285729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
2858a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
2859a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
2860a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
286148f3cc2b2be1d32df14234904539b34e6e387e4aFariborz JahanianExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
2862a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
28631d922960e083906a586609ac6978678147250177Sebastian Redl
28644fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
28651d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
28661d922960e083906a586609ac6978678147250177Sebastian Redl
28675f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  SmallVector<IdentifierInfo *, 12> KeyIdents;
2868a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
2869458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
28704a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  BalancedDelimiterTracker T(*this, tok::l_paren);
28714a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeOpen();
28724a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor
2873458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  if (Tok.is(tok::code_completion)) {
2874458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2875458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                     KeyIdents.size());
28767d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis    cutOffParsing();
2877458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    return ExprError();
2878458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  }
2879458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
28802fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
28815add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner  if (!SelIdent &&  // missing selector name.
28825add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
28831d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
28841d922960e083906a586609ac6978678147250177Sebastian Redl
2885b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
2886887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
2887887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
2888a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
28895add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
28905add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        ++nColons;
28915add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        KeyIdents.push_back(0);
28925add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      } else if (Tok.isNot(tok::colon))
28931d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
28941d922960e083906a586609ac6978678147250177Sebastian Redl
28955add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      ++nColons;
28963b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      ConsumeToken(); // Eat the ':' or '::'.
2897a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
2898a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2899458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2900458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      if (Tok.is(tok::code_completion)) {
2901458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2902458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                         KeyIdents.size());
29037d100872341f233c81e1d7b72b40457e62c36862Argyrios Kyrtzidis        cutOffParsing();
2904458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        return ExprError();
2905458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      }
2906458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2907a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
2908a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
29092fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
2910b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
29113b3e1a9e79703da067d23756e5624a4f487d6278Chris Lattner      if (!SelIdent && Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
2912a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2913a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
2914887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
29154a8dfb511e8f84b2e38b7a86d8ddf05ac1e1a41bDouglas Gregor  T.consumeClose();
2916887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
2917d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber  return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
2918d32b94d4d685522d1552d3d1345ae9616d7f27abNico Weber                                             T.getOpenLocation(),
291948f3cc2b2be1d32df14234904539b34e6e387e4aFariborz Jahanian                                             T.getCloseLocation());
292058065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
2921140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
29226c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanianvoid Parser::ParseLexedObjCMethodDefs(LexedMethod &LM, bool parseMethod) {
29236c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // MCDecl might be null due to error in method or c-function  prototype, etc.
29246c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  Decl *MCDecl = LM.D;
29256c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  bool skip = MCDecl &&
29266c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian              ((parseMethod && !Actions.isObjCMethodDecl(MCDecl)) ||
29276c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian              (!parseMethod && Actions.isObjCMethodDecl(MCDecl)));
29286c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  if (skip)
29296c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian    return;
29306c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian
2931a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  // Save the current token position.
2932a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  SourceLocation OrigLoc = Tok.getLocation();
2933a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
2934140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  assert(!LM.Toks.empty() && "ParseLexedObjCMethodDef - Empty body!");
2935140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Append the current token at the end of the new token stream so that it
2936140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // doesn't get lost.
2937140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  LM.Toks.push_back(Tok);
2938140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  PP.EnterTokenStream(LM.Toks.data(), LM.Toks.size(), true, false);
2939140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
2940140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  // Consume the previously pushed token.
2941ab2d09b6287a5dc6fa94d78739444f46f9a78bfbArgyrios Kyrtzidis  ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
2942140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
29439e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian  assert((Tok.is(tok::l_brace) || Tok.is(tok::kw_try) ||
29449e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian          Tok.is(tok::colon)) &&
29459e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian          "Inline objective-c method not starting with '{' or 'try' or ':'");
29466c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // Enter a scope for the method or c-fucntion body.
2947140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian  ParseScope BodyScope(this,
29486c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                       parseMethod
29496c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                       ? Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope
29506c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian                       : Scope::FnScope|Scope::DeclScope);
2951140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian
29526c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // Tell the actions module that we have entered a method or c-function definition
29536c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  // with the specified Declarator for the method/function.
29548c6cb462946b8be19167fad7ff21d4f9fbaebfefFariborz Jahanian  if (parseMethod)
29558c6cb462946b8be19167fad7ff21d4f9fbaebfefFariborz Jahanian    Actions.ActOnStartOfObjCMethodDef(getCurScope(), MCDecl);
29568c6cb462946b8be19167fad7ff21d4f9fbaebfefFariborz Jahanian  else
29578c6cb462946b8be19167fad7ff21d4f9fbaebfefFariborz Jahanian    Actions.ActOnStartOfFunctionDef(getCurScope(), MCDecl);
29582eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian  if (Tok.is(tok::kw_try))
29592eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian    MCDecl = ParseFunctionTryBlock(MCDecl, BodyScope);
29609e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian  else {
29619e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian    if (Tok.is(tok::colon))
29629e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian      ParseConstructorInitializer(MCDecl);
29632eb362b50f34296c39d5ec3e5e1bd6a2c9a5877eFariborz Jahanian    MCDecl = ParseFunctionStatementBody(MCDecl, BodyScope);
29649e5df312551bc92f0a6c908288effef2e2ed0ee7Fariborz Jahanian  }
29656940972c33891ff5d2189fbf232dcdeee0e9daf8Fariborz Jahanian
2966a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  if (Tok.getLocation() != OrigLoc) {
2967a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // Due to parsing error, we either went over the cached tokens or
2968a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // there are still cached tokens left. If it's the latter case skip the
2969a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // leftover tokens.
2970a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // Since this is an uncommon situation that should be avoided, use the
2971a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    // expensive isBeforeInTranslationUnit call.
2972a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis    if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
2973a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis                                                     OrigLoc))
2974a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis      while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
2975a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis        ConsumeAnyToken();
2976a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis  }
2977a24195aaf71cee202f92ea4bad50358f3d0b701fArgyrios Kyrtzidis
29786c89eafc90f5c51a0bf185a993961170aee530c2Fariborz Jahanian  return;
2979140ab234c23f392d5422691c5de1ee3c15026defFariborz Jahanian}
2980