ParseObjc.cpp revision 1e37765c9257ef1d051f54a674eaa964bdba9693
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.
7891e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek    llvm::OwningPtr<AttributeList> MethodAttrs;
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
7911e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek      MethodAttrs.reset(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,
7971e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek                                          0, CargNames, MethodAttrs.get(),
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.
8651e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek  llvm::OwningPtr<AttributeList> MethodAttrs;
8661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
8671e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek    MethodAttrs.reset(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,
8761e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek                                        &ArgInfos[0], CargNames,
8771e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek                                        MethodAttrs.get(),
878335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
87954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
8801e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek
8811e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek  // Delete referenced AttributeList objects.
8821e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek  for (llvm::SmallVectorImpl<Action::ObjCArgInfo>::iterator
8831e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek       I = ArgInfos.begin(), E = ArgInfos.end(); I != E; ++I)
8841e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek    delete I->ArgAttrs;
8851e37765c9257ef1d051f54a674eaa964bdba9693Ted Kremenek
88654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
887294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
888294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
889dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
890dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
891dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
8927caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
893b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
89471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
89571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
89671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
897e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
8981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
89971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
9001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
901e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
9021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
903e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
90455385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
90555385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
90655385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
90755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      ConsumeToken();
90855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
90955385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
910e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
911e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
912e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
913e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
914e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
915e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
916e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
91771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
918e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
9191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
920e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
921e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
922e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
923e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
9241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
925e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
926e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
927e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
928e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
929e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
931e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
9321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
933e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
934e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
935e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
936e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
937e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
938e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
939e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
940dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
941dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
942dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
943dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
944dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
945dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
946dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
947dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
948dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
949dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
950dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
951dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
952dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
953dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
954dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
955ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
956dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
957dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
9581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
959dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
960b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
96160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
962df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
963b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
964e13594279a952537ac903325efff57e3edca79d9Chris Lattner
9651a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
96672de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
967ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
9681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
969aa847fe3c88ee5e8d180e48537c58b805d48d95dFariborz Jahanian  tok::ObjCKeywordKind visibility = tok::objc_protected;
970ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
971df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
972ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
974ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
975df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
976c2253f5ca170984fcd4f30f8823148e8cb71336bChris Lattner      Diag(Tok, diag::ext_extra_struct_semi)
97729d9c1adfadf65e2d847d44bec37746844b9e0e3Chris Lattner        << CodeModificationHint::CreateRemoval(Tok.getLocation());
978ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
979ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
980ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
9811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
982ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
983df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
984ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
985c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
986c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      if (Tok.is(tok::code_completion)) {
987c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor        Actions.CodeCompleteObjCAtVisibility(CurScope);
988c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor        ConsumeToken();
989c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      }
990c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
991861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
992ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
993ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
994ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
995ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
996861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
997ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
999ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
1000ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
1001ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
1002ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
1003ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1005c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    if (Tok.is(tok::code_completion)) {
1006c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      Actions.CodeCompleteOrdinaryName(CurScope,
1007c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor                                       Action::CCC_ObjCInstanceVariableList);
1008c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor      ConsumeToken();
1009c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor    }
1010c38c3e1e726630458154534227d74eda833d26a0Douglas Gregor
1011bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
1012bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
1013bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy IDecl;
1014bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
1015bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
1016bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1017bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
1018bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                       llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
1019bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
1020bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1021bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1022bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy invoke(FieldDeclarator &FD) {
1023bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
1024bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy Field
1025bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          = P.Actions.ActOnIvar(P.CurScope,
1026bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
1027bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                IDecl, FD.D, FD.BitfieldSize, visibility);
1028bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        AllIvarDecls.push_back(Field);
1029bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
1030bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
1031bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
1032bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
1033e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
1034e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
1035bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
10361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1037df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
1038ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
1039ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
1040ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
1041ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
1042ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
1043ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
1044ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
104560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
10468749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
10478749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
10488749be53f53384e7846502791ceda6c657228d07Steve Naroff  Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
1049beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                      AllIvarDecls.data(), AllIvarDecls.size(),
10501bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
1051ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
10525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1053dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1054dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
1055dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
1056dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
1057dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1058dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
10611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1062dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1063dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1064dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1065dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1066dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1067dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
10683536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1069dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1070b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1071b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                                      AttributeList *attrList) {
1072861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
10737ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
10747ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
10751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1076083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
1077083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(CurScope);
1078083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor    ConsumeToken();
1079083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1080083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1081df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
10827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1083b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
10847ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10857ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
10867ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
10877ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1089df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
10907caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
10917ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
10921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
1093bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
10947ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1096df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
10977caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
10987caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
10997caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
11007ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
11017ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
11027ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1103df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
11047ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
11057ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1106b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner        return DeclPtrTy();
11077ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
11087caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
11097caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
11107ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
11111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1112df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
11137ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
11147ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
11157ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
11167ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1117b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1119e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
11201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1121bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
1122bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
11237caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11257ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
112671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
11277caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
1128b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
112971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
11307caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
113171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
113271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1133b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
11341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1135b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ProtoType =
1136e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1137beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1138beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
113918df52bbb5d28ca082064d31ae7558dbdae52377Douglas Gregor                                        ProtocolLocs.data(),
1140246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                        EndProtoLoc, attrList);
114125e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1142bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1143dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1145dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1146dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1149dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1150dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1151dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1152dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1153dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1154dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1155b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
1156ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1157ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1158ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1159ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11613b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
11623b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
11633b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(CurScope);
11643b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    ConsumeToken();
11653b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
11663b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1167df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1168ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1169b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1170ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1171ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1172ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1173ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
11741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1176ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1177ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1178ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1179ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
11801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
118133ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
118233ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
118333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      ConsumeToken();
118433ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
118533ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1186df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1187ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1188ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1189ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1190ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1191b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
11921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1193df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1194ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1195ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1196b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1197ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1198ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1199b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
12001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    atLoc, nameId, nameLoc, categoryId,
12018f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1202a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
120363e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.push_back(ObjCImpDecl);
1204b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1205ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1206ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1207ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1208ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1209df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1210ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1211ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1212df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1213ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1214b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1215ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1216ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1217ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1218ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1219b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
1220cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1221ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122360fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
122460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
1225a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
122663e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  PendingObjCImpDecl.push_back(ObjCImpDecl);
122763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1228b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
12295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
123060fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1231782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted KremenekParser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceRange atEnd) {
1232ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1233ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1234b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy Result = ObjCImpDecl;
1235ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1236a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
1237782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    Actions.ActOnAtEnd(atEnd, ObjCImpDecl);
1238b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    ObjCImpDecl = DeclPtrTy();
123963e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.pop_back();
1240a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
1241782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  else {
1242782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    // missing @implementation
1243782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek    Diag(atEnd.getBegin(), diag::warn_expected_implementation);
1244782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  }
1245a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  return Result;
12465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1247e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
12481ac7104947020a60430ba6f519cd5869af77046fFariborz JahanianParser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
124963e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  if (PendingObjCImpDecl.empty())
125063e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
125163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
1252782f2f52b78d8ca785110398a7f7b56b830b9ac7Ted Kremenek  Actions.ActOnAtEnd(SourceRange(), ImpDecl);
125363e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  return Actions.ConvertDeclToDeclGroup(ImpDecl);
125463e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian}
125563e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1256e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1257e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1258e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1259b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1260e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1261e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1262e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1263df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1264e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1265b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1266e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1267243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1268243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1269df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1270e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1271b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1272e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1273243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1274243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1275243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
12761ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1277b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1278243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1279b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1280b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
12815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1283ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1284ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1285ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1286ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1287ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1288ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1289ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1290ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1291ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1292ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1293ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1294b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1295ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1296ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1297f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
12981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1299b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  while (true) {
1300322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    if (Tok.is(tok::code_completion)) {
1301424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1302322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      ConsumeToken();
1303322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor    }
1304322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1305b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1306b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      Diag(Tok, diag::err_synthesized_property_name);
1307b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      SkipUntil(tok::semi);
1308b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor      return DeclPtrTy();
1309b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    }
1310b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor
1311f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1312f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1313f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1314df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1315ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1316ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1317322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1318322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      if (Tok.is(tok::code_completion)) {
1319322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor        Actions.CodeCompleteObjCPropertySynthesizeIvar(CurScope, propertyId,
1320322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor                                                       ObjCImpDecl);
1321322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor        ConsumeToken();
1322322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor      }
1323322328b8a65ad2e45829eb06d245addb64037f6fDouglas Gregor
1324df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1325ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1326ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1327ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1328f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1329ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume ivar-name
1330ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1331f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1332f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian                                  propertyId, propertyIvar);
1333df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1334ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1335ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1336ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1337b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  if (Tok.isNot(tok::semi)) {
13381ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1339b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor    SkipUntil(tok::semi);
1340b328c4251a9d2db704b3bd46ec04884dc8e56332Douglas Gregor  }
1341d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian  else
1342d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian    ConsumeToken(); // consume ';'
1343b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1344ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1345ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1346ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1347ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1348ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1349ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1350ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1351ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1352ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1353b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1354ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1355ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1356ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1357424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor  while (true) {
1358424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.is(tok::code_completion)) {
1359424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Actions.CodeCompleteObjCPropertyDefinition(CurScope, ObjCImpDecl);
1360424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      ConsumeToken();
1361424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1362424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1363424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    if (Tok.isNot(tok::identifier)) {
1364424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      Diag(Tok, diag::err_expected_ident);
1365424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      SkipUntil(tok::semi);
1366424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor      return DeclPtrTy();
1367424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor    }
1368424b2a546dbd09cf70d43087771c7fff851ca158Douglas Gregor
1369c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1370c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1371c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1372c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian                                  propertyId, 0);
1373c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1374df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1375ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1376ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1377ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1378df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
13791ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
1380b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1381ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
13821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1383397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1384397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1385397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
138643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
138715faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Res(Actions);
1388397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1389df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
139039f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
13910e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1392397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
139343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1394397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1395397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
139639f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian  ConsumeToken(); // consume ';'
1397e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff  return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
1398397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1399397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1400c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
140178a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1402c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
140343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult
140443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1405fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1406fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
14071ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
140843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1409fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1410fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
14112f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
14120e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
1413fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
141443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1415fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1416fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
14171ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
141843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1419fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1420fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
142178a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
14221ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
142343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
142478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
14253ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
14263ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
14278935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::DeclScope);
14283ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
142961364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult SynchBody(ParseCompoundStatementBody());
14300e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
14318935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
14320e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (SynchBody.isInvalid())
1433fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
143476ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
1435c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1436c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1437397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1438397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1439397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1440397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1441397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1442397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1443397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1444397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1445397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1446397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1447397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
144843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1449397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
145043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1451397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1452df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
14531ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
145443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1455397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
145615faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult CatchStmts(Actions);
145715faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult FinallyStmt(Actions);
14588935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
145961364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult TryBody(ParseCompoundStatementBody());
14608935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
14610e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1462bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1463a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1464df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
14656b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
14666b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
14676b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
14686b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
14696b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
14706b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
14716b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
14720e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1473161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1474cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1475b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      DeclPtrTy FirstPart;
14763b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1477df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1478397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1479e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1480df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1481397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1482397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
148343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl          // For some odd reason, the name of the exception variable is
14841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // optional. As a result, we need to use "PrototypeContext", because
14857ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // we must accept either 'declarator' or 'abstract-declarator' here.
14867ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          Declarator ParmDecl(DS, Declarator::PrototypeContext);
14877ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
14887ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
14897ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // Inform the actions module about the parameter declarator, so it
14907ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
1491d219a3a462c31fc9aa0c0bcfb749e9e8e56b5e35Fariborz Jahanian          // FIXME. Probably can build a VarDecl and avoid setting DeclContext.
14927ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
14932f764f11f513c7b51c716fffa5d02e5de816836fFariborz Jahanian          Actions.ActOnObjCCatchParam(FirstPart);
149464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1495397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
14961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149793a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
14981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
149993a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
150093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
150193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
150293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
150393a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
150415faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl        OwningStmtResult CatchBody(Actions, true);
1505c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1506c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1507c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1508c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
15090e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
15103b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
15110e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
15127ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff                        RParenLoc, FirstPart, move(CatchBody),
151376ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                        move(CatchStmts));
151464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
15151ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
15161ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
151743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1518397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1519397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
15206b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
15216b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
152264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
15238935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
15240e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
152515faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl      OwningStmtResult FinallyBody(Actions, true);
1526c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1527c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1528c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1529c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
15300e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1531161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
15320e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
153376ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                                   move(FinallyBody));
1534397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1535397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1536397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1537397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1538bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1539397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
154043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1541bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
154276ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
154376ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                    move(FinallyStmt));
1544397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1545ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
15463536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1547ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1548b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1549b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
15501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155149f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
155249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        PP.getSourceManager(),
155349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        "parsing Objective-C method");
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1555ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1556209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1557496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    if (ObjCImpDecl) {
1558496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
155929d9c1adfadf65e2d847d44bec37746844b9e0e3Chris Lattner        << CodeModificationHint::CreateRemoval(Tok.getLocation());
1560496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1561ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1562209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1563ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1564409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1565df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1566da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
15671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1568409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1569409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
15701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1571409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1572409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1573b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1574ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1575409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1577409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
15788935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
15791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1580409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1581394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
1582ebf6443a4f493233f7e8d92b3a991848c8b1c00dSteve Naroff  Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
158361364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
158461364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult FnBody(ParseCompoundStatementBody());
158561364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
1586409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
15870e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (FnBody.isInvalid())
1588a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1589a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                       MultiStmtArg(Actions), false);
1590798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
159132ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  // TODO: Pass argument information.
159232ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
15931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1594409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
15958935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
1596798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
159771c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
15985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15995508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
160043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
16019a0c85e640a08174569a303db22981612f05d385Douglas Gregor  if (Tok.is(tok::code_completion)) {
16029a0c85e640a08174569a303db22981612f05d385Douglas Gregor    Actions.CodeCompleteObjCAtStatement(CurScope);
16039a0c85e640a08174569a303db22981612f05d385Douglas Gregor    ConsumeToken();
16049a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return StmtError();
16055d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  }
16065d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16075d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_try))
16086b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
16095d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16105d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_throw))
161164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
16125d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
16135d8031687d086701b4dadaab3e0de1def448da9dChris Lattner  if (Tok.isObjCAtKeyword(tok::objc_synchronized))
161464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
16155d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
1616d8c4e15138e69a51754cc259c8a592cc47950c8eSebastian Redl  OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
16170e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
161864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
161964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
162064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
162164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
162243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
162364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
16245d8031687d086701b4dadaab3e0de1def448da9dChris Lattner
162564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
162664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
16275ee56e95c3905d2e7bc403631b03865cdbdd8a42Anders Carlsson  return Actions.ActOnExprStmt(Actions.MakeFullExpr(Res));
162864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
162964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
16301d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
16315508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
16329a0c85e640a08174569a303db22981612f05d385Douglas Gregor  case tok::code_completion:
16339a0c85e640a08174569a303db22981612f05d385Douglas Gregor    Actions.CodeCompleteObjCAtExpression(CurScope);
16349a0c85e640a08174569a303db22981612f05d385Douglas Gregor    ConsumeToken();
16359a0c85e640a08174569a303db22981612f05d385Douglas Gregor    return ExprError();
16369a0c85e640a08174569a303db22981612f05d385Douglas Gregor
1637b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1638b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
16391d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1640b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
16414fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
16421d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
16432f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
16444fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
16454fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
16461d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
16474fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
16481d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
16494fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
16501d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
16514fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
16521d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
16534fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
16545508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
16555508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
16565508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
16571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
16580ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
16590ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16600ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-receiver:
16610ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
16620ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
16630ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
16641d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCMessageExpression() {
1665699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1666699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1667699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
1668699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  // Parse receiver
166914dd98a7952c9559e0d17fff8272bf3be67135afChris Lattner  if (isTokObjCMessageIdentifierReceiver()) {
1670699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1671d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1672d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      SourceLocation NameLoc = ConsumeToken();
1673d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1674d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian                                            ExprArg(Actions));
1675d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
1676699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
1677699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
16782f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
16790e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
16805c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
16811d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
1682699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
16831d922960e083906a586609ac6978678147250177Sebastian Redl
16840e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
168576ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                        0, move(Res));
1686699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
16871d922960e083906a586609ac6978678147250177Sebastian Redl
1688699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1689699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// the rest of a message expression.
16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
16910ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
16920ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
16930ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
16940ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16950ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
16960ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
16970ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
16980ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
17000ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
17010ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
17020ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
17030ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
17040ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
17050ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
17060ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
17070ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
17081d922960e083906a586609ac6978678147250177Sebastian Redl///
17091d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
1710699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
17115cb93b8bf009c4b0ae09b71ba85f54b2a7ea8022Steve Naroff                                       SourceLocation NameLoc,
1712699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                       IdentifierInfo *ReceiverName,
17131d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
1714c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
1715c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    if (ReceiverName)
1716d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1717d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                           0, 0);
1718c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
1719d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1720d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                              0, 0);
1721c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    ConsumeToken();
1722c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
1723d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
1724a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
17254b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
17262fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
172768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1728ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson  SourceLocation SelectorLoc = Loc;
17291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1731a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
173268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1733df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1734a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
1735a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
173668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
173737387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
1738df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
1739a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
17404fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17414fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17424fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17434fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17441d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
1745a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
17461d922960e083906a586609ac6978678147250177Sebastian Redl
174768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
17481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
17492f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
17500e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
17514fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17524fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17534fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17544fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17551d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
175637387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
17571d922960e083906a586609ac6978678147250177Sebastian Redl
175837387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
1759effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
17601d922960e083906a586609ac6978678147250177Sebastian Redl
1761d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      // Code completion after each argument.
1762d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      if (Tok.is(tok::code_completion)) {
1763d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        if (ReceiverName)
1764d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor          Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc,
1765d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.data(),
1766d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                               KeyIdents.size());
1767d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        else
1768d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor          Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get(),
1769d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.data(),
1770d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor                                                  KeyIdents.size());
1771d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor        ConsumeToken();
1772d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor      }
1773d3c6854153fd6bc6a412a29e4491dbd0a47bdb14Douglas Gregor
177437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
17752fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
1776df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
1777a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
1778a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
1779a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1780a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
1781df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
178249f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
17831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
17842f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
17850e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
17864fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
17874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
17884fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
17894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
17901d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
179149f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
17920e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
179349f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
1794effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
1795a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1796a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
1797a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
17981d922960e083906a586609ac6978678147250177Sebastian Redl
17994fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
18004fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
18014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
18024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
18031d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1804a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
18051d922960e083906a586609ac6978678147250177Sebastian Redl
1806df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
1807a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_rsquare);
18084fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
18094fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
18104fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
18114fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
18121d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1813a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
18141d922960e083906a586609ac6978678147250177Sebastian Redl
1815699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
18161d922960e083906a586609ac6978678147250177Sebastian Redl
181729238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
1818ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
1819ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
1820ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
18211d922960e083906a586609ac6978678147250177Sebastian Redl
1822ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // We've just parsed a keyword message.
18231d922960e083906a586609ac6978678147250177Sebastian Redl  if (ReceiverName)
18241d922960e083906a586609ac6978678147250177Sebastian Redl    return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
18251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           LBracLoc, NameLoc, SelectorLoc,
1826ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                           RBracLoc,
18271d922960e083906a586609ac6978678147250177Sebastian Redl                                           KeyExprs.take(), KeyExprs.size()));
18281d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
1829ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                            LBracLoc, SelectorLoc, RBracLoc,
18301d922960e083906a586609ac6978678147250177Sebastian Redl                                            KeyExprs.take(), KeyExprs.size()));
18310ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
18320ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
18331d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
183420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  OwningExprResult Res(ParseStringLiteralExpression());
18351d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
18361d922960e083906a586609ac6978678147250177Sebastian Redl
1837b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
1838b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
1839b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
1840b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<SourceLocation, 4> AtLocs;
1841a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
1842b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
1843effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
18440e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1845b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
1846b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
1847b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
184815faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
184997cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
185097cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
1851b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
185297cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    OwningExprResult Lit(ParseStringLiteralExpression());
18530e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
18541d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
18550e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1856effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
1857b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
18581d922960e083906a586609ac6978678147250177Sebastian Redl
18591d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
18601d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
18615508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
1862f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1863f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
1864f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
18651d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
18661d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
1867861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
18681d922960e083906a586609ac6978678147250177Sebastian Redl
1869f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
18701d922960e083906a586609ac6978678147250177Sebastian Redl
18714fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
18721d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
18731d922960e083906a586609ac6978678147250177Sebastian Redl
1874f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
18751d922960e083906a586609ac6978678147250177Sebastian Redl
1876809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
18771d922960e083906a586609ac6978678147250177Sebastian Redl
18784988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
18791d922960e083906a586609ac6978678147250177Sebastian Redl
1880809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1881809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1882809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
18831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1884809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                                 Ty.get(), RParenLoc));
1885f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
188629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
188729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
188829b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
18891d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
18901d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
189129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
18921d922960e083906a586609ac6978678147250177Sebastian Redl
18934fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
18941d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
18951d922960e083906a586609ac6978678147250177Sebastian Redl
189629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
18971d922960e083906a586609ac6978678147250177Sebastian Redl
18984fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
18991d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
19001d922960e083906a586609ac6978678147250177Sebastian Redl
1901390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
190229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
19031d922960e083906a586609ac6978678147250177Sebastian Redl
19044988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
190529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
19061d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
19071d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
190829b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
1909a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
1910a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
1911a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
19121d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
19131d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
1914a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
19151d922960e083906a586609ac6978678147250177Sebastian Redl
19164fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
19171d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
19181d922960e083906a586609ac6978678147250177Sebastian Redl
1919b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1920a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
1921a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
19222fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
19231d922960e083906a586609ac6978678147250177Sebastian Redl  if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
19241d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
19251d922960e083906a586609ac6978678147250177Sebastian Redl
1926b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
1927887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
1928887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
1929a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
19304fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      if (Tok.isNot(tok::colon))
19311d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
19321d922960e083906a586609ac6978678147250177Sebastian Redl
1933cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      nColons++;
1934a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
1935a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
1936a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1937a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
1938a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
19392fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
1940b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
1941a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
1942a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1943a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
1944887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
1945a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1946887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
19471d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
19481d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
194958065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
1950