ParseObjc.cpp revision 40ed9a13f5b67b2941f5a9521616e57e9e31ba97
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
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
154985aceceb9b9261b876b515d32726175c13a775Steve Naroff#include "clang/Parse/DeclSpec.h"
163b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian#include "clang/Parse/Scope.h"
17500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22891dca671a80c865c6def7259f170ba785e4faaaChris Lattner/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       external-declaration: [C99 6.9]
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
2591fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-class-declaration
2691fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-alias-declaration
2791fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-protocol-definition
2891fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-method-definition
2991fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  '@' 'end'
30b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtDirectives() {
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation AtLoc = ConsumeToken(); // the "@"
321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
3423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, false);
35dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
36c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  }
37c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
38861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  switch (Tok.getObjCKeywordID()) {
395ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_class:
405ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtClassDeclaration(AtLoc);
415ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_interface:
425ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtInterfaceDeclaration(AtLoc);
435ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_protocol:
445ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtProtocolDeclaration(AtLoc);
455ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
465ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtImplementationDeclaration(AtLoc);
475ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
485ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtEndDeclaration(AtLoc);
495ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
505ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtAliasDeclaration(AtLoc);
515ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
525ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertySynthesize(AtLoc);
535ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
545ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertyDynamic(AtLoc);
555ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
565ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
575ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
58b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// objc-class-declaration:
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
66b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
69c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  llvm::SmallVector<SourceLocation, 8> ClassLocs;
70c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek
711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
73df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
76b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
79c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek    ClassLocs.push_back(Tok.getLocation());
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
855f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
90b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
92c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
93c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassLocs.data(),
94c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassNames.size());
955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
97dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
98dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
99dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
100dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
101dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
102dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
1031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier objc-superclass[opt]
104dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-class-instance-variables[opt]
106dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
107dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
108dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
109dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
1101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier '(' identifier[opt] ')'
111dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
112dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
113dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
114dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
115dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
116dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
117dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
118dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
119dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
120dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
122dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
123dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
124dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
125b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation atLoc, AttributeList *attrList) {
127861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
128dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
129dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1313b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@interface'.
1323b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
13323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(getCurScope());
134dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
1353b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
1363b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
137df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
138dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
139b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
140dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
14163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
142dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1437ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
1455512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  if (Tok.is(tok::l_paren) &&
1465512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      !isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { // we have a category.
147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation lparenLoc = ConsumeParen();
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation categoryLoc, rparenLoc;
149dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
15033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
15123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(getCurScope(), nameId, nameLoc);
152dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
15333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
15433ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
155527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
156df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
157dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
15905511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
16005511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    else if (!getLang().ObjC2) {
161527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
162b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
163dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
164df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
165dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_rparen);
166dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      SkipUntil(tok::r_paren, false); // don't stop at ';'
167b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
168dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
169dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    rparenLoc = ConsumeParen();
1705512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    // Next, we need to check for any protocol references.
1715512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    SourceLocation LAngleLoc, EndProtoLoc;
1725512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
1735512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1745512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::less) &&
1755512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
17671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
1775512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      return DeclPtrTy();
17805511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian
1795512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (attrList) // categories don't support attributes.
1805512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian      Diag(Tok, diag::err_objc_no_attributes_on_category);
1815512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian
1825512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    DeclPtrTy CategoryType =
1835512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    Actions.ActOnStartCategoryInterface(atLoc,
1845512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        nameId, nameLoc,
1855512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        categoryId, categoryLoc,
1865512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.data(),
1875512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolRefs.size(),
1885512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        ProtocolLocs.data(),
1895512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian                                        EndProtoLoc);
1905512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    if (Tok.is(tok::l_brace))
19183c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian      ParseObjCClassInstanceVariables(CategoryType, tok::objc_private,
19283c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                      atLoc);
19383c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian
1945512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
1955512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian    return CategoryType;
196dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
197dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
198dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
199dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
2007ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
201df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
202dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
2033b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
2043b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
2053b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
20623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCSuperclass(getCurScope(), nameId, nameLoc);
207dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
2083b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2093b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
210df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
211dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
212b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
213dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
214dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
215dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
216dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
217dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
218b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
21971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
22071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
22106036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
22271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
22371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
224b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DeclPtrTy ClsType =
2271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
22806036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
229beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
23018df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                     ProtocolLocs.data(),
23106036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     EndProtoLoc, attrList);
2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
233df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
23483c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian    ParseObjCClassInstanceVariables(ClsType, tok::objc_protected, atLoc);
235dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
23625e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
2375512ba538f3f6b0576623f680fa7d930fa085ccdFariborz Jahanian  return ClsType;
238dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
239dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
240d0014540005f2a5ab837365db6bd40f479406758John McCall/// The Objective-C property callback.  This should be defined where
241d0014540005f2a5ab837365db6bd40f479406758John McCall/// it's used, but instead it's been lifted to here to support VS2005.
242d0014540005f2a5ab837365db6bd40f479406758John McCallstruct Parser::ObjCPropertyCallback : FieldCallback {
243d0014540005f2a5ab837365db6bd40f479406758John McCall  Parser &P;
244d0014540005f2a5ab837365db6bd40f479406758John McCall  DeclPtrTy IDecl;
245d0014540005f2a5ab837365db6bd40f479406758John McCall  llvm::SmallVectorImpl<DeclPtrTy> &Props;
246d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCDeclSpec &OCDS;
247d0014540005f2a5ab837365db6bd40f479406758John McCall  SourceLocation AtLoc;
248d0014540005f2a5ab837365db6bd40f479406758John McCall  tok::ObjCKeywordKind MethodImplKind;
249d0014540005f2a5ab837365db6bd40f479406758John McCall
250d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
251d0014540005f2a5ab837365db6bd40f479406758John McCall                       llvm::SmallVectorImpl<DeclPtrTy> &Props,
252d0014540005f2a5ab837365db6bd40f479406758John McCall                       ObjCDeclSpec &OCDS, SourceLocation AtLoc,
253d0014540005f2a5ab837365db6bd40f479406758John McCall                       tok::ObjCKeywordKind MethodImplKind) :
254d0014540005f2a5ab837365db6bd40f479406758John McCall    P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
255d0014540005f2a5ab837365db6bd40f479406758John McCall    MethodImplKind(MethodImplKind) {
256d0014540005f2a5ab837365db6bd40f479406758John McCall  }
257d0014540005f2a5ab837365db6bd40f479406758John McCall
258d0014540005f2a5ab837365db6bd40f479406758John McCall  DeclPtrTy invoke(FieldDeclarator &FD) {
259d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.D.getIdentifier() == 0) {
260d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
261d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
262d0014540005f2a5ab837365db6bd40f479406758John McCall      return DeclPtrTy();
263d0014540005f2a5ab837365db6bd40f479406758John McCall    }
264d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.BitfieldSize) {
265d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_bitfield)
266d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
267d0014540005f2a5ab837365db6bd40f479406758John McCall      return DeclPtrTy();
268d0014540005f2a5ab837365db6bd40f479406758John McCall    }
269d0014540005f2a5ab837365db6bd40f479406758John McCall
270d0014540005f2a5ab837365db6bd40f479406758John McCall    // Install the property declarator into interfaceDecl.
271d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SelName =
272d0014540005f2a5ab837365db6bd40f479406758John McCall      OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
273d0014540005f2a5ab837365db6bd40f479406758John McCall
274d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector GetterSel =
275d0014540005f2a5ab837365db6bd40f479406758John McCall      P.PP.getSelectorTable().getNullarySelector(SelName);
276d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SetterName = OCDS.getSetterName();
277d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector SetterSel;
278d0014540005f2a5ab837365db6bd40f479406758John McCall    if (SetterName)
279d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
280d0014540005f2a5ab837365db6bd40f479406758John McCall    else
281d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
282d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     P.PP.getSelectorTable(),
283d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     FD.D.getIdentifier());
284d0014540005f2a5ab837365db6bd40f479406758John McCall    bool isOverridingProperty = false;
285d0014540005f2a5ab837365db6bd40f479406758John McCall    DeclPtrTy Property =
28623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      P.Actions.ActOnProperty(P.getCurScope(), AtLoc, FD, OCDS,
287d0014540005f2a5ab837365db6bd40f479406758John McCall                              GetterSel, SetterSel, IDecl,
288d0014540005f2a5ab837365db6bd40f479406758John McCall                              &isOverridingProperty,
289d0014540005f2a5ab837365db6bd40f479406758John McCall                              MethodImplKind);
290d0014540005f2a5ab837365db6bd40f479406758John McCall    if (!isOverridingProperty)
291d0014540005f2a5ab837365db6bd40f479406758John McCall      Props.push_back(Property);
292d0014540005f2a5ab837365db6bd40f479406758John McCall
293d0014540005f2a5ab837365db6bd40f479406758John McCall    return Property;
294d0014540005f2a5ab837365db6bd40f479406758John McCall  }
295d0014540005f2a5ab837365db6bd40f479406758John McCall};
296d0014540005f2a5ab837365db6bd40f479406758John McCall
297dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
298dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
299dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
300294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
3013536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
302dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
303dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
304dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
305294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
306294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
307294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
308294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
309b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
310cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                        tok::ObjCKeywordKind contextKey) {
311b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> allMethods;
312b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 16> allProperties;
313682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
31400933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
3151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
316782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  SourceRange AtEnd;
317bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
318294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
319e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
320df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      DeclPtrTy methodPrototype =
322df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
32325e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
3243536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
3253536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
326b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
327b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner                       "", tok::semi);
328294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
329294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
33005511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    if (Tok.is(tok::l_paren)) {
33105511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      Diag(Tok, diag::err_expected_minus_or_plus);
33205511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      DeclPtrTy methodPrototype = ParseObjCMethodDecl(Tok.getLocation(),
33305511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian                                                      tok::minus,
33405511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian                                                      interfaceDecl,
33505511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian                                                      MethodImplKind);
33605511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian      continue;
33705511fa6349ef0820a778f8c840d0b64e05e9aeeFariborz Jahanian    }
338e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
339e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
340294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
341e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
342e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
344bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
345e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
346e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
3471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
348b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    // Code completion within an Objective-C interface.
349b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    if (Tok.is(tok::code_completion)) {
35023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
351b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor                                  ObjCImpDecl? Action::CCC_ObjCImplementation
352b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor                                             : Action::CCC_ObjCInterface);
353dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
354b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    }
355b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor
356e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
357e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
3581fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
3591fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
3601fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
3611fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
3621fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
3631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3644985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // FIXME: as the name implies, this rule allows function definitions.
3654985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // We could pass a flag or check for functions during semantic analysis.
366bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
367e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
368e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
370e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
371e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
372c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    if (Tok.is(tok::code_completion)) {
37323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
374dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
375c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor      break;
376c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    }
377c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
378a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
3791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
380a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
381782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setBegin(AtLoc);
382782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setEnd(Tok.getLocation());
383e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
384c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor    } else if (DirectiveKind == tok::objc_not_keyword) {
385c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      Diag(Tok, diag::err_objc_unknown_at);
386c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      SkipUntil(tok::semi);
387c3d43b783dfb1a1502aa8b31ab1985cf237b1f77Douglas Gregor      continue;
388bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
3891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
390bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
391bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
392bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
393a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
394a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
395bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
396bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
397bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
398bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
399f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
400bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
401a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
402a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
405a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
406a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
407bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
408e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
409bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
410a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
411bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
412a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
414a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
415f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
416f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner        Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
417f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
418e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
4191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
4208ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.is(tok::l_paren))
4214ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        ParseObjCPropertyAttribute(OCDS, interfaceDecl,
4224ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                   allMethods.data(), allMethods.size());
4231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
424d0014540005f2a5ab837365db6bd40f479406758John McCall      ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
425d0014540005f2a5ab837365db6bd40f479406758John McCall                                    OCDS, AtLoc, MethodImplKind);
426bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
427e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
428e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      DeclSpec DS;
429bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
4301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
431a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
432a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner                       tok::at);
433a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
434f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
435294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
436bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
437bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
438bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
439c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
44023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtDirective(getCurScope(), ObjCImpDecl, true);
441dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
442c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  } else if (Tok.isObjCAtKeyword(tok::objc_end))
443bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
444bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  else
445bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    Diag(Tok, diag::err_objc_missing_end);
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
447a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
448bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
44923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnAtEnd(getCurScope(), AtEnd, interfaceDecl,
4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
451beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
452beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
453294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
454294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
455d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
456d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
457d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
458d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
459d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
460d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
461d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
462d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
463d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
464d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
465d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
466d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
467d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
468d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
469d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
470d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
4714ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregorvoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
4724ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                        DeclPtrTy *Methods,
4734ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                        unsigned NumMethods) {
4748ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
475dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  SourceLocation LHSLoc = ConsumeParen(); // consume '('
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
477cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
478ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
47923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyFlags(getCurScope(), DS);
480dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
481ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
482d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
4831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
484f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
485f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
486f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      MatchRHSPunctuation(tok::r_paren, LHSLoc);
487f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
488f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
4891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
490156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
4911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
49292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
493e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
49492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
495e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
49692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
497e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
49892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
499e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
50092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
501e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
50292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
503e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
50492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
505e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
506156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
507156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                           tok::r_paren))
508dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5104ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      if (Tok.is(tok::code_completion)) {
5114ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        if (II->getNameStart()[0] == 's')
51223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCPropertySetter(getCurScope(), ClassDecl,
5134ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                                 Methods, NumMethods);
5144ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        else
51523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCPropertyGetter(getCurScope(), ClassDecl,
5164ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                                 Methods, NumMethods);
517dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
5184ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      }
5194ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
5208ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.isNot(tok::identifier)) {
5211ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(Tok, diag::err_expected_ident);
5228ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
5238ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
5248ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
526e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar      if (II->getNameStart()[0] == 's') {
5278ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
5288ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setSetterName(Tok.getIdentifierInfo());
529156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
5301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
531e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian        if (ExpectAndConsume(tok::colon,
532e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian                             diag::err_expected_colon_after_setter_name, "",
533156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
5348ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
5358ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
5368ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
5378ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setGetterName(Tok.getIdentifierInfo());
538156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
5398ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
540e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
541a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
542cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
543cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
544cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
546156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
547156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
549156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
550d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
5511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
552156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner  MatchRHSPunctuation(tok::r_paren, LHSLoc);
553d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
554d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
5553536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
5573536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
558294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
559294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
560294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
561294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5624985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
5634985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
5644985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
566b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
567df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
568294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
570bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
5711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
572b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
5733536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
5742bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
575f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
576294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
577294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
578294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
579294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
580294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
581294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
582294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
583294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
584294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
585294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5862fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
587ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
588ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
589ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
590ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
591ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
592ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
5939298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
594ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
595ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
596ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
597ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
598ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
599ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
600ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
601ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
602ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
603ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
604ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
605ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
606ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
607ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
608ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
609ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
610ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
611ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
612ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
613ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
614ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
615ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
616ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
617ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
618ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
619ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
620ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
621ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
622ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
623ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
624ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
625ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
626ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
627ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
628ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
629ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
630ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
631ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
632ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
633ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
634ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
635ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
636ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
637ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
638ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
639ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
640ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
641ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
642ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
643ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
644ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
645ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
646ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
647ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
648ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
649ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
650ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
651ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
652ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
653ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
654ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
655ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
656ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
657ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
658ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
659ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
6604b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
661ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
662d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
663294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
664294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6650196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
6660196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
667335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
6683ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
6693ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
6703ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
6711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
6725ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
6730196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
6740196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
675a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
676e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
677e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
678294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
679294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
680294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
681294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
682294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
683a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
684e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
685cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
686e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
6871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
688e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
689e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
690a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
691e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
6921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
693a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
694e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
695e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      default: assert(0 && "Unknown decl qualifier");
696a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
697a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
698a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
699a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
700a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
701a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
702e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
703a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
704e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
705e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
706e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
707e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
7081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
709e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
710e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
711e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
712e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
713e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
714e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
715e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
716e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
717e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
718a526c5c67e5a0473c340903ee542ce570119665fTed KremenekParser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
719df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7214a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  SourceLocation LParenLoc = ConsumeParen();
722e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
7231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
72419d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
725a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ParseObjCTypeQualifierList(DS);
7264fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
7274a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  TypeTy *Ty = 0;
728809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
729809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult TypeSpec = ParseTypeName();
730809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (!TypeSpec.isInvalid())
731809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      Ty = TypeSpec.get();
732809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
7331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7344a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
7354a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    ConsumeParen();
7364a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
737e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
7381ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
7394a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
7404a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
7414a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
7424a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
7434a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    MatchRHSPunctuation(tok::r_paren, LParenLoc);
744294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
745f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
746294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
747294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
748294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
749294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
7504985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
751294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
7524985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
753294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
754294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
7551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
756294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
757294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
758294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
7597ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
7607ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
7617ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
7627ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
763294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7644985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
7654985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
766294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7674985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
7684985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
769294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7704985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
771294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
772294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7737ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
7747ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
7757ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
776b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
777b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              tok::TokenKind mType,
778b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              DeclPtrTy IDecl,
779b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
78054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
78154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
782e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
78323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
784e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor                                       /*ReturnType=*/0, IDecl);
785dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
786e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
787e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
788e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
789ff38491c18b060526d754765b952f4a497a89416Chris Lattner  TypeTy *ReturnType = 0;
790a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
791df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
792f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ReturnType = ParseObjCTypeName(DSRet);
7931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7949e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // If attributes exist before the method, parse them.
7959e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  llvm::OwningPtr<AttributeList> MethodAttrs;
7969e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
7979e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek    MethodAttrs.reset(ParseGNUAttributes());
7989e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek
799e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  if (Tok.is(tok::code_completion)) {
80023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMethodDecl(getCurScope(), mType == tok::minus,
801e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor                                       ReturnType, IDecl);
802dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
803e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor  }
804e8f5a1710a7738deff40e10efcd05b1bd6af184fDouglas Gregor
8059e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek  // Now parse the selector.
806bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
8072fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
808e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
80984c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
81084c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
8111ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
8121ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
813e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
814e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
815b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
816e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
8171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8184f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian  llvm::SmallVector<DeclaratorChunk::ParamInfo, 8> CParamInfo;
819df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
820ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
8211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
8229e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek      MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
8239e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek                                          ParseGNUAttributes()));
8241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
825ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
82654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DeclPtrTy Result
82754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall         = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
8281f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                          mType, IDecl, DSRet, ReturnType, Sel,
8294f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          0,
8304f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          CParamInfo.data(), CParamInfo.size(),
8314f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                          MethodAttrs.get(),
8321c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                          MethodImplKind);
83354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
83454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
835ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
836f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
83768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
838e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner  llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
8391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
840ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
841e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    Action::ObjCArgInfo ArgInfo;
8421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
843ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
844df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
845ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
846ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
847ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
848ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
850e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Type = 0;
851e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
852e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
853e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
854ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
855e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
856df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
857bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      ArgInfo.ArgAttrs = ParseGNUAttributes();
8587ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
85940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    // Code completion for the next piece of the selector.
86040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    if (Tok.is(tok::code_completion)) {
86140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      ConsumeCodeCompletionToken();
86240ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.push_back(SelIdent);
86340ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
86440ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 mType == tok::minus,
86540ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/true,
86640ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 ReturnType,
86740ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.data(),
86840ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 KeyIdents.size());
86940ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      KeyIdents.pop_back();
87040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor      break;
87140ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor    }
87240ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor
873df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
874ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
875ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
8764985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
8771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
878e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
879e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
880ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
882e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
883e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
884e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
8851f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    // Code completion for the next piece of the selector.
8861f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    if (Tok.is(tok::code_completion)) {
8871f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      ConsumeCodeCompletionToken();
8881f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      Actions.CodeCompleteObjCMethodDeclSelector(getCurScope(),
8891f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 mType == tok::minus,
89040ed9a13f5b67b2941f5a9521616e57e9e31ba97Douglas Gregor                                                 /*AtParameterName=*/false,
8911f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 ReturnType,
8921f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.data(),
8931f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor                                                 KeyIdents.size());
8941f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor      break;
8951f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor    }
8961f5537aaac1e775aff1d523f2cc59a9a3bd6c946Douglas Gregor
897ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
8984b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
8992fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner    SelIdent = ParseObjCSelectorPiece(Loc);
900df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
901ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
902ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
903ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
905335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
907ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
908df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
909ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
910df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
911335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
9124985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
913ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
9144985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
915ff38491c18b060526d754765b952f4a497a89416Chris Lattner    DeclSpec DS;
916ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
9171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
918ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
919ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
9204f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    IdentifierInfo *ParmII = ParmDecl.getIdentifier();
92123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    DeclPtrTy Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
9224f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian    CParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
9234f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    ParmDecl.getIdentifierLoc(),
9244f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                    Param,
9254f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                                   0));
9264f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian
9274985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
929ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // FIXME: Add support for optional parmameter list...
930e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
9329e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek    MethodAttrs.reset(addAttributeLists(MethodAttrs.take(),
9339e0493576af77dba1c48858c03e31a2897d0681eTed Kremenek                                        ParseGNUAttributes()));
9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9353688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian  if (KeyIdents.size() == 0)
9363688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian    return DeclPtrTy();
937ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
938ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
93954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  DeclPtrTy Result
94054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall       = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
9413688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian                                        mType, IDecl, DSRet, ReturnType, Sel,
9424f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        &ArgInfos[0],
9434f4fd92c6c64ecbc65507f63ddd09211f732622cFariborz Jahanian                                        CParamInfo.data(), CParamInfo.size(),
9441e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek                                        MethodAttrs.get(),
945335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
94654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
9471e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek
9481e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek  // Delete referenced AttributeList objects.
9491e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek  for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
9501e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek       I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
9511e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek    delete I->ArgAttrs;
9521e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek
95354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
954294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
955294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
956dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
957dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
958dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
9597caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
960b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
96171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
96271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
96371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
964e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
9651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
9671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
968e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
9691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
970e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
97155385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
97255385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
97355385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
974dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
97555385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
97655385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
977e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
978e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
979e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
980e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
981e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
982e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
983e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
98471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
985e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
9861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
987e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
988e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
989e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
990e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
9911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
992e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
993e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
994e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
995e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
996e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
998e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1000e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
1001e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
1002e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
1003e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
1004e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
1005e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
1006e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
1007dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
1008dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
1009dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1010dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
1011dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
1012dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
1013dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
1014dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
1015dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
1016dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
1017dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1018dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
1019dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
1020dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
1021dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
1022ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
1023dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1024dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
10251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
1026dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1027b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
102883c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian                                             tok::ObjCKeywordKind visibility,
102960fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
1030df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
1031b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
1032e13594279a952537ac903325efff57e3edca79d9Chris Lattner
10331a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
103472de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
1035ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1037ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
1038df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
1039ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1041ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
1042df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1043f13ca06e57ac094ed05ea08c26a499af1ba0ce88Douglas Gregor      Diag(Tok, diag::ext_extra_ivar_semi)
1044849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1045ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1046ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
1047ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
10481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1049ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
1050df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
1051ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
1052c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1053c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
105423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(getCurScope());
1055dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
1056c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
1057c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1058861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
1059ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
1060ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
1061ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
1062ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
1063861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
1064ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
10651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
1066ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
1067ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1068ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
1069ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1070ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1072c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
107323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteOrdinaryName(getCurScope(),
1074c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor                                       Action::CCC_ObjCInstanceVariableList);
1075dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1076c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1077c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1078bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1079bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1080bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy IDecl;
1081bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
1082bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1083bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1084bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1085bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                       llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1086bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1087bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1088bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1089bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy invoke(FieldDeclarator &FD) {
1090bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1091bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy Field
109223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          = P.Actions.ActOnIvar(P.getCurScope(),
1093bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1094bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                IDecl, FD.D, FD.BitfieldSize, visibility);
10950bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian        if (Field)
10960bd04596e4645ff145a046dfb3475f39674060d9Fariborz Jahanian          AllIvarDecls.push_back(Field);
1097bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
1098bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1099bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1100bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1101e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
1102e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
1103bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1105df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1106ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1107ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1108ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1109ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1110ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1111ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1112ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
111360fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
11148749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
11158749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
111623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnFields(getCurScope(), atLoc, interfaceDecl,
1117beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                      AllIvarDecls.data(), AllIvarDecls.size(),
11181bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
1119ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1122dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1123dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1124dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1125dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
11271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
11281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1130dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1131dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1132dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1133dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1134dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1135dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
11363536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1137dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1138b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1139b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                                      AttributeList *attrList) {
1140861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
11417ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
11427ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
11431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1144083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
114523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(getCurScope());
1146dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
1147083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1148083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1149df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
11507ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1151b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
11527ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
11537ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
11547ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
11557ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1157df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
11587caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
11597ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
1161bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
11627ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1164df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
11657caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
11667caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
11677caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
11687ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
11697ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
11707ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1171df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
11727ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
11737ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1174b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner        return DeclPtrTy();
11757ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
11767caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
11777caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
11787ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
11791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1180df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
11817ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
11827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
11837ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
11847ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1185b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1187e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
11881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1189bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
1190bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
11917caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
11921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11937ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
119471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
11957caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
1196b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
119771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
11987caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
119971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
120071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1201b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1203b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ProtoType =
1204e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1205beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1206beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
120718df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
1208246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                        EndProtoLoc, attrList);
120925e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1210bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1211dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1212dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1213dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1214dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1215dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1216dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1217dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1218dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1219dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1220dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1221dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1222dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1223b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
1224ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1225ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1226ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1227ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12293b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
12303b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
123123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(getCurScope());
1232dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
12333b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
12343b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1235df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1236ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1237b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1238ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1239ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1240ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1241ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
12421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1244ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1245ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1246ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1247ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124933ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
125023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(getCurScope(), nameId, nameLoc);
1251dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
125233ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
125333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1254df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1255ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1256ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1257ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1258ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1259b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
12601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1261df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1262ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1263ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1264b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1265ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1266ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1267b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    atLoc, nameId, nameLoc, categoryId,
12698f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1270a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
127163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.push_back(ObjCImpDecl);
1272b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1273ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1274ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1275ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1276ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1277df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1278ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1279ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1280df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1281ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1282b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1283ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1284ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1285ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1286ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1287b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
1288cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1289ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
129160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
129283c481ade785a919ba21a33f9a8b1b21c1212fb3Fariborz Jahanian    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/,
129301f1bfc3284d5817517d35217885ea9ecb252817Fariborz Jahanian                                    tok::objc_private, atLoc);
1294a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
129563e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  PendingObjCImpDecl.push_back(ObjCImpDecl);
129663e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1297b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
12985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
129960fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1300782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted KremenekParser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1301ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1302ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1303b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy Result = ObjCImpDecl;
1304ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1305a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
130623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnAtEnd(getCurScope(), atEnd, ObjCImpDecl);
1307b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    ObjCImpDecl = DeclPtrTy();
130863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.pop_back();
1309a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
1310782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  else {
1311782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1312782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1313782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  }
1314a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  return Result;
13155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1316e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
13171ac7104947020a60430ba6f519cd5869af77046fFariborz JahanianParser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
131863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  if (PendingObjCImpDecl.empty())
131963e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
132063e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
132123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnAtEnd(getCurScope(), SourceRange(), ImpDecl);
132263e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  return Actions.ConvertDeclToDeclGroup(ImpDecl);
132363e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian}
132463e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1325e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1326e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1327e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1328b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1329e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1330e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1331e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1332df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1333e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1334b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1335e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1336243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1337243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1338df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1339e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1340b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1341e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1342243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1343243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1344243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
13451ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1346b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1347243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1348b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1349b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
13505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1352ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1353ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1354ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1355ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1356ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1357ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1358ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1359ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1360ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1361ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1362ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1363b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1364ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1365ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1366f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
13671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1368b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1369322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
137023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
1371dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1372322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1373322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1374b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1375b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1376b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1377b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      return DeclPtrTy();
1378b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1379b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1380f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1381f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1382f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1383df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1384ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1385ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1386322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1387322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
138823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        Actions.CodeCompleteObjCPropertySynthesizeIvar(getCurScope(), propertyId,
1389322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor                                                       ObjCImpDecl);
1390dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
1391322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1392322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1393df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1394ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1395ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1396ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1397f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1398ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume ivar-name
1399ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
140023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, true, ObjCImpDecl,
1401f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian                                  propertyId, propertyIvar);
1402df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1403ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1404ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1405ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1406b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  if (Tok.isNot(tok::semi)) {
14071ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1408b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    SkipUntil(tok::semi);
1409b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  }
1410d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian  else
1411d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian    ConsumeToken(); // consume ';'
1412b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1413ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1414ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1415ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1416ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1417ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1418ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1419ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1420ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1421ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1422b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1423ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1424ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1425ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1426424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1427424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
142823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(getCurScope(), ObjCImpDecl);
1429dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor      ConsumeCodeCompletionToken();
1430424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1431424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1432424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1433424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1434424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1435424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      return DeclPtrTy();
1436424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1437424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1438c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1439c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
144023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.ActOnPropertyImplDecl(getCurScope(), atLoc, propertyLoc, false, ObjCImpDecl,
1441c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian                                  propertyId, 0);
1442c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1443df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1444ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1445ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1446ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
144794b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
14481ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
144994b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian    SkipUntil(tok::semi);
145094b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian  }
145194b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian  else
145294b24db37e80a75c4088e592d4150fb12b00bfb3Fariborz Jahanian    ConsumeToken(); // consume ';'
1453b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1454ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1456397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1457397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1458397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
145943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
146015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Res(Actions);
1461397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1462df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
146339f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
14640e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1465397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
146643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1467397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1468397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
146902418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  // consume ';'
147002418c7f0cb8bb83f1a1a1fad9bf6104efa83e0eTed Kremenek  ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@throw");
147123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), getCurScope());
1472397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1473397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1474c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
147578a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1476c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
147743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult
147843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1479fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1480fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
14811ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
148243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1483fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1484fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
14852f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
14860e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
1487fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
148843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1489fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1490fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
14911ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
149243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1493fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1494fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
149578a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
14961ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
149743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
149878a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
14993ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
15003ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
15018935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::DeclScope);
15023ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
150361364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult SynchBody(ParseCompoundStatementBody());
15040e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
15058935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
15060e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (SynchBody.isInvalid())
1507fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
150876ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
1509c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1510c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1511397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1512397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1513397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1514397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1515397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1516397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1517397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1518397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1519397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1520397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1521397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
152243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1523397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
152443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1525397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1526df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
15271ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
152843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1529397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
15308f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  StmtVector CatchStmts(Actions);
153115faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult FinallyStmt(Actions);
15328935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
153361364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult TryBody(ParseCompoundStatementBody());
15348935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
15350e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1536bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1537a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1538df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
15396b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
15406b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
15416b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
15426b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
15436b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
15446b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
15456b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
15460e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1547161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1548cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1549b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      DeclPtrTy FirstPart;
15503b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1551df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1552397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1553e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1554df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1555397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1556397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
155743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl          // For some odd reason, the name of the exception variable is
15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // optional. As a result, we need to use "PrototypeContext", because
15597ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // we must accept either 'declarator' or 'abstract-declarator' here.
15607ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          Declarator ParmDecl(DS, Declarator::PrototypeContext);
15617ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
15627ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
15634e6c0d19b7c072758922cf80525a81aeefc6e64bDouglas Gregor          // Inform the actions module about the declarator, so it
15647ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
156523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          FirstPart = Actions.ActOnObjCExceptionDecl(getCurScope(), ParmDecl);
156664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1567397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
156993a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
15701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
157193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
157293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
157393a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
157493a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
157593a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
157615faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl        OwningStmtResult CatchBody(Actions, true);
1577c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1578c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1579c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1580c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
15810e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
15823b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
15838f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
15848f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        OwningStmtResult Catch = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
15858f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              RParenLoc,
15868f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              FirstPart,
15878f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                                              move(CatchBody));
15888f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor        if (!Catch.isInvalid())
15898f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor          CatchStmts.push_back(Catch.release());
15908f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
159164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
15921ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
15931ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
159443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1595397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1596397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
15976b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
15986b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
159964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
16008935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
16010e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
160215faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl      OwningStmtResult FinallyBody(Actions, true);
1603c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1604c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1605c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1606c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
16070e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1608161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
16090e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
161076ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                                   move(FinallyBody));
1611397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1612397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1613397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1614397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1615bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1616397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
161743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1618bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
16198f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor
16208f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor  return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody),
16218f5e3dd32e443768d9dbbad7191e123e6733750cDouglas Gregor                                    move_arg(CatchStmts),
162276ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                    move(FinallyStmt));
1623397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1624ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
16253536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1626ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1627b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1628b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
163049f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
163149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        PP.getSourceManager(),
163249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        "parsing Objective-C method");
16331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1634ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1635209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1636496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    if (ObjCImpDecl) {
1637496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
1638849b243d4065f56742a4677d6dc8277609a151f8Douglas Gregor        << FixItHint::CreateRemoval(Tok.getLocation());
1639496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1640ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1641209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1642ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1643409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1644df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1645da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
16461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1647409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1648409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
16491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1650409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1651409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1652b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1653ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1654409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1656409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
165715faee19fdb9017dd6d08a690427b18c3b062c2dChris Lattner  ParseScope BodyScope(this,
165815faee19fdb9017dd6d08a690427b18c3b062c2dChris Lattner                       Scope::ObjCMethodScope|Scope::FnScope|Scope::DeclScope);
16591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1660409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1661394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
166223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  Actions.ActOnStartOfObjCMethodDef(getCurScope(), MDecl);
166361364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
166461364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult FnBody(ParseCompoundStatementBody());
166561364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
1666409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
16670e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (FnBody.isInvalid())
1668a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1669a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                       MultiStmtArg(Actions), false);
1670798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
167132ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  // TODO: Pass argument information.
167232ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
16731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1674409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
16758935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
1676798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
167771c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
16785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16795508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
168043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
16819a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
168223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtStatement(getCurScope());
1683dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
16849a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
16855d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
16865d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16875d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
16886b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
16895d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16905d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
169164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
16925d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16935d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
169464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
16955d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
1696d8c4e15138e69a51754cc259c8a592cc47950c8eSebastian Redl  OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
16970e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
169864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
169964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
170064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
170164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
170243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
170364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
17045d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
170564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
170664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
17075ee56e95c3905d2e7bc403631b03865cdbdd8a42Anders Carlsson  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
170864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
170964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
17101d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
17115508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
17129a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
171323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCAtExpression(getCurScope());
1714dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
17159a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
17169a0c85e640a08174569a303db22981612f05d385Douglas Gregor
1717b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1718b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
17191d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1720b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
17214fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
17221d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
17232f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
17244fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
17254fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
17261d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
17274fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
17281d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
17294fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
17301d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
17314fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
17321d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
17334fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
17345508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
17355508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
17365508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
17376aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \brirg Parse the receiver of an Objective-C++ message send.
17386aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
17396aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// This routine parses the receiver of a message send in
17406aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// Objective-C++ either as a type or as an expression. Note that this
17416aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// routine must not be called to parse a send to 'super', since it
17426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// has no way to return such a result.
17436aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
17446aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param IsExpr Whether the receiver was parsed as an expression.
17456aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
17466aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \param TypeOrExpr If the receiver was parsed as an expression (\c
17476aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// IsExpr is true), the parsed expression. If the receiver was parsed
17486aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// as a type (\c IsExpr is false), the parsed type.
17496aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
17506aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// \returns True if an error occurred during parsing or semantic
17516aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// analysis, in which case the arguments do not have valid
17526aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor/// values. Otherwise, returns false for a successful parse.
17536aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
17546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///   objc-receiver: [C++]
17556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     'super' [not parsed here]
17566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     expression
17576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     simple-type-specifier
17586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///     typename-specifier
17596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregorbool Parser::ParseObjCXXMessageReceiver(bool &IsExpr, void *&TypeOrExpr) {
17606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
17616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Tok.is(tok::kw_typename) || Tok.is(tok::annot_cxxscope))
17626aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TryAnnotateTypeOrScopeToken();
17636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
17646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (!isCXXSimpleTypeSpecifier()) {
17656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   objc-receiver:
17666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     expression
17676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    OwningExprResult Receiver = ParseExpression();
17686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
17696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
17706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
17716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
17726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
17736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
17746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
17756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
17766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // objc-receiver:
17776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   typename-specifier
17786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   simple-type-specifier
17796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  //   expression (that starts with one of the above)
17806aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  DeclSpec DS;
17816aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  ParseCXXSimpleTypeSpecifier(DS);
17826aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
17836aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Tok.is(tok::l_paren)) {
17846aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // If we see an opening parentheses at this point, we are
17856aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // actually parsing an expression that starts with a
17866aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // function-style cast, e.g.,
17876aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
17886aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //   postfix-expression:
17896aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     simple-type-specifier ( expression-list [opt] )
17906aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //     typename-specifier ( expression-list [opt] )
17916aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    //
17926aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the remainder of this case, then the (optional)
17936aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // postfix-expression suffix, followed by the (optional)
17946aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // right-hand side of the binary expression. We have an
17956aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // instance method.
17966aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    OwningExprResult Receiver = ParseCXXTypeConstructExpression(DS);
17976aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
17986aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Receiver = ParsePostfixExpressionSuffix(move(Receiver));
17996aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (!Receiver.isInvalid())
18006aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      Receiver = ParseRHSOfBinaryExpression(move(Receiver), prec::Comma);
18016aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Receiver.isInvalid())
18026aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return true;
18036aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18046aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    IsExpr = true;
18056aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    TypeOrExpr = Receiver.take();
18066aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return false;
18076aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  }
18086aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18096aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // We have a class message. Turn the simple-type-specifier or
18106aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // typename-specifier we parsed into a type and parse the
18116aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  // remainder of the class message.
18126aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
181323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  TypeResult Type = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
18146aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (Type.isInvalid())
18156aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return true;
18166aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18176aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  IsExpr = false;
18186aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  TypeOrExpr = Type.get();
18196aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  return false;
18206aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor}
18216aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18221b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// \brief Determine whether the parser is currently referring to a an
18231b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// Objective-C message send, using a simplified heuristic to avoid overhead.
18241b730e847ded503f2e615154035c083c4f94a067Douglas Gregor///
18251b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// This routine will only return true for a subset of valid message-send
18261b730e847ded503f2e615154035c083c4f94a067Douglas Gregor/// expressions.
18271b730e847ded503f2e615154035c083c4f94a067Douglas Gregorbool Parser::isSimpleObjCMessageExpression() {
1828c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  assert(Tok.is(tok::l_square) && getLang().ObjC1 &&
18291b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         "Incorrect start for isSimpleObjCMessageExpression");
18301b730e847ded503f2e615154035c083c4f94a067Douglas Gregor  return GetLookAheadToken(1).is(tok::identifier) &&
18311b730e847ded503f2e615154035c083c4f94a067Douglas Gregor         GetLookAheadToken(2).is(tok::identifier);
18321b730e847ded503f2e615154035c083c4f94a067Douglas Gregor}
18331b730e847ded503f2e615154035c083c4f94a067Douglas Gregor
18341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
18350ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
18360ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
18372725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///   objc-receiver: [C]
1838eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner///     'super'
18390ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
18400ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
18410ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
18426aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor///
18431d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCMessageExpression() {
1844699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1845699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1846699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
18478e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  if (Tok.is(tok::code_completion)) {
184823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    Actions.CodeCompleteObjCMessageReceiver(getCurScope());
18498e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    ConsumeCodeCompletionToken();
18508e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    SkipUntil(tok::r_square);
18518e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor    return ExprError();
18528e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor  }
18538e254cfe5a6ab4202c7fcc4b64bdd1ca0fe071acDouglas Gregor
18546aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor  if (getLang().CPlusPlus) {
18556aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // We completely separate the C and C++ cases because C++ requires
18566aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // more complicated (read: slower) parsing.
18576aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18586aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Handle send to super.
18596aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // FIXME: This doesn't benefit from the same typo-correction we
18606aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // get in Objective-C.
18616aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (Tok.is(tok::identifier) && Tok.getIdentifierInfo() == Ident_super &&
186223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor        NextToken().isNot(tok::period) && getCurScope()->isInObjcMethodScope())
18636aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
18646aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor                                            ExprArg(Actions));
18656aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18666aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    // Parse the receiver, which is either a type or an expression.
18676aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    bool IsExpr;
18686aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    void *TypeOrExpr;
18696aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (ParseObjCXXMessageReceiver(IsExpr, TypeOrExpr)) {
18706aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      SkipUntil(tok::r_square);
18716aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ExprError();
18726aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    }
18736aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18746aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    if (IsExpr)
18756aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0,
18766aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor                                         OwningExprResult(Actions, TypeOrExpr));
18776aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor
18786aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor    return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
18796aa14d832704ae176c92d4e0f22dfb3f3d83a70aDouglas Gregor                                          TypeOrExpr, ExprArg(Actions));
1880c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  }
1881c59cb38810c63a806270385f79ea84e0203754eaChris Lattner
1882c59cb38810c63a806270385f79ea84e0203754eaChris Lattner  if (Tok.is(tok::identifier)) {
18831dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    IdentifierInfo *Name = Tok.getIdentifierInfo();
18841dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    SourceLocation NameLoc = Tok.getLocation();
18851569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    TypeTy *ReceiverType;
188623c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    switch (Actions.getObjCMessageKind(getCurScope(), Name, NameLoc,
18871dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor                                       Name == Ident_super,
18881569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       NextToken().is(tok::period),
18891569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                       ReceiverType)) {
18901dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    case Action::ObjCSuperMessage:
18912725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
1892d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian                                            ExprArg(Actions));
18932725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
18941569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor    case Action::ObjCClassMessage:
18951569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      if (!ReceiverType) {
18962725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        SkipUntil(tok::r_square);
18972725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        return ExprError();
18982725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      }
18992725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor
19001569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      ConsumeToken(); // the type name
19011569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor
19021569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor      return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
19031569f95831a8c99e9f664137bf8f40e47ee3d90fDouglas Gregor                                            ReceiverType,
19042725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                            ExprArg(Actions));
19051dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor
19061dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor    case Action::ObjCInstanceMessage:
19072725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor      // Fall through to parse an expression.
19081dbca6ea983231b4cab1a8f1edda8f6e13c21f12Douglas Gregor      break;
1909d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
1910699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
1911eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner
1912eb483eb3ee80300f15d6d13573d82493c2194461Chris Lattner  // Otherwise, an arbitrary expression can be the receiver of a send.
19132f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
19140e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
19155c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
19161d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
1917699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
19181d922960e083906a586609ac6978678147250177Sebastian Redl
19192725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), 0,
19202725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                        move(Res));
1921699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
19221d922960e083906a586609ac6978678147250177Sebastian Redl
19232725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \brief Parse the remainder of an Objective-C message following the
19242725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// '[' objc-receiver.
19252725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
19262725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// This routine handles sends to super, class messages (sent to a
19272725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class name), and instance messages (sent to an object), and the
19282725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// target is represented by \p SuperLoc, \p ReceiverType, or \p
19292725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// ReceiverExpr, respectively. Only one of these parameters may have
19302725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// a valid value.
19312725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
19322725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param LBracLoc The location of the opening '['.
19332725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
19342725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param SuperLoc If this is a send to 'super', the location of the
19352725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// 'super' keyword that indicates a send to the superclass.
19362725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
19372725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverType If this is a class message, the type of the
19382725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// class we are sending a message to.
19392725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor///
19402725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// \param ReceiverExpr If this is an instance message, the expression
19412725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor/// used to compute the receiver object.
19421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
19430ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
19440ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
19450ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
19460ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
19470ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
19480ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
19490ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
19500ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
19511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
19520ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
19530ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
19540ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
19550ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
19560ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
19570ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
19580ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
19590ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
19601d922960e083906a586609ac6978678147250177Sebastian Redl///
19611d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
1962699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
19632725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                       SourceLocation SuperLoc,
19642725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                       TypeTy *ReceiverType,
19651d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
1966c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
19672725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    if (SuperLoc.isValid())
196823c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc, 0, 0);
19692725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor    else if (ReceiverType)
197023c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType, 0, 0);
1971c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
197223c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor      Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr.get(),
1973d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                              0, 0);
1974dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor    ConsumeCodeCompletionToken();
1975c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
1976d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
1977a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
19784b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
19792fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
198068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1981ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson  SourceLocation SelectorLoc = Loc;
19821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
198368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1984a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
198568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1986df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1987a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
1988a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
198968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
199037387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
1991df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
1992a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
19934fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
19944fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
19954fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
19964fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
19971d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
1998a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
19991d922960e083906a586609ac6978678147250177Sebastian Redl
200068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
20011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
20022f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
20030e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
20044fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
20054fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
20064fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
20074fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
20081d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
200937387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
20101d922960e083906a586609ac6978678147250177Sebastian Redl
201137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
2012effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
20131d922960e083906a586609ac6978678147250177Sebastian Redl
2014d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
2015d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
20162725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        if (SuperLoc.isValid())
201723c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCSuperMessage(getCurScope(), SuperLoc,
20182725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                               KeyIdents.data(),
20192725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                               KeyIdents.size());
20202725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor        else if (ReceiverType)
202123c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCClassMessage(getCurScope(), ReceiverType,
2022d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
2023d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.size());
2024d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
202523c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor          Actions.CodeCompleteObjCInstanceMessage(getCurScope(), ReceiverExpr.get(),
2026d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
2027d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.size());
2028dc8453422bec3bbf70c03920e01498d75783d122Douglas Gregor        ConsumeCodeCompletionToken();
2029d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
2030d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
203137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
20322fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
2033df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
2034a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
2035a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
2036a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2037a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
2038df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
203949f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
20401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
20412f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
20420e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
20434fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
20444fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
20454fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
20464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
20471d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
204849f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
20490e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
205049f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
2051effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
2052a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
2053a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
2054a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
20551d922960e083906a586609ac6978678147250177Sebastian Redl
20564fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
20574fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
20584fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
20594fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
20601d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2061a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
2062809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian
2063df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
2064809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    if (Tok.is(tok::identifier))
2065809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_colon);
2066809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian    else
2067809872eca7f6c024e2ab41ddffbbeeae807bf5dcFariborz Jahanian      Diag(Tok, diag::err_expected_rsquare);
20684fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
20694fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
20704fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
20714fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
20721d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
2073a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
20741d922960e083906a586609ac6978678147250177Sebastian Redl
2075699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
20761d922960e083906a586609ac6978678147250177Sebastian Redl
207729238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
2078ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
2079ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
2080ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
20811d922960e083906a586609ac6978678147250177Sebastian Redl
20822725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  if (SuperLoc.isValid())
208323c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnSuperMessage(getCurScope(), SuperLoc, Sel,
20842725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                     LBracLoc, SelectorLoc, RBracLoc,
20852725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                     Action::MultiExprArg(Actions,
20862725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                                          KeyExprs.take(),
20872725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                                          KeyExprs.size()));
20882725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor  else if (ReceiverType)
208923c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor    return Actions.ActOnClassMessage(getCurScope(), ReceiverType, Sel,
20902725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                     LBracLoc, SelectorLoc, RBracLoc,
20912725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                     Action::MultiExprArg(Actions,
20922725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                                          KeyExprs.take(),
20932725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                                          KeyExprs.size()));
209423c94dbb6631fecdb55ba401aa93722803d980c6Douglas Gregor  return Actions.ActOnInstanceMessage(getCurScope(), move(ReceiverExpr), Sel,
20952725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                      LBracLoc, SelectorLoc, RBracLoc,
20962725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                      Action::MultiExprArg(Actions,
20972725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                                           KeyExprs.take(),
20982725ca8eb3354975ca77ed4b88ede7b60b216b9aDouglas Gregor                                                           KeyExprs.size()));
20990ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
21000ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
21011d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
210220df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  OwningExprResult Res(ParseStringLiteralExpression());
21031d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
21041d922960e083906a586609ac6978678147250177Sebastian Redl
2105b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
2106b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
2107b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
2108b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<SourceLocation, 4> AtLocs;
2109a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
2110b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
2111effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
21120e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2113b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
2114b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
2115b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
211615faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
211797cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
211897cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
2119b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
212097cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    OwningExprResult Lit(ParseStringLiteralExpression());
21210e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
21221d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
21230e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
2124effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
2125b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
21261d922960e083906a586609ac6978678147250177Sebastian Redl
21271d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
21281d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
21295508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
2130f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
2131f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
2132f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
21331d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
21341d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
2135861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
21361d922960e083906a586609ac6978678147250177Sebastian Redl
2137f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
21381d922960e083906a586609ac6978678147250177Sebastian Redl
21394fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
21401d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
21411d922960e083906a586609ac6978678147250177Sebastian Redl
2142f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
21431d922960e083906a586609ac6978678147250177Sebastian Redl
2144809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
21451d922960e083906a586609ac6978678147250177Sebastian Redl
21464988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
21471d922960e083906a586609ac6978678147250177Sebastian Redl
2148809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
2149809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
2150809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
21511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
2152809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                                 Ty.get(), RParenLoc));
2153f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
215429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
215529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
215629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
21571d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
21581d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
215929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
21601d922960e083906a586609ac6978678147250177Sebastian Redl
21614fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
21621d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
21631d922960e083906a586609ac6978678147250177Sebastian Redl
216429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
21651d922960e083906a586609ac6978678147250177Sebastian Redl
21664fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
21671d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
21681d922960e083906a586609ac6978678147250177Sebastian Redl
2169390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
217029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
21711d922960e083906a586609ac6978678147250177Sebastian Redl
21724988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
217329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
21741d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
21751d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
217629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
2177a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
2178a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
2179a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
21801d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
21811d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
2182a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
21831d922960e083906a586609ac6978678147250177Sebastian Redl
21844fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
21851d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
21861d922960e083906a586609ac6978678147250177Sebastian Redl
2187b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
2188a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
2189a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
21902fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
21911d922960e083906a586609ac6978678147250177Sebastian Redl  if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
21921d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
21931d922960e083906a586609ac6978678147250177Sebastian Redl
2194b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
2195887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
2196887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
2197a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
21984fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      if (Tok.isNot(tok::colon))
21991d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
22001d922960e083906a586609ac6978678147250177Sebastian Redl
2201cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      nColons++;
2202a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
2203a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
2204a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2205a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
2206a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
22072fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
2208b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
2209a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
2210a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
2211a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
2212887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
2213a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
2214887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
22151d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
22161d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
221758065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
2218