ParseObjc.cpp revision e0097db2848c463a534c18c235c6d3e53f2f1b87
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)) {
34c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, false);
35c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    ConsumeToken();
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)) {
1333b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(CurScope);
1343b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    ConsumeToken();
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();
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
146df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren)) { // we have a category.
147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation lparenLoc = ConsumeParen();
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation categoryLoc, rparenLoc;
149dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
1501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15133ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
15233ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
15333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      ConsumeToken();
15433ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
15533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
156527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
157df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
159dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
160527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    } 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();
1701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
171dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    // Next, we need to check for any protocol references.
17271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    SourceLocation LAngleLoc, EndProtoLoc;
173b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
17471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1756bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    if (Tok.is(tok::less) &&
17671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
17771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
178b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    if (attrList) // categories don't support attributes.
181dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_objc_no_attributes_on_category);
1821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
183beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    DeclPtrTy CategoryType =
1841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Actions.ActOnStartCategoryInterface(atLoc,
185beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          nameId, nameLoc,
186beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          categoryId, categoryLoc,
187beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          ProtocolRefs.data(),
188beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          ProtocolRefs.size(),
18918df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                          ProtocolLocs.data(),
190beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          EndProtoLoc);
1911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
192fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian    ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
193bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    return CategoryType;
194dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
195dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
196dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
197dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
1987ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
199df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
200dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
2013b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
2023b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
2033b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
2043b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor      Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
2053b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor      ConsumeToken();
2063b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2073b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
208df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
209dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
210b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
211dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
212dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
213dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
214dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
215dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
216b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
21771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
21871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
21906036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
22071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
22171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
222b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
2231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DeclPtrTy ClsType =
2251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
22606036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
227beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
22818df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                     ProtocolLocs.data(),
22906036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     EndProtoLoc, attrList);
2301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
231df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
23260fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ClsType, atLoc);
233dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
23425e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
235bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ClsType;
236dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
237dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
238d0014540005f2a5ab837365db6bd40f479406758John McCall/// The Objective-C property callback.  This should be defined where
239d0014540005f2a5ab837365db6bd40f479406758John McCall/// it's used, but instead it's been lifted to here to support VS2005.
240d0014540005f2a5ab837365db6bd40f479406758John McCallstruct Parser::ObjCPropertyCallback : FieldCallback {
241d0014540005f2a5ab837365db6bd40f479406758John McCall  Parser &P;
242d0014540005f2a5ab837365db6bd40f479406758John McCall  DeclPtrTy IDecl;
243d0014540005f2a5ab837365db6bd40f479406758John McCall  llvm::SmallVectorImpl<DeclPtrTy> &Props;
244d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCDeclSpec &OCDS;
245d0014540005f2a5ab837365db6bd40f479406758John McCall  SourceLocation AtLoc;
246d0014540005f2a5ab837365db6bd40f479406758John McCall  tok::ObjCKeywordKind MethodImplKind;
247d0014540005f2a5ab837365db6bd40f479406758John McCall
248d0014540005f2a5ab837365db6bd40f479406758John McCall  ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
249d0014540005f2a5ab837365db6bd40f479406758John McCall                       llvm::SmallVectorImpl<DeclPtrTy> &Props,
250d0014540005f2a5ab837365db6bd40f479406758John McCall                       ObjCDeclSpec &OCDS, SourceLocation AtLoc,
251d0014540005f2a5ab837365db6bd40f479406758John McCall                       tok::ObjCKeywordKind MethodImplKind) :
252d0014540005f2a5ab837365db6bd40f479406758John McCall    P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
253d0014540005f2a5ab837365db6bd40f479406758John McCall    MethodImplKind(MethodImplKind) {
254d0014540005f2a5ab837365db6bd40f479406758John McCall  }
255d0014540005f2a5ab837365db6bd40f479406758John McCall
256d0014540005f2a5ab837365db6bd40f479406758John McCall  DeclPtrTy invoke(FieldDeclarator &FD) {
257d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.D.getIdentifier() == 0) {
258d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
259d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
260d0014540005f2a5ab837365db6bd40f479406758John McCall      return DeclPtrTy();
261d0014540005f2a5ab837365db6bd40f479406758John McCall    }
262d0014540005f2a5ab837365db6bd40f479406758John McCall    if (FD.BitfieldSize) {
263d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Diag(AtLoc, diag::err_objc_property_bitfield)
264d0014540005f2a5ab837365db6bd40f479406758John McCall        << FD.D.getSourceRange();
265d0014540005f2a5ab837365db6bd40f479406758John McCall      return DeclPtrTy();
266d0014540005f2a5ab837365db6bd40f479406758John McCall    }
267d0014540005f2a5ab837365db6bd40f479406758John McCall
268d0014540005f2a5ab837365db6bd40f479406758John McCall    // Install the property declarator into interfaceDecl.
269d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SelName =
270d0014540005f2a5ab837365db6bd40f479406758John McCall      OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
271d0014540005f2a5ab837365db6bd40f479406758John McCall
272d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector GetterSel =
273d0014540005f2a5ab837365db6bd40f479406758John McCall      P.PP.getSelectorTable().getNullarySelector(SelName);
274d0014540005f2a5ab837365db6bd40f479406758John McCall    IdentifierInfo *SetterName = OCDS.getSetterName();
275d0014540005f2a5ab837365db6bd40f479406758John McCall    Selector SetterSel;
276d0014540005f2a5ab837365db6bd40f479406758John McCall    if (SetterName)
277d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
278d0014540005f2a5ab837365db6bd40f479406758John McCall    else
279d0014540005f2a5ab837365db6bd40f479406758John McCall      SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
280d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     P.PP.getSelectorTable(),
281d0014540005f2a5ab837365db6bd40f479406758John McCall                                                     FD.D.getIdentifier());
282d0014540005f2a5ab837365db6bd40f479406758John McCall    bool isOverridingProperty = false;
283d0014540005f2a5ab837365db6bd40f479406758John McCall    DeclPtrTy Property =
284d0014540005f2a5ab837365db6bd40f479406758John McCall      P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
285d0014540005f2a5ab837365db6bd40f479406758John McCall                              GetterSel, SetterSel, IDecl,
286d0014540005f2a5ab837365db6bd40f479406758John McCall                              &isOverridingProperty,
287d0014540005f2a5ab837365db6bd40f479406758John McCall                              MethodImplKind);
288d0014540005f2a5ab837365db6bd40f479406758John McCall    if (!isOverridingProperty)
289d0014540005f2a5ab837365db6bd40f479406758John McCall      Props.push_back(Property);
290d0014540005f2a5ab837365db6bd40f479406758John McCall
291d0014540005f2a5ab837365db6bd40f479406758John McCall    return Property;
292d0014540005f2a5ab837365db6bd40f479406758John McCall  }
293d0014540005f2a5ab837365db6bd40f479406758John McCall};
294d0014540005f2a5ab837365db6bd40f479406758John McCall
295dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
296dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
297dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
298294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
2993536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
300dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
301dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
302dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
303294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
304294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
305294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
306294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
307b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
308cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                        tok::ObjCKeywordKind contextKey) {
309b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> allMethods;
310b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 16> allProperties;
311682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
31200933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
3131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
314782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  SourceRange AtEnd;
315bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
316294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
317e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
318df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
3191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      DeclPtrTy methodPrototype =
320df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
32125e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
3223536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
3233536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
324b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
325b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner                       "", tok::semi);
326294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
327294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
3281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
329e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
330e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
331294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
332e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
333e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
335bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
336e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
337e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
339b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    // Code completion within an Objective-C interface.
340b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    if (Tok.is(tok::code_completion)) {
341b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor      Actions.CodeCompleteOrdinaryName(CurScope,
342b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor                                  ObjCImpDecl? Action::CCC_ObjCImplementation
343b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor                                             : Action::CCC_ObjCInterface);
344b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor      ConsumeToken();
345b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor    }
346b6ac2451bfed36206c5cec7217372c4299f67f2bDouglas Gregor
347e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
348e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
3491fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
3501fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
3511fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
3521fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
3531fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
3541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3554985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // FIXME: as the name implies, this rule allows function definitions.
3564985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // We could pass a flag or check for functions during semantic analysis.
357bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition(0));
358e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
359e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
3601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
361e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
362e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
363c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    if (Tok.is(tok::code_completion)) {
364c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor      Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
365c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor      ConsumeToken();
366c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor      break;
367c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    }
368c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor
369a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
3701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
371a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
372782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setBegin(AtLoc);
373782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek      AtEnd.setEnd(Tok.getLocation());
374e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
375bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
3761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
377bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
378bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
379bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
380a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
381a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
382bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
383bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
384bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
385bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
386f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
387bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
388a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
389a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
3901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
391a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
392a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
393a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
394bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
395e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
396bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
397a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
398bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
399a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
4001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
401a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
402f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
403f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner        Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
404f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
405e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
4078ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.is(tok::l_paren))
4084ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        ParseObjCPropertyAttribute(OCDS, interfaceDecl,
4094ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                   allMethods.data(), allMethods.size());
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
411d0014540005f2a5ab837365db6bd40f479406758John McCall      ObjCPropertyCallback Callback(*this, interfaceDecl, allProperties,
412d0014540005f2a5ab837365db6bd40f479406758John McCall                                    OCDS, AtLoc, MethodImplKind);
413bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
414e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
415e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      DeclSpec DS;
416bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
4171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
418a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
419a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner                       tok::at);
420a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
421f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
422294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
423bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
424bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
425bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
426c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  if (Tok.is(tok::code_completion)) {
427c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    Actions.CodeCompleteObjCAtDirective(CurScope, ObjCImpDecl, true);
428c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor    ConsumeToken();
429c464ae8444edb6d07ea49b7a0eae1674c0fa1bb8Douglas Gregor  } else if (Tok.isObjCAtKeyword(tok::objc_end))
430bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
431bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  else
432bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    Diag(Tok, diag::err_objc_missing_end);
4331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
434a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
435bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
436782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  Actions.ActOnAtEnd(AtEnd, interfaceDecl,
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
438beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
439beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
440294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
441294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
442d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
443d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
444d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
445d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
446d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
447d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
448d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
449d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
450d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
451d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
452d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
453d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
454d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
455d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
456d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
457d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
4584ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregorvoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS, DeclPtrTy ClassDecl,
4594ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                        DeclPtrTy *Methods,
4604ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                        unsigned NumMethods) {
4618ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
462dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  SourceLocation LHSLoc = ConsumeParen(); // consume '('
4631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
464cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
465ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
466a93b108e025ef2480fa867cc533e7781a40a639bDouglas Gregor      Actions.CodeCompleteObjCPropertyFlags(CurScope, DS);
467ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff      ConsumeToken();
468ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
469d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
471f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
472f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
473f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      MatchRHSPunctuation(tok::r_paren, LHSLoc);
474f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
475f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
4761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
477156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
47992e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
480e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
48192e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
482e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
48392e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
484e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
48592e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
486e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
48792e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
488e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
48992e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
490e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
49192e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
492e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
493156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
494156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                           tok::r_paren))
495dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
4961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4974ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      if (Tok.is(tok::code_completion)) {
4984ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        if (II->getNameStart()[0] == 's')
4994ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor          Actions.CodeCompleteObjCPropertySetter(CurScope, ClassDecl,
5004ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                                 Methods, NumMethods);
5014ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        else
5024ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor          Actions.CodeCompleteObjCPropertyGetter(CurScope, ClassDecl,
5034ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor                                                 Methods, NumMethods);
5044ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor        ConsumeToken();
5054ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor      }
5064ad9685b3e2d5e2923c9cda7baaf7973ef0b1c62Douglas Gregor
5078ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.isNot(tok::identifier)) {
5081ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(Tok, diag::err_expected_ident);
5098ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
5108ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
5118ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
5121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
513e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar      if (II->getNameStart()[0] == 's') {
5148ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
5158ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setSetterName(Tok.getIdentifierInfo());
516156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
5171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
518e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian        if (ExpectAndConsume(tok::colon,
519e0097db2848c463a534c18c235c6d3e53f2f1b87Fariborz Jahanian                             diag::err_expected_colon_after_setter_name, "",
520156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
5218ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
5228ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
5238ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
5248ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setGetterName(Tok.getIdentifierInfo());
525156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
5268ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
527e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
528a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
529cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
530cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
531cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
5321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
533156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
534156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
536156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
537d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
5381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
539156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner  MatchRHSPunctuation(tok::r_paren, LHSLoc);
540d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
541d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
5423536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
5431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
5443536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
545294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
546294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
547294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
548294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5494985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
5504985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
5514985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
5521eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
553b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
554df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
555294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
557bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
5581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
559b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
5603536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
5612bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
562f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
563294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
564294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
565294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
566294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
567294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
568294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
569294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
570294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
571294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
572294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5732fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
574ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
575ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
576ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
577ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
578ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
579ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
5809298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
581ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
582ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
583ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
584ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
585ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
586ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
587ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
588ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
589ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
590ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
591ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
592ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
593ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
594ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
595ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
596ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
597ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
598ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
599ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
600ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
601ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
602ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
603ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
604ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
605ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
606ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
607ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
608ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
609ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
610ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
611ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
612ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
613ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
614ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
615ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
616ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
617ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
618ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
619ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
620ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
621ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
622ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
623ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
624ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
625ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
626ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
627ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
628ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
629ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
630ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
631ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
632ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
633ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
634ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
635ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
636ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
637ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
638ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
639ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
640ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
641ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
642ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
643ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
644ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
645ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
646ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
6474b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
648ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
649d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
650294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
651294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6520196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
6530196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
654335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
6553ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
6563ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
6573ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
6581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
6595ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
6600196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
6610196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
662a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
663e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
664e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
665294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
666294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
667294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
668294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
669294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
670a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
671e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
672cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
673e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
6741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
675e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
676e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
677a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
678e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
6791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
680a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
681e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
682e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      default: assert(0 && "Unknown decl qualifier");
683a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
684a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
685a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
686a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
687a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
688a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
689e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
690a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
691e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
692e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
693e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
694e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
6951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
696e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
697e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
698e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
699e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
700e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
701e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
702e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
703e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
704e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
705a526c5c67e5a0473c340903ee542ce570119665fTed KremenekParser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
706df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7084a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  SourceLocation LParenLoc = ConsumeParen();
709e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
7101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71119d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
712a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ParseObjCTypeQualifierList(DS);
7134fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
7144a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  TypeTy *Ty = 0;
715809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
716809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult TypeSpec = ParseTypeName();
717809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (!TypeSpec.isInvalid())
718809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      Ty = TypeSpec.get();
719809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7214a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
7224a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    ConsumeParen();
7234a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
724e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
7251ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
7264a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
7274a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
7284a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
7294a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
7304a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    MatchRHSPunctuation(tok::r_paren, LParenLoc);
731294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
732f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
733294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
734294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
735294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
736294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
7374985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
738294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
7394985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
740294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
741294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
7421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
743294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
744294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
745294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
7467ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
7477ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
7487ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
7497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
750294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7514985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
7524985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
753294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7544985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
7554985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
756294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7574985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
758294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
759294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7607ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
7617ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
7627ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
763b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
764b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              tok::TokenKind mType,
765b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              DeclPtrTy IDecl,
766b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
76754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
76854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
769e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
770ff38491c18b060526d754765b952f4a497a89416Chris Lattner  TypeTy *ReturnType = 0;
771a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
772df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
773f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ReturnType = ParseObjCTypeName(DSRet);
7741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
775bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
7762fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
777e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
77884c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
77984c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
7801ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
7811ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
782e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
783e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
784b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
785e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
7861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
787439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian  llvm::SmallVector<Declarator, 8> CargNames;
788df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
789ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
7901e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek    llvm::OwningPtr<AttributeList> MethodAttrs;
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
7921e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek      MethodAttrs.reset(ParseGNUAttributes());
7931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
794ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
79554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DeclPtrTy Result
79654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall         = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
7971f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                          mType, IDecl, DSRet, ReturnType, Sel,
7981e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek                                          0, CargNames, MethodAttrs.get(),
7991c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                          MethodImplKind);
80054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
80154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
802ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
803f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
80468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
805e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner  llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
807ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
808e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    Action::ObjCArgInfo ArgInfo;
8091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
810ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
811df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
812ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
813ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
814ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
815ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
817e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Type = 0;
818e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
819e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
820e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
821ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
822e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
823df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
824bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      ArgInfo.ArgAttrs = ParseGNUAttributes();
8257ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
826df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
827ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
828ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
8294985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
8301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
831e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
832e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
833ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
8341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
835e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
836e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
837e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
838ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
8394b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
8402fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner    SelIdent = ParseObjCSelectorPiece(Loc);
841df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
842ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
843ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
844ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
8451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
846335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
8471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
848ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
849df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
850ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
851df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
852335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
8534985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
854ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
8554985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
856ff38491c18b060526d754765b952f4a497a89416Chris Lattner    DeclSpec DS;
857ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
8581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
859ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
860ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
861439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian    CargNames.push_back(ParmDecl);
8624985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
8631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
864ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // FIXME: Add support for optional parmameter list...
865e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
8661e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek  llvm::OwningPtr<AttributeList> MethodAttrs;
8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
8681e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek    MethodAttrs.reset(ParseGNUAttributes());
8691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8703688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian  if (KeyIdents.size() == 0)
8713688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian    return DeclPtrTy();
872ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
873ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
87454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  DeclPtrTy Result
87554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall       = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
8763688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian                                        mType, IDecl, DSRet, ReturnType, Sel,
8771e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek                                        &ArgInfos[0], CargNames,
8781e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek                                        MethodAttrs.get(),
879335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
88054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
8811e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek
8821e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek  // Delete referenced AttributeList objects.
8831e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek  for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
8841e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek       I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
8851e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek    delete I->ArgAttrs;
8861e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek
88754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
888294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
889294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
890dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
891dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
892dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
8937caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
894b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
89571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
89671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
89771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
898e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
902e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
904e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
90555385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
90655385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
90755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
90855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      ConsumeToken();
90955385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
91055385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
911e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
912e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
913e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
914e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
915e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
916e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
917e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
91871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
919e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
921e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
922e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
923e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
924e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
926e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
927e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
928e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
929e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
930e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
9311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
932e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
9331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
934e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
935e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
936e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
937e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
938e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
939e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
940e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
941dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
942dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
943dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
944dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
945dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
946dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
947dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
948dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
949dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
950dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
951dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
952dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
953dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
954dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
955dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
956ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
957dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
958dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
9591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
960dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
961b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
96260fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
963df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
964b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
965e13594279a952537ac903325efff57e3edca79d9Chris Lattner
9661a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
96772de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
968ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
9691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
970aa847fe3c88ee5e8d180e48537c58b805d48d95dFariborz Jahanian  tok::ObjCKeywordKind visibility = tok::objc_protected;
971ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
972df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
973ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
975ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
976df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
977c2253f5ca170984fcd4f30f8823148e8cb71336bChris Lattner      Diag(Tok, diag::ext_extra_struct_semi)
97829d9c1adfadf65e2d847d44bec37746844b9e0e3Chris Lattner        << CodeModificationHint::CreateRemoval(Tok.getLocation());
979ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
980ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
981ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
9821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
983ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
984df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
985ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
986c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
987c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
988c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(CurScope);
989c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor        ConsumeToken();
990c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
991c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
992861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
993ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
994ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
995ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
996ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
997861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
998ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
1000ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
1001ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1002ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
1003ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1004ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
10051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1006c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
1007c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      Actions.CodeCompleteOrdinaryName(CurScope,
1008c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor                                       Action::CCC_ObjCInstanceVariableList);
1009c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      ConsumeToken();
1010c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1011c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1012bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1013bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1014bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy IDecl;
1015bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
1016bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1017bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1018bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1019bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                       llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1020bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1021bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1022bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1023bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy invoke(FieldDeclarator &FD) {
1024bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1025bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy Field
1026bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          = P.Actions.ActOnIvar(P.CurScope,
1027bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1028bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                IDecl, FD.D, FD.BitfieldSize, visibility);
1029bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        AllIvarDecls.push_back(Field);
1030bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
1031bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1032bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1033bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1034e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
1035e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
1036bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1038df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1039ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1040ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1041ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1042ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1043ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1044ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1045ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
104660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
10478749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
10488749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
10498749be53f53384e7846502791ceda6c657228d07Steve Naroff  Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
1050beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                      AllIvarDecls.data(), AllIvarDecls.size(),
10511bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
1052ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
10535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1054dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1055dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1056dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1057dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1058dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1059dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
10611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
10621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1063dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1064dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1065dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1066dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1067dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1068dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
10693536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1070dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1071b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1072b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                                      AttributeList *attrList) {
1073861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
10747ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
10757ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1077083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
1078083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(CurScope);
1079083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor    ConsumeToken();
1080083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1081083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1082df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
10837ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1084b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
10857ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10867ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
10877ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
10887ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
10891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1090df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
10917caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
10927ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
1094bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
10957ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1097df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
10987caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
10997caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
11007caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
11017ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
11027ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
11037ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1104df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
11057ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
11067ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1107b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner        return DeclPtrTy();
11087ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
11097caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
11107caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
11117ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
11121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1113df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
11147ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
11157ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
11167ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
11177ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1118b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
11191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1120e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1122bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
1123bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
11247caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11267ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
112771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
11287caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
1129b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
113071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
11317caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
113271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
113371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1134b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
11351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1136b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ProtoType =
1137e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1138beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1139beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
114018df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
1141246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                        EndProtoLoc, attrList);
114225e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1143bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1145dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1146dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1149dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1150dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1151dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1152dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1153dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1154dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1155dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1156b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
1157ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1158ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1159ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1160ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
11611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11623b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
11633b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
11643b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(CurScope);
11653b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    ConsumeToken();
11663b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
11673b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1168df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1169ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1170b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1171ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1172ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1173ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1174ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1177ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1178ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1179ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1180ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
11811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
118233ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
118333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
118433ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      ConsumeToken();
118533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
118633ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1187df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1188ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1189ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1190ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1191ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1192b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1194df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1195ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1196ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1197b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1198ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1199ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1200b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    atLoc, nameId, nameLoc, categoryId,
12028f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1203a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
120463e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.push_back(ObjCImpDecl);
1205b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1206ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1207ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1208ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1209ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1210df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1211ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1212ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1213df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1214ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1215b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1216ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1217ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1218ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1219ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1220b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
1221cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1222ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
122560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
1226a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
122763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  PendingObjCImpDecl.push_back(ObjCImpDecl);
122863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1229b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
12305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
123160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1232782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted KremenekParser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1233ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1234ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1235b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy Result = ObjCImpDecl;
1236ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1237a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
1238782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
1239b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    ObjCImpDecl = DeclPtrTy();
124063e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.pop_back();
1241a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
1242782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  else {
1243782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1244782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1245782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  }
1246a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  return Result;
12475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1248e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
12491ac7104947020a60430ba6f519cd5869af77046fFariborz JahanianParser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
125063e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  if (PendingObjCImpDecl.empty())
125163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
125263e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1253782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  Actions.ActOnAtEnd(SourceRange(), ImpDecl);
125463e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  return Actions.ConvertDeclToDeclGroup(ImpDecl);
125563e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian}
125663e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1257e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1258e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1259e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1260b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1261e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1262e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1263e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1264df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1265e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1266b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1267e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1268243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1269243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1270df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1271e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1272b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1273e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1274243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1275243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1276243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
12771ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1278b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1279243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1280b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1281b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1284ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1285ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1286ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1287ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1288ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1289ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1290ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1291ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1292ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1293ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1294ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1295b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1296ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1297ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1298f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
12991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1300b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1301322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
1302424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1303322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      ConsumeToken();
1304322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1305322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1306b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1307b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1308b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1309b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      return DeclPtrTy();
1310b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1311b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1312f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1313f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1314f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1315df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1316ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1317ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1318322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1319322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
1320322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor        Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1321322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor                                                       ObjCImpDecl);
1322322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor        ConsumeToken();
1323322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1324322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1325df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1326ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1327ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1328ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1329f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1330ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume ivar-name
1331ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1332f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1333f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian                                  propertyId, propertyIvar);
1334df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1335ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1336ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1337ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1338b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  if (Tok.isNot(tok::semi)) {
13391ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1340b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    SkipUntil(tok::semi);
1341b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  }
1342d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian  else
1343d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian    ConsumeToken(); // consume ';'
1344b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1345ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1346ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1347ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1348ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1349ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1350ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1351ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1352ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1353ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1354b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1355ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1356ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1357ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1358424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1359424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
1360424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1361424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      ConsumeToken();
1362424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1363424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1364424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1365424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1366424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1367424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      return DeclPtrTy();
1368424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1369424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1370c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1371c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1372c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1373c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian                                  propertyId, 0);
1374c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1375df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1376ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1377ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1378ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1379df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
13801ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
1381b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1382ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
13831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1384397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1385397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1386397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
138743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
138815faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Res(Actions);
1389397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1390df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
139139f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
13920e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1393397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
139443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1395397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1396397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
139739f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian  ConsumeToken(); // consume ';'
1398e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff  return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
1399397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1400397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1401c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
140278a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1403c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
140443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult
140543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1406fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1407fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
14081ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
140943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1410fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1411fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
14122f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
14130e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
1414fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
141543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1416fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1417fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
14181ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
141943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1420fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1421fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
142278a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
14231ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
142443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
142578a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
14263ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
14273ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
14288935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::DeclScope);
14293ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
143061364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult SynchBody(ParseCompoundStatementBody());
14310e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
14328935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
14330e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (SynchBody.isInvalid())
1434fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
143576ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
1436c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1437c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1438397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1439397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1440397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1441397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1442397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1443397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1444397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1445397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1446397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1447397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1448397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
144943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1450397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
145143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1452397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1453df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
14541ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
145543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1456397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
145715faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult CatchStmts(Actions);
145815faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult FinallyStmt(Actions);
14598935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
146061364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult TryBody(ParseCompoundStatementBody());
14618935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
14620e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1463bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1464a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1465df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
14666b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
14676b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
14686b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
14696b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
14706b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
14716b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
14726b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
14730e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1474161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1475cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1476b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      DeclPtrTy FirstPart;
14773b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1478df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1479397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1480e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1481df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1482397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1483397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
148443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl          // For some odd reason, the name of the exception variable is
14851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // optional. As a result, we need to use "PrototypeContext", because
14867ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // we must accept either 'declarator' or 'abstract-declarator' here.
14877ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          Declarator ParmDecl(DS, Declarator::PrototypeContext);
14887ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
14897ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
14907ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // Inform the actions module about the parameter declarator, so it
14917ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
1492d219a3a462c31fc9aa0c0bcfb749e9e8e56b5e35Fariborz Jahanian          // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
14937ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
14942f764f11f513c7b51c716fffa5d02e5de816836fFariborz Jahanian          Actions.ActOnObjCCatchParam(FirstPart);
149564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1496397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
14971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149893a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
150093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
150193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
150293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
150393a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
150493a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
150515faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl        OwningStmtResult CatchBody(Actions, true);
1506c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1507c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1508c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1509c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
15100e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
15113b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
15120e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
15137ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff                        RParenLoc, FirstPart, move(CatchBody),
151476ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                        move(CatchStmts));
151564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
15161ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
15171ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
151843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1519397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1520397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
15216b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
15226b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
152364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
15248935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
15250e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
152615faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl      OwningStmtResult FinallyBody(Actions, true);
1527c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1528c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1529c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1530c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
15310e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1532161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
15330e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
153476ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                                   move(FinallyBody));
1535397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1536397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1537397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1538397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1539bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1540397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
154143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1542bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
154376ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
154476ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                    move(FinallyStmt));
1545397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1546ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
15473536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1548ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1549b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1550b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
15511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
155349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        PP.getSourceManager(),
155449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        "parsing Objective-C method");
15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1556ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1557209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1558496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    if (ObjCImpDecl) {
1559496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
156029d9c1adfadf65e2d847d44bec37746844b9e0e3Chris Lattner        << CodeModificationHint::CreateRemoval(Tok.getLocation());
1561496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1562ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1563209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1564ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1565409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1566df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1567da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
15681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1569409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1570409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1572409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1573409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1574b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1575ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1576409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
15771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1578409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
15798935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
15801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1581409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1582394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
1583ebf6443a4f493233f7e8d92b3a991848c8b1c00dSteve Naroff  Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
158461364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
158561364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult FnBody(ParseCompoundStatementBody());
158661364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
1587409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
15880e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (FnBody.isInvalid())
1589a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1590a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                       MultiStmtArg(Actions), false);
1591798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
159232ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  // TODO: Pass argument information.
159332ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
15941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1595409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
15968935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
1597798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
159871c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
15995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
16005508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
160143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
16029a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
16039a0c85e640a08174569a303db22981612f05d385Douglas Gregor    Actions.CodeCompleteObjCAtStatement(CurScope);
16049a0c85e640a08174569a303db22981612f05d385Douglas Gregor    ConsumeToken();
16059a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
16065d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
16075d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16085d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
16096b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
16105d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16115d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
161264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
16135d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16145d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
161564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
16165d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
1617d8c4e15138e69a51754cc259c8a592cc47950c8eSebastian Redl  OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
16180e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
161964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
162064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
162164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
162264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
162343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
162464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
16255d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
162664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
162764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
16285ee56e95c3905d2e7bc403631b03865cdbdd8a42Anders Carlsson  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
162964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
163064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
16311d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
16325508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
16339a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
16349a0c85e640a08174569a303db22981612f05d385Douglas Gregor    Actions.CodeCompleteObjCAtExpression(CurScope);
16359a0c85e640a08174569a303db22981612f05d385Douglas Gregor    ConsumeToken();
16369a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
16379a0c85e640a08174569a303db22981612f05d385Douglas Gregor
1638b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1639b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
16401d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1641b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
16424fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
16431d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
16442f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
16454fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
16464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
16471d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
16484fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
16491d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
16504fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
16511d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
16524fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
16531d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
16544fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
16555508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
16565508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
16575508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
16590ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
16600ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16610ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-receiver:
16620ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
16630ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
16640ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
16651d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCMessageExpression() {
1666699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1667699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1668699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
1669699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  // Parse receiver
167014dd98a7952c9559e0d17fff8272bf3be67135afChris Lattner  if (isTokObjCMessageIdentifierReceiver()) {
1671699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1672d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1673d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      SourceLocation NameLoc = ConsumeToken();
1674d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1675d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian                                            ExprArg(Actions));
1676d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
1677699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
1678699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
16792f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
16800e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
16815c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
16821d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
1683699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
16841d922960e083906a586609ac6978678147250177Sebastian Redl
16850e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
168676ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                        0, move(Res));
1687699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
16881d922960e083906a586609ac6978678147250177Sebastian Redl
1689699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1690699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// the rest of a message expression.
16911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
16920ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
16930ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
16940ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
16950ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16960ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
16970ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
16980ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
16990ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
17010ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
17020ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
17030ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
17040ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
17050ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
17060ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
17070ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
17080ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
17091d922960e083906a586609ac6978678147250177Sebastian Redl///
17101d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
1711699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
17125cb93b8bf009c4b0ae09b71ba85f54b2a7ea8022Steve Naroff                                       SourceLocation NameLoc,
1713699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                       IdentifierInfo *ReceiverName,
17141d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
1715c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
1716c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    if (ReceiverName)
1717d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1718d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                           0, 0);
1719c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
1720d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1721d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                              0, 0);
1722c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    ConsumeToken();
1723c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
1724d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
1725a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
17264b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
17272fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
172868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1729ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson  SourceLocation SelectorLoc = Loc;
17301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1732a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
173368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1734df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1735a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
1736a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
173768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
173837387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
1739df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
1740a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
17414fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17424fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17434fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17444fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17451d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
1746a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
17471d922960e083906a586609ac6978678147250177Sebastian Redl
174868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
17491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
17502f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
17510e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
17524fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17534fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17544fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17554fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17561d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
175737387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
17581d922960e083906a586609ac6978678147250177Sebastian Redl
175937387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
1760effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
17611d922960e083906a586609ac6978678147250177Sebastian Redl
1762d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
1763d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
1764d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        if (ReceiverName)
1765d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor          Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1766d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
1767d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.size());
1768d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
1769d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor          Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1770d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
1771d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.size());
1772d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        ConsumeToken();
1773d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
1774d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
177537387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
17762fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
1777df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
1778a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
1779a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
1780a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1781a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
1782df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
178349f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
17841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
17852f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
17860e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
17874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17884fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17904fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17911d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
179249f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
17930e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
179449f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
1795effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
1796a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1797a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
1798a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
17991d922960e083906a586609ac6978678147250177Sebastian Redl
18004fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
18014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
18024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
18034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
18041d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1805a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
18061d922960e083906a586609ac6978678147250177Sebastian Redl
1807df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
1808a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_rsquare);
18094fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
18104fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
18114fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
18124fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
18131d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1814a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
18151d922960e083906a586609ac6978678147250177Sebastian Redl
1816699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
18171d922960e083906a586609ac6978678147250177Sebastian Redl
181829238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
1819ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
1820ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
1821ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
18221d922960e083906a586609ac6978678147250177Sebastian Redl
1823ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // We've just parsed a keyword message.
18241d922960e083906a586609ac6978678147250177Sebastian Redl  if (ReceiverName)
18251d922960e083906a586609ac6978678147250177Sebastian Redl    return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
18261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           LBracLoc, NameLoc, SelectorLoc,
1827ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                           RBracLoc,
18281d922960e083906a586609ac6978678147250177Sebastian Redl                                           KeyExprs.take(), KeyExprs.size()));
18291d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
1830ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                            LBracLoc, SelectorLoc, RBracLoc,
18311d922960e083906a586609ac6978678147250177Sebastian Redl                                            KeyExprs.take(), KeyExprs.size()));
18320ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
18330ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
18341d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
183520df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  OwningExprResult Res(ParseStringLiteralExpression());
18361d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
18371d922960e083906a586609ac6978678147250177Sebastian Redl
1838b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
1839b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
1840b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
1841b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<SourceLocation, 4> AtLocs;
1842a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
1843b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
1844effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
18450e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1846b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
1847b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
1848b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
184915faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
185097cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
185197cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
1852b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
185397cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    OwningExprResult Lit(ParseStringLiteralExpression());
18540e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
18551d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
18560e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1857effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
1858b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
18591d922960e083906a586609ac6978678147250177Sebastian Redl
18601d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
18611d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
18625508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
1863f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1864f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
1865f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
18661d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
18671d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
1868861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
18691d922960e083906a586609ac6978678147250177Sebastian Redl
1870f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
18711d922960e083906a586609ac6978678147250177Sebastian Redl
18724fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
18731d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
18741d922960e083906a586609ac6978678147250177Sebastian Redl
1875f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
18761d922960e083906a586609ac6978678147250177Sebastian Redl
1877809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
18781d922960e083906a586609ac6978678147250177Sebastian Redl
18794988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
18801d922960e083906a586609ac6978678147250177Sebastian Redl
1881809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1882809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1883809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
18841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1885809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                                 Ty.get(), RParenLoc));
1886f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
188729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
188829b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
188929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
18901d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
18911d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
189229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
18931d922960e083906a586609ac6978678147250177Sebastian Redl
18944fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
18951d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
18961d922960e083906a586609ac6978678147250177Sebastian Redl
189729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
18981d922960e083906a586609ac6978678147250177Sebastian Redl
18994fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
19001d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
19011d922960e083906a586609ac6978678147250177Sebastian Redl
1902390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
190329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
19041d922960e083906a586609ac6978678147250177Sebastian Redl
19054988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
190629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
19071d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
19081d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
190929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
1910a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
1911a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
1912a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
19131d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
19141d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
1915a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
19161d922960e083906a586609ac6978678147250177Sebastian Redl
19174fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
19181d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
19191d922960e083906a586609ac6978678147250177Sebastian Redl
1920b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1921a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
1922a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
19232fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
19241d922960e083906a586609ac6978678147250177Sebastian Redl  if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
19251d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
19261d922960e083906a586609ac6978678147250177Sebastian Redl
1927b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
1928887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
1929887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
1930a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
19314fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      if (Tok.isNot(tok::colon))
19321d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
19331d922960e083906a586609ac6978678147250177Sebastian Redl
1934cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      nColons++;
1935a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
1936a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
1937a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1938a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
1939a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
19402fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
1941b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
1942a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
1943a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1944a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
1945887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
1946a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1947887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
19481d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
19491d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
195058065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
1951