ParseObjc.cpp revision d219a3a462c31fc9aa0c0bcfb749e9e8e56b5e35
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
518156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
519156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
5208ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
5218ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
5228ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
5238ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setGetterName(Tok.getIdentifierInfo());
524156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
5258ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
526e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
527a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
528cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
529cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
530cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
532156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
533156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
5341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
535156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
536d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
5371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
538156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner  MatchRHSPunctuation(tok::r_paren, LHSLoc);
539d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
540d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
5413536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
5433536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
544294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
545294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
546294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
547294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5484985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
5494985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
5504985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
5511eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
552b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
553df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
554294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
5551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
556bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
5571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
558b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
5593536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
5602bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
561f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
562294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
563294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
564294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
565294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
566294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
567294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
568294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
569294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
570294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
571294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5722fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
573ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
574ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
575ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
576ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
577ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
578ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
5799298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
580ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
581ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
582ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
583ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
584ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
585ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
586ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
587ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
588ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
589ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
590ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
591ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
592ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
593ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
594ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
595ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
596ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
597ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
598ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
599ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
600ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
601ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
602ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
603ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
604ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
605ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
606ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
607ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
608ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
609ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
610ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
611ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
612ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
613ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
614ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
615ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
616ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
617ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
618ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
619ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
620ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
621ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
622ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
623ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
624ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
625ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
626ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
627ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
628ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
629ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
630ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
631ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
632ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
633ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
634ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
635ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
636ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
637ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
638ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
639ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
640ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
641ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
642ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
643ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
644ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
645ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
6464b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
647ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
648d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
649294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
650294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6510196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
6520196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
653335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
6543ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
6553ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
6563ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
6571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
6585ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
6590196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
6600196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
661a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
662e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
663e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
664294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
665294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
666294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
667294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
668294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
669a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
670e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
671cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
672e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
6731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
674e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
675e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
676a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
677e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
6781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
679a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
680e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
681e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      default: assert(0 && "Unknown decl qualifier");
682a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
683a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
684a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
685a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
686a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
687a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
688e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
689a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
690e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
691e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
692e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
693e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
695e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
696e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
697e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
698e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
699e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
700e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
701e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
702e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
703e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
704a526c5c67e5a0473c340903ee542ce570119665fTed KremenekParser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
705df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7074a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  SourceLocation LParenLoc = ConsumeParen();
708e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
7091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
71019d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
711a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ParseObjCTypeQualifierList(DS);
7124fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
7134a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  TypeTy *Ty = 0;
714809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
715809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult TypeSpec = ParseTypeName();
716809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (!TypeSpec.isInvalid())
717809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      Ty = TypeSpec.get();
718809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
7191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7204a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
7214a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    ConsumeParen();
7224a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
723e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
7241ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
7254a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
7264a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
7274a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
7284a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
7294a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    MatchRHSPunctuation(tok::r_paren, LParenLoc);
730294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
731f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
732294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
733294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
734294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
735294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
7364985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
737294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
7384985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
739294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
740294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
7411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
742294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
743294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
744294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
7457ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
7467ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
7477ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
7487ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
749294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7504985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
7514985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
752294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7534985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
7544985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
755294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7564985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
757294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
758294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7597ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
7607ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
7617ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
762b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
763b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              tok::TokenKind mType,
764b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              DeclPtrTy IDecl,
765b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
76654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
76754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
768e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
769ff38491c18b060526d754765b952f4a497a89416Chris Lattner  TypeTy *ReturnType = 0;
770a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
771df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
772f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ReturnType = ParseObjCTypeName(DSRet);
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
774bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
7752fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
776e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
77784c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
77884c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
7791ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
7801ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
781e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
782e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
783b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
784e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
7851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
786439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian  llvm::SmallVector<Declarator, 8> CargNames;
787df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
788ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
789ff38491c18b060526d754765b952f4a497a89416Chris Lattner    AttributeList *MethodAttrs = 0;
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
791bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      MethodAttrs = ParseGNUAttributes();
7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
793ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
79454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DeclPtrTy Result
79554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall         = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
7961f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                          mType, IDecl, DSRet, ReturnType, Sel,
7971c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                          0, CargNames, MethodAttrs,
7981c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                          MethodImplKind);
79954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
80054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
801ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
802f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
80368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
804e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner  llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
8051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
806ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
807e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    Action::ObjCArgInfo ArgInfo;
8081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
809ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
810df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
811ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
812ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
813ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
814ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
8151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
816e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Type = 0;
817e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
818e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
819e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
820ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
821e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
822df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
823bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt      ArgInfo.ArgAttrs = ParseGNUAttributes();
8247ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
825df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
826ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
827ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
8284985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
8291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
830e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
831e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
832ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
834e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
835e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
836e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
837ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
8384b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
8392fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner    SelIdent = ParseObjCSelectorPiece(Loc);
840df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
841ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
842ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
843ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
8441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
845335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
8461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
847ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
848df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
849ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
850df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
851335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
8524985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
853ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
8544985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
855ff38491c18b060526d754765b952f4a497a89416Chris Lattner    DeclSpec DS;
856ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
8571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
858ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
859ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
860439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian    CargNames.push_back(ParmDecl);
8614985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
8621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
863ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // FIXME: Add support for optional parmameter list...
864e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
865ff38491c18b060526d754765b952f4a497a89416Chris Lattner  AttributeList *MethodAttrs = 0;
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
867bbd37c62e34db3f5a95c899723484a76c71d7757Sean Hunt    MethodAttrs = ParseGNUAttributes();
8681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8693688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian  if (KeyIdents.size() == 0)
8703688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian    return DeclPtrTy();
871ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
872ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
87354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  DeclPtrTy Result
87454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall       = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
8753688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian                                        mType, IDecl, DSRet, ReturnType, Sel,
8761c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                        &ArgInfos[0], CargNames, MethodAttrs,
877335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
87854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
87954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
880294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
881294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
882dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
883dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
884dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
8857caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
886b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
88771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
88871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
88971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
890e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
8911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
8931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
894e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
896e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
89755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
89855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
89955385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
90055385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      ConsumeToken();
90155385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
90255385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
903e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
904e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
905e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
906e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
907e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
908e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
909e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
91071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
911e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
9121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
913e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
914e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
915e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
916e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
9171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
918e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
919e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
920e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
921e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
922e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
924e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
926e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
927e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
928e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
929e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
930e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
931e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
932e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
933dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
934dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
935dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
936dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
937dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
938dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
939dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
940dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
941dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
942dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
943dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
944dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
945dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
946dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
947dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
948ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
949dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
950dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
9511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
952dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
953b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
95460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
955df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
956b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
957e13594279a952537ac903325efff57e3edca79d9Chris Lattner
9581a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
95972de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
960ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
962aa847fe3c88ee5e8d180e48537c58b805d48d95dFariborz Jahanian  tok::ObjCKeywordKind visibility = tok::objc_protected;
963ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
964df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
965ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
9661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
967ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
968df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
969c2253f5ca170984fcd4f30f8823148e8cb71336bChris Lattner      Diag(Tok, diag::ext_extra_struct_semi)
97029d9c1adfadf65e2d847d44bec37746844b9e0e3Chris Lattner        << CodeModificationHint::CreateRemoval(Tok.getLocation());
971ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
972ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
973ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
975ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
976df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
977ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
978c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
979c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
980c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(CurScope);
981c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor        ConsumeToken();
982c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
983c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
984861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
985ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
986ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
987ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
988ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
989861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
990ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
9911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
992ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
993ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
994ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
995ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
996ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
998c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
999c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      Actions.CodeCompleteOrdinaryName(CurScope,
1000c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor                                       Action::CCC_ObjCInstanceVariableList);
1001c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      ConsumeToken();
1002c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1003c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1004bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1005bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1006bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy IDecl;
1007bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
1008bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1009bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1010bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1011bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                       llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1012bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1013bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1014bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1015bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy invoke(FieldDeclarator &FD) {
1016bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1017bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy Field
1018bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          = P.Actions.ActOnIvar(P.CurScope,
1019bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1020bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                IDecl, FD.D, FD.BitfieldSize, visibility);
1021bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        AllIvarDecls.push_back(Field);
1022bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
1023bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1024bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1025bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1026e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
1027e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
1028bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
10291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1030df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1031ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1032ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1033ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1034ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1035ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1036ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1037ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
103860fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
10398749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
10408749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
10418749be53f53384e7846502791ceda6c657228d07Steve Naroff  Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
1042beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                      AllIvarDecls.data(), AllIvarDecls.size(),
10431bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
1044ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
10455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1046dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1047dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1048dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1049dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1050dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1051dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
10521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
10531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1055dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1056dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1057dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1058dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1059dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1060dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
10613536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1062dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1063b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1064b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                                      AttributeList *attrList) {
1065861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
10667ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
10677ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
10681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1069083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
1070083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(CurScope);
1071083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor    ConsumeToken();
1072083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1073083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1074df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
10757ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1076b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
10777ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10787ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
10797ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
10807ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
10811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1082df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
10837caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
10847ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
1086bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
10877ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1089df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
10907caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
10917caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
10927caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
10937ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
10947ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
10957ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1096df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
10977ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
10987ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1099b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner        return DeclPtrTy();
11007ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
11017caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
11027caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
11037ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1105df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
11067ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
11077ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
11087ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
11097ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1110b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1112e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1114bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
1115bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
11167caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11187ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
111971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
11207caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
1121b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
112271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
11237caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
112471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
112571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1126b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
11271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1128b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ProtoType =
1129e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1130beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1131beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
113218df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
1133246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                        EndProtoLoc, attrList);
113425e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1135bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1136dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1137dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1138dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1139dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1140dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1141dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1142dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1143dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1145dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1146dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1148b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
1149ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1150ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1151ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1152ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
11531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11543b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
11553b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
11563b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(CurScope);
11573b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    ConsumeToken();
11583b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
11593b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1160df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1161ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1162b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1163ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1164ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1165ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1166ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
11671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1169ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1170ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1171ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1172ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
11731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117433ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
117533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
117633ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      ConsumeToken();
117733ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
117833ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1179df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1180ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1181ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1182ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1183ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1184b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1186df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1187ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1188ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1189b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1190ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1191ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1192b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    atLoc, nameId, nameLoc, categoryId,
11948f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1195a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
119663e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.push_back(ObjCImpDecl);
1197b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1198ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1199ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1200ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1201ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1202df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1203ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1204ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1205df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1206ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1207b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1208ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1209ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1210ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1211ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1212b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
1213cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1214ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
12151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
121660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
121760fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
1218a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
121963e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  PendingObjCImpDecl.push_back(ObjCImpDecl);
122063e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1221b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
12225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
122360fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1224782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted KremenekParser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1225ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1226ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1227b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy Result = ObjCImpDecl;
1228ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1229a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
1230782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
1231b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    ObjCImpDecl = DeclPtrTy();
123263e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.pop_back();
1233a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
1234782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  else {
1235782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1236782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1237782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  }
1238a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  return Result;
12395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1240e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
12411ac7104947020a60430ba6f519cd5869af77046fFariborz JahanianParser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
124263e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  if (PendingObjCImpDecl.empty())
124363e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
124463e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1245782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  Actions.ActOnAtEnd(SourceRange(), ImpDecl);
124663e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  return Actions.ConvertDeclToDeclGroup(ImpDecl);
124763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian}
124863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1249e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1250e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1251e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1252b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1253e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1254e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1255e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1256df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1257e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1258b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1259e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1260243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1261243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1262df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1263e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1264b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1265e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1266243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1267243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1268243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
12691ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1270b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1271243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1272b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1273b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
12745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1276ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1277ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1278ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1279ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1280ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1281ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1282ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1283ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1284ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1285ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1286ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1287b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1288ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1289ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1290f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
12911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1292b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1293322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
1294424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1295322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      ConsumeToken();
1296322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1297322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1298b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1299b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1300b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1301b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      return DeclPtrTy();
1302b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1303b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1304f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1305f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1306f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1307df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1308ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1309ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1310322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1311322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
1312322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor        Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1313322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor                                                       ObjCImpDecl);
1314322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor        ConsumeToken();
1315322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1316322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1317df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1318ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1319ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1320ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1321f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1322ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume ivar-name
1323ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1324f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1325f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian                                  propertyId, propertyIvar);
1326df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1327ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1328ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1329ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1330b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  if (Tok.isNot(tok::semi)) {
13311ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1332b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    SkipUntil(tok::semi);
1333b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  }
1334d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian  else
1335d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian    ConsumeToken(); // consume ';'
1336b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1337ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1338ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1339ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1340ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1341ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1342ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1343ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1344ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1345ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1346b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1347ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1348ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1349ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1350424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1351424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
1352424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1353424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      ConsumeToken();
1354424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1355424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1356424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1357424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1358424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1359424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      return DeclPtrTy();
1360424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1361424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1362c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1363c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1364c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1365c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian                                  propertyId, 0);
1366c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1367df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1368ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1369ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1370ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1371df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
13721ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
1373b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1374ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
13751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1376397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1377397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1378397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
137943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
138015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Res(Actions);
1381397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1382df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
138339f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
13840e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1385397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
138643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1387397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1388397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
138939f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian  ConsumeToken(); // consume ';'
1390e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff  return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
1391397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1392397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1393c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
139478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1395c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
139643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult
139743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1398fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1399fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
14001ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
140143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1402fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1403fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
14042f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
14050e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
1406fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
140743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1408fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1409fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
14101ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
141143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1412fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1413fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
141478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
14151ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
141643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
141778a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
14183ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
14193ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
14208935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::DeclScope);
14213ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
142261364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult SynchBody(ParseCompoundStatementBody());
14230e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
14248935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
14250e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (SynchBody.isInvalid())
1426fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
142776ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
1428c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1429c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1430397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1431397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1432397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1433397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1434397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1435397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1436397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1437397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1438397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1439397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1440397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
144143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1442397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
144343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1444397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1445df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
14461ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
144743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1448397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
144915faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult CatchStmts(Actions);
145015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult FinallyStmt(Actions);
14518935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
145261364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult TryBody(ParseCompoundStatementBody());
14538935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
14540e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1455bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1456a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1457df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
14586b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
14596b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
14606b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
14616b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
14626b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
14636b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
14646b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
14650e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1466161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1467cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1468b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      DeclPtrTy FirstPart;
14693b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1470df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1471397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1472e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1473df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1474397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1475397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
147643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl          // For some odd reason, the name of the exception variable is
14771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // optional. As a result, we need to use "PrototypeContext", because
14787ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // we must accept either 'declarator' or 'abstract-declarator' here.
14797ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          Declarator ParmDecl(DS, Declarator::PrototypeContext);
14807ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
14817ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
14827ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // Inform the actions module about the parameter declarator, so it
14837ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
1484d219a3a462c31fc9aa0c0bcfb749e9e8e56b5e35Fariborz Jahanian          // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
14857ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
14862f764f11f513c7b51c716fffa5d02e5de816836fFariborz Jahanian          Actions.ActOnObjCCatchParam(FirstPart);
148764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1488397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
14891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
14911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
149393a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
149493a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
149593a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
149693a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
149715faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl        OwningStmtResult CatchBody(Actions, true);
1498c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1499c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1500c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1501c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
15020e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
15033b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
15040e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
15057ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff                        RParenLoc, FirstPart, move(CatchBody),
150676ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                        move(CatchStmts));
150764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
15081ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
15091ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
151043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1511397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1512397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
15136b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
15146b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
151564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
15168935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
15170e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
151815faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl      OwningStmtResult FinallyBody(Actions, true);
1519c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1520c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1521c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1522c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
15230e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1524161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
15250e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
152676ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                                   move(FinallyBody));
1527397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1528397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1529397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1530397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1531bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1532397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
153343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1534bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
153576ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
153676ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                    move(FinallyStmt));
1537397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1538ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
15393536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1540ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1541b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1542b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
15431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
154449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
154549f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        PP.getSourceManager(),
154649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        "parsing Objective-C method");
15471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1548ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1549209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1550496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    if (ObjCImpDecl) {
1551496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
155229d9c1adfadf65e2d847d44bec37746844b9e0e3Chris Lattner        << CodeModificationHint::CreateRemoval(Tok.getLocation());
1553496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1554ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1555209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1556ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1557409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1558df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1559da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
15601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1561409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1562409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
15631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1564409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1565409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1566b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1567ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1568409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1570409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
15718935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
15721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1573409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1574394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
1575ebf6443a4f493233f7e8d92b3a991848c8b1c00dSteve Naroff  Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
157661364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
157761364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult FnBody(ParseCompoundStatementBody());
157861364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
1579409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
15800e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (FnBody.isInvalid())
1581a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1582a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                       MultiStmtArg(Actions), false);
1583798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
158432ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  // TODO: Pass argument information.
158532ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
15861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1587409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
15888935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
1589798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
159071c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
15915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15925508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
159343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
15949a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
15959a0c85e640a08174569a303db22981612f05d385Douglas Gregor    Actions.CodeCompleteObjCAtStatement(CurScope);
15969a0c85e640a08174569a303db22981612f05d385Douglas Gregor    ConsumeToken();
15979a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
15985d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
15995d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16005d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
16016b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
16025d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16035d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
160464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
16055d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16065d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
160764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
16085d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
1609d8c4e15138e69a51754cc259c8a592cc47950c8eSebastian Redl  OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
16100e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
161164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
161264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
161364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
161464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
161543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
161664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
16175d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
161864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
161964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
16205ee56e95c3905d2e7bc403631b03865cdbdd8a42Anders Carlsson  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
162164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
162264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
16231d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
16245508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
16259a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
16269a0c85e640a08174569a303db22981612f05d385Douglas Gregor    Actions.CodeCompleteObjCAtExpression(CurScope);
16279a0c85e640a08174569a303db22981612f05d385Douglas Gregor    ConsumeToken();
16289a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
16299a0c85e640a08174569a303db22981612f05d385Douglas Gregor
1630b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1631b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
16321d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1633b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
16344fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
16351d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
16362f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
16374fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
16384fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
16391d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
16404fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
16411d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
16424fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
16431d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
16444fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
16451d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
16464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
16475508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
16485508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
16495508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
16510ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
16520ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16530ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-receiver:
16540ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
16550ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
16560ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
16571d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCMessageExpression() {
1658699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1659699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1660699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
1661699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  // Parse receiver
166214dd98a7952c9559e0d17fff8272bf3be67135afChris Lattner  if (isTokObjCMessageIdentifierReceiver()) {
1663699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1664d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1665d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      SourceLocation NameLoc = ConsumeToken();
1666d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1667d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian                                            ExprArg(Actions));
1668d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
1669699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
1670699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
16712f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
16720e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
16735c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
16741d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
1675699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
16761d922960e083906a586609ac6978678147250177Sebastian Redl
16770e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
167876ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                        0, move(Res));
1679699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
16801d922960e083906a586609ac6978678147250177Sebastian Redl
1681699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1682699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// the rest of a message expression.
16831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
16840ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
16850ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
16860ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
16870ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16880ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
16890ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
16900ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
16910ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
16930ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
16940ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16950ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
16960ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
16970ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16980ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
16990ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
17000ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
17011d922960e083906a586609ac6978678147250177Sebastian Redl///
17021d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
1703699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
17045cb93b8bf009c4b0ae09b71ba85f54b2a7ea8022Steve Naroff                                       SourceLocation NameLoc,
1705699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                       IdentifierInfo *ReceiverName,
17061d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
1707c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
1708c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    if (ReceiverName)
1709d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1710d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                           0, 0);
1711c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
1712d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1713d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                              0, 0);
1714c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    ConsumeToken();
1715c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
1716d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
1717a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
17184b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
17192fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
172068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1721ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson  SourceLocation SelectorLoc = Loc;
17221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
172368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1724a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
172568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1726df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1727a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
1728a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
172968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
173037387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
1731df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
1732a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
17334fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17344fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17354fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17364fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17371d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
1738a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
17391d922960e083906a586609ac6978678147250177Sebastian Redl
174068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
17411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
17422f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
17430e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
17444fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17454fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17474fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17481d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
174937387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
17501d922960e083906a586609ac6978678147250177Sebastian Redl
175137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
1752effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
17531d922960e083906a586609ac6978678147250177Sebastian Redl
1754d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
1755d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
1756d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        if (ReceiverName)
1757d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor          Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1758d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
1759d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.size());
1760d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
1761d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor          Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1762d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
1763d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.size());
1764d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        ConsumeToken();
1765d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
1766d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
176737387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
17682fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
1769df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
1770a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
1771a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
1772a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1773a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
1774df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
177549f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
17761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
17772f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
17780e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
17794fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17804fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17814fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17824fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17831d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
178449f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
17850e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
178649f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
1787effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
1788a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1789a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
1790a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
17911d922960e083906a586609ac6978678147250177Sebastian Redl
17924fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
17934fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17944fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
17954fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
17961d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1797a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
17981d922960e083906a586609ac6978678147250177Sebastian Redl
1799df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
1800a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_rsquare);
18014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
18024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
18034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
18044fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
18051d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1806a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
18071d922960e083906a586609ac6978678147250177Sebastian Redl
1808699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
18091d922960e083906a586609ac6978678147250177Sebastian Redl
181029238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
1811ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
1812ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
1813ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
18141d922960e083906a586609ac6978678147250177Sebastian Redl
1815ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // We've just parsed a keyword message.
18161d922960e083906a586609ac6978678147250177Sebastian Redl  if (ReceiverName)
18171d922960e083906a586609ac6978678147250177Sebastian Redl    return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
18181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           LBracLoc, NameLoc, SelectorLoc,
1819ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                           RBracLoc,
18201d922960e083906a586609ac6978678147250177Sebastian Redl                                           KeyExprs.take(), KeyExprs.size()));
18211d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
1822ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                            LBracLoc, SelectorLoc, RBracLoc,
18231d922960e083906a586609ac6978678147250177Sebastian Redl                                            KeyExprs.take(), KeyExprs.size()));
18240ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
18250ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
18261d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
182720df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  OwningExprResult Res(ParseStringLiteralExpression());
18281d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
18291d922960e083906a586609ac6978678147250177Sebastian Redl
1830b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
1831b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
1832b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
1833b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<SourceLocation, 4> AtLocs;
1834a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
1835b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
1836effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
18370e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1838b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
1839b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
1840b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
184115faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
184297cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
184397cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
1844b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
184597cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    OwningExprResult Lit(ParseStringLiteralExpression());
18460e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
18471d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
18480e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1849effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
1850b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
18511d922960e083906a586609ac6978678147250177Sebastian Redl
18521d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
18531d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
18545508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
1855f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1856f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
1857f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
18581d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
18591d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
1860861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
18611d922960e083906a586609ac6978678147250177Sebastian Redl
1862f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
18631d922960e083906a586609ac6978678147250177Sebastian Redl
18644fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
18651d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
18661d922960e083906a586609ac6978678147250177Sebastian Redl
1867f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
18681d922960e083906a586609ac6978678147250177Sebastian Redl
1869809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
18701d922960e083906a586609ac6978678147250177Sebastian Redl
18714988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
18721d922960e083906a586609ac6978678147250177Sebastian Redl
1873809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1874809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1875809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
18761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1877809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                                 Ty.get(), RParenLoc));
1878f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
187929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
188029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
188129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
18821d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
18831d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
188429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
18851d922960e083906a586609ac6978678147250177Sebastian Redl
18864fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
18871d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
18881d922960e083906a586609ac6978678147250177Sebastian Redl
188929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
18901d922960e083906a586609ac6978678147250177Sebastian Redl
18914fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
18921d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
18931d922960e083906a586609ac6978678147250177Sebastian Redl
1894390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
189529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
18961d922960e083906a586609ac6978678147250177Sebastian Redl
18974988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
189829b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
18991d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
19001d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
190129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
1902a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
1903a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
1904a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
19051d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
19061d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
1907a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
19081d922960e083906a586609ac6978678147250177Sebastian Redl
19094fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
19101d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
19111d922960e083906a586609ac6978678147250177Sebastian Redl
1912b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1913a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
1914a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
19152fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
19161d922960e083906a586609ac6978678147250177Sebastian Redl  if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
19171d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
19181d922960e083906a586609ac6978678147250177Sebastian Redl
1919b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
1920887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
1921887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
1922a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
19234fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      if (Tok.isNot(tok::colon))
19241d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
19251d922960e083906a586609ac6978678147250177Sebastian Redl
1926cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      nColons++;
1927a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
1928a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
1929a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1930a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
1931a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
19322fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
1933b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
1934a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
1935a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1936a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
1937887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
1938a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1939887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
19401d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
19411d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
194258065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
1943