ParseObjc.cpp revision f6ed85533583dae18a44ddc4be6cfc4d68749e36
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"
175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/Diagnostic.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ParseExternalDeclaration:
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'
30dac269b65eed82182fc3e96566dedd6562dfe11eSteve NaroffParser::DeclTy *Parser::ParseObjCAtDirectives() {
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation AtLoc = ConsumeToken(); // the "@"
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
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);
535ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return 0;
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// objc-class-declaration:
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
61dac269b65eed82182fc3e96566dedd6562dfe11eSteve NaroffParser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
66df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
69dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      return 0;
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
74df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
82dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return 0;
835f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
84e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff  return Actions.ActOnForwardClassDeclaration(atLoc,
853536b443bc50d58a79f14fca9b6842541a434854Steve Naroff                                      &ClassNames[0], ClassNames.size());
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
88dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
89dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
90dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
91dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
92dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
93dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
94dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '@' 'interface' identifier objc-superclass[opt]
95dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
96dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
97dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
98dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
99dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
100dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
101dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '@' 'interface' identifier '(' identifier[opt] ')'
102dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
103dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
104dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
105dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
106dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
107dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
108dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
109dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
110dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
111dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
112dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
113dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
114dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
115dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
116dac269b65eed82182fc3e96566dedd6562dfe11eSteve NaroffParser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
117dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation atLoc, AttributeList *attrList) {
118861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
119dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
122df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
123dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
124dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return 0;
125dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1277ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
128dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
129dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
130df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren)) { // we have a category.
131dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation lparenLoc = ConsumeParen();
132dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation categoryLoc, rparenLoc;
133dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
134dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
135527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
136df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
137dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
138dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
139527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    } else if (!getLang().ObjC2) {
140527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
141527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      return 0;
142dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
143df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_rparen);
145dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      SkipUntil(tok::r_paren, false); // don't stop at ';'
146dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      return 0;
147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    rparenLoc = ConsumeParen();
1496bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner
150dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    // Next, we need to check for any protocol references.
1516bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    SourceLocation EndProtoLoc;
1526bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
1536bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    if (Tok.is(tok::less) &&
1546bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner        ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
1556bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner      return 0;
1566bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner
157dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    if (attrList) // categories don't support attributes.
158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_objc_no_attributes_on_category);
159dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
160e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
1613a165b066e665870c163af64844073a664c042d0Steve Naroff                                     nameId, nameLoc, categoryId, categoryLoc,
162423cb565abc681b770fb4b9b4bc24d398c98157bSteve Naroff                                     &ProtocolRefs[0], ProtocolRefs.size(),
1636bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner                                     EndProtoLoc);
164fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian
165fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian    ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
166bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    return CategoryType;
167dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
168dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
169dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
170dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
1717ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
172df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
173dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
174df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
175dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
176dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      return 0;
177dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
178dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
179dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
181dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
18206036d3709955a53297b4cbe14e20db88f321470Chris Lattner  llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
18306036d3709955a53297b4cbe14e20db88f321470Chris Lattner  SourceLocation EndProtoLoc;
18406036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
18506036d3709955a53297b4cbe14e20db88f321470Chris Lattner      ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
18606036d3709955a53297b4cbe14e20db88f321470Chris Lattner    return 0;
18706036d3709955a53297b4cbe14e20db88f321470Chris Lattner
18806036d3709955a53297b4cbe14e20db88f321470Chris Lattner  DeclTy *ClsType =
18906036d3709955a53297b4cbe14e20db88f321470Chris Lattner    Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
19006036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
19106036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     &ProtocolRefs[0], ProtocolRefs.size(),
19206036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     EndProtoLoc, attrList);
193f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
194df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
19560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ClsType, atLoc);
196dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
19725e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
198bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ClsType;
199dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
200dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
2014d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar/// constructSetterName - Return the setter name for the given
2024d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar/// identifier, i.e. "set" + Name where the initial character of Name
2034d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar/// has been capitalized.
2044d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbarstatic IdentifierInfo *constructSetterName(IdentifierTable &Idents,
2054d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar                                           const IdentifierInfo *Name) {
2064d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  unsigned N = Name->getLength();
2074d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  char *SelectorName = new char[3 + N];
2084d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  memcpy(SelectorName, "set", 3);
2094d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  memcpy(&SelectorName[3], Name->getName(), N);
2104d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  SelectorName[3] = toupper(SelectorName[3]);
2114d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar
2124d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  IdentifierInfo *Setter =
2134d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar    &Idents.get(SelectorName, &SelectorName[3 + N]);
2144d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  delete[] SelectorName;
2154d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  return Setter;
2164d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar}
2174d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar
218dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
219dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
220dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
221294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
2223536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
223dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
224dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
225dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
226294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
227294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
228294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
229294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
23025e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanianvoid Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
231cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                        tok::ObjCKeywordKind contextKey) {
2328a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek  llvm::SmallVector<DeclTy*, 32> allMethods;
23382a5fe3d1cf204b672cdab24d72275b6ad2c3527Fariborz Jahanian  llvm::SmallVector<DeclTy*, 16> allProperties;
23400933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
23560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
236bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  SourceLocation AtEndLoc;
237bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
238294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
239e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
240df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
241df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      DeclTy *methodPrototype =
242df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
24325e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
2443536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
2453536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
246d16245b6d48c6b4e7bbd40713aed6565b882746dSteve Naroff      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
247294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
248294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
249f366b4c84485b646c30d6891a249ad6f65abef7cFariborz Jahanian
250e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
251e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
252294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
253e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
254e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
255e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
256bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
257e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
258e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
259e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
260e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
261e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
2624985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // FIXME: as the name implies, this rule allows function definitions.
2634985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // We could pass a flag or check for functions during semantic analysis.
2643536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      ParseDeclarationOrFunctionDefinition();
265e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
266e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
267e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
268e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
269e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
270a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
271e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
272a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
273e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      AtEndLoc = AtLoc;
274e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
275bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
276e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
277bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
278bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
279bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
280a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
281a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
282bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
283bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
284bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
285bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
286f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
287bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
288a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
289a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
290a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner
291a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
292a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
293a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
294bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
295e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
296bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
297a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
298bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
299a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
300a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner
301a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
302f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
303f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner        Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
304f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
305e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
306e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse property attribute list, if any.
307f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (Tok.is(tok::l_paren)) {
308e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ParseObjCPropertyAttribute(OCDS);
309f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      }
310dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner
311e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
312e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      DeclSpec DS;
313e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
314e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ParseStructDeclaration(DS, FieldDeclarators);
315e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
316a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
317a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner                       tok::at);
318a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner
319e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Convert them all to property declarations.
320e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
321e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        FieldDeclarator &FD = FieldDeclarators[i];
322da3253d8a97981257185c89ced71ce137278b121Chris Lattner        if (FD.D.getIdentifier() == 0) {
323da3253d8a97981257185c89ced71ce137278b121Chris Lattner          Diag(AtLoc, diag::err_objc_property_requires_field_name,
324da3253d8a97981257185c89ced71ce137278b121Chris Lattner               FD.D.getSourceRange());
325da3253d8a97981257185c89ced71ce137278b121Chris Lattner          continue;
326da3253d8a97981257185c89ced71ce137278b121Chris Lattner        }
327da3253d8a97981257185c89ced71ce137278b121Chris Lattner
328e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        // Install the property declarator into interfaceDecl.
329da3253d8a97981257185c89ced71ce137278b121Chris Lattner        IdentifierInfo *SelName =
330da3253d8a97981257185c89ced71ce137278b121Chris Lattner          OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
331da3253d8a97981257185c89ced71ce137278b121Chris Lattner
332e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        Selector GetterSel =
333da3253d8a97981257185c89ced71ce137278b121Chris Lattner          PP.getSelectorTable().getNullarySelector(SelName);
334e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        IdentifierInfo *SetterName = OCDS.getSetterName();
335e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        if (!SetterName)
336e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner          SetterName = constructSetterName(PP.getIdentifierTable(),
337e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner                                           FD.D.getIdentifier());
338e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        Selector SetterSel =
339e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner          PP.getSelectorTable().getUnarySelector(SetterName);
340da3253d8a97981257185c89ced71ce137278b121Chris Lattner        DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
341da3253d8a97981257185c89ced71ce137278b121Chris Lattner                                                 GetterSel, SetterSel,
342da3253d8a97981257185c89ced71ce137278b121Chris Lattner                                                 MethodImplKind);
343e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        allProperties.push_back(Property);
344e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      }
345a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
346f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
347294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
348bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
349bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
350bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
351bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  if (Tok.isObjCAtKeyword(tok::objc_end))
352bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
353bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  else
354bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    Diag(Tok, diag::err_objc_missing_end);
355bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
356a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
357bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
3588a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek  Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
3598a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek                     allMethods.empty() ? 0 : &allMethods[0],
3608a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek                     allMethods.size(),
3618a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek                     allProperties.empty() ? 0 : &allProperties[0],
3628a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek                     allProperties.size());
363294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
364294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
365d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
366d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
367d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
368d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
369d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
370d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
371d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
372d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
373d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
374d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
375d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
376d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
377d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
378d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
379d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
380d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
3817caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnervoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
382dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  SourceLocation LHSLoc = ConsumeParen(); // consume '('
383dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner
384cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
385d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
386f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
387f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
388f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
389f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      MatchRHSPunctuation(tok::r_paren, LHSLoc);
390f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
391f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
392f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
393d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    // getter/setter require extra treatment.
394a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    if (II == ObjCPropertyAttrs[objc_getter] ||
395a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek        II == ObjCPropertyAttrs[objc_setter]) {
396d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian      // skip getter/setter part.
397d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian      SourceLocation loc = ConsumeToken();
398df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::equal)) {
399d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian        loc = ConsumeToken();
400df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.is(tok::identifier)) {
401a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek          if (II == ObjCPropertyAttrs[objc_setter]) {
402a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek            DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
40382a5fe3d1cf204b672cdab24d72275b6ad2c3527Fariborz Jahanian            DS.setSetterName(Tok.getIdentifierInfo());
404d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian            loc = ConsumeToken();  // consume method name
405df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner            if (Tok.isNot(tok::colon)) {
406d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian              Diag(loc, diag::err_expected_colon);
407cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner              SkipUntil(tok::r_paren);
408dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner              return;
409d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian            }
410dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner          } else {
411a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek            DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
41282a5fe3d1cf204b672cdab24d72275b6ad2c3527Fariborz Jahanian            DS.setGetterName(Tok.getIdentifierInfo());
41382a5fe3d1cf204b672cdab24d72275b6ad2c3527Fariborz Jahanian          }
414dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        } else {
415d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian          Diag(loc, diag::err_expected_ident);
416cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner          SkipUntil(tok::r_paren);
417dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner          return;
418cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner        }
419d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian      }
420d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian      else {
421d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian        Diag(loc, diag::err_objc_expected_equal);
422cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner        SkipUntil(tok::r_paren);
423dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
424d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian      }
425dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner    } else if (II == ObjCPropertyAttrs[objc_readonly])
426a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
427a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    else if (II == ObjCPropertyAttrs[objc_assign])
428a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
429a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    else if (II == ObjCPropertyAttrs[objc_readwrite])
430f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
431a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    else if (II == ObjCPropertyAttrs[objc_retain])
432a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
433a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    else if (II == ObjCPropertyAttrs[objc_copy])
434a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
435a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    else if (II == ObjCPropertyAttrs[objc_nonatomic])
436a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
437f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    else {
438cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      Diag(Tok.getLocation(), diag::err_objc_expected_property_attr,
439cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner           II->getName());
440cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
441cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
442cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
44382a5fe3d1cf204b672cdab24d72275b6ad2c3527Fariborz Jahanian
444d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    ConsumeToken(); // consume last attribute token
445df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::comma)) {
446dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner      ConsumeToken();
447d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian      continue;
448d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    }
449dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner
450dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner    if (Tok.is(tok::r_paren)) {
451dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner      ConsumeParen();
452dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner      return;
453dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner    }
454dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner
455dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner    MatchRHSPunctuation(tok::r_paren, LHSLoc);
456d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    return;
457d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
458d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
459d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
4603536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
461ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     objc-instance-method objc-method-decl objc-method-attributes[opt]
4623536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
463294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
464294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
465294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
466294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
4674985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
4684985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
4694985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
47000933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz JahanianParser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
471cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                          tok::ObjCKeywordKind MethodImplKind) {
472df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
473294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
474294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  tok::TokenKind methodType = Tok.getKind();
475bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
476294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
4771f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian  DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
4783536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
4792bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
480f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
481294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
482294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
483294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
484294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
485294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
486294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
487294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
488294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
489294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
490294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
4914b6c9051c6522894978c9ba6a819a659d102db36Fariborz JahanianIdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
492ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
493ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
494ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
495ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
496ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
497ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
4989298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
499ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
500ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
501ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
502ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
503ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
504ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
505ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
506ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
507ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
508ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
509ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
510ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
511ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
512ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
513ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
514ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
515ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
516ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
517ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
518ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
519ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
520ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
521ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
522ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
523ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
524ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
525ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
526ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
527ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
528ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
529ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
530ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
531ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
532ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
533ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
534ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
535ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
536ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
537ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
538ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
539ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
540ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
541ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
542ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
543ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
544ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
545ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
546ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
547ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
548ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
549ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
550ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
551ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
552ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
553ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
554ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
555ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
556ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
557ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
558ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
559ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
560ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
561ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
562ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
563ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
564ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
5654b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
566ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
567d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
568294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
569294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
5700196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
5710196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
572335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
5733ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
5743ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
5753ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
576335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanian  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
5775ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
5780196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
5790196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
580a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
581e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
582e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
583294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
584294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
585294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
586294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
587294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
588a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
589e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
590cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
591e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
592e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
593e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
594e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
595a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
596e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
597e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
598a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
599e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
600e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      default: assert(0 && "Unknown decl qualifier");
601a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
602a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
603a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
604a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
605a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
606a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
607e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
608a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
609e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
610e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
611e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
612e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
613e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
614e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
615e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
616e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
617e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
618e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
619e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
620e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
621e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
622e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
623a526c5c67e5a0473c340903ee542ce570119665fTed KremenekParser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
624df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
625294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
626294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  SourceLocation LParenLoc = ConsumeParen(), RParenLoc;
627e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
628271f1a6373c3fe8fa457fe1ca6c0c908416110fdChris Lattner  TypeTy *Ty = 0;
629294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
63019d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
631a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ParseObjCTypeQualifierList(DS);
6324fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
633294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  if (isTypeSpecifierQualifier()) {
634f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    Ty = ParseTypeName();
635f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    // FIXME: back when Sema support is in place...
636f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    // assert(Ty && "Parser::ParseObjCTypeName(): missing type");
637294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
638e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
639df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_paren)) {
640e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
641e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    if (Tok.getLocation() == TypeStartLoc) {
642e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner      Diag(Tok.getLocation(), diag::err_expected_type);
643e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner      SkipUntil(tok::r_brace);
644e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    } else {
645e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner      // Otherwise, we found *something*, but didn't get a ')' in the right
646e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner      // place.  Emit an error then return what we have as the type.
647e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner      MatchRHSPunctuation(tok::r_paren, LParenLoc);
648e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    }
649294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
650294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  RParenLoc = ConsumeParen();
651f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
652294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
653294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
654294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
655294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
6564985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
657294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
6584985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
659294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
660294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
6617ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-keyword-decl
662294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
663294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
664294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
6657ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
6667ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
6677ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
6687ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
669294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6704985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
6714985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
672294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6734985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
6744985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
675294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6764985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
677294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
678294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6797ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
6807ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
6817ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
682bef1185418705e16012b3dd50cd7c260c8d6b79cSteve NaroffParser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
6831f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                            tok::TokenKind mType,
6841f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                            DeclTy *IDecl,
685cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                            tok::ObjCKeywordKind MethodImplKind)
68668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff{
687e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
688ff38491c18b060526d754765b952f4a497a89416Chris Lattner  TypeTy *ReturnType = 0;
689a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
690df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
691f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ReturnType = ParseObjCTypeName(DSRet);
692e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
693bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
694bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
695e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
696e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  if (!SelIdent) { // missing selector name.
697e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    Diag(Tok.getLocation(), diag::err_expected_selector_for_method,
698e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner         SourceRange(mLoc, Tok.getLocation()));
699e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
700e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
701e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    return 0;
702e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
703e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
704df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
705ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
706ff38491c18b060526d754765b952f4a497a89416Chris Lattner    AttributeList *MethodAttrs = 0;
707df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
708ff38491c18b060526d754765b952f4a497a89416Chris Lattner      MethodAttrs = ParseAttributes();
709ff38491c18b060526d754765b952f4a497a89416Chris Lattner
710ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
711bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff    return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
7121f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                          mType, IDecl, DSRet, ReturnType, Sel,
713f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian                                          0, 0, 0, MethodAttrs, MethodImplKind);
714ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
715f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
71668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
71768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
718a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
71968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
720ff38491c18b060526d754765b952f4a497a89416Chris Lattner
721ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Action::TypeTy *TypeInfo;
722ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
723ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(SelIdent);
72468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
725ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
726df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
727ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
728ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
729ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
730ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
731a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCDeclSpec DSType;
732e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type.
733f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian      TypeInfo = ParseObjCTypeName(DSType);
734ff38491c18b060526d754765b952f4a497a89416Chris Lattner    else
735ff38491c18b060526d754765b952f4a497a89416Chris Lattner      TypeInfo = 0;
736ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyTypes.push_back(TypeInfo);
737f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ArgTypeQuals.push_back(DSType);
738f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
739ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
740df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
741ff38491c18b060526d754765b952f4a497a89416Chris Lattner      ParseAttributes(); // FIXME: pass attributes through.
7427ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
743df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
744ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
745ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
7464985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
747ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ArgNames.push_back(Tok.getIdentifierInfo());
748ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
749ff38491c18b060526d754765b952f4a497a89416Chris Lattner
750ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
7514b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
7524b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelIdent = ParseObjCSelector(Loc);
753df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
754ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
755ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
756ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
757ff38491c18b060526d754765b952f4a497a89416Chris Lattner
758335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
759335eafa5be51f6440672a74c73d588af72e96732Steve Naroff
760ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
761df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
762ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
763df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
764335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
7654985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
766ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
7674985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
768335eafa5be51f6440672a74c73d588af72e96732Steve Naroff    // FIXME: implement this...
769ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Parse the c-style argument declaration-specifier.
770ff38491c18b060526d754765b952f4a497a89416Chris Lattner    DeclSpec DS;
771ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
772ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Parse the declarator.
773ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
774ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
7754985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
776ff38491c18b060526d754765b952f4a497a89416Chris Lattner
777ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // FIXME: Add support for optional parmameter list...
778e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
779ff38491c18b060526d754765b952f4a497a89416Chris Lattner  AttributeList *MethodAttrs = 0;
780df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
781ff38491c18b060526d754765b952f4a497a89416Chris Lattner    MethodAttrs = ParseAttributes();
782ff38491c18b060526d754765b952f4a497a89416Chris Lattner
783ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
784ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
785bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
7861f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                        mType, IDecl, DSRet, ReturnType, Sel,
787f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian                                        &ArgTypeQuals[0], &KeyTypes[0],
788335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        &ArgNames[0], MethodAttrs,
789335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
790294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
791294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
792dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
793dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
794dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
7957caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
796e13b9595dc1e2f4288bec34f3412359f648e84a5Chris LattnerParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
797e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                            bool WarnOnDeclarations, SourceLocation &EndLoc) {
798e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
799e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
800e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  ConsumeToken(); // the "<"
801e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
802e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
803e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
804e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
805e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
806e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
807e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
808e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
809e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
810e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
811e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
812e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
813e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
814e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
815e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
816e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
817e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
818e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
819e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
820e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
821e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
822e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
823e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
824e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
825e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
826e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
827e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
828e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
829e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
830e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
831e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
832e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
833e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
834dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
835dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
836dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
837dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
838dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
839dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
840dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
841dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
842dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
843dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
844dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
845dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
846dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
847dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
848dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
849ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
850dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
851dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
852dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     struct-declaration
853dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
85460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroffvoid Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
85560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
856df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
8577d6402f753916a82d93ef0bf0f703ae4243a52beFariborz Jahanian  llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
858e13594279a952537ac903325efff57e3edca79d9Chris Lattner  llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
859e13594279a952537ac903325efff57e3edca79d9Chris Lattner
860ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
861ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff
862aa847fe3c88ee5e8d180e48537c58b805d48d95dFariborz Jahanian  tok::ObjCKeywordKind visibility = tok::objc_protected;
863ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
864df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
865ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
866ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff
867ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
868df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
869ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::ext_extra_struct_semi);
870ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
871ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
872ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
873e13594279a952537ac903325efff57e3edca79d9Chris Lattner
874ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
875df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
876ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
877861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
878ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
879ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
880ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
881ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
882861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
883ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
884ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
885ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
886ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
887ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
888ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
889ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
890e13594279a952537ac903325efff57e3edca79d9Chris Lattner
891e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
892e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
893e13594279a952537ac903325efff57e3edca79d9Chris Lattner    FieldDeclarators.clear();
894e13594279a952537ac903325efff57e3edca79d9Chris Lattner    ParseStructDeclaration(DS, FieldDeclarators);
895e13594279a952537ac903325efff57e3edca79d9Chris Lattner
896e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Convert them all to fields.
897e13594279a952537ac903325efff57e3edca79d9Chris Lattner    for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
898e13594279a952537ac903325efff57e3edca79d9Chris Lattner      FieldDeclarator &FD = FieldDeclarators[i];
899e13594279a952537ac903325efff57e3edca79d9Chris Lattner      // Install the declarator into interfaceDecl.
9001d78cc443084134428c4c333fab25148900c3ca4Fariborz Jahanian      DeclTy *Field = Actions.ActOnIvar(CurScope,
901e13594279a952537ac903325efff57e3edca79d9Chris Lattner                                         DS.getSourceRange().getBegin(),
9021d78cc443084134428c4c333fab25148900c3ca4Fariborz Jahanian                                         FD.D, FD.BitfieldSize, visibility);
903e13594279a952537ac903325efff57e3edca79d9Chris Lattner      AllIvarDecls.push_back(Field);
9047d6402f753916a82d93ef0bf0f703ae4243a52beFariborz Jahanian    }
9053536b443bc50d58a79f14fca9b6842541a434854Steve Naroff
906df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
907ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
908ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
909ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
910ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
911ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
912ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
913ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
91460fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
9158749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
9168749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
9178749be53f53384e7846502791ceda6c657228d07Steve Naroff  Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
9188749be53f53384e7846502791ceda6c657228d07Steve Naroff                      &AllIvarDecls[0], AllIvarDecls.size(),
9191bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
920ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
9215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
922dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
923dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
924dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
925dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
926dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
927dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
928dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier
929dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
9303536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///       objc-interface-decl-list
931dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
932dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
933dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
934dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
935dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
936dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
9373536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
938dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
939246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel DunbarParser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
940246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                               AttributeList *attrList) {
941861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
9427ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
9437ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
9447ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
945df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
9467ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
9477ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    return 0;
9487ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
9497ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
9507ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
9517ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
9527ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
953df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
9547caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
9557ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
9567caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1);
9577ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
9587caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
959df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
9607caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
9617caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
9627caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
9637ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
9647ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
9657ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
966df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
9677ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
9687ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
9697ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        return 0;
9707ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
9717caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
9727caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
9737ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
9747ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
975df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
9767ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
9777ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
9787ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
9797ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
9807ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      return 0;
9817caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
982e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
98337e58d104d7756f7c18f525b74e46b8eeec8ef81Steve Naroff                                                   &ProtocolRefs[0],
98437e58d104d7756f7c18f525b74e46b8eeec8ef81Steve Naroff                                                   ProtocolRefs.size());
9857caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
9867caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
9877ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
988e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  SourceLocation EndProtoLoc;
9897caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
990e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
9917caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
992e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
9937caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    return 0;
99425e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian
995e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  DeclTy *ProtoType =
996e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
997e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                        &ProtocolRefs[0], ProtocolRefs.size(),
998246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                        EndProtoLoc, attrList);
99925e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1000bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1001dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1002dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1003dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1004dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1005dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1006dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1007dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1008dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1009dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1010dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1011dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1012dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1013dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1014ac00b7f4a933e60e2f0afd83092339160adc140cFariborz JahanianParser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1015ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1016ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1017ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1018ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
1019ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1020df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1021ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1022ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    return 0;
1023ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1024ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1025ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1026ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1027ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1028df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren)) {
1029ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1030ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1031ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1032ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
1033ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1034df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1035ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1036ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1037ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1038ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1039ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      return 0;
1040ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1041df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1042ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1043ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1044ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      return 0;
1045ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1046ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1047e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
10488f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    atLoc, nameId, nameLoc, categoryId,
10498f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1050a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
1051db8f3d3bb96c75440f513c43ac73adace2aae5ffFariborz Jahanian    return 0;
1052ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1053ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1054ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1055ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1056df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1057ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1058ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1059df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1060ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1061ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      return 0;
1062ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1063ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1064ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1065ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1066e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff  DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
1067cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1068ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
1069ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian
107060fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
107160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
1072a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
1073ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1074db8f3d3bb96c75440f513c43ac73adace2aae5ffFariborz Jahanian  return 0;
10755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
107660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1077ac00b7f4a933e60e2f0afd83092339160adc140cFariborz JahanianParser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1078ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1079ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1080ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
108194cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian  if (ObjCImpDecl)
1082a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
108394cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian  else
108494cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian    Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
1085a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  return ObjCImpDecl;
10865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1087e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
1088e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1089e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1090e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1091e992af01d14e2e31037562c123af0a71ae1ed374Fariborz JahanianParser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1092e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1093e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1094e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1095df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1096e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1097e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    return 0;
1098e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1099243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1100243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1101df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1102e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1103e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    return 0;
1104e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1105243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1106243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1107243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
11088cd8c66914638429294ac510a4f15f43ba7f36a5Fariborz Jahanian    Diag(Tok, diag::err_expected_semi_after, "@compatibility_alias");
1109243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian    return 0;
1110243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1111243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1112243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian                                                   aliasId, aliasLoc,
1113243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian                                                   classId, classLoc);
1114243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  return ClsType;
11155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1117ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1118ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1119ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1120ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1121ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1122ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1123ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1124ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1125ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1126ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1127ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1128ac00b7f4a933e60e2f0afd83092339160adc140cFariborz JahanianParser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1129ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1130ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1131f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
1132df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1133ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1134ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    return 0;
1135ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1136df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::identifier)) {
1137f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1138f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1139f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1140df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1141ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1142ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1143df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1144ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1145ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1146ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1147f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1148ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume ivar-name
1149ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1150f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1151f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian                                  propertyId, propertyIvar);
1152df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1153ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1154ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1155ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1156df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
1157ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_semi_after, "@synthesize");
1158ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  return 0;
1159ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1160ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1161ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1162ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1163ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1164ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1165ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1166ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1167ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1168ac00b7f4a933e60e2f0afd83092339160adc140cFariborz JahanianParser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1169ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1170ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1171ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1172df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1173ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1174ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    return 0;
1175ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1176df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::identifier)) {
1177c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1178c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1179c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1180c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian                                  propertyId, 0);
1181c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1182df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1183ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1184ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1185ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1186df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
1187ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_semi_after, "@dynamic");
1188ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  return 0;
1189ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1190397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1191397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1192397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1193397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
119439f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz JahanianParser::StmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
119539f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian  ExprResult Res;
1196397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1197df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
119839f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
1199397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    if (Res.isInvalid) {
1200397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
120139f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian      return true;
1202397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1203397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
120439f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian  ConsumeToken(); // consume ';'
1205a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  return Actions.ActOnObjCAtThrowStmt(atLoc, Res.Val);
1206397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1207397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1208c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
120978a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1210c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
1211c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz JahanianParser::StmtResult Parser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1212fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1213fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
1214fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    Diag (Tok, diag::err_expected_lparen_after, "@synchronized");
1215fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    return true;
1216fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1217fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
121878a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  ExprResult Res = ParseExpression();
1219fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Res.isInvalid) {
1220fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
1221fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    return true;
1222fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1223fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
122478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian    Diag (Tok, diag::err_expected_lbrace);
1225fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    return true;
1226fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1227fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
122878a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
122978a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian    Diag (Tok, diag::err_expected_lbrace);
123078a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian    return true;
123178a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
12323ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
12333ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
12343ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  EnterScope(Scope::DeclScope);
12353ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
1236fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  StmtResult SynchBody = ParseCompoundStatementBody();
12373ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
12383ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  ExitScope();
1239fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (SynchBody.isInvalid)
1240fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
1241fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.Val, SynchBody.Val);
1242c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1243c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1244397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1245397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1246397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1247397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1248397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1249397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1250397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1251397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1252397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1253397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1254397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
12556b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris LattnerParser::StmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1256397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
125764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
1258397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1259df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1260397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag (Tok, diag::err_expected_lbrace);
1261b210bd0404f84b99259c9987d347a44d3c202238Fariborz Jahanian    return true;
1262397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
12633b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian  StmtResult CatchStmts;
1264161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian  StmtResult FinallyStmt;
12653527b59f246cde170e6877e3e62f78a95dbcdfceTed Kremenek  EnterScope(Scope::DeclScope);
1266397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  StmtResult TryBody = ParseCompoundStatementBody();
12673527b59f246cde170e6877e3e62f78a95dbcdfceTed Kremenek  ExitScope();
1268bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (TryBody.isInvalid)
1269bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
12706b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner
1271df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
12726b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
12736b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
12746b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
12756b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
12766b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
12776b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
12786b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
12796b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner
1280161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1281cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
12823b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      StmtTy *FirstPart = 0;
12833b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1284df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1285397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
12863b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian        EnterScope(Scope::DeclScope);
1287df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1288397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1289397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
1290459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          // For some odd reason, the name of the exception variable is
1291459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          // optional. As a result, we need to use PrototypeContext.
1292459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
12933b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          ParseDeclarator(DeclaratorInfo);
1294459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          if (DeclaratorInfo.getIdentifier()) {
1295459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff            DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1296914701ed49f31323176a784b49df05a0d177d1adDaniel Dunbar                                                          DeclaratorInfo, 0);
1297459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff            StmtResult stmtResult =
1298459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff              Actions.ActOnDeclStmt(aBlockVarDecl,
1299459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff                                    DS.getSourceRange().getBegin(),
1300459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff                                    DeclaratorInfo.getSourceRange().getEnd());
1301459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff            FirstPart = stmtResult.isInvalid ? 0 : stmtResult.Val;
1302459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          }
130364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1304397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
13053b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian        SourceLocation RParenLoc = ConsumeParen();
1306c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner
1307c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        StmtResult CatchBody(true);
1308c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1309c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1310c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1311c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
13123b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian        if (CatchBody.isInvalid)
13133b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
1314a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek        CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc, RParenLoc,
13153b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          FirstPart, CatchBody.Val, CatchStmts.Val);
13163b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian        ExitScope();
131764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
1318161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after,
1319161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian             "@catch clause");
1320b210bd0404f84b99259c9987d347a44d3c202238Fariborz Jahanian        return true;
1321397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1322397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
13236b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
13246b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
132564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
1326aefc36656d3113ef32a50cea9ae0621e07c8b26cTed Kremenek      EnterScope(Scope::DeclScope);
1327aefc36656d3113ef32a50cea9ae0621e07c8b26cTed Kremenek
1328c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner
1329c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      StmtResult FinallyBody(true);
1330c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1331c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1332c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1333c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
1334161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian      if (FinallyBody.isInvalid)
1335161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
1336a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
1337161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian                                                   FinallyBody.Val);
1338397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1339aefc36656d3113ef32a50cea9ae0621e07c8b26cTed Kremenek      ExitScope();
1340397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1341397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1342397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1343bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1344397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
1345bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    return true;
1346bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
1347a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  return Actions.ActOnObjCAtTryStmt(atLoc, TryBody.Val, CatchStmts.Val,
1348bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian                                    FinallyStmt.Val);
1349397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1350ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
13513536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1352ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
135371c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve NaroffParser::DeclTy *Parser::ParseObjCMethodDefinition() {
1354a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
1355ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1356df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi))
1357ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1358ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1359409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1360df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1361da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
1362409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1363409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1364409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
1365409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1366409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1367409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
136871c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff      return 0;
1369ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1370409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
1371409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1372409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
1373409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  EnterScope(Scope::FnScope|Scope::DeclScope);
1374409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1375409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1376394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
1377394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
1378409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1379409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  StmtResult FnBody = ParseCompoundStatementBody();
1380409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1381409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
1382409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  if (FnBody.isInvalid)
1383409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc, 0, 0, false);
1384409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1385409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
1386409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  ExitScope();
1387409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1388409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // TODO: Pass argument information.
1389394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  Actions.ActOnFinishFunctionBody(MDecl, FnBody.Val);
139071c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
13915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13925508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
139364515f31850024a263e8f55f81e9ea4b39925cfaSteve NaroffParser::StmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
139464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  if (Tok.isObjCAtKeyword(tok::objc_try)) {
13956b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
139664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  } else if (Tok.isObjCAtKeyword(tok::objc_throw))
139764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
139864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
139964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
140064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExprResult Res = ParseExpressionWithLeadingAt(AtLoc);
140164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  if (Res.isInvalid) {
140264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
140364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
140464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
140564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
140664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return true;
140764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
140864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
140964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
141064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  return Actions.ActOnExprStmt(Res.Val);
141164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
141264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
1413a642beb9f143130588dcf622f5f7020832b22707Steve NaroffParser::ExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
14145508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
1415b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1416b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
1417b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1418b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
14194fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
14204fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      return Diag(AtLoc, diag::err_unexpected_at);
14214fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
14224fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
14234fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
14244fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
14254fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
14264fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
14274fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
14284fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
14294fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
14304fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      return Diag(AtLoc, diag::err_unexpected_at);
14314fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
14325508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
14335508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
14345508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
14350ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-expr:
14360ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
14370ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14380ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-receiver:
14390ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
14400ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
14410ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
1442699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ExprResult Parser::ParseObjCMessageExpression() {
1443699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1444699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1445699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
1446699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  // Parse receiver
144714dd98a7952c9559e0d17fff8272bf3be67135afChris Lattner  if (isTokObjCMessageIdentifierReceiver()) {
1448699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1449699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    ConsumeToken();
1450699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    return ParseObjCMessageExpressionBody(LBracLoc, ReceiverName, 0);
1451699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
1452699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
145369d349a8743f156b965141cda80520bddda84601Chris Lattner  ExprResult Res = ParseExpression();
1454699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  if (Res.isInvalid) {
14555c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
1456699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    return Res;
1457699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
14584fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
1459699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  return ParseObjCMessageExpressionBody(LBracLoc, 0, Res.Val);
1460699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
1461699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
1462699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1463699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// the rest of a message expression.
14640ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14650ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
14660ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
14670ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
14680ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14690ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
14700ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
14710ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
14720ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14730ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg:
14740ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
14750ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14760ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
14770ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
14780ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14790ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
14800ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
14810ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
14820ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
1483699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ExprResult
1484699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
1485699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                       IdentifierInfo *ReceiverName,
1486699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                       ExprTy *ReceiverExpr) {
1487a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
14884b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
14894b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  IdentifierInfo *selIdent = ParseObjCSelector(Loc);
149068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
149168d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
149268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<Action::ExprTy *, 12> KeyExprs;
149368d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1494df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1495a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
1496a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
149768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
149837387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
1499df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
1500a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
15014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
15024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
15044fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
150537387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff        return true;
1506a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
15074fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
150868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
1509a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      ///  Parse the expression after ':'
151037387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      ExprResult Res = ParseAssignmentExpression();
151137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      if (Res.isInvalid) {
15124fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
15134fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15144fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
15154fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
151637387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff        return Res;
151737387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
15184fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
151937387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
152068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyExprs.push_back(Res.Val);
1521a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian
152237387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
15234b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian      selIdent = ParseObjCSelector(Loc);
1524df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
1525a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
1526a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
1527a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1528a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
1529df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
153049f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
153149f109c786f99eb7468dac3976db083a65493444Steve Naroff      ///  Parse the expression after ','
153249f109c786f99eb7468dac3976db083a65493444Steve Naroff      ExprResult Res = ParseAssignmentExpression();
153349f109c786f99eb7468dac3976db083a65493444Steve Naroff      if (Res.isInvalid) {
15344fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
15354fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15364fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
15374fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
153849f109c786f99eb7468dac3976db083a65493444Steve Naroff        return Res;
153949f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
15404fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
154149f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
154249f109c786f99eb7468dac3976db083a65493444Steve Naroff      KeyExprs.push_back(Res.Val);
1543a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1544a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
1545a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
15464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
15474fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
15484fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15494fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
15504fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
15510ba0aa1460ae234423609c09da1b6ad11fae24f3Fariborz Jahanian    return true;
1552a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
1553ff38491c18b060526d754765b952f4a497a89416Chris Lattner
1554df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
1555a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_rsquare);
15564fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
15574fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15584fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
15594fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
15600ba0aa1460ae234423609c09da1b6ad11fae24f3Fariborz Jahanian    return true;
1561a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
15624fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
1563699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
156437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
156529238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
1566ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
1567ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
1568ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
1569ff38491c18b060526d754765b952f4a497a89416Chris Lattner
1570ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // We've just parsed a keyword message.
157137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff  if (ReceiverName)
15720523aaf9759cd9524956f0930558737458849743Fariborz Jahanian    return Actions.ActOnClassMessage(CurScope,
1573699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                     ReceiverName, Sel, LBracLoc, RBracLoc,
157449f109c786f99eb7468dac3976db083a65493444Steve Naroff                                     &KeyExprs[0], KeyExprs.size());
1575699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  return Actions.ActOnInstanceMessage(ReceiverExpr, Sel, LBracLoc, RBracLoc,
157649f109c786f99eb7468dac3976db083a65493444Steve Naroff                                      &KeyExprs[0], KeyExprs.size());
15770ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
15780ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
1579beaf299a2701c5559a4e5d76b0c40f805afb8e6aSteve NaroffParser::ExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
15805508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  ExprResult Res = ParseStringLiteralExpression();
15815508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  if (Res.isInvalid) return Res;
1582b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
1583b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
1584b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
1585b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
1586b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<SourceLocation, 4> AtLocs;
1587b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<ExprTy*, 4> AtStrings;
1588b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
1589b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtStrings.push_back(Res.Val);
1590b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
1591b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
1592b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
1593b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
1594b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    ExprResult Res(true);  // Invalid unless there is a string literal.
1595b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    if (isTokenStringLiteral())
1596b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner      Res = ParseStringLiteralExpression();
1597b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    else
1598b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner      Diag(Tok, diag::err_objc_concat_string);
1599b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
1600b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    if (Res.isInvalid) {
1601b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner      while (!AtStrings.empty()) {
1602b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner        Actions.DeleteExpr(AtStrings.back());
1603b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner        AtStrings.pop_back();
1604b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner      }
1605b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner      return Res;
1606b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    }
1607b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
1608b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtStrings.push_back(Res.Val);
1609b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
161079a99f2c063ce7cd9b18852a39bf4c937fc1faf8Fariborz Jahanian
1611b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  return Actions.ParseObjCStringLiteral(&AtLocs[0], &AtStrings[0],
1612b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner                                        AtStrings.size());
16135508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
1614f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1615f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
1616f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
1617674af9541256dc3ef803e3723027a8b028f1f7a2Chris LattnerParser::ExprResult Parser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
1618861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
1619f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1620f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
1621f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
16224fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
16234fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    return Diag(Tok, diag::err_expected_lparen_after, "@encode");
1624f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1625f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
1626f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1627f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  TypeTy *Ty = ParseTypeName();
1628f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
16294988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1630f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1631674af9541256dc3ef803e3723027a8b028f1f7a2Chris Lattner  return Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
16324988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson                                           RParenLoc);
1633f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
163429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
163529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
163629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
163729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
16382a35fa9b5e0f9d9429c04adb2e1249b6fd425e7aFariborz JahanianParser::ExprResult Parser::ParseObjCProtocolExpression(SourceLocation AtLoc)
163929b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson{
164029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
164129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
16424fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
16434fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    return Diag(Tok, diag::err_expected_lparen_after, "@protocol");
164429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
164529b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
164629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
16474fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
16484fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    return Diag(Tok, diag::err_expected_ident);
16494fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
1650390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
165129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
165229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
16534988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
165429b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
1655390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  return Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
1656390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian                                             LParenLoc, RParenLoc);
165729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
1658a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
1659a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
1660a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
16612a35fa9b5e0f9d9429c04adb2e1249b6fd425e7aFariborz JahanianParser::ExprResult Parser::ParseObjCSelectorExpression(SourceLocation AtLoc)
1662a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian{
1663a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
1664a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
16654fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
16664fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    return Diag(Tok, diag::err_expected_lparen_after, "@selector");
1667a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
1668b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1669a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
1670a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
1671a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
16724fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (!SelIdent && Tok.isNot(tok::colon))
16734fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    return Diag(Tok, diag::err_expected_ident); // missing selector name.
16744fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
1675b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
1676887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
1677887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
1678a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
16794fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      if (Tok.isNot(tok::colon))
16804fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        return Diag(Tok, diag::err_expected_colon);
16814fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner
1682cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      nColons++;
1683a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
1684a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
1685a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1686a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
1687a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
1688a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SelIdent = ParseObjCSelector(Loc);
1689b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
1690a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
1691a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1692a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
1693887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
1694a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1695887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
16962a35fa9b5e0f9d9429c04adb2e1249b6fd425e7aFariborz Jahanian  return Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc, LParenLoc,
1697b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian                                             RParenLoc);
169858065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
1699