ParseObjc.cpp revision 1fd80116b49782c367ff5d5f50a8b76dd8a5d7f7
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"
17a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl#include "AstGuard.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/Diagnostic.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
23891dca671a80c865c6def7259f170ba785e4faaaChris Lattner/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       external-declaration: [C99 6.9]
255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
2691fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-class-declaration
2791fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-alias-declaration
2891fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-protocol-definition
2991fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-method-definition
3091fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  '@' 'end'
31dac269b65eed82182fc3e96566dedd6562dfe11eSteve NaroffParser::DeclTy *Parser::ParseObjCAtDirectives() {
325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation AtLoc = ConsumeToken(); // the "@"
335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
34861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  switch (Tok.getObjCKeywordID()) {
355ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_class:
365ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtClassDeclaration(AtLoc);
375ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_interface:
385ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtInterfaceDeclaration(AtLoc);
395ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_protocol:
405ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtProtocolDeclaration(AtLoc);
415ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
425ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtImplementationDeclaration(AtLoc);
435ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
445ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtEndDeclaration(AtLoc);
455ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
465ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtAliasDeclaration(AtLoc);
475ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
485ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertySynthesize(AtLoc);
495ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
505ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertyDynamic(AtLoc);
515ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
525ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
535ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
545ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return 0;
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// objc-class-declaration:
605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
62dac269b65eed82182fc3e96566dedd6562dfe11eSteve NaroffParser::DeclTy *Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
67df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
70dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      return 0;
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
75df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
825f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
83dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return 0;
845f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
85e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff  return Actions.ActOnForwardClassDeclaration(atLoc,
863536b443bc50d58a79f14fca9b6842541a434854Steve Naroff                                      &ClassNames[0], ClassNames.size());
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
885f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
89dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
90dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
91dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
92dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
93dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
94dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
95dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '@' 'interface' identifier objc-superclass[opt]
96dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
97dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
98dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
99dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
100dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
101dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
102dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '@' 'interface' identifier '(' identifier[opt] ')'
103dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
104dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
105dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
106dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
107dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
108dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
109dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
110dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
111dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
112dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
113dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
114dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
115dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
116dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
117dac269b65eed82182fc3e96566dedd6562dfe11eSteve NaroffParser::DeclTy *Parser::ParseObjCAtInterfaceDeclaration(
118dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation atLoc, AttributeList *attrList) {
119861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
120dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
121dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
122dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
123df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
124dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
125dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    return 0;
126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
127dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1287ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
129dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
130dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
131df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren)) { // we have a category.
132dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation lparenLoc = ConsumeParen();
133dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation categoryLoc, rparenLoc;
134dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
135dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
136527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
137df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
138dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
139dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
140527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    } else if (!getLang().ObjC2) {
141527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
142527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      return 0;
143dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
144df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
145dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_rparen);
146dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      SkipUntil(tok::r_paren, false); // don't stop at ';'
147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      return 0;
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
149dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    rparenLoc = ConsumeParen();
1506bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner
151dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    // Next, we need to check for any protocol references.
1526bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    SourceLocation EndProtoLoc;
1536bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
1546bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    if (Tok.is(tok::less) &&
1556bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner        ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
1566bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner      return 0;
1576bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner
158dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    if (attrList) // categories don't support attributes.
159dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_objc_no_attributes_on_category);
160dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
161e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    DeclTy *CategoryType = Actions.ActOnStartCategoryInterface(atLoc,
1623a165b066e665870c163af64844073a664c042d0Steve Naroff                                     nameId, nameLoc, categoryId, categoryLoc,
163423cb565abc681b770fb4b9b4bc24d398c98157bSteve Naroff                                     &ProtocolRefs[0], ProtocolRefs.size(),
1646bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner                                     EndProtoLoc);
165fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian
166fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian    ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
167bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    return CategoryType;
168dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
169dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
170dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
171dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
1727ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
173df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
174dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
175df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
176dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
177dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      return 0;
178dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
179dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
181dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
182dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
18306036d3709955a53297b4cbe14e20db88f321470Chris Lattner  llvm::SmallVector<Action::DeclTy*, 8> ProtocolRefs;
18406036d3709955a53297b4cbe14e20db88f321470Chris Lattner  SourceLocation EndProtoLoc;
18506036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
18606036d3709955a53297b4cbe14e20db88f321470Chris Lattner      ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
18706036d3709955a53297b4cbe14e20db88f321470Chris Lattner    return 0;
18806036d3709955a53297b4cbe14e20db88f321470Chris Lattner
18906036d3709955a53297b4cbe14e20db88f321470Chris Lattner  DeclTy *ClsType =
19006036d3709955a53297b4cbe14e20db88f321470Chris Lattner    Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
19106036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
19206036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     &ProtocolRefs[0], ProtocolRefs.size(),
19306036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     EndProtoLoc, attrList);
194f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
195df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
19660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ClsType, atLoc);
197dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
19825e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
199bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ClsType;
200dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
201dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
2024d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar/// constructSetterName - Return the setter name for the given
2034d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar/// identifier, i.e. "set" + Name where the initial character of Name
2044d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar/// has been capitalized.
2054d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbarstatic IdentifierInfo *constructSetterName(IdentifierTable &Idents,
2064d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar                                           const IdentifierInfo *Name) {
207edc66f3ab000f54e0e03c5840d7b54e9bf8615cbChris Lattner  llvm::SmallString<100> SelectorName;
20869d27b9f2e1c678b6b8a199fa3f705f2b3ff5e0dChris Lattner  SelectorName = "set";
209edc66f3ab000f54e0e03c5840d7b54e9bf8615cbChris Lattner  SelectorName.append(Name->getName(), Name->getName()+Name->getLength());
2104d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar  SelectorName[3] = toupper(SelectorName[3]);
211edc66f3ab000f54e0e03c5840d7b54e9bf8615cbChris Lattner  return &Idents.get(&SelectorName[0], &SelectorName[SelectorName.size()]);
2124d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar}
2134d7da2f4a39adcc2ea211fb30600fabde4f3a0b1Daniel Dunbar
214dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
215dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
216dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
217294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
2183536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
219dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
220dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
221dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
222294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
223294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
224294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
225294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
22625e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanianvoid Parser::ParseObjCInterfaceDeclList(DeclTy *interfaceDecl,
227cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                        tok::ObjCKeywordKind contextKey) {
2288a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek  llvm::SmallVector<DeclTy*, 32> allMethods;
22982a5fe3d1cf204b672cdab24d72275b6ad2c3527Fariborz Jahanian  llvm::SmallVector<DeclTy*, 16> allProperties;
23000933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
23160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
232bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  SourceLocation AtEndLoc;
233bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
234294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
235e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
236df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
237df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      DeclTy *methodPrototype =
238df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
23925e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
2403536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
2413536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
242d16245b6d48c6b4e7bbd40713aed6565b882746dSteve Naroff      ExpectAndConsume(tok::semi, diag::err_expected_semi_after,"method proto");
243294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
244294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
245f366b4c84485b646c30d6891a249ad6f65abef7cFariborz Jahanian
246e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
247e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
248294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
249e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
250e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
251e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
252bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
253e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
254e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
255e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
256e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
257e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
2581fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
2591fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
2601fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
2611fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
2621fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
2631fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner
2644985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // FIXME: as the name implies, this rule allows function definitions.
2654985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // We could pass a flag or check for functions during semantic analysis.
2663536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      ParseDeclarationOrFunctionDefinition();
267e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
268e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
269e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
270e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
271e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
272a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
273e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
274a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
275e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      AtEndLoc = AtLoc;
276e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
277bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
278e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
279bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
280bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
281bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
282a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
283a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
284bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
285bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
286bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
287bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
288f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
289bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
290a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
291a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
292a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner
293a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
294a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
295a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
296bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
297e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
298bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
299a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
300bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
301a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
302a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner
303a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
304f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
305f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner        Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
306f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
307e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
308e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse property attribute list, if any.
3098ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.is(tok::l_paren))
310e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ParseObjCPropertyAttribute(OCDS);
311dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner
312e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
313e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      DeclSpec DS;
314e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
315e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ParseStructDeclaration(DS, FieldDeclarators);
316e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner
317a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
318a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner                       tok::at);
319a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner
320e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Convert them all to property declarations.
321e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
322e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        FieldDeclarator &FD = FieldDeclarators[i];
323da3253d8a97981257185c89ced71ce137278b121Chris Lattner        if (FD.D.getIdentifier() == 0) {
324ef708fd4abccf4d21b1f82ab2ab62f6ae7cc1265Chris Lattner          Diag(AtLoc, diag::err_objc_property_requires_field_name)
325ef708fd4abccf4d21b1f82ab2ab62f6ae7cc1265Chris Lattner            << FD.D.getSourceRange();
326da3253d8a97981257185c89ced71ce137278b121Chris Lattner          continue;
327da3253d8a97981257185c89ced71ce137278b121Chris Lattner        }
328da3253d8a97981257185c89ced71ce137278b121Chris Lattner
329e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        // Install the property declarator into interfaceDecl.
330da3253d8a97981257185c89ced71ce137278b121Chris Lattner        IdentifierInfo *SelName =
331da3253d8a97981257185c89ced71ce137278b121Chris Lattner          OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
332da3253d8a97981257185c89ced71ce137278b121Chris Lattner
333e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        Selector GetterSel =
334da3253d8a97981257185c89ced71ce137278b121Chris Lattner          PP.getSelectorTable().getNullarySelector(SelName);
335e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        IdentifierInfo *SetterName = OCDS.getSetterName();
336e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        if (!SetterName)
337e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner          SetterName = constructSetterName(PP.getIdentifierTable(),
338e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner                                           FD.D.getIdentifier());
339e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        Selector SetterSel =
340e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner          PP.getSelectorTable().getUnarySelector(SetterName);
3418cf0bb3c2a798ce3acacaac2d3178648cd4c65c6Fariborz Jahanian        bool isOverridingProperty = false;
342da3253d8a97981257185c89ced71ce137278b121Chris Lattner        DeclTy *Property = Actions.ActOnProperty(CurScope, AtLoc, FD, OCDS,
343da3253d8a97981257185c89ced71ce137278b121Chris Lattner                                                 GetterSel, SetterSel,
3448cf0bb3c2a798ce3acacaac2d3178648cd4c65c6Fariborz Jahanian                                                 interfaceDecl,
3458cf0bb3c2a798ce3acacaac2d3178648cd4c65c6Fariborz Jahanian                                                 &isOverridingProperty,
346da3253d8a97981257185c89ced71ce137278b121Chris Lattner                                                 MethodImplKind);
3478cf0bb3c2a798ce3acacaac2d3178648cd4c65c6Fariborz Jahanian        if (!isOverridingProperty)
3488cf0bb3c2a798ce3acacaac2d3178648cd4c65c6Fariborz Jahanian          allProperties.push_back(Property);
349e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      }
350a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
351f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
352294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
353bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
354bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
355bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
356bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  if (Tok.isObjCAtKeyword(tok::objc_end))
357bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
358bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  else
359bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    Diag(Tok, diag::err_objc_missing_end);
360bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
361a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
362bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
3638a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek  Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
3648a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek                     allMethods.empty() ? 0 : &allMethods[0],
3658a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek                     allMethods.size(),
3668a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek                     allProperties.empty() ? 0 : &allProperties[0],
3678a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek                     allProperties.size());
368294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
369294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
370d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
371d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
372d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
373d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
374d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
375d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
376d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
377d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
378d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
379d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
380d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
381d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
382d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
383d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
384d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
385d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
3867caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnervoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
3878ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
388dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  SourceLocation LHSLoc = ConsumeParen(); // consume '('
389dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner
390cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
391d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
392f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
393f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
394f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
395f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      MatchRHSPunctuation(tok::r_paren, LHSLoc);
396f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
397f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
398f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
399156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
400156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner
40192e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
402e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
40392e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
404e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
40592e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
406e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
40792e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
408e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
40992e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
410e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
41192e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
412e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
41392e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
414e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
415156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
416156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                           tok::r_paren))
417dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
418156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner
4198ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.isNot(tok::identifier)) {
4201ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(Tok, diag::err_expected_ident);
4218ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
4228ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
4238ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
4248ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner
4255fd80fada1eeecd279af8333a7f58b86d8df8016Chris Lattner      if (II->getName()[0] == 's') {
4268ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
4278ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setSetterName(Tok.getIdentifierInfo());
428156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
429156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner
430156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
431156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
4328ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
4338ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
4348ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
4358ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setGetterName(Tok.getIdentifierInfo());
436156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
4378ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
438e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
439a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
440cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
441cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
442cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
44382a5fe3d1cf204b672cdab24d72275b6ad2c3527Fariborz Jahanian
444156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
445156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
446dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner
447156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
448d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
449156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner
450156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner  MatchRHSPunctuation(tok::r_paren, LHSLoc);
451d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
452d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
4533536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
454ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     objc-instance-method objc-method-decl objc-method-attributes[opt]
4553536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
456294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
457294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
458294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
459294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
4604985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
4614985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
4624985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
46300933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz JahanianParser::DeclTy *Parser::ParseObjCMethodPrototype(DeclTy *IDecl,
464cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                          tok::ObjCKeywordKind MethodImplKind) {
465df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
466294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
467294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  tok::TokenKind methodType = Tok.getKind();
468bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
469294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
4701f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian  DeclTy *MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl, MethodImplKind);
4713536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
4722bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
473f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
474294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
475294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
476294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
477294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
478294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
479294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
480294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
481294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
482294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
483294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
4844b6c9051c6522894978c9ba6a819a659d102db36Fariborz JahanianIdentifierInfo *Parser::ParseObjCSelector(SourceLocation &SelectorLoc) {
485ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
486ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
487ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
488ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
489ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
490ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
4919298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
492ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
493ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
494ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
495ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
496ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
497ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
498ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
499ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
500ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
501ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
502ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
503ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
504ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
505ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
506ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
507ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
508ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
509ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
510ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
511ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
512ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
513ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
514ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
515ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
516ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
517ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
518ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
519ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
520ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
521ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
522ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
523ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
524ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
525ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
526ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
527ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
528ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
529ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
530ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
531ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
532ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
533ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
534ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
535ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
536ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
537ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
538ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
539ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
540ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
541ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
542ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
543ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
544ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
545ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
546ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
547ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
548ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
549ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
550ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
551ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
552ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
553ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
554ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
555ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
556ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
557ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
5584b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
559ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
560d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
561294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
562294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
5630196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
5640196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
565335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
5663ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
5673ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
5683ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
569335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanian  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
5705ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
5710196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
5720196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
573a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
574e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
575e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
576294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
577294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
578294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
579294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
580294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
581a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
582e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
583cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
584e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
585e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
586e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
587e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
588a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
589e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
590e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
591a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
592e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
593e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      default: assert(0 && "Unknown decl qualifier");
594a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
595a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
596a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
597a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
598a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
599a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
600e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
601a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
602e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
603e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
604e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
605e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
606e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
607e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
608e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
609e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
610e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
611e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
612e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
613e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
614e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
615e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
616a526c5c67e5a0473c340903ee542ce570119665fTed KremenekParser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
617df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
618294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
6194a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  SourceLocation LParenLoc = ConsumeParen();
620e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
621294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
62219d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
623a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ParseObjCTypeQualifierList(DS);
6244fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
6254a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  TypeTy *Ty = 0;
6264a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (isTypeSpecifierQualifier())
627f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    Ty = ParseTypeName();
628e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
6294a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
6304a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    ConsumeParen();
6314a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
632e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
6331ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
6344a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
6354a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
6364a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
6374a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
6384a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    MatchRHSPunctuation(tok::r_paren, LParenLoc);
639294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
640f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
641294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
642294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
643294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
644294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
6454985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
646294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
6474985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
648294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
649294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
6507ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-keyword-decl
651294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
652294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
653294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
6547ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
6557ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
6567ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
6577ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
658294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6594985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
6604985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
661294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6624985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
6634985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
664294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6654985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
666294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
667294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6687ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
6697ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
6707ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
671bef1185418705e16012b3dd50cd7c260c8d6b79cSteve NaroffParser::DeclTy *Parser::ParseObjCMethodDecl(SourceLocation mLoc,
6721f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                            tok::TokenKind mType,
6731f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                            DeclTy *IDecl,
674cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                            tok::ObjCKeywordKind MethodImplKind)
67568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff{
676e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
677ff38491c18b060526d754765b952f4a497a89416Chris Lattner  TypeTy *ReturnType = 0;
678a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
679df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
680f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ReturnType = ParseObjCTypeName(DSRet);
681e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
682bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
683bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  IdentifierInfo *SelIdent = ParseObjCSelector(selLoc);
684e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
685e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  if (!SelIdent) { // missing selector name.
6861ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
6871ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
688e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
689e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
690e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    return 0;
691e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
692e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
693439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian  llvm::SmallVector<Declarator, 8> CargNames;
694df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
695ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
696ff38491c18b060526d754765b952f4a497a89416Chris Lattner    AttributeList *MethodAttrs = 0;
697df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
698ff38491c18b060526d754765b952f4a497a89416Chris Lattner      MethodAttrs = ParseAttributes();
699ff38491c18b060526d754765b952f4a497a89416Chris Lattner
700ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
701bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff    return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
7021f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                          mType, IDecl, DSRet, ReturnType, Sel,
703439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian                                          0, 0, 0, CargNames,
704439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian                                          MethodAttrs, MethodImplKind);
705ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
706f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
70768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
70868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<Action::TypeTy *, 12> KeyTypes;
709a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  llvm::SmallVector<ObjCDeclSpec, 12> ArgTypeQuals;
71068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> ArgNames;
711ff38491c18b060526d754765b952f4a497a89416Chris Lattner
712ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Action::TypeTy *TypeInfo;
713ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
714ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(SelIdent);
71568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
716ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
717df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
718ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
719ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
720ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
721ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
722a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCDeclSpec DSType;
723e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type.
724f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian      TypeInfo = ParseObjCTypeName(DSType);
725ff38491c18b060526d754765b952f4a497a89416Chris Lattner    else
726ff38491c18b060526d754765b952f4a497a89416Chris Lattner      TypeInfo = 0;
727ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyTypes.push_back(TypeInfo);
728f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ArgTypeQuals.push_back(DSType);
729f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
730ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
731df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
732ff38491c18b060526d754765b952f4a497a89416Chris Lattner      ParseAttributes(); // FIXME: pass attributes through.
7337ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
734df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
735ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
736ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
7374985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
738ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ArgNames.push_back(Tok.getIdentifierInfo());
739ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
740ff38491c18b060526d754765b952f4a497a89416Chris Lattner
741ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
7424b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
7434b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelIdent = ParseObjCSelector(Loc);
744df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
745ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
746ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
747ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
748ff38491c18b060526d754765b952f4a497a89416Chris Lattner
749335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
750335eafa5be51f6440672a74c73d588af72e96732Steve Naroff
751ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
752df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
753ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
754df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
755335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
7564985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
757ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
7584985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
759ff38491c18b060526d754765b952f4a497a89416Chris Lattner    DeclSpec DS;
760ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
761ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Parse the declarator.
762ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
763ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
764439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian    CargNames.push_back(ParmDecl);
7654985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
766ff38491c18b060526d754765b952f4a497a89416Chris Lattner
767ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // FIXME: Add support for optional parmameter list...
768e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
769ff38491c18b060526d754765b952f4a497a89416Chris Lattner  AttributeList *MethodAttrs = 0;
770df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
771ff38491c18b060526d754765b952f4a497a89416Chris Lattner    MethodAttrs = ParseAttributes();
772ff38491c18b060526d754765b952f4a497a89416Chris Lattner
773ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
774ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
775bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  return Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
7761f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                        mType, IDecl, DSRet, ReturnType, Sel,
777f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian                                        &ArgTypeQuals[0], &KeyTypes[0],
778439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian                                        &ArgNames[0], CargNames,
779439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian                                        MethodAttrs,
780335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
781294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
782294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
783dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
784dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
785dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
7867caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
787e13b9595dc1e2f4288bec34f3412359f648e84a5Chris LattnerParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclTy*> &Protocols,
788e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                            bool WarnOnDeclarations, SourceLocation &EndLoc) {
789e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
790e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
791e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  ConsumeToken(); // the "<"
792e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
793e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
794e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
795e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
796e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
797e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
798e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
799e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
800e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
801e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
802e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
803e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
804e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
805e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
806e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
807e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
808e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
809e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
810e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
811e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
812e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
813e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
814e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
815e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
816e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
817e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
818e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
819e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
820e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
821e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
822e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
823e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
824e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
825dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
826dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
827dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
828dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
829dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
830dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
831dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
832dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
833dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
834dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
835dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
836dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
837dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
838dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
839dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
840ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
841dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
842dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
843dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     struct-declaration
844dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
84560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroffvoid Parser::ParseObjCClassInstanceVariables(DeclTy *interfaceDecl,
84660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
847df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
8487d6402f753916a82d93ef0bf0f703ae4243a52beFariborz Jahanian  llvm::SmallVector<DeclTy*, 32> AllIvarDecls;
849e13594279a952537ac903325efff57e3edca79d9Chris Lattner  llvm::SmallVector<FieldDeclarator, 8> FieldDeclarators;
850e13594279a952537ac903325efff57e3edca79d9Chris Lattner
85172de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope);
85272de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
853ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
854ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff
855aa847fe3c88ee5e8d180e48537c58b805d48d95dFariborz Jahanian  tok::ObjCKeywordKind visibility = tok::objc_protected;
856ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
857df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
858ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
859ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff
860ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
861df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
862ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::ext_extra_struct_semi);
863ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
864ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
865ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
866e13594279a952537ac903325efff57e3edca79d9Chris Lattner
867ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
868df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
869ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
870861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
871ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
872ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
873ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
874ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
875861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
876ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
877ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
878ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
879ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
880ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
881ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
882ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
883e13594279a952537ac903325efff57e3edca79d9Chris Lattner
884e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
885e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
886e13594279a952537ac903325efff57e3edca79d9Chris Lattner    FieldDeclarators.clear();
887e13594279a952537ac903325efff57e3edca79d9Chris Lattner    ParseStructDeclaration(DS, FieldDeclarators);
888e13594279a952537ac903325efff57e3edca79d9Chris Lattner
889e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Convert them all to fields.
890e13594279a952537ac903325efff57e3edca79d9Chris Lattner    for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
891e13594279a952537ac903325efff57e3edca79d9Chris Lattner      FieldDeclarator &FD = FieldDeclarators[i];
892e13594279a952537ac903325efff57e3edca79d9Chris Lattner      // Install the declarator into interfaceDecl.
8931d78cc443084134428c4c333fab25148900c3ca4Fariborz Jahanian      DeclTy *Field = Actions.ActOnIvar(CurScope,
894e13594279a952537ac903325efff57e3edca79d9Chris Lattner                                         DS.getSourceRange().getBegin(),
8951d78cc443084134428c4c333fab25148900c3ca4Fariborz Jahanian                                         FD.D, FD.BitfieldSize, visibility);
896e13594279a952537ac903325efff57e3edca79d9Chris Lattner      AllIvarDecls.push_back(Field);
8977d6402f753916a82d93ef0bf0f703ae4243a52beFariborz Jahanian    }
8983536b443bc50d58a79f14fca9b6842541a434854Steve Naroff
899df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
900ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
901ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
902ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
903ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
904ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
905ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
906ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
90760fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
9088749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
9098749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
9108749be53f53384e7846502791ceda6c657228d07Steve Naroff  Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
9118749be53f53384e7846502791ceda6c657228d07Steve Naroff                      &AllIvarDecls[0], AllIvarDecls.size(),
9121bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
913ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
9145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
915dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
916dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
917dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
918dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
919dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
920dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
921dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier
922dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
9233536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///       objc-interface-decl-list
924dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
925dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
926dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
927dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
928dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
929dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
9303536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
931dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
932246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel DunbarParser::DeclTy *Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
933246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                               AttributeList *attrList) {
934861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
9357ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
9367ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
9377ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
938df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
9397ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
9407ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    return 0;
9417ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
9427ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
9437ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
9447ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
9457ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
946df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
9477caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
9487ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
949bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
950bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
9517ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
9527caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
953df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
9547caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
9557caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
9567caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
9577ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
9587ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
9597ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
960df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
9617ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
9627ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
9637ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        return 0;
9647ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
9657caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
9667caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
9677ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
9687ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
969df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
9707ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
9717ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
9727ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
9737ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
9747ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      return 0;
9757caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
976e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
97737e58d104d7756f7c18f525b74e46b8eeec8ef81Steve Naroff                                                   &ProtocolRefs[0],
978bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
979bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
9807caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
9817caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
9827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
983e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  SourceLocation EndProtoLoc;
9847caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
985e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<DeclTy *, 8> ProtocolRefs;
9867caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
987e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      ParseObjCProtocolReferences(ProtocolRefs, true, EndProtoLoc))
9887caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    return 0;
98925e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian
990e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  DeclTy *ProtoType =
991e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
992e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                        &ProtocolRefs[0], ProtocolRefs.size(),
993246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                        EndProtoLoc, attrList);
99425e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
995bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
996dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
997dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
998dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
999dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1000dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1001dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1002dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1003dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1004dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1005dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1006dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1007dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1008dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1009ac00b7f4a933e60e2f0afd83092339160adc140cFariborz JahanianParser::DeclTy *Parser::ParseObjCAtImplementationDeclaration(
1010ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1011ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1012ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1013ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
1014ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1015df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1016ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1017ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    return 0;
1018ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1019ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1020ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1021ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
1022ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1023df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren)) {
1024ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1025ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1026ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1027ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
1028ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1029df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1030ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1031ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1032ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1033ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1034ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      return 0;
1035ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1036df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1037ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1038ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1039ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      return 0;
1040ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1041ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1042e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    DeclTy *ImplCatType = Actions.ActOnStartCategoryImplementation(
10438f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    atLoc, nameId, nameLoc, categoryId,
10448f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1045a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
1046db8f3d3bb96c75440f513c43ac73adace2aae5ffFariborz Jahanian    return 0;
1047ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1048ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1049ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1050ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1051df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1052ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1053ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1054df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1055ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1056ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      return 0;
1057ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1058ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1059ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1060ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1061e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff  DeclTy *ImplClsType = Actions.ActOnStartClassImplementation(
1062cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1063ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
1064ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian
106560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
106660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
1067a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
1068ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1069db8f3d3bb96c75440f513c43ac73adace2aae5ffFariborz Jahanian  return 0;
10705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
107160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1072ac00b7f4a933e60e2f0afd83092339160adc140cFariborz JahanianParser::DeclTy *Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1073ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1074ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1075ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
107694cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian  if (ObjCImpDecl)
1077a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
107894cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian  else
107994cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian    Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
1080a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  return ObjCImpDecl;
10815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1082e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
1083e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1084e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1085e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1086e992af01d14e2e31037562c123af0a71ae1ed374Fariborz JahanianParser::DeclTy *Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1087e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1088e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1089e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1090df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1091e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1092e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    return 0;
1093e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1094243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1095243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1096df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1097e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1098e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    return 0;
1099e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1100243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1101243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1102243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
11031ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1104243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian    return 0;
1105243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1106243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  DeclTy *ClsType = Actions.ActOnCompatiblityAlias(atLoc,
1107243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian                                                   aliasId, aliasLoc,
1108243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian                                                   classId, classLoc);
1109243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  return ClsType;
11105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1112ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1113ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1114ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1115ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1116ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1117ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1118ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1119ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1120ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1121ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1122ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1123ac00b7f4a933e60e2f0afd83092339160adc140cFariborz JahanianParser::DeclTy *Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1124ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1125ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1126f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
1127df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1128ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1129ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    return 0;
1130ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1131df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::identifier)) {
1132f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1133f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1134f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1135df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1136ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1137ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1138df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1139ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1140ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1141ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1142f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1143ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume ivar-name
1144ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1145f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1146f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian                                  propertyId, propertyIvar);
1147df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1148ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1149ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1150ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1151df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
11521ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1153ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  return 0;
1154ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1155ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1156ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1157ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1158ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1159ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1160ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1161ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1162ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1163ac00b7f4a933e60e2f0afd83092339160adc140cFariborz JahanianParser::DeclTy *Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1164ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1165ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1166ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1167df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1168ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1169ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    return 0;
1170ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1171df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::identifier)) {
1172c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1173c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1174c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1175c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian                                  propertyId, 0);
1176c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1177df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1178ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1179ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1180ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1181df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
11821ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
1183ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  return 0;
1184ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1185397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1186397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1187397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1188397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
118943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
119015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Res(Actions);
1191397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1192df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
119339f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
11940e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1195397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
119643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1197397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1198397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
119939f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian  ConsumeToken(); // consume ';'
120043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl  return Owned(Actions.ActOnObjCAtThrowStmt(atLoc, Res.release()));
1201397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1202397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1203c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
120478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1205c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
120643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult
120743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1208fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1209fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
12101ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
121143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1212fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1213fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
12142f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
12150e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
1216fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
121743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1218fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1219fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
12201ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
122143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1222fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1223fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
122478a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
12251ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
122643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
122778a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
12283ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
12293ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
12308935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::DeclScope);
12313ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
123261364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult SynchBody(ParseCompoundStatementBody());
12330e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
12348935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
12350e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (SynchBody.isInvalid())
1236fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
123743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl  return Owned(Actions.ActOnObjCAtSynchronizedStmt(atLoc, Res.release(),
123843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl                                                   SynchBody.release()));
1239c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1240c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1241397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1242397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1243397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1244397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1245397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1246397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1247397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1248397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1249397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1250397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1251397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
125243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1253397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
125443bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1255397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1256df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
12571ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
125843bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1259397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
126015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult CatchStmts(Actions);
126115faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult FinallyStmt(Actions);
12628935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
126361364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult TryBody(ParseCompoundStatementBody());
12648935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
12650e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1266bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1267a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1268df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
12696b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
12706b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
12716b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
12726b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
12736b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
12746b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
12756b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
12760e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1277161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1278cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
127915faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl      OwningStmtResult FirstPart(Actions);
12803b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1281df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1282397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
12838935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor        ParseScope CatchScope(this, Scope::DeclScope);
1284df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1285397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1286397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
128743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl          // For some odd reason, the name of the exception variable is
1288459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          // optional. As a result, we need to use PrototypeContext.
1289459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          Declarator DeclaratorInfo(DS, Declarator::PrototypeContext);
12903b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          ParseDeclarator(DeclaratorInfo);
1291459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          if (DeclaratorInfo.getIdentifier()) {
129243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl            DeclTy *aBlockVarDecl = Actions.ActOnDeclarator(CurScope,
1293914701ed49f31323176a784b49df05a0d177d1adDaniel Dunbar                                                          DeclaratorInfo, 0);
12940e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl            FirstPart =
129543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl              Actions.ActOnDeclStmt(aBlockVarDecl,
1296459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff                                    DS.getSourceRange().getBegin(),
1297459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff                                    DeclaratorInfo.getSourceRange().getEnd());
1298459a1e2212115f4ff89db7f444df5af41792b18aSteve Naroff          }
129964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1300397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
13013b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian        SourceLocation RParenLoc = ConsumeParen();
1302c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner
130315faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl        OwningStmtResult CatchBody(Actions, true);
1304c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1305c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1306c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1307c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
13080e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
13093b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
13100e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
1311effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl          RParenLoc, FirstPart.release(), CatchBody.release(),
1312effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl          CatchStmts.release());
131364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
13141ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
13151ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
131643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1317397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1318397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
13196b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
13206b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
132164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
13228935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
13230e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
132415faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl      OwningStmtResult FinallyBody(Actions, true);
1325c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1326c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1327c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1328c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
13290e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1330161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
13310e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
1332effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl                                                   FinallyBody.release());
1333397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1334397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1335397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1336397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1337bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1338397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
133943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1340bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
134143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl  return Owned(Actions.ActOnObjCAtTryStmt(atLoc, TryBody.release(),
134243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl                                          CatchStmts.release(),
134343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl                                          FinallyStmt.release()));
1344397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1345ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
13463536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1347ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
134871c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve NaroffParser::DeclTy *Parser::ParseObjCMethodDefinition() {
1349a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  DeclTy *MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
1350ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1351df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi))
1352ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1353ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1354409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1355df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1356da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
1357409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1358409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1359409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
1360409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1361409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1362409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
136371c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff      return 0;
1364ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1365409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
1366409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1367409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
13688935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
1369409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff
1370409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1371394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
1372394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  Actions.ObjCActOnStartOfMethodDef(CurScope, MDecl);
137361364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
137461364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult FnBody(ParseCompoundStatementBody());
137561364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
1376409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
13770e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (FnBody.isInvalid())
1378a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1379a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                       MultiStmtArg(Actions), false);
1380798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
1381409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
13828935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
1383798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
1384409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // TODO: Pass argument information.
1385798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl  Actions.ActOnFinishFunctionBody(MDecl, move_convert(FnBody));
138671c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
13875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
13885508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
138943bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
139064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  if (Tok.isObjCAtKeyword(tok::objc_try)) {
13916b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
139264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  } else if (Tok.isObjCAtKeyword(tok::objc_throw))
139364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
139464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
139564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
1396d8c4e15138e69a51754cc259c8a592cc47950c8eSebastian Redl  OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
13970e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
139864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
139964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
140064515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
140164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
140243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
140364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
140464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
140564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
1406a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl  return Actions.ActOnExprStmt(move_convert(Res));
140764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
140864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
14091d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
14105508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
1411b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1412b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
14131d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1414b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
14154fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
14161d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
14172f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
14184fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
14194fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
14201d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
14214fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
14221d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
14234fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
14241d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
14254fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
14261d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
14274fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
14285508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
14295508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
14305508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
14310ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-expr:
14320ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
14330ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14340ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-receiver:
14350ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
14360ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
14370ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
14381d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCMessageExpression() {
1439699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1440699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1441699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
1442699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  // Parse receiver
144314dd98a7952c9559e0d17fff8272bf3be67135afChris Lattner  if (isTokObjCMessageIdentifierReceiver()) {
1444699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
14455cb93b8bf009c4b0ae09b71ba85f54b2a7ea8022Steve Naroff    SourceLocation NameLoc = ConsumeToken();
14461d922960e083906a586609ac6978678147250177Sebastian Redl    return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
14471d922960e083906a586609ac6978678147250177Sebastian Redl                                          ExprArg(Actions));
1448699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
1449699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
14502f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
14510e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
14525c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
14531d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
1454699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
14551d922960e083906a586609ac6978678147250177Sebastian Redl
14560e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
14571d922960e083906a586609ac6978678147250177Sebastian Redl                                        0, move_convert(Res));
1458699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
14591d922960e083906a586609ac6978678147250177Sebastian Redl
1460699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1461699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// the rest of a message expression.
14621d922960e083906a586609ac6978678147250177Sebastian Redl///
14630ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
14640ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
14650ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
14660ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14670ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
14680ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
14690ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
14700ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14710ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg:
14720ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
14730ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14740ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
14750ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
14760ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14770ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
14780ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
14790ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
14801d922960e083906a586609ac6978678147250177Sebastian Redl///
14811d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
1482699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
14835cb93b8bf009c4b0ae09b71ba85f54b2a7ea8022Steve Naroff                                       SourceLocation NameLoc,
1484699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                       IdentifierInfo *ReceiverName,
14851d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
1486a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
14874b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
14884b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  IdentifierInfo *selIdent = ParseObjCSelector(Loc);
148968d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
149068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1491a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
149268d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1493df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1494a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
1495a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
149668d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
149737387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
1498df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
1499a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
15004fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
15014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
15034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
15041d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
1505a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
15061d922960e083906a586609ac6978678147250177Sebastian Redl
150768d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
1508a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      ///  Parse the expression after ':'
15092f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
15100e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
15114fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
15124fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15134fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
15144fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
15151d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
151637387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
15171d922960e083906a586609ac6978678147250177Sebastian Redl
151837387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
1519effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
15201d922960e083906a586609ac6978678147250177Sebastian Redl
152137387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
15224b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian      selIdent = ParseObjCSelector(Loc);
1523df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
1524a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
1525a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
1526a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1527a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
1528df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
152949f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
153049f109c786f99eb7468dac3976db083a65493444Steve Naroff      ///  Parse the expression after ','
15312f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
15320e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
15334fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
15344fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15354fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
15364fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
15371d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
153849f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
15390e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
154049f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
1541effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
1542a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1543a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
1544a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
15451d922960e083906a586609ac6978678147250177Sebastian Redl
15464fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
15474fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15484fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
15494fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
15501d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1551a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
15521d922960e083906a586609ac6978678147250177Sebastian Redl
1553df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
1554a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_rsquare);
15554fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
15564fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15574fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
15584fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
15591d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1560a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
15611d922960e083906a586609ac6978678147250177Sebastian Redl
1562699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
15631d922960e083906a586609ac6978678147250177Sebastian Redl
156429238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
1565ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
1566ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
1567ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
15681d922960e083906a586609ac6978678147250177Sebastian Redl
1569ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // We've just parsed a keyword message.
15701d922960e083906a586609ac6978678147250177Sebastian Redl  if (ReceiverName)
15711d922960e083906a586609ac6978678147250177Sebastian Redl    return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
15721d922960e083906a586609ac6978678147250177Sebastian Redl                                           LBracLoc, NameLoc, RBracLoc,
15731d922960e083906a586609ac6978678147250177Sebastian Redl                                           KeyExprs.take(), KeyExprs.size()));
15741d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
15751d922960e083906a586609ac6978678147250177Sebastian Redl                                            LBracLoc, RBracLoc,
15761d922960e083906a586609ac6978678147250177Sebastian Redl                                            KeyExprs.take(), KeyExprs.size()));
15770ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
15780ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
15791d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
158020df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  OwningExprResult Res(ParseStringLiteralExpression());
15811d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
15821d922960e083906a586609ac6978678147250177Sebastian Redl
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;
1587a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
1588b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
1589effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
15900e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1591b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
1592b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
1593b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
159415faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
159515faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    OwningExprResult Lit(Actions, true);
1596b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    if (isTokenStringLiteral())
1597a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl      Lit = ParseStringLiteralExpression();
1598b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    else
1599b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner      Diag(Tok, diag::err_objc_concat_string);
1600b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
16010e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
16021d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
16030e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1604effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
1605b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
16061d922960e083906a586609ac6978678147250177Sebastian Redl
16071d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
16081d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
16095508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
1610f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1611f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
1612f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
16131d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
16141d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
1615861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
16161d922960e083906a586609ac6978678147250177Sebastian Redl
1617f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
16181d922960e083906a586609ac6978678147250177Sebastian Redl
16194fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
16201d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
16211d922960e083906a586609ac6978678147250177Sebastian Redl
1622f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
16231d922960e083906a586609ac6978678147250177Sebastian Redl
1624f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  TypeTy *Ty = ParseTypeName();
16251d922960e083906a586609ac6978678147250177Sebastian Redl
16264988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
16271d922960e083906a586609ac6978678147250177Sebastian Redl
16281d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc, Ty,
16291d922960e083906a586609ac6978678147250177Sebastian Redl                                                 RParenLoc));
1630f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
163129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
163229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
163329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
16341d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
16351d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
163629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
16371d922960e083906a586609ac6978678147250177Sebastian Redl
16384fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
16391d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
16401d922960e083906a586609ac6978678147250177Sebastian Redl
164129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
16421d922960e083906a586609ac6978678147250177Sebastian Redl
16434fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
16441d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
16451d922960e083906a586609ac6978678147250177Sebastian Redl
1646390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
164729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
16481d922960e083906a586609ac6978678147250177Sebastian Redl
16494988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
165029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
16511d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
16521d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
165329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
1654a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
1655a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
1656a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
16571d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
16581d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
1659a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
16601d922960e083906a586609ac6978678147250177Sebastian Redl
16614fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
16621d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
16631d922960e083906a586609ac6978678147250177Sebastian Redl
1664b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1665a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
1666a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
1667a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  IdentifierInfo *SelIdent = ParseObjCSelector(sLoc);
16681d922960e083906a586609ac6978678147250177Sebastian Redl  if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
16691d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
16701d922960e083906a586609ac6978678147250177Sebastian Redl
1671b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
1672887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
1673887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
1674a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
16754fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      if (Tok.isNot(tok::colon))
16761d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
16771d922960e083906a586609ac6978678147250177Sebastian Redl
1678cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      nColons++;
1679a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
1680a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
1681a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1682a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
1683a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
1684a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SelIdent = ParseObjCSelector(Loc);
1685b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
1686a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
1687a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1688a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
1689887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
1690a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1691887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
16921d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
16931d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
169458065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
1695