ParseObjc.cpp revision 0fe5397b26695926a835fa99eceb7fc879b307af
1a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the Objective-C portions of the Parser interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
14500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
1519510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Parse/Parser.h"
160fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor#include "RAIIObjectsForParser.h"
1719510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
18f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall#include "clang/Sema/PrettyDeclStackTrace.h"
1919510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/Scope.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
24891dca671a80c865c6def7259f170ba785e4faaaChris Lattner/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       external-declaration: [C99 6.9]
265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
2791fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-class-declaration
2891fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-alias-declaration
2991fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-protocol-definition
3091fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-method-definition
3191fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  '@' 'end'
32d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtDirectives() {
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation AtLoc = ConsumeToken(); // the "@"
341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
35c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
3623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
37dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
38c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  }
39c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
40861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  switch (Tok.getObjCKeywordID()) {
415ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_class:
425ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtClassDeclaration(AtLoc);
437f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_interface: {
447f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ParsedAttributes attrs;
457f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseObjCAtInterfaceDeclaration(AtLoc, attrs);
467f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
477f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  case tok::objc_protocol: {
487f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    ParsedAttributes attrs;
497f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    return ParseObjCAtProtocolDeclaration(AtLoc, attrs);
507f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  }
515ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
525ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtImplementationDeclaration(AtLoc);
535ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
545ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtEndDeclaration(AtLoc);
555ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
565ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtAliasDeclaration(AtLoc);
575ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
585ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertySynthesize(AtLoc);
595ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
605ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertyDynamic(AtLoc);
615ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
625ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
635ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
64d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// objc-class-declaration:
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
72d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
75c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  llvm::SmallVector<SourceLocation, 8> ClassLocs;
76c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek
771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
79df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
82d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
85c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek    ClassLocs.push_back(Tok.getLocation());
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
88df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
96d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
98c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
99c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassLocs.data(),
100c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassNames.size());
1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
103dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
104dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
105dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
106dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
107dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
108dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
1091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier objc-superclass[opt]
110dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
1111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-class-instance-variables[opt]
112dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
113dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
114dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
115dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
1161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier '(' identifier[opt] ')'
117dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
118dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
119dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
120dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
122dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
123dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
124dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
125dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
127dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
128dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
129dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
130dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1317f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCallDecl *Parser::ParseObjCAtInterfaceDeclaration(SourceLocation atLoc,
1327f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                              ParsedAttributes &attrs) {
133861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
134dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
135dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1373b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@interface'.
1383b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
13923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
140dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
1413b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
1423b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
143df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
145d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
146dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
14763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
150dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
1515512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  if (Tok.is(tok::l_paren) &&
1525512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
153dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation lparenLoc = ConsumeParen();
154dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation categoryLoc, rparenLoc;
155dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
15633ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
15723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
158dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
15933ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
16033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
161527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
162df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
163dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
164dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
16505511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
16605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    else if (!getLang().ObjC2) {
167527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
168d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
169dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
170df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
171dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_rparen);
172dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      SkipUntil(tok::r_paren, false); // don't stop at ';'
173d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
174dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
175dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    rparenLoc = ConsumeParen();
1765512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    // Next, we need to check for any protocol references.
1775512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    SourceLocation LAngleLoc, EndProtoLoc;
178d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    llvm::SmallVector<Decl *, 8> ProtocolRefs;
1795512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1805512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::less) &&
1815512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
18271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
183d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
18405511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian
1857f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (!attrs.empty()) // categories don't support attributes.
1865512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      Diag(Tok, diag::err_objc_no_attributes_on_category);
1875512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian
188d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *CategoryType =
1895512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    Actions.ActOnStartCategoryInterface(atLoc,
1905512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        nameId, nameLoc,
1915512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        categoryId, categoryLoc,
1925512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.data(),
1935512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.size(),
1945512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolLocs.data(),
1955512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        EndProtoLoc);
1965512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::l_brace))
19783c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian      ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
19883c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                      atLoc);
19983c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian
2005512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
2015512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    return CategoryType;
202dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
203dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
204dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
205dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
2067ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
207df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
208dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
2093b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
2103b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
2113b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
21223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
213dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
2143b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2153b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
216df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
217dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
218d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
219dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
220dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
221dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
222dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
223dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
224d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl *, 8> ProtocolRefs;
22571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
22671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
22706036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
22871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
22971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
230d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
2311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
232d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ClsType =
2331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
23406036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
235beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
23618df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                     ProtocolLocs.data(),
2377f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                     EndProtoLoc, attrs.getList());
2381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
239df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
24083c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian    ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
241dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
24225e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
2435512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  return ClsType;
244dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
245dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
246d0014540005f2a5ab837365db6bd40f479406758John McCall/// The Objective-C property callback.  This should be defined where
247d0014540005f2a5ab837365db6bd40f479406758John McCall/// it's used, but instead it's been lifted to here to support VS2005.
248d0014540005f2a5ab837365db6bd40f479406758John McCallstruct Parser::ObjCPropertyCallback : FieldCallback {
249d0014540005f2a5ab837365db6bd40f479406758John McCall  Parser &P;
250d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *IDecl;
251d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVectorImpl<Decl *> &Props;
252d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCDeclSpec &OCDS;
253d0014540005f2a5ab837365db6bd40f479406758John McCall  SourceLocation AtLoc;
254d0014540005f2a5ab837365db6bd40f479406758John McCall  tok::ObjCKeywordKind MethodImplKind;
255d0014540005f2a5ab837365db6bd40f479406758John McCall
256d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  ObjCPropertyCallback(Parser &P, Decl *IDecl,
257d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                       llvm::SmallVectorImpl<Decl *> &Props,
258d0014540005f2a5ab837365db6bd40f479406758John McCall                       ObjCDeclSpec &OCDS, SourceLocation AtLoc,
259d0014540005f2a5ab837365db6bd40f479406758John McCall                       tok::ObjCKeywordKind MethodImplKind) :
260d0014540005f2a5ab837365db6bd40f479406758John McCall    P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
261d0014540005f2a5ab837365db6bd40f479406758John McCall    MethodImplKind(MethodImplKind) {
262d0014540005f2a5ab837365db6bd40f479406758John McCall  }
263d0014540005f2a5ab837365db6bd40f479406758John McCall
264d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *invoke(FieldDeclarator &FD) {
265d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.D.getIdentifier() == 0) {
266d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
267d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
268d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
269d0014540005f2a5ab837365db6bd40f479406758John McCall    }
270d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.BitfieldSize) {
271d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_bitfield)
272d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
273d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
274d0014540005f2a5ab837365db6bd40f479406758John McCall    }
275d0014540005f2a5ab837365db6bd40f479406758John McCall
276d0014540005f2a5ab837365db6bd40f479406758John McCall    // Install the property declarator into interfaceDecl.
277d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SelName =
278d0014540005f2a5ab837365db6bd40f479406758John McCall      OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
279d0014540005f2a5ab837365db6bd40f479406758John McCall
280d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector GetterSel =
281d0014540005f2a5ab837365db6bd40f479406758John McCall      P.PP.getSelectorTable().getNullarySelector(SelName);
282d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SetterName = OCDS.getSetterName();
283d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector SetterSel;
284d0014540005f2a5ab837365db6bd40f479406758John McCall    if (SetterName)
285d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
286d0014540005f2a5ab837365db6bd40f479406758John McCall    else
287d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
288d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     P.PP.getSelectorTable(),
289d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     FD.D.getIdentifier());
290d0014540005f2a5ab837365db6bd40f479406758John McCall    bool isOverridingProperty = false;
291d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Property =
29223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
293d0014540005f2a5ab837365db6bd40f479406758John McCall                              GetterSel, SetterSel, IDecl,
294d0014540005f2a5ab837365db6bd40f479406758John McCall                              &isOverridingProperty,
295d0014540005f2a5ab837365db6bd40f479406758John McCall                              MethodImplKind);
296d0014540005f2a5ab837365db6bd40f479406758John McCall    if (!isOverridingProperty)
297d0014540005f2a5ab837365db6bd40f479406758John McCall      Props.push_back(Property);
298d0014540005f2a5ab837365db6bd40f479406758John McCall
299d0014540005f2a5ab837365db6bd40f479406758John McCall    return Property;
300d0014540005f2a5ab837365db6bd40f479406758John McCall  }
301d0014540005f2a5ab837365db6bd40f479406758John McCall};
302d0014540005f2a5ab837365db6bd40f479406758John McCall
303dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
304dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
305dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
306294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
3073536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
308dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
309dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
310dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
311294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
312294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
313294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
314294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
315d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseObjCInterfaceDeclList(Decl *interfaceDecl,
316cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                        tok::ObjCKeywordKind contextKey) {
317d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl *, 32> allMethods;
318d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl *, 16> allProperties;
319682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
32000933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
322782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  SourceRange AtEnd;
323bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
324294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
325e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
326df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
327d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *methodPrototype =
328df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
32925e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
3303536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
3313536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
332b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
333b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner                       "", tok::semi);
334294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
335294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
33605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    if (Tok.is(tok::l_paren)) {
33705511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      Diag(Tok, diag::err_expected_minus_or_plus);
338d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ParseObjCMethodDecl(Tok.getLocation(),
339d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          tok::minus,
340d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          interfaceDecl,
341d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                          MethodImplKind);
34205511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      continue;
34305511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
344e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
345e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
346294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
347e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
348e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
350bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
351e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
352e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
3531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
354b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    // Code completion within an Objective-C interface.
355b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    if (Tok.is(tok::code_completion)) {
35623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
357f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                  ObjCImpDecl? Sema::PCC_ObjCImplementation
358f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                             : Sema::PCC_ObjCInterface);
359dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
360b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    }
361b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor
362e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
363e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
3641fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
3651fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
3661fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
3671fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
3681fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3704985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // FIXME: as the name implies, this rule allows function definitions.
3714985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // We could pass a flag or check for functions during semantic analysis.
3727f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      ParsedAttributes attrs;
3737f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(attrs));
374e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
375e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
377e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
378e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
379c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    if (Tok.is(tok::code_completion)) {
38023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
381dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
382c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor      break;
383c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    }
384c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
385a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
3861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
387a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
388782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setBegin(AtLoc);
389782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setEnd(Tok.getLocation());
390e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
391c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor    } else if (DirectiveKind == tok::objc_not_keyword) {
392c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      Diag(Tok, diag::err_objc_unknown_at);
393c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      SkipUntil(tok::semi);
394c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      continue;
395bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
3961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
397bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
398bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
399bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
400a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
401a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
402bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
403bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
404bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
405bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
406f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
407bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
408a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
409a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
41046d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
41146d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian    case tok::objc_implementation:
412df81c2c6ce37f4ae3c45dd01093b6274fa0b6692Fariborz Jahanian    case tok::objc_interface:
41346d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      Diag(Tok, diag::err_objc_missing_end);
41446d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      ConsumeToken();
41546d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian      break;
41646d545e355a4fea2a201652c3c8bbf22a13187a6Fariborz Jahanian
417a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
418a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
419a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
420bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
421e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
422bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
423a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
424bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
425a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
427a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
428f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
429b321c0c0ba957d78475e72cebde4028fdaa00f8fChris Lattner        Diag(AtLoc, diag::err_objc_properties_require_objc2);
430f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
431e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
4321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
4338ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.is(tok::l_paren))
434bdb2d5056fd675c27307b34efd371bbba6839e92Douglas Gregor        ParseObjCPropertyAttribute(OCDS, interfaceDecl);
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
436d0014540005f2a5ab837365db6bd40f479406758John McCall      ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
437d0014540005f2a5ab837365db6bd40f479406758John McCall                                    OCDS, AtLoc, MethodImplKind);
438bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
439e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
440e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      DeclSpec DS;
441bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
443a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
444a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner                       tok::at);
445a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
446f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
447294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
448bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
449bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
450bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
451c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
45223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
453dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
454c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  } else if (Tok.isObjCAtKeyword(tok::objc_end))
455bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
456bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  else
457bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    Diag(Tok, diag::err_objc_missing_end);
4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
459a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
460bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
46123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
4621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
463beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
464beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
465294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
466294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
467d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
468d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
469d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
470d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
471d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
472d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
473d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
474d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
475d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
476d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
477d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
478d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
479d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
480d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
481d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
482d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
483bdb2d5056fd675c27307b34efd371bbba6839e92Douglas Gregorvoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, Decl *ClassDecl) {
4848ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
485dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  SourceLocation LHSLoc = ConsumeParen(); // consume '('
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
487cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
488ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
48923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
490dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
491ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
492d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
494f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
495f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
496f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      MatchRHSPunctuation(tok::r_paren, LHSLoc);
497f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
498f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
4991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
500156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
50292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
503e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
50492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
505e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
50692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
507e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
50892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
509e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
51092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
511e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
51292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
513e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
514dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian    else if (II->isStr("atomic"))
515dd4430e596fac34e9ce44228a249f71e73effd4aFariborz Jahanian      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_atomic);
51692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
51742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      bool IsSetter = II->getNameStart()[0] == 's';
51842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
519e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
52042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      unsigned DiagID = IsSetter ? diag::err_objc_expected_equal_for_setter :
52142499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        diag::err_objc_expected_equal_for_getter;
52242499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
52342499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (ExpectAndConsume(tok::equal, DiagID, "", tok::r_paren))
524dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5264ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      if (Tok.is(tok::code_completion)) {
52742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        if (IsSetter)
528bdb2d5056fd675c27307b34efd371bbba6839e92Douglas Gregor          Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl);
5294ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        else
530bdb2d5056fd675c27307b34efd371bbba6839e92Douglas Gregor          Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl);
531dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
5324ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      }
5334ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
53442499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
53542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      SourceLocation SelLoc;
53642499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      IdentifierInfo *SelIdent = ParseObjCSelectorPiece(SelLoc);
53742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson
53842499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (!SelIdent) {
53942499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        Diag(Tok, diag::err_objc_expected_selector_for_getter_setter)
54042499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson          << IsSetter;
5418ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
5428ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
5438ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
54542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson      if (IsSetter) {
5468ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
54742499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setSetterName(SelIdent);
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
549e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian        if (ExpectAndConsume(tok::colon,
550e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian                             diag::err_expected_colon_after_setter_name, "",
551156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
5528ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
5538ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
5548ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
55542499be29b620d2eae34beb0f4d0f9da5a9584daAnders Carlsson        DS.setGetterName(SelIdent);
5568ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
557e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
558a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
559cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
560cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
561cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
5621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
563156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
564156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
566156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
567d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
569156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner  MatchRHSPunctuation(tok::r_paren, LHSLoc);
570d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
571d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
5723536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
5743536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
575294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
576294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
577294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
578294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5794985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
5804985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
5814985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
582d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodPrototype(Decl *IDecl,
583d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                                       tok::ObjCKeywordKind MethodImplKind) {
584df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
585294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
587bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
588d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
5893536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
5902bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
591f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
592294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
593294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
594294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
595294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
596294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
597294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
598294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
599294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
600294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
601294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6022fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
603be74740cc246ce08d42804a684385a42eb814edbFariborz Jahanian
604ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
605ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
606ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
607afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampamp:
608afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::ampequal:
609afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::amp:
610afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipe:
611afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::tilde:
612afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaim:
613afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::exclaimequal:
614afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipepipe:
615afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::pipeequal:
616afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caret:
617afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  case tok::caretequal: {
6183846ca29a8cc1d376a4b695194c29952dbbfb544Fariborz Jahanian    std::string ThisTok(PP.getSpelling(Tok));
619afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    if (isalpha(ThisTok[0])) {
620afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      IdentifierInfo *II = &PP.getIdentifierTable().get(ThisTok.data());
621afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      Tok.setKind(tok::identifier);
622afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      SelectorLoc = ConsumeToken();
623afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian      return II;
624afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    }
625afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian    return 0;
626afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian  }
627afbc68177cc11b8bfa47464b20e15d5f8fb21d4eFariborz Jahanian
628ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
629ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
630ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
6319298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
632ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
633ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
634ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
635ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
636ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
637ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
638ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
639ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
640ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
641ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
642ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
643ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
644ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
645ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
646ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
647ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
648ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
649ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
650ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
651ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
652ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
653ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
654ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
655ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
656ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
657ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
658ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
659ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
660ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
661ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
662ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
663ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
664ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
665ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
666ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
667ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
668ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
669ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
670ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
671ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
672ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
673ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
674ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
675ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
676ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
677ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
678ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
679ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
680ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
681ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
682ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
683ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
684ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
685ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
686ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
687ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
688ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
689ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
690ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
691ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
692ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
693ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
694ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
695ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
696ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
697ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
6984b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
699ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
700d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
701294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
702294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
7030196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
7040196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
705335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
7063ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
7073ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
7083ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
7105ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
7110196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
7120196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
713a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
714e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
715e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
716294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
717294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
718294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
719294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
720294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
721d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregorvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS, bool IsParameter) {
722e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
723d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    if (Tok.is(tok::code_completion)) {
724d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor      Actions.CodeCompleteObjCPassingType(getCurScope(), DS);
725d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor      ConsumeCodeCompletionToken();
726d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    }
727d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor
728cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
729e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
731e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
732e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
733a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
734e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
7351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
736a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
737e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
738e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      default: assert(0 && "Unknown decl qualifier");
739a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
740a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
741a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
742a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
743a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
744a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
745e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
746a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
747e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
748e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
749e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
750e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
7511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
752e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
753e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
754e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
755e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
756e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
757e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
758e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
759e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
760e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
761b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCallParsedType Parser::ParseObjCTypeName(ObjCDeclSpec &DS, bool IsParameter) {
762df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7644a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  SourceLocation LParenLoc = ConsumeParen();
765e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
76719d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
768d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor  ParseObjCTypeQualifierList(DS, IsParameter);
7694fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
770b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType Ty;
771809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
772809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult TypeSpec = ParseTypeName();
773809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (!TypeSpec.isInvalid())
774809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      Ty = TypeSpec.get();
775809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
7761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7774a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
7784a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    ConsumeParen();
7794a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
780e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
7811ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
7824a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
7834a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
7844a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
7854a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
7864a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    MatchRHSPunctuation(tok::r_paren, LParenLoc);
787294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
788f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
789294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
790294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
791294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
792294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
7934985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
794294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
7954985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
796294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
797294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
7981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
799294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
800294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
801294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
8027ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
8037ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
8047ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
8057ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
806294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
8074985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
8084985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
809294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
8104985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
8114985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
812294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
8134985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
814294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
815294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
8167ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
8177ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
8187ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
819d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
8202ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor                                  tok::TokenKind mType,
8212ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor                                  Decl *IDecl,
8222ccccb3ff40c64927817a7e1ddf1da8c188ed224Douglas Gregor                                  tok::ObjCKeywordKind MethodImplKind) {
82354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
82454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
825e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
82623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
827b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                       /*ReturnType=*/ ParsedType(), IDecl);
828dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
829e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
830e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
831e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
832b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  ParsedType ReturnType;
833a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
834df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
835d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor    ReturnType = ParseObjCTypeName(DSRet, false);
8361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8379e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // If attributes exist before the method, parse them.
8387f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  ParsedAttributes attrs;
8397f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (getLang().ObjC2)
8407f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    MaybeParseGNUAttributes(attrs);
8419e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek
842e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
84323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
844e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor                                       ReturnType, IDecl);
845dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
846e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
847e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
8489e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // Now parse the selector.
849bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
8502fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
851e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
85284c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
85384c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
8541ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
8551ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
856e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
857e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
858d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
859e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
8601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8614f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian  llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
862df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
863ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
8647f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (getLang().ObjC2)
8657f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      MaybeParseGNUAttributes(attrs);
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
867ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
868d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Result
86954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall         = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
8701f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                          mType, IDecl, DSRet, ReturnType, Sel,
8714f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          0,
8724f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          CParamInfo.data(), CParamInfo.size(),
8737f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                          attrs.getList(), MethodImplKind);
87454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
87554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
876ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
877f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
87868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
879f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  llvm::SmallVector<Sema::ObjCArgInfo, 12> ArgInfos;
8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
881ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
882f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    Sema::ObjCArgInfo ArgInfo;
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
884ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
885df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
886ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
887ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
888ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
889ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
891b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ArgInfo.Type = ParsedType();
892e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
893d32b0225e29fcafb2b2b2a4b1c51dcb1518af9c6Douglas Gregor      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec, true);
894e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
895ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
896e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
8977f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    if (getLang().ObjC2) {
8987f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      ParsedAttributes attrs;
8997f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      MaybeParseGNUAttributes(attrs);
9007f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall      ArgInfo.ArgAttrs = attrs.getList();
9017f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    }
9027ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
90340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    // Code completion for the next piece of the selector.
90440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    if (Tok.is(tok::code_completion)) {
90540ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      ConsumeCodeCompletionToken();
90640ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.push_back(SelIdent);
90740ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
90840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 mType == tok::minus,
90940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/true,
91040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 ReturnType,
91140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.data(),
91240ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.size());
91340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.pop_back();
91440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      break;
91540ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    }
91640ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor
917df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
918ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
919ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
9204985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
9211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
922e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
923e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
924ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
926e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
927e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
928e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
9291f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    // Code completion for the next piece of the selector.
9301f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    if (Tok.is(tok::code_completion)) {
9311f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      ConsumeCodeCompletionToken();
9321f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
9331f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 mType == tok::minus,
93440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/false,
9351f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 ReturnType,
9361f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.data(),
9371f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.size());
9381f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      break;
9391f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    }
9401f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor
941ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
9424b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
9432fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner    SelIdent = ParseObjCSelectorPiece(Loc);
944df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
945ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
946ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
947ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
9481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
949335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
9501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
951ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
952df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
953ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
954df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
955335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
9564985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
957ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
9584985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
959ff38491c18b060526d754765b952f4a497a89416Chris Lattner    DeclSpec DS;
960ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
962ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
963ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
9644f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    IdentifierInfo *ParmII = ParmDecl.getIdentifier();
965d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
9664f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
9674f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    ParmDecl.getIdentifierLoc(),
9684f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    Param,
9694f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                   0));
9704f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian
9714985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9739c4bb2c08989265411925a04252fd4f93c26e3b1Cameron Esfahani  // FIXME: Add support for optional parameter list...
974e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
9757f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall  if (getLang().ObjC2)
9767f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall    MaybeParseGNUAttributes(attrs);
9771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9783688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian  if (KeyIdents.size() == 0)
979d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
980ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
981ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
982d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Result
98354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall       = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
9843688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian                                        mType, IDecl, DSRet, ReturnType, Sel,
9854f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        &ArgInfos[0],
9864f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        CParamInfo.data(), CParamInfo.size(),
9877f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                        attrs.getList(),
988335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
98954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
99054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
991294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
992294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
993dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
994dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
995dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
9967caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
997d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallParseObjCProtocolReferences(llvm::SmallVectorImpl<Decl *> &Protocols,
99871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
99971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
100071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
1001e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
10021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
100371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1005e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
10061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1007e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
100855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
100955385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
101055385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
1011dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
101255385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
101355385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
1014e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
1015e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
1016e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
1017e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
1018e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
1019e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
1020e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
102171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
1022e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
10231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1024e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
1025e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
1026e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
1027e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
10281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1029e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
1030e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
1031e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
1032e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
1033e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
10341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1035e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1037e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
1038e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
1039e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
1040e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
1041e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
1042e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
1043e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
10449bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// \brief Parse the Objective-C protocol qualifiers that follow a typename
10459bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor/// in a decl-specifier-seq, starting at the '<'.
104646f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregorbool Parser::ParseObjCProtocolQualifiers(DeclSpec &DS) {
10479bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(Tok.is(tok::less) && "Protocol qualifiers start with '<'");
10489bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  assert(getLang().ObjC1 && "Protocol qualifiers only exist in Objective-C");
10499bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  SourceLocation LAngleLoc, EndProtoLoc;
10509bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  llvm::SmallVector<Decl *, 8> ProtocolDecl;
10519bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
105246f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  bool Result = ParseObjCProtocolReferences(ProtocolDecl, ProtocolLocs, false,
105346f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor                                            LAngleLoc, EndProtoLoc);
10549bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  DS.setProtocolQualifiers(ProtocolDecl.data(), ProtocolDecl.size(),
10559bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor                           ProtocolLocs.data(), LAngleLoc);
10569bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor  if (EndProtoLoc.isValid())
10579bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor    DS.SetRangeEnd(EndProtoLoc);
105846f936e055d763437accd1e5a1bc49e7e5dbc0a3Douglas Gregor  return Result;
10599bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor}
10609bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
10619bd1d8d174a9d15ae343246c8322299248b9e92aDouglas Gregor
1062dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
1063dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
1064dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1065dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
1066dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
1067dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
1068dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
1069dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
1070dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
1071dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
1072dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1073dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
1074dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
1075dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
1076dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
1077ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
1078dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1079dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
1081dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1082d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallvoid Parser::ParseObjCClassInstanceVariables(Decl *interfaceDecl,
108383c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                             tok::ObjCKeywordKind visibility,
108460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
1085df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
1086d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl *, 32> AllIvarDecls;
1087e13594279a952537ac903325efff57e3edca79d9Chris Lattner
10881a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
108972de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
1090ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
10911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1092ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
1093df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1094ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
10951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1096ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
1097df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1098f13ca06e57ac094ed05ea08c26a499af1ba0ce88Douglas Gregor      Diag(Tok, diag::ext_extra_ivar_semi)
1099849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1100ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1101ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
1102ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
11031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1104ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
1105df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
1106ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
1107c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1108c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
110923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(getCurScope());
1110dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
1111c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
1112c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1113861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
1114ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
1115ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
1116ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
1117ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
1118861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
1119ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
11201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
1121ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
1122ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1123ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
1124ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1125ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1127c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
112823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
1129f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                       Sema::PCC_ObjCInstanceVariableList);
1130dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1131c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1132c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1133bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1134bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1135d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *IDecl;
1136bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
1137d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      llvm::SmallVectorImpl<Decl *> &AllIvarDecls;
1138bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1139d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      ObjCIvarCallback(Parser &P, Decl *IDecl, tok::ObjCKeywordKind V,
1140d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall                       llvm::SmallVectorImpl<Decl *> &AllIvarDecls) :
1141bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1142bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1143bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1144d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *invoke(FieldDeclarator &FD) {
1145bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1146d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        Decl *Field
114723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          = P.Actions.ActOnIvar(P.getCurScope(),
1148bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1149bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                IDecl, FD.D, FD.BitfieldSize, visibility);
11500bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian        if (Field)
11510bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian          AllIvarDecls.push_back(Field);
1152bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
1153bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1154bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1155d097be8f81fbf4ed96ac10bae18562dd8202666bFariborz Jahanian
1156e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
1157e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
1158bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
11591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1160df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1161ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1162ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1163ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1164ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1165ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1166ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1167ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
116860fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
1169d097be8f81fbf4ed96ac10bae18562dd8202666bFariborz Jahanian  Actions.ActOnLastBitfield(RBraceLoc, interfaceDecl, AllIvarDecls);
11708749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
11718749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
117223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1173beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                      AllIvarDecls.data(), AllIvarDecls.size(),
11741bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
1175ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
11765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1177dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1178dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1179dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1181dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1182dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1186dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1187dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1188dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1189dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1190dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1191dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
11923536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1193dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1194d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
11957f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                             ParsedAttributes &attrs) {
1196861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
11977ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
11987ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1200083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
120123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(getCurScope());
1202dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
1203083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1204083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1205df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
12067ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1207d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
12087ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
12097ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
12107ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
12117ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
12121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1213df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
12147caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
12157ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
12161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
12177f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
12187ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
12191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1220df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
12217caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
12227caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
12237caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
12247ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
12257ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
12267ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1227df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
12287ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
12297ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1230d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall        return 0;
12317ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
12327caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
12337caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
12347ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1236df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
12377ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
12387ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
12397ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
12407ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1241d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
12421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1243e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
12441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1245bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
12467f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                                   attrs.getList());
12477caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
125071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
12517caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
1252d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  llvm::SmallVector<Decl *, 8> ProtocolRefs;
125371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
12547caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
125571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
125671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1257d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
12581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1259d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ProtoType =
1260e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1261beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1262beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
126318df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
12647f040a9d817cd1c72b565e92abff473510bf9e1dJohn McCall                                        EndProtoLoc, attrs.getList());
126525e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1266bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1267dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1268dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1269dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1270dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1271dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1272dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1273dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1274dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1275dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1276dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1277dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1278dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1279d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtImplementationDeclaration(
1280ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1281ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1282ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1283ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
12841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12853b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
12863b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
128723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(getCurScope());
1288dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
12893b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
12903b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1291df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1292ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1293d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1294ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1295ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1296ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1297ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1300ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1301ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1302ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1303ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
130533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
130623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
1307dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
130833ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
130933ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1310df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1311ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1312ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1313ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1314ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1315d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
13161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1317df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1318ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1319ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1320d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1321ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1322ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1323d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    Decl *ImplCatType = Actions.ActOnStartCategoryImplementation(
13241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    atLoc, nameId, nameLoc, categoryId,
13258f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1326a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
132763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.push_back(ObjCImpDecl);
1328d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1329ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1330ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1331ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1332ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1333df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1334ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1335ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1336df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1337ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1338d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1339ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1340ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1341ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1342ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1343d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ImplClsType = Actions.ActOnStartClassImplementation(
1344cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1345ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
134760fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
134883c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
134901f1bfc3284d5817517d35217885ea9ecb252817Fariborz Jahanian                                    tok::objc_private, atLoc);
1350a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
135163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  PendingObjCImpDecl.push_back(ObjCImpDecl);
135263e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1353d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
13545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
135560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1356d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1357ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1358ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1359d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *Result = ObjCImpDecl;
1360ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1361a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
136223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
1363d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    ObjCImpDecl = 0;
136463e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.pop_back();
1365a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
1366782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  else {
1367782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1368782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1369782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  }
1370a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  return Result;
13715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1372e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
13733fe104154dd2e8ffb351142d74f308938b5c99bfFariborz JahanianParser::DeclGroupPtrTy Parser::FinishPendingObjCActions() {
13743fe104154dd2e8ffb351142d74f308938b5c99bfFariborz Jahanian  Actions.DiagnoseUseOfUnimplementedSelectors();
137563e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  if (PendingObjCImpDecl.empty())
1376d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return Actions.ConvertDeclToDeclGroup(0);
1377d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *ImpDecl = PendingObjCImpDecl.pop_back_val();
137823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
137963e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  return Actions.ConvertDeclToDeclGroup(ImpDecl);
138063e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian}
138163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1382e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1383e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1384e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1385d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1386e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1387e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1388e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1389df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1390e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1391d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1392e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1393243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1394243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1395df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1396e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1397d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1398e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1399243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1400243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1401243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
14021ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1403d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall    return 0;
1404243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1405b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1406b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
14075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1409ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1410ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1411ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1412ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1413ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1414ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1415ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1416ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1417ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1418ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1419ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1420d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1421ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1422ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1423f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
14241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1425b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1426322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
142723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
1428dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1429322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1430322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1431b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1432b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1433b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1434d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1435b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1436b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1437f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1438f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1439f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1440a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor    SourceLocation propertyIvarLoc;
1441df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1442ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1443ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1444322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1445322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
144623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
1447322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor                                                       ObjCImpDecl);
1448dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
1449322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1450322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1451df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1452ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1453ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1454ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1455f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1456a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor      propertyIvarLoc = ConsumeToken(); // consume ivar-name
1457ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
145823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
1459a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, propertyIvar, propertyIvarLoc);
1460df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1461ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1462ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1463ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1464b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  if (Tok.isNot(tok::semi)) {
14651ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1466b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    SkipUntil(tok::semi);
1467b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  }
1468d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian  else
1469d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian    ConsumeToken(); // consume ';'
1470d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1471ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1472ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1473ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1474ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1475ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1476ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1477ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1478ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1479ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1480d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1481ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1482ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1483ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1484424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1485424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
148623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
1487dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1488424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1489424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1490424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1491424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1492424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1493d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1494424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1495424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1496c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1497c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
149823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
1499a4ffd85a6684e42f900aad5459e58ad91bb88755Douglas Gregor                                  propertyId, 0, SourceLocation());
1500c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1501df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1502ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1503ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1504ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
150594b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
15061ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
150794b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian    SkipUntil(tok::semi);
150894b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian  }
150994b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian  else
151094b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian    ConsumeToken(); // consume ';'
1511d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  return 0;
1512ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
15131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1514397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1515397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1516397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
151760d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
151860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res;
1519397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1520df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
152139f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
15220e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1523397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
152443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1525397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1526397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
152702418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  // consume ';'
152802418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
15299ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtThrowStmt(atLoc, Res.take(), getCurScope());
1530397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1531397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1532c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
153378a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1534c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
153560d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult
153643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1537fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1538fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
15391ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
154043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1541fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1542fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
154360d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpression());
15440e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
1545fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
154643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1547fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1548fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
15491ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
155043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1551fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1552fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
155378a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
15541ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
155543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
155678a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
15573ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
15583ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
15598935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::DeclScope);
15603ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
156160d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult SynchBody(ParseCompoundStatementBody());
15620e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
15638935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
15640e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (SynchBody.isInvalid())
1565fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
15669ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.take(), SynchBody.take());
1567c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1568c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1569397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1570397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1571397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1572397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1573397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1574397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1575397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1576397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1577397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1578397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1579397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
158060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1581397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
158243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1583397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1584df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
15851ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
158643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1587397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
15888f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  StmtVector CatchStmts(Actions);
158960d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult FinallyStmt;
15908935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
159160d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult TryBody(ParseCompoundStatementBody());
15928935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
15930e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1594bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1595a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1596df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
15976b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
15986b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
15996b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
16006b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
16016b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
16026b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
16036b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
16040e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1605161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1606cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1607d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      Decl *FirstPart = 0;
16083b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1609df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1610397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1611e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1612df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1613397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1614397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
161543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl          // For some odd reason, the name of the exception variable is
16161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // optional. As a result, we need to use "PrototypeContext", because
16177ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // we must accept either 'declarator' or 'abstract-declarator' here.
16187ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          Declarator ParmDecl(DS, Declarator::PrototypeContext);
16197ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
16207ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
16214e6c0d19b7c072758922cf80525a81aeefc6e64bDouglas Gregor          // Inform the actions module about the declarator, so it
16227ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
162323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
162464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1625397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
16261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
162793a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
16281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
162993a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
163093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
163193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
163293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
163393a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
163460d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult CatchBody(true);
1635c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1636c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1637c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1638c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
16390e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
16403b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
16418f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
164260d7b3a319d84d688752be3870615ac0f111fb16John McCall        StmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
16438f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              RParenLoc,
16448f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              FirstPart,
16459ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                              CatchBody.take());
16468f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        if (!Catch.isInvalid())
16478f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor          CatchStmts.push_back(Catch.release());
16488f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
164964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
16501ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
16511ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
165243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1653397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1654397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
16556b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
16566b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
165764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
16588935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
16590e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
166060d7b3a319d84d688752be3870615ac0f111fb16John McCall      StmtResult FinallyBody(true);
1661c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1662c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1663c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1664c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
16650e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1666161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
16670e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
16689ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                                   FinallyBody.take());
1669397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1670397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1671397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1672397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1673bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1674397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
167543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1676bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
16778f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
16789ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.take(),
16798f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                    move_arg(CatchStmts),
16809ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                    FinallyStmt.take());
1681397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1682ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
16833536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1684ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1685d226f65006733ed7f709c3174f22ce33391cb58fJohn McCallDecl *Parser::ParseObjCMethodDefinition() {
1686d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall  Decl *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
16871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1688f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall  PrettyDeclStackTraceEntry CrashInfo(Actions, MDecl, Tok.getLocation(),
1689f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      "parsing Objective-C method");
16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1691ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1692209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1693496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    if (ObjCImpDecl) {
1694496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
1695849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1696496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1697ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1698209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1699ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1700409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1701df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1702da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
17031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1704409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1705409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
17061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1707409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1708409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1709d226f65006733ed7f709c3174f22ce33391cb58fJohn McCall      return 0;
1710ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1711409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
17121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1713409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
171415faee19fdb9017dd6d08a690427b18c3b062c2dChris Lattner  ParseScope BodyScope(this,
171515faee19fdb9017dd6d08a690427b18c3b062c2dChris Lattner                       Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
17161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1717409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1718394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
171923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
172061364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
17210fe5397b26695926a835fa99eceb7fc879b307afArgyrios Kyrtzidis  if (MaybeSkipFunctionBodyForCodeCompletion())
17220fe5397b26695926a835fa99eceb7fc879b307afArgyrios Kyrtzidis    return Actions.ActOnFinishFunctionBody(MDecl, 0);
17230fe5397b26695926a835fa99eceb7fc879b307afArgyrios Kyrtzidis
172460d7b3a319d84d688752be3870615ac0f111fb16John McCall  StmtResult FnBody(ParseCompoundStatementBody());
172561364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
1726409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
17270e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (FnBody.isInvalid())
1728a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1729a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                       MultiStmtArg(Actions), false);
1730798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
173132ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  // TODO: Pass argument information.
17329ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  Actions.ActOnFinishFunctionBody(MDecl, FnBody.take());
17331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1734409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
17358935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
1736798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
173771c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
17385f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
17395508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
174060d7b3a319d84d688752be3870615ac0f111fb16John McCallStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
17419a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
174223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtStatement(getCurScope());
1743dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
17449a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
17455d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
17465d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
17475d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
17486b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
17495d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
17505d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
175164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
17525d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
17535d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
175464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
17555d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
175660d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
17570e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
175864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
175964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
176064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
176164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
176243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
176364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
17645d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
176564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
17669ba23b4ceacd77cd264501690a7a9e94184ef71bDouglas Gregor  ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
17679ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res.take()));
176864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
176964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
177060d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
17715508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
17729a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
177323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtExpression(getCurScope());
1774dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
17759a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
17769a0c85e640a08174569a303db22981612f05d385Douglas Gregor
1777b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1778b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
17791d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1780b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
17814fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
17821d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
17832f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
17844fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
17854fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
17861d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
17874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
17881d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
17894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
17901d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
17914fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
17921d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
17934fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
17945508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
17955508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
17965508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
17976aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brirg Parse the receiver of an Objective-C++ message send.
17986aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
17996aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// This routine parses the receiver of a message send in
18006aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// Objective-C++ either as a type or as an expression. Note that this
18016aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// routine must not be called to parse a send to 'super', since it
18026aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// has no way to return such a result.
18036aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18046aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param IsExpr Whether the receiver was parsed as an expression.
18056aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18066aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param TypeOrExpr If the receiver was parsed as an expression (\c
18076aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// IsExpr is true), the parsed expression. If the receiver was parsed
18086aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// as a type (\c IsExpr is false), the parsed type.
18096aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18106aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \returns True if an error occurred during parsing or semantic
18116aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// analysis, in which case the arguments do not have valid
18126aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// values. Otherwise, returns false for a successful parse.
18136aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18146aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///   objc-receiver: [C++]
18156aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     'super' [not parsed here]
18166aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     expression
18176aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     simple-type-specifier
18186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     typename-specifier
18196aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
18200fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
18210fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
18226aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
18236aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
18246aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TryAnnotateTypeOrScopeToken();
18256aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18266aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (!isCXXSimpleTypeSpecifier()) {
18276aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   objc-receiver:
18286aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     expression
182960d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseExpression();
18306aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
18316aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
18326aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
18346aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
18356aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
18366aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
18376aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18386aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // objc-receiver:
18396aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   typename-specifier
18406aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   simple-type-specifier
18416aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   expression (that starts with one of the above)
18426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  DeclSpec DS;
18436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  ParseCXXSimpleTypeSpecifier(DS);
18446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::l_paren)) {
18466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // If we see an opening parentheses at this point, we are
18476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // actually parsing an expression that starts with a
18486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // function-style cast, e.g.,
18496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
18506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   postfix-expression:
18516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     simple-type-specifier ( expression-list [opt] )
18526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     typename-specifier ( expression-list [opt] )
18536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
18546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the remainder of this case, then the (optional)
18556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // postfix-expression suffix, followed by the (optional)
18566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // right-hand side of the binary expression. We have an
18576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // instance method.
185860d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Receiver = ParseCXXTypeConstructExpression(DS);
18596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
18609ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParsePostfixExpressionSuffix(Receiver.take());
18616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
18629ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Receiver = ParseRHSOfBinaryExpression(Receiver.take(), prec::Comma);
18636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
18646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
18656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
18676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
18686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
18696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
18706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // We have a class message. Turn the simple-type-specifier or
18726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // typename-specifier we parsed into a type and parse the
18736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // remainder of the class message.
18746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
187523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
18766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Type.isInvalid())
18776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
18786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  IsExpr = false;
1880b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  TypeOrExpr = Type.get().getAsOpaquePtr();
18816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
18826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
18836aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18841b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// \brief Determine whether the parser is currently referring to a an
18851b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// Objective-C message send, using a simplified heuristic to avoid overhead.
18861b730e847ded503f2e615154035c083c4f94a067Douglas Gregor///
18871b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// This routine will only return true for a subset of valid message-send
18881b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// expressions.
18891b730e847ded503f2e615154035c083c4f94a067Douglas Gregorbool Parser::isSimpleObjCMessageExpression() {
1890c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
18911b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         "Incorrect start for isSimpleObjCMessageExpression");
18921b730e847ded503f2e615154035c083c4f94a067Douglas Gregor  return GetLookAheadToken(1).is(tok::identifier) &&
18931b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         GetLookAheadToken(2).is(tok::identifier);
18941b730e847ded503f2e615154035c083c4f94a067Douglas Gregor}
18951b730e847ded503f2e615154035c083c4f94a067Douglas Gregor
18969497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregorbool Parser::isStartOfObjCClassMessageMissingOpenBracket() {
18979497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!getLang().ObjC1 || !NextToken().is(tok::identifier) ||
18989497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      InMessageExpression)
18999497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
19009497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19019497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19029497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  ParsedType Type;
19039497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19049497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (Tok.is(tok::annot_typename))
19059497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = getTypeAnnotation(Tok);
19069497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else if (Tok.is(tok::identifier))
19079497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    Type = Actions.getTypeName(*Tok.getIdentifierInfo(), Tok.getLocation(),
19089497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor                               getCurScope());
19099497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  else
19109497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    return false;
19119497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19129497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  if (!Type.get().isNull() && Type.get()->isObjCObjectOrInterfaceType()) {
19139497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    const Token &AfterNext = GetLookAheadToken(2);
19149497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    if (AfterNext.is(tok::colon) || AfterNext.is(tok::r_square)) {
19159497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      if (Tok.is(tok::identifier))
19169497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor        TryAnnotateTypeOrScopeToken();
19179497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19189497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor      return Tok.is(tok::annot_typename);
19199497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor    }
19209497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  }
19219497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19229497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor  return false;
19239497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor}
19249497a73ad0d54859edbf48beb93ebb19a7ae50c9Douglas Gregor
19251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
19260ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
19270ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
19282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///   objc-receiver: [C]
1929eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner///     'super'
19300ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
19310ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
19320ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
19336aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
193460d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCMessageExpression() {
1935699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1936699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1937699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
19388e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  if (Tok.is(tok::code_completion)) {
193923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMessageReceiver(getCurScope());
19408e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    ConsumeCodeCompletionToken();
19418e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    SkipUntil(tok::r_square);
19428e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    return ExprError();
19438e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  }
19448e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor
19450fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
19460fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
19476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (getLang().CPlusPlus) {
19486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // We completely separate the C and C++ cases because C++ requires
19496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // more complicated (read: slower) parsing.
19506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Handle send to super.
19526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: This doesn't benefit from the same typo-correction we
19536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // get in Objective-C.
19546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
195523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
1956b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1957b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
19586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the receiver, which is either a type or an expression.
19606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    bool IsExpr;
1961304b752a450c0fc5968c20ba25446d0bb7c6f68dNick Lewycky    void *TypeOrExpr = NULL;
19626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
19636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      SkipUntil(tok::r_square);
19646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ExprError();
19656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    }
19666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (IsExpr)
1968b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1969b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(),
19709ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            static_cast<Expr*>(TypeOrExpr));
19716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
19726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1973b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                              ParsedType::getFromOpaquePtr(TypeOrExpr),
1974b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                          0);
1975c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  }
1976c59cb38810c63a806270385f79ea84e0203754eaChris Lattner
1977c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  if (Tok.is(tok::identifier)) {
19781dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    IdentifierInfo *Name = Tok.getIdentifierInfo();
19791dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    SourceLocation NameLoc = Tok.getLocation();
1980b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall    ParsedType ReceiverType;
198123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
19821dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor                                       Name == Ident_super,
19831569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       NextToken().is(tok::period),
19841569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       ReceiverType)) {
1985f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCSuperMessage:
1986b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(),
1987b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                            ParsedType(), 0);
19882725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
1989f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCClassMessage:
19901569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      if (!ReceiverType) {
19912725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        SkipUntil(tok::r_square);
19922725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        return ExprError();
19932725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      }
19942725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
19951569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      ConsumeToken(); // the type name
19961569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
19971569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
19989ae2f076ca5ab1feb3ba95629099ec2319833701John McCall                                            ReceiverType, 0);
19991dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor
2000f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall    case Sema::ObjCInstanceMessage:
20012725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      // Fall through to parse an expression.
20021dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor      break;
2003d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
2004699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
2005eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
2006eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  // Otherwise, an arbitrary expression can be the receiver of a send.
200760d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseExpression());
20080e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
20095c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
20101d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
2011699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
20121d922960e083906a586609ac6978678147250177Sebastian Redl
2013b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
2014b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                        ParsedType(), Res.take());
2015699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
20161d922960e083906a586609ac6978678147250177Sebastian Redl
20172725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Parse the remainder of an Objective-C message following the
20182725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// '[' objc-receiver.
20192725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
20202725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine handles sends to super, class messages (sent to a
20212725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class name), and instance messages (sent to an object), and the
20222725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// target is represented by \p SuperLoc, \p ReceiverType, or \p
20232725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// ReceiverExpr, respectively. Only one of these parameters may have
20242725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// a valid value.
20252725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
20262725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening '['.
20272725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
20282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc If this is a send to 'super', the location of the
20292725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 'super' keyword that indicates a send to the superclass.
20302725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
20312725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType If this is a class message, the type of the
20322725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class we are sending a message to.
20332725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
20342725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverExpr If this is an instance message, the expression
20352725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// used to compute the receiver object.
20361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
20370ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
20380ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
20390ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
20400ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
20410ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
20420ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
20430ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
20440ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
20451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
20460ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
20470ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
20480ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
20490ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
20500ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
20510ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
20520ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
20530ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
20541d922960e083906a586609ac6978678147250177Sebastian Redl///
205560d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
2056699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
20572725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                       SourceLocation SuperLoc,
2058b3d8748e797c6c2f1dc01186c8eeb3b1b5fe970cJohn McCall                                       ParsedType ReceiverType,
20591d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
20600fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor  InMessageExpressionRAIIObject InMessage(*this, true);
20610fbda68b50ce17d7ad36ef7a5ed77518a5cd272eDouglas Gregor
2062c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
20632725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    if (SuperLoc.isValid())
206470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0,
206570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
20662725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    else if (ReceiverType)
206770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0,
206870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                           false);
2069c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
20709ae2f076ca5ab1feb3ba95629099ec2319833701John McCall      Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
207170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                              0, 0, false);
2072dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
2073c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
2074d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
2075a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
20764b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
20772fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
207868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
2079ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson  SourceLocation SelectorLoc = Loc;
20801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
208168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
2082a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
208368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
2084df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
2085a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
2086a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
208768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
208837387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
2089df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
2090a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
20914fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
20924fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
20934fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
20944fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
20951d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
2096a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
20971d922960e083906a586609ac6978678147250177Sebastian Redl
209868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
20991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
210070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
210170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      if (Tok.is(tok::code_completion)) {
210270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        if (SuperLoc.isValid())
210370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
210470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
210570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
210670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
210770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else if (ReceiverType)
210870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
210970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.data(),
211070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
211170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/true);
211270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        else
211370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
211470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.data(),
211570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
211670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  /*AtArgumentEpression=*/true);
211770c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
211870c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        ConsumeCodeCompletionToken();
211970c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        SkipUntil(tok::r_square);
212070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
212170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor      }
212270c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor
212360d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
21240e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
21254fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
21264fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
21274fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
21284fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
21291d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
213037387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
21311d922960e083906a586609ac6978678147250177Sebastian Redl
213237387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
2133effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
21341d922960e083906a586609ac6978678147250177Sebastian Redl
2135d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
2136d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
21372725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        if (SuperLoc.isValid())
213823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
21392725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                               KeyIdents.data(),
214070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
214170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
21422725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        else if (ReceiverType)
214323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2144d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
214570c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               KeyIdents.size(),
214670c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                               /*AtArgumentEpression=*/false);
2147d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
21489ae2f076ca5ab1feb3ba95629099ec2319833701John McCall          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr,
2149d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
215070c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                  KeyIdents.size(),
215170c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor                                                /*AtArgumentEpression=*/false);
2152dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
215370c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        SkipUntil(tok::r_square);
215470c5ac70ace21b011dc2d4001bae26cdcf62ff8dDouglas Gregor        return ExprError();
2155d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
2156d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
215737387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
21582fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
2159df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
2160a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
2161a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
2162a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2163a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
2164df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
216549f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
21661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
216760d7b3a319d84d688752be3870615ac0f111fb16John McCall      ExprResult Res(ParseAssignmentExpression());
21680e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
21694fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
21704fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
21714fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
21724fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
21731d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
217449f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
21750e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
217649f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
2177effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
2178a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2179a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
2180a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
21811d922960e083906a586609ac6978678147250177Sebastian Redl
21824fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
21834fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
21844fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
21854fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
21861d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2187a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
2188809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian
2189df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
2190809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    if (Tok.is(tok::identifier))
2191809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_colon);
2192809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    else
2193809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_rsquare);
21944fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
21954fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
21964fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
21974fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
21981d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2199a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
22001d922960e083906a586609ac6978678147250177Sebastian Redl
2201699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
22021d922960e083906a586609ac6978678147250177Sebastian Redl
220329238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
2204ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
2205ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
2206ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
22071d922960e083906a586609ac6978678147250177Sebastian Redl
22082725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
220923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
22102725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                     LBracLoc, SelectorLoc, RBracLoc,
2211f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2212f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2213f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
22142725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  else if (ReceiverType)
221523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
22162725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                     LBracLoc, SelectorLoc, RBracLoc,
2217f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                     MultiExprArg(Actions,
2218f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.take(),
2219f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                  KeyExprs.size()));
22209ae2f076ca5ab1feb3ba95629099ec2319833701John McCall  return Actions.ActOnInstanceMessage(getCurScope(), ReceiverExpr, Sel,
22212725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                      LBracLoc, SelectorLoc, RBracLoc,
2222f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                      MultiExprArg(Actions,
2223f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.take(),
2224f312b1ea179f1c44371f9ee0cd0bc006f612de11John McCall                                                   KeyExprs.size()));
22250ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
22260ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
222760d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
222860d7b3a319d84d688752be3870615ac0f111fb16John McCall  ExprResult Res(ParseStringLiteralExpression());
22291d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
22301d922960e083906a586609ac6978678147250177Sebastian Redl
2231b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
2232b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
2233b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
2234b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<SourceLocation, 4> AtLocs;
2235a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
2236b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
2237effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
22380e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2239b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
2240b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
2241b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
224215faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
224397cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
224497cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
2245b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
224660d7b3a319d84d688752be3870615ac0f111fb16John McCall    ExprResult Lit(ParseStringLiteralExpression());
22470e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
22481d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
22490e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2250effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
2251b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
22521d922960e083906a586609ac6978678147250177Sebastian Redl
22531d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
22541d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
22555508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
2256f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
2257f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
2258f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
225960d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
22601d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
2261861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
22621d922960e083906a586609ac6978678147250177Sebastian Redl
2263f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
22641d922960e083906a586609ac6978678147250177Sebastian Redl
22654fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
22661d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
22671d922960e083906a586609ac6978678147250177Sebastian Redl
2268f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
22691d922960e083906a586609ac6978678147250177Sebastian Redl
2270809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
22711d922960e083906a586609ac6978678147250177Sebastian Redl
22724988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
22731d922960e083906a586609ac6978678147250177Sebastian Redl
2274809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2275809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2276809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
22771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
2278809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                                 Ty.get(), RParenLoc));
2279f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
228029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
228129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
228229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
228360d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult
22841d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
228529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
22861d922960e083906a586609ac6978678147250177Sebastian Redl
22874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
22881d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
22891d922960e083906a586609ac6978678147250177Sebastian Redl
229029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
22911d922960e083906a586609ac6978678147250177Sebastian Redl
22924fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
22931d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
22941d922960e083906a586609ac6978678147250177Sebastian Redl
2295390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
229629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
22971d922960e083906a586609ac6978678147250177Sebastian Redl
22984988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
229929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
23001d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
23011d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
230229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
2303a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
2304a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
2305a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
230660d7b3a319d84d688752be3870615ac0f111fb16John McCallExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
2307a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
23081d922960e083906a586609ac6978678147250177Sebastian Redl
23094fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
23101d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
23111d922960e083906a586609ac6978678147250177Sebastian Redl
2312b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
2313a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
2314a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
2315458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2316458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  if (Tok.is(tok::code_completion)) {
2317458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2318458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                     KeyIdents.size());
2319458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    ConsumeCodeCompletionToken();
2320458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    MatchRHSPunctuation(tok::r_paren, LParenLoc);
2321458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor    return ExprError();
2322458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor  }
2323458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
23242fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
23255add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner  if (!SelIdent &&  // missing selector name.
23265add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      Tok.isNot(tok::colon) && Tok.isNot(tok::coloncolon))
23271d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
23281d922960e083906a586609ac6978678147250177Sebastian Redl
2329b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
2330887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
2331887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
2332a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
23335add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      if (Tok.is(tok::coloncolon)) { // Handle :: in C++.
23345add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        ++nColons;
23355add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner        KeyIdents.push_back(0);
23365add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      } else if (Tok.isNot(tok::colon))
23371d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
23381d922960e083906a586609ac6978678147250177Sebastian Redl
23395add7541726348b9d1c8e96ac5031b87c41acff0Chris Lattner      ++nColons;
2340a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
2341a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
2342a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2343458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2344458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      if (Tok.is(tok::code_completion)) {
2345458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        Actions.CodeCompleteObjCSelector(getCurScope(), KeyIdents.data(),
2346458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor                                         KeyIdents.size());
2347458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        ConsumeCodeCompletionToken();
2348458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        MatchRHSPunctuation(tok::r_paren, LParenLoc);
2349458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor        return ExprError();
2350458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor      }
2351458433d2f0f5c96a9e0d21decdd44bebccf20b11Douglas Gregor
2352a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
2353a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
23542fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
2355b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
2356a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
2357a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2358a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
2359887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
2360a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2361887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
23621d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
23631d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
236458065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
2365