ParseObjc.cpp revision 33ced0b8550f3e7169f326944731ee02e9338659
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
33861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  switch (Tok.getObjCKeywordID()) {
345ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_class:
355ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtClassDeclaration(AtLoc);
365ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_interface:
375ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtInterfaceDeclaration(AtLoc);
385ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_protocol:
395ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtProtocolDeclaration(AtLoc);
405ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
415ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtImplementationDeclaration(AtLoc);
425ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
435ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtEndDeclaration(AtLoc);
445ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
455ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtAliasDeclaration(AtLoc);
465ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
475ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertySynthesize(AtLoc);
485ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
495ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertyDynamic(AtLoc);
505ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
515ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
525ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
53b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// objc-class-declaration:
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
61b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
64c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  llvm::SmallVector<SourceLocation, 8> ClassLocs;
65c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek
661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
68df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
71b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
74c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek    ClassLocs.push_back(Tok.getLocation());
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
77df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
85b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek  return Actions.ActOnForwardClassDeclaration(atLoc, ClassNames.data(),
88c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassLocs.data(),
89c09cba67d0ad01e53e0fed07322e95dd281bcfd9Ted Kremenek                                              ClassNames.size());
905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
92dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
93dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
94dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
95dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
96dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
97dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier objc-superclass[opt]
99dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
1001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-class-instance-variables[opt]
101dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
102dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
103dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
104dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
1051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier '(' identifier[opt] ')'
106dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
107dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
108dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
109dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
110dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
111dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
112dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
113dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
114dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
115dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
116dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
117dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
118dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
119dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
120b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation atLoc, AttributeList *attrList) {
122861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
123dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
124dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1263b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@interface'.
1273b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
1283b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    Actions.CodeCompleteObjCInterfaceDecl(CurScope);
1293b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    ConsumeToken();
1303b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
1313b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
132df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
133dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
134b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
135dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
13663e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
137dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1387ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
139dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
1401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
141df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren)) { // we have a category.
142dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation lparenLoc = ConsumeParen();
143dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation categoryLoc, rparenLoc;
144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14633ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
14733ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      Actions.CodeCompleteObjCInterfaceCategory(CurScope, nameId);
14833ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      ConsumeToken();
14933ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
15033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
151527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
152df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
153dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
154dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
155527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    } else if (!getLang().ObjC2) {
156527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
157b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
159df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
160dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_rparen);
161dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      SkipUntil(tok::r_paren, false); // don't stop at ';'
162b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
163dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
164dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    rparenLoc = ConsumeParen();
1651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
166dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    // Next, we need to check for any protocol references.
16771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    SourceLocation LAngleLoc, EndProtoLoc;
168b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
16971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1706bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    if (Tok.is(tok::less) &&
17171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
17271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
173b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
175dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    if (attrList) // categories don't support attributes.
176dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_objc_no_attributes_on_category);
1771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
178beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    DeclPtrTy CategoryType =
1791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Actions.ActOnStartCategoryInterface(atLoc,
180beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          nameId, nameLoc,
181beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          categoryId, categoryLoc,
182beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          ProtocolRefs.data(),
183beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          ProtocolRefs.size(),
184beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          EndProtoLoc);
1851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
186fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian    ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
187bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    return CategoryType;
188dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
189dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
190dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
191dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
1927ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
193df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
194dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
1953b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1963b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    // Code completion of superclass names.
1973b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    if (Tok.is(tok::code_completion)) {
1983b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor      Actions.CodeCompleteObjCSuperclass(CurScope, nameId);
1993b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor      ConsumeToken();
2003b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    }
2013b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
202df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
203dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
204b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
205dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
206dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
207dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
208dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
209dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
210b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
21171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
21271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
21306036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
21471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
21571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
216b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
2171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DeclPtrTy ClsType =
2191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
22006036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
221beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
22206036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     EndProtoLoc, attrList);
2231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
224df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
22560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ClsType, atLoc);
226dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
22725e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
228bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ClsType;
229dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
230dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
231dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
232dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
233dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
234294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
2353536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
236dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
237dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
238dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
239294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
240294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
241294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
242294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
243b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
244cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                        tok::ObjCKeywordKind contextKey) {
245b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> allMethods;
246b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 16> allProperties;
247682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
24800933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
2491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
250bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  SourceLocation AtEndLoc;
251bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
252294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
253e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
254df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
2551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      DeclPtrTy methodPrototype =
256df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
25725e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
2583536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
2593536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
260b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
261b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner                       "", tok::semi);
262294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
263294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
2641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
265e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
266e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
267294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
268e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
269e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
2701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
271bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
272e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
273e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
2741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
275e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
276e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
2771fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
2781fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
2791fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
2801fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
2811fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
2821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2834985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // FIXME: as the name implies, this rule allows function definitions.
2844985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // We could pass a flag or check for functions during semantic analysis.
285682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
286e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
287e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
289e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
290e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
291a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
293a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
294e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      AtEndLoc = AtLoc;
295e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
296bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
2971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
298bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
299bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
300bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
301a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
302a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
303bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
304bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
305bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
306bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
307f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
308bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
309a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
310a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
3111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
312a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
313a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
314a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
315bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
316e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
317bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
318a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
319bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
320a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
322a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
323f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
324f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner        Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
325f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
326e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
3271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
3288ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.is(tok::l_paren))
329e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ParseObjCPropertyAttribute(OCDS);
3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
331bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      struct ObjCPropertyCallback : FieldCallback {
332bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        Parser &P;
333bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy IDecl;
334bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        llvm::SmallVectorImpl<DeclPtrTy> &Props;
335bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        ObjCDeclSpec &OCDS;
336bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        SourceLocation AtLoc;
337bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        tok::ObjCKeywordKind MethodImplKind;
338bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
339bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
340bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                             llvm::SmallVectorImpl<DeclPtrTy> &Props,
341bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                             ObjCDeclSpec &OCDS, SourceLocation AtLoc,
342bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                             tok::ObjCKeywordKind MethodImplKind) :
343bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
344bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          MethodImplKind(MethodImplKind) {
345bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        }
346bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
347bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy invoke(FieldDeclarator &FD) {
348bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          if (FD.D.getIdentifier() == 0) {
349bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
350bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall              << FD.D.getSourceRange();
351bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            return DeclPtrTy();
352bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          }
353bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          if (FD.BitfieldSize) {
354bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            P.Diag(AtLoc, diag::err_objc_property_bitfield)
355bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall              << FD.D.getSourceRange();
356bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            return DeclPtrTy();
357bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          }
358bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
359bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          // Install the property declarator into interfaceDecl.
360bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          IdentifierInfo *SelName =
361bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
362bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
363bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          Selector GetterSel =
364bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            P.PP.getSelectorTable().getNullarySelector(SelName);
365bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          IdentifierInfo *SetterName = OCDS.getSetterName();
366bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          Selector SetterSel;
367bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          if (SetterName)
368bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
369bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          else
370bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
371bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                                           P.PP.getSelectorTable(),
372bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                                           FD.D.getIdentifier());
373bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          bool isOverridingProperty = false;
374bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          DeclPtrTy Property =
375bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
376bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                    GetterSel, SetterSel, IDecl,
377bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                    &isOverridingProperty,
378bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                    MethodImplKind);
379bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          if (!isOverridingProperty)
380bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            Props.push_back(Property);
381bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
382bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          return Property;
383bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        }
384bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      } Callback(*this, interfaceDecl, allProperties,
385bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                 OCDS, AtLoc, MethodImplKind);
386bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
387e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
388e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      DeclSpec DS;
389bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
3901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
391a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
392a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner                       tok::at);
393a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
394f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
395294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
396bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
397bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
398bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
399bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  if (Tok.isObjCAtKeyword(tok::objc_end))
400bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
401bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  else
402bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    Diag(Tok, diag::err_objc_missing_end);
4031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
404a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
405bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
4068a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek  Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
4071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
408beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
409beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
410294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
411294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
412d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
413d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
414d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
415d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
416d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
417d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
418d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
419d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
420d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
421d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
422d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
423d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
424d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
425d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
426d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
427d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
4287caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnervoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
4298ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
430dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  SourceLocation LHSLoc = ConsumeParen(); // consume '('
4311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
432cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
433ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
434ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff      Actions.CodeCompleteObjCProperty(CurScope, DS);
435ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff      ConsumeToken();
436ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
437d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
439f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
440f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
441f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      MatchRHSPunctuation(tok::r_paren, LHSLoc);
442f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
443f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
4441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
445156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
44792e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
448e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
44992e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
450e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
45192e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
452e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
45392e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
454e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
45592e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
456e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
45792e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
458e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
45992e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
460e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
461156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
462156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                           tok::r_paren))
463dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
4641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4658ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.isNot(tok::identifier)) {
4661ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(Tok, diag::err_expected_ident);
4678ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
4688ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
4698ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
471e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar      if (II->getNameStart()[0] == 's') {
4728ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
4738ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setSetterName(Tok.getIdentifierInfo());
474156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
4751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
476156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
477156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
4788ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
4798ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
4808ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
4818ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setGetterName(Tok.getIdentifierInfo());
482156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
4838ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
484e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
485a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
486cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
487cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
488cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
4891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
490156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
491156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
493156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
494d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
496156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner  MatchRHSPunctuation(tok::r_paren, LHSLoc);
497d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
498d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
4993536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
5001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
5013536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
502294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
503294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
504294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
505294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5064985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
5074985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
5084985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
5091eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
510b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
511df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
512294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
514bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
5151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
516b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
5173536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
5182bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
519f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
520294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
521294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
522294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
523294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
524294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
525294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
526294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
527294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
528294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
529294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5302fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
531ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
532ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
533ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
534ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
535ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
536ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
5379298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
538ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
539ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
540ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
541ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
542ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
543ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
544ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
545ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
546ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
547ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
548ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
549ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
550ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
551ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
552ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
553ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
554ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
555ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
556ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
557ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
558ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
559ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
560ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
561ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
562ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
563ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
564ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
565ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
566ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
567ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
568ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
569ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
570ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
571ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
572ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
573ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
574ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
575ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
576ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
577ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
578ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
579ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
580ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
581ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
582ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
583ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
584ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
585ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
586ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
587ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
588ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
589ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
590ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
591ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
592ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
593ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
594ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
595ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
596ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
597ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
598ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
599ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
600ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
601ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
602ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
603ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
6044b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
605ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
606d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
607294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
608294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6090196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
6100196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
611335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
6123ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
6133ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
6143ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
6151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
6165ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
6170196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
6180196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
619a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
620e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
621e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
622294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
623294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
624294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
625294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
626294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
627a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
628e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
629cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
630e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
632e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
633e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
634a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
635e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
637a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
638e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
639e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      default: assert(0 && "Unknown decl qualifier");
640a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
641a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
642a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
643a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
644a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
645a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
646e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
647a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
648e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
649e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
650e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
651e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
6521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
653e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
654e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
655e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
656e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
657e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
658e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
659e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
660e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
661e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
662a526c5c67e5a0473c340903ee542ce570119665fTed KremenekParser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
663df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
6641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6654a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  SourceLocation LParenLoc = ConsumeParen();
666e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66819d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
669a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ParseObjCTypeQualifierList(DS);
6704fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
6714a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  TypeTy *Ty = 0;
672809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
673809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult TypeSpec = ParseTypeName();
674809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (!TypeSpec.isInvalid())
675809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      Ty = TypeSpec.get();
676809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
6771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6784a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
6794a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    ConsumeParen();
6804a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
681e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
6821ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
6834a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
6844a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
6854a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
6864a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
6874a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    MatchRHSPunctuation(tok::r_paren, LParenLoc);
688294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
689f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
690294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
691294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
692294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
693294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
6944985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
695294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
6964985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
697294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
698294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
6991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
700294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
701294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
702294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
7037ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
7047ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
7057ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
7067ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
707294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7084985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
7094985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
710294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7114985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
7124985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
713294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7144985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
715294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
716294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
7177ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
7187ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
7197ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
720b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
721b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              tok::TokenKind mType,
722b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              DeclPtrTy IDecl,
723b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
72454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
72554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
726e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
727ff38491c18b060526d754765b952f4a497a89416Chris Lattner  TypeTy *ReturnType = 0;
728a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
729df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
730f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ReturnType = ParseObjCTypeName(DSRet);
7311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
732bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
7332fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
734e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
73584c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
73684c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
7371ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
7381ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
739e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
740e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
741b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
742e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
744439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian  llvm::SmallVector<Declarator, 8> CargNames;
745df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
746ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
747ff38491c18b060526d754765b952f4a497a89416Chris Lattner    AttributeList *MethodAttrs = 0;
7481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
749ff38491c18b060526d754765b952f4a497a89416Chris Lattner      MethodAttrs = ParseAttributes();
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
751ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
75254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DeclPtrTy Result
75354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall         = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
7541f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                          mType, IDecl, DSRet, ReturnType, Sel,
7551c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                          0, CargNames, MethodAttrs,
7561c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                          MethodImplKind);
75754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
75854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
759ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
760f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
76168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
762e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner  llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
7631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
764ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
765e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    Action::ObjCArgInfo ArgInfo;
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
767ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
768df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
769ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
770ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
771ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
772ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
7731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
774e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Type = 0;
775e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
776e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
777e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
778ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
779e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
780df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
781e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner      ArgInfo.ArgAttrs = ParseAttributes();
7827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
783df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
784ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
785ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
7864985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
7871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
788e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
789e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
790ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
7911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
792e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
793e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
794e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
795ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
7964b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
7972fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner    SelIdent = ParseObjCSelectorPiece(Loc);
798df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
799ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
800ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
801ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
8021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
803335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
8041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
805ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
806df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
807ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
808df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
809335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
8104985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
811ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
8124985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
813ff38491c18b060526d754765b952f4a497a89416Chris Lattner    DeclSpec DS;
814ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
8151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
816ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
817ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
818439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian    CargNames.push_back(ParmDecl);
8194985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
8201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
821ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // FIXME: Add support for optional parmameter list...
822e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
823ff38491c18b060526d754765b952f4a497a89416Chris Lattner  AttributeList *MethodAttrs = 0;
8241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
825ff38491c18b060526d754765b952f4a497a89416Chris Lattner    MethodAttrs = ParseAttributes();
8261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8273688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian  if (KeyIdents.size() == 0)
8283688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian    return DeclPtrTy();
829ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
830ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
83154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  DeclPtrTy Result
83254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall       = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
8333688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian                                        mType, IDecl, DSRet, ReturnType, Sel,
8341c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                        &ArgInfos[0], CargNames, MethodAttrs,
835335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
83654abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
83754abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
838294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
839294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
840dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
841dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
842dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
8437caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
844b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
84571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
84671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
84771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
848e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
85071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
8511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
852e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
8531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
854e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
85555385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    if (Tok.is(tok::code_completion)) {
85655385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      Actions.CodeCompleteObjCProtocolReferences(ProtocolIdents.data(),
85755385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor                                                 ProtocolIdents.size());
85855385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor      ConsumeToken();
85955385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor    }
86055385fe3e723cd675001e45f42d61adde6b7f075Douglas Gregor
861e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
862e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
863e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
864e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
865e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
866e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
867e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
86871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
869e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
871e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
872e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
873e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
874e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
876e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
877e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
878e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
879e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
880e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
8811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
882e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
884e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
885e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
886e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
887e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
888e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
889e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
890e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
891dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
892dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
893dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
894dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
895dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
896dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
897dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
898dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
899dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
900dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
901dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
902dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
903dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
904dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
905dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
906ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
907dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
908dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
9091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
910dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
911b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
91260fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
913df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
914b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
915e13594279a952537ac903325efff57e3edca79d9Chris Lattner
9161a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
91772de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
918ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
9191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
920aa847fe3c88ee5e8d180e48537c58b805d48d95dFariborz Jahanian  tok::ObjCKeywordKind visibility = tok::objc_protected;
921ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
922df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
923ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
9241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
925ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
926df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
927c2253f5ca170984fcd4f30f8823148e8cb71336bChris Lattner      Diag(Tok, diag::ext_extra_struct_semi)
928c2253f5ca170984fcd4f30f8823148e8cb71336bChris Lattner        << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
929ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
930ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
931ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
9321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
933ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
934df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
935ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
936861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
937ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
938ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
939ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
940ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
941861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
942ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
944ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
945ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
946ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
947ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
948ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
950bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
951bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
952bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy IDecl;
953bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
954bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
955bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
956bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
957bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                       llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
958bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
959bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
960bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
961bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy invoke(FieldDeclarator &FD) {
962bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
963bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy Field
964bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          = P.Actions.ActOnIvar(P.CurScope,
965bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
966bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                IDecl, FD.D, FD.BitfieldSize, visibility);
967bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        AllIvarDecls.push_back(Field);
968bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
969bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
970bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
971bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
972e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
973e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
974bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
9751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
976df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
977ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
978ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
979ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
980ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
981ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
982ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
983ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
98460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
9858749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
9868749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
9878749be53f53384e7846502791ceda6c657228d07Steve Naroff  Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
988beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                      AllIvarDecls.data(), AllIvarDecls.size(),
9891bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
990ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
9915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
992dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
993dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
994dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
995dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
996dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
997dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
9991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
1001dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
1002dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1003dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
1004dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
1005dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1006dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
10073536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
1008dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
1009b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
1010b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                                      AttributeList *attrList) {
1011861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
10127ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
10137ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
10141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1015083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  if (Tok.is(tok::code_completion)) {
1016083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor    Actions.CodeCompleteObjCProtocolDecl(CurScope);
1017083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor    ConsumeToken();
1018083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor  }
1019083128f6b13dfa4fc615a838c49b516d901b1ac0Douglas Gregor
1020df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
10217ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
1022b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
10237ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10247ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
10257ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
10267ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
10271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1028df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
10297caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
10307ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
10311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
1032bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
10337ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1035df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
10367caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
10377caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
10387caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
10397ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
10407ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
10417ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1042df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
10437ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
10447ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1045b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner        return DeclPtrTy();
10467ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
10477caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
10487caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
10497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
10501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1051df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
10527ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
10537ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
10547ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
10557ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1056b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
10571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1058e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1060bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
1061bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
10627caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10647ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
106571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
10667caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
1067b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
106871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
10697caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
107071b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
107171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1072b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
10731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1074b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ProtoType =
1075e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1076beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1077beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
1078246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                        EndProtoLoc, attrList);
107925e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1080bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1081dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1082dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1083dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1084dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1085dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1086dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1087dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1088dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1089dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1090dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1091dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1092dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1093b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
1094ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1095ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1096ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1097ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
10981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10993b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  // Code completion after '@implementation'.
11003b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  if (Tok.is(tok::code_completion)) {
11013b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    Actions.CodeCompleteObjCImplementationDecl(CurScope);
11023b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor    ConsumeToken();
11033b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor  }
11043b49aca913dc0c1838321b9bb2dc9a4cb4681922Douglas Gregor
1105df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1106ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1107b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1108ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1109ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1110ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1111ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
11121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1114ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1115ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1116ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1117ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111933ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    if (Tok.is(tok::code_completion)) {
112033ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      Actions.CodeCompleteObjCImplementationCategory(CurScope, nameId);
112133ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor      ConsumeToken();
112233ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor    }
112333ced0b8550f3e7169f326944731ee02e9338659Douglas Gregor
1124df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1125ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1126ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1127ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1128ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1129b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
11301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1131df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1132ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1133ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1134b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1135ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1136ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1137b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
11381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    atLoc, nameId, nameLoc, categoryId,
11398f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1140a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
114163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.push_back(ObjCImpDecl);
1142b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1143ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1144ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1145ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1146ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1147df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1148ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1149ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1150df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1151ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1152b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1153ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1154ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1155ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1156ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1157b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
1158cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1159ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
116260fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
1163a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
116463e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  PendingObjCImpDecl.push_back(ObjCImpDecl);
116563e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1166b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
11675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
116860fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1169b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1170ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1171ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1172b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy Result = ObjCImpDecl;
1173ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1174a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
1175a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
1176b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    ObjCImpDecl = DeclPtrTy();
117763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    PendingObjCImpDecl.pop_back();
1178a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
117994cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian  else
118094cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian    Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
1181a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  return Result;
11825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1183e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
11841ac7104947020a60430ba6f519cd5869af77046fFariborz JahanianParser::DeclGroupPtrTy Parser::RetrievePendingObjCImpDecl() {
118563e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  if (PendingObjCImpDecl.empty())
118663e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian    return Actions.ConvertDeclToDeclGroup(DeclPtrTy());
118763e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  DeclPtrTy ImpDecl = PendingObjCImpDecl.pop_back_val();
118863e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  Actions.ActOnAtEnd(SourceLocation(), ImpDecl);
118963e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian  return Actions.ConvertDeclToDeclGroup(ImpDecl);
119063e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian}
119163e963cdffca9530f920dbab58b9b4eecb2a582cFariborz Jahanian
1192e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1193e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1194e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1195b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1196e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1197e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1198e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1199df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1200e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1201b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1202e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1203243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1204243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1205df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1206e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1207b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1208e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1209243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1210243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1211243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
12121ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1213b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1214243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1215b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1216b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
12175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
12185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1219ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1220ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1221ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1222ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1223ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1224ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1225ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1226ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1227ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1228ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1229ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1230b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1231ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1232ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1233f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
1234df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1235ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1236b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1237ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1239df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::identifier)) {
1240f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1241f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1242f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1243df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1244ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1245ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1246df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1247ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1248ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1249ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1250f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1251ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume ivar-name
1252ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1253f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1254f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian                                  propertyId, propertyIvar);
1255df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1256ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1257ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1258ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1259df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
12601ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1261d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian  else
1262d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian    ConsumeToken(); // consume ';'
1263b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1264ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1265ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1266ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1267ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1268ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1269ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1270ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1271ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1272ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1273b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1274ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1275ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1276ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1277df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1278ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1279b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1280ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1281df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::identifier)) {
1282c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1283c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1284c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1285c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian                                  propertyId, 0);
1286c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1287df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1288ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1289ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1290ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1291df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
12921ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
1293b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1294ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
12951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1296397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1297397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1298397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
129943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
130015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Res(Actions);
1301397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1302df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
130339f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
13040e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1305397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
130643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1307397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1308397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
130939f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian  ConsumeToken(); // consume ';'
1310e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff  return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
1311397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1312397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1313c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
131478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1315c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
131643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult
131743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1318fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1319fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
13201ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
132143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1322fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1323fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
13242f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
13250e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
1326fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
132743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1328fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1329fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
13301ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
133143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1332fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1333fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
133478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
13351ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
133643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
133778a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
13383ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
13393ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
13408935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::DeclScope);
13413ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
134261364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult SynchBody(ParseCompoundStatementBody());
13430e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
13448935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
13450e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (SynchBody.isInvalid())
1346fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
134776ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
1348c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1349c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1350397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1351397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1352397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1353397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1354397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1355397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1356397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1357397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1358397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1359397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1360397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
136143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1362397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
136343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1364397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1365df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
13661ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
136743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1368397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
136915faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult CatchStmts(Actions);
137015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult FinallyStmt(Actions);
13718935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
137261364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult TryBody(ParseCompoundStatementBody());
13738935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
13740e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1375bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1376a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1377df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
13786b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
13796b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
13806b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
13816b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
13826b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
13836b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
13846b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
13850e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1386161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1387cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1388b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      DeclPtrTy FirstPart;
13893b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1390df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1391397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1392e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1393df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1394397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1395397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
139643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl          // For some odd reason, the name of the exception variable is
13971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // optional. As a result, we need to use "PrototypeContext", because
13987ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // we must accept either 'declarator' or 'abstract-declarator' here.
13997ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          Declarator ParmDecl(DS, Declarator::PrototypeContext);
14007ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
14017ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
14027ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // Inform the actions module about the parameter declarator, so it
14037ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
14047ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
140564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1406397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
140893a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
141093a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
141193a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
141293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
141393a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
141493a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
141515faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl        OwningStmtResult CatchBody(Actions, true);
1416c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1417c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1418c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1419c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
14200e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
14213b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
14220e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
14237ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff                        RParenLoc, FirstPart, move(CatchBody),
142476ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                        move(CatchStmts));
142564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
14261ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
14271ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
142843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1429397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1430397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
14316b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
14326b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
143364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
14348935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
14350e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
143615faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl      OwningStmtResult FinallyBody(Actions, true);
1437c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1438c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1439c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1440c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
14410e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1442161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
14430e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
144476ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                                   move(FinallyBody));
1445397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1446397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1447397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1448397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1449bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1450397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
145143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1452bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
145376ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
145476ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                    move(FinallyStmt));
1455397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1456ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
14573536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1458ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1459b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1460b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
146249f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
146349f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        PP.getSourceManager(),
146449f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        "parsing Objective-C method");
14651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1466ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1467209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1468496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    if (ObjCImpDecl) {
1469496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek      Diag(Tok, diag::warn_semicolon_before_method_body)
1470496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek        << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
1471496e45ef3350fabc312c5a807d308c65c50af4dbTed Kremenek    }
1472ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1473209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1474ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1475409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1476df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1477da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
14781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1479409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1480409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
14811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1482409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1483409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1484b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1485ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1486409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1488409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
14898935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1491409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1492394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
1493ebf6443a4f493233f7e8d92b3a991848c8b1c00dSteve Naroff  Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
149461364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
149561364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult FnBody(ParseCompoundStatementBody());
149661364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
1497409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
14980e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (FnBody.isInvalid())
1499a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1500a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                       MultiStmtArg(Actions), false);
1501798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
150232ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  // TODO: Pass argument information.
150332ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
15041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1505409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
15068935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
1507798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
150871c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
15095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
15105508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
151143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
151264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  if (Tok.isObjCAtKeyword(tok::objc_try)) {
15136b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
151464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  } else if (Tok.isObjCAtKeyword(tok::objc_throw))
151564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
151664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
151764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
1518d8c4e15138e69a51754cc259c8a592cc47950c8eSebastian Redl  OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
15190e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
152064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
152164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
152264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
152364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
152443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
152564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
152664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
152764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
15286b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders Carlsson  return Actions.ActOnExprStmt(Actions.FullExpr(Res));
152964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
153064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
15311d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
15325508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
1533b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1534b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
15351d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1536b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
15374fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
15381d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
15392f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
15404fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
15414fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
15421d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
15434fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
15441d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
15454fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
15461d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
15474fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
15481d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
15494fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
15505508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
15515508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
15525508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
15531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
15540ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
15550ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
15560ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-receiver:
15570ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
15580ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
15590ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
15601d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCMessageExpression() {
1561699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1562699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1563699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
1564699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  // Parse receiver
156514dd98a7952c9559e0d17fff8272bf3be67135afChris Lattner  if (isTokObjCMessageIdentifierReceiver()) {
1566699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1567d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1568d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      SourceLocation NameLoc = ConsumeToken();
1569d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1570d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian                                            ExprArg(Actions));
1571d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
1572699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
1573699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
15742f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
15750e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
15765c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
15771d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
1578699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
15791d922960e083906a586609ac6978678147250177Sebastian Redl
15800e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
158176ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                        0, move(Res));
1582699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
15831d922960e083906a586609ac6978678147250177Sebastian Redl
1584699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1585699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// the rest of a message expression.
15861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
15870ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
15880ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
15890ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
15900ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
15910ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
15920ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
15930ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
15940ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
15951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
15960ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
15970ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
15980ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
15990ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
16000ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
16010ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
16020ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
16030ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
16041d922960e083906a586609ac6978678147250177Sebastian Redl///
16051d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
1606699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
16075cb93b8bf009c4b0ae09b71ba85f54b2a7ea8022Steve Naroff                                       SourceLocation NameLoc,
1608699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                       IdentifierInfo *ReceiverName,
16091d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
1610c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  if (Tok.is(tok::code_completion)) {
1611c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    if (ReceiverName)
161260b01cc44855d62979f76dc4cdffa4277f321049Douglas Gregor      Actions.CodeCompleteObjCClassMessage(CurScope, ReceiverName, NameLoc);
1613c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    else
161460b01cc44855d62979f76dc4cdffa4277f321049Douglas Gregor      Actions.CodeCompleteObjCInstanceMessage(CurScope, ReceiverExpr.get());
1615c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff    ConsumeToken();
1616c4df6d2c05c647a6a5770ba0c749782b6c023a3aSteve Naroff  }
1617a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
16184b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
16192fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
162068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1621ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson  SourceLocation SelectorLoc = Loc;
16221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
162368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1624a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
162568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1626df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1627a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
1628a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
162968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
163037387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
1631df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
1632a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
16334fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
16344fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
16354fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
16364fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
16371d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
1638a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
16391d922960e083906a586609ac6978678147250177Sebastian Redl
164068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
16422f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
16430e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
16444fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
16454fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
16464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
16474fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
16481d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
164937387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
16501d922960e083906a586609ac6978678147250177Sebastian Redl
165137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
1652effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
16531d922960e083906a586609ac6978678147250177Sebastian Redl
165437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
16552fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
1656df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
1657a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
1658a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
1659a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1660a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
1661df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
166249f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
16631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
16642f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
16650e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
16664fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
16674fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
16684fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
16694fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
16701d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
167149f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
16720e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
167349f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
1674effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
1675a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1676a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
1677a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
16781d922960e083906a586609ac6978678147250177Sebastian Redl
16794fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
16804fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
16814fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
16824fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
16831d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1684a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
16851d922960e083906a586609ac6978678147250177Sebastian Redl
1686df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
1687a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_rsquare);
16884fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
16894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
16904fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
16914fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
16921d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1693a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
16941d922960e083906a586609ac6978678147250177Sebastian Redl
1695699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
16961d922960e083906a586609ac6978678147250177Sebastian Redl
169729238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
1698ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
1699ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
1700ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
17011d922960e083906a586609ac6978678147250177Sebastian Redl
1702ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // We've just parsed a keyword message.
17031d922960e083906a586609ac6978678147250177Sebastian Redl  if (ReceiverName)
17041d922960e083906a586609ac6978678147250177Sebastian Redl    return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
17051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           LBracLoc, NameLoc, SelectorLoc,
1706ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                           RBracLoc,
17071d922960e083906a586609ac6978678147250177Sebastian Redl                                           KeyExprs.take(), KeyExprs.size()));
17081d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
1709ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                            LBracLoc, SelectorLoc, RBracLoc,
17101d922960e083906a586609ac6978678147250177Sebastian Redl                                            KeyExprs.take(), KeyExprs.size()));
17110ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
17120ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
17131d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
171420df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  OwningExprResult Res(ParseStringLiteralExpression());
17151d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
17161d922960e083906a586609ac6978678147250177Sebastian Redl
1717b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
1718b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
1719b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
1720b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<SourceLocation, 4> AtLocs;
1721a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
1722b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
1723effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
17240e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1725b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
1726b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
1727b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
172815faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
172997cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
173097cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
1731b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
173297cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    OwningExprResult Lit(ParseStringLiteralExpression());
17330e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
17341d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
17350e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1736effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
1737b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
17381d922960e083906a586609ac6978678147250177Sebastian Redl
17391d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
17401d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
17415508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
1742f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1743f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
1744f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
17451d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
17461d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
1747861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
17481d922960e083906a586609ac6978678147250177Sebastian Redl
1749f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
17501d922960e083906a586609ac6978678147250177Sebastian Redl
17514fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
17521d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
17531d922960e083906a586609ac6978678147250177Sebastian Redl
1754f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
17551d922960e083906a586609ac6978678147250177Sebastian Redl
1756809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
17571d922960e083906a586609ac6978678147250177Sebastian Redl
17584988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
17591d922960e083906a586609ac6978678147250177Sebastian Redl
1760809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1761809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1762809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1764809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                                 Ty.get(), RParenLoc));
1765f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
176629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
176729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
176829b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
17691d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
17701d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
177129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
17721d922960e083906a586609ac6978678147250177Sebastian Redl
17734fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
17741d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
17751d922960e083906a586609ac6978678147250177Sebastian Redl
177629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
17771d922960e083906a586609ac6978678147250177Sebastian Redl
17784fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
17791d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
17801d922960e083906a586609ac6978678147250177Sebastian Redl
1781390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
178229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
17831d922960e083906a586609ac6978678147250177Sebastian Redl
17844988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
178529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
17861d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
17871d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
178829b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
1789a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
1790a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
1791a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
17921d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
17931d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
1794a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
17951d922960e083906a586609ac6978678147250177Sebastian Redl
17964fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
17971d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
17981d922960e083906a586609ac6978678147250177Sebastian Redl
1799b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1800a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
1801a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
18022fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
18031d922960e083906a586609ac6978678147250177Sebastian Redl  if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
18041d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
18051d922960e083906a586609ac6978678147250177Sebastian Redl
1806b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
1807887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
1808887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
1809a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
18104fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      if (Tok.isNot(tok::colon))
18111d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
18121d922960e083906a586609ac6978678147250177Sebastian Redl
1813cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      nColons++;
1814a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
1815a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
1816a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1817a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
1818a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
18192fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
1820b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
1821a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
1822a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1823a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
1824887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
1825a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1826887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
18271d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
18281d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
182958065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
1830