ParseObjc.cpp revision d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28
1a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek//===--- ParseObjC.cpp - Objective C Parsing ------------------------------===//
25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//                     The LLVM Compiler Infrastructure
45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
50bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// This file is distributed under the University of Illinois Open Source
60bc735ffcfb223c0186419547abaa5c84482663eChris Lattner// License. See LICENSE.TXT for details.
75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//  This file implements the Objective-C portions of the Parser interface.
115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//
125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===//
135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Parse/Parser.h"
154985aceceb9b9261b876b515d32726175c13a775Steve Naroff#include "clang/Parse/DeclSpec.h"
163b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian#include "clang/Parse/Scope.h"
17500d3297d2a21edeac4d46cbcbe21bc2352c2a28Chris Lattner#include "clang/Parse/ParseDiagnostic.h"
185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "llvm/ADT/SmallVector.h"
195f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerusing namespace clang;
205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
215f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
22891dca671a80c865c6def7259f170ba785e4faaaChris Lattner/// ParseObjCAtDirectives - Handle parts of the external-declaration production:
235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///       external-declaration: [C99 6.9]
245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// [OBJC]  objc-class-definition
2591fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-class-declaration
2691fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-alias-declaration
2791fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-protocol-definition
2891fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  objc-method-definition
2991fa0b7759b575045142abd6ee48c076297ad77bSteve Naroff/// [OBJC]  '@' 'end'
30b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtDirectives() {
315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  SourceLocation AtLoc = ConsumeToken(); // the "@"
321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
33861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  switch (Tok.getObjCKeywordID()) {
345ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_class:
355ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtClassDeclaration(AtLoc);
365ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_interface:
375ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtInterfaceDeclaration(AtLoc);
385ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_protocol:
395ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtProtocolDeclaration(AtLoc);
405ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_implementation:
415ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtImplementationDeclaration(AtLoc);
425ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_end:
435ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtEndDeclaration(AtLoc);
445ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_compatibility_alias:
455ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCAtAliasDeclaration(AtLoc);
465ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_synthesize:
475ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertySynthesize(AtLoc);
485ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  case tok::objc_dynamic:
495ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    return ParseObjCPropertyDynamic(AtLoc);
505ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner  default:
515ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    Diag(AtLoc, diag::err_unexpected_at);
525ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner    SkipUntil(tok::semi);
53b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///
581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// objc-class-declaration:
595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer///    '@' 'class' identifier-list ';'
601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
61b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtClassDeclaration(SourceLocation atLoc) {
625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  ConsumeToken(); // the identifier "class"
635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  llvm::SmallVector<IdentifierInfo *, 8> ClassNames;
641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  while (1) {
66df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      Diag(Tok, diag::err_expected_ident);
685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      SkipUntil(tok::semi);
69b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    }
715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ClassNames.push_back(Tok.getIdentifierInfo());
725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
74df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer      break;
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer    ConsumeToken();
785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  }
791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  // Consume the ';'.
815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer  if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@class"))
82b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
84e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff  return Actions.ActOnForwardClassDeclaration(atLoc,
853536b443bc50d58a79f14fca9b6842541a434854Steve Naroff                                      &ClassNames[0], ClassNames.size());
865f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
875f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
88dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
89dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface:
90dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-interface-attributes[opt] objc-class-interface
91dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-interface
92dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
93dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface:
941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier objc-superclass[opt]
95dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-class-instance-variables[opt]
97dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
98dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
99dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
100dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-interface:
1011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     '@' 'interface' identifier '(' identifier[opt] ')'
102dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-protocol-refs[opt]
103dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-interface-decl-list
104dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
105dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
106dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-superclass:
107dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ':' identifier
108dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
109dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-interface-attributes:
110dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("default")))
111dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((visibility("hidden")))
112dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((deprecated))
113dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((unavailable))
114dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     __attribute__((objc_exception)) - used by NSException on 64-bit
115dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
116b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtInterfaceDeclaration(
117dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation atLoc, AttributeList *attrList) {
118861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_interface) &&
119dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff         "ParseObjCAtInterfaceDeclaration(): Expected @interface");
120dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  ConsumeToken(); // the "interface" identifier
1211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
123dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    Diag(Tok, diag::err_expected_ident); // missing class or category name.
124b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
125dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
126dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // We have a class or category name - consume it.
1277ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *nameId = Tok.getIdentifierInfo();
128dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation nameLoc = ConsumeToken();
1291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
130df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren)) { // we have a category.
131dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation lparenLoc = ConsumeParen();
132dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    SourceLocation categoryLoc, rparenLoc;
133dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    IdentifierInfo *categoryId = 0;
1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
135527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    // For ObjC2, the category name is optional (not an error).
136df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
137dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryId = Tok.getIdentifierInfo();
138dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      categoryLoc = ConsumeToken();
139527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff    } else if (!getLang().ObjC2) {
140527fe23f8cbc6a4da1737a547a2517bc92fa88c8Steve Naroff      Diag(Tok, diag::err_expected_ident); // missing category name.
141b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
142dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
143df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
144dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_rparen);
145dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      SkipUntil(tok::r_paren, false); // don't stop at ';'
146b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
147dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
148dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    rparenLoc = ConsumeParen();
1491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
150dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    // Next, we need to check for any protocol references.
15171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    SourceLocation LAngleLoc, EndProtoLoc;
152b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
15371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
1546bd6d0b9d8944c5e192097bef24f2becb83af172Chris Lattner    if (Tok.is(tok::less) &&
15571b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis        ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
15671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                    LAngleLoc, EndProtoLoc))
157b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
159dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    if (attrList) // categories don't support attributes.
160dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_objc_no_attributes_on_category);
1611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
162beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad    DeclPtrTy CategoryType =
1631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Actions.ActOnStartCategoryInterface(atLoc,
164beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          nameId, nameLoc,
165beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          categoryId, categoryLoc,
166beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          ProtocolRefs.data(),
167beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          ProtocolRefs.size(),
168beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                          EndProtoLoc);
1691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
170fd225cc227143553898f2d3902242d25db9a4902Fariborz Jahanian    ParseObjCInterfaceDeclList(CategoryType, tok::objc_not_keyword);
171bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    return CategoryType;
172dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
173dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Parse a class interface.
174dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  IdentifierInfo *superClassId = 0;
175dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  SourceLocation superClassLoc;
1767ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
177df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) { // a super class is specified.
178dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    ConsumeToken();
179df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
180dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff      Diag(Tok, diag::err_expected_ident); // missing super class name.
181b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
182dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    }
183dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassId = Tok.getIdentifierInfo();
184dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff    superClassLoc = ConsumeToken();
185dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  }
186dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff  // Next, we need to check for any protocol references.
187b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<Action::DeclPtrTy, 8> ProtocolRefs;
18871b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
18971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
19006036d3709955a53297b4cbe14e20db88f321470Chris Lattner  if (Tok.is(tok::less) &&
19171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, true,
19271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
193b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  DeclPtrTy ClsType =
1961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Actions.ActOnStartClassInterface(atLoc, nameId, nameLoc,
19706036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     superClassId, superClassLoc,
198beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                     ProtocolRefs.data(), ProtocolRefs.size(),
19906036d3709955a53297b4cbe14e20db88f321470Chris Lattner                                     EndProtoLoc, attrList);
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
201df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_brace))
20260fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ClsType, atLoc);
203dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
20425e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ClsType, tok::objc_interface);
205bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ClsType;
206dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
207dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
208dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-interface-decl-list:
209dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     empty
210dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list objc-property-decl [OBJC2]
211294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-interface-decl-list objc-method-requirement [OBJC2]
2123536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-interface-decl-list objc-method-proto ';'
213dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list declaration
214dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-interface-decl-list ';'
215dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
216294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-requirement: [OBJC2]
217294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @required
218294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     @optional
219294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
220b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCInterfaceDeclList(DeclPtrTy interfaceDecl,
221cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                        tok::ObjCKeywordKind contextKey) {
222b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> allMethods;
223b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 16> allProperties;
224682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner  llvm::SmallVector<DeclGroupPtrTy, 8> allTUVariables;
22500933591a2795d09dd1acff12a2d21bce7cb12c5Fariborz Jahanian  tok::ObjCKeywordKind MethodImplKind = tok::objc_not_keyword;
2261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
227bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  SourceLocation AtEndLoc;
228bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
229294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  while (1) {
230e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If this is a method prototype, parse it.
231df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::minus) || Tok.is(tok::plus)) {
2321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      DeclPtrTy methodPrototype =
233df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        ParseObjCMethodPrototype(interfaceDecl, MethodImplKind);
23425e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian      allMethods.push_back(methodPrototype);
2353536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // Consume the ';' here, since ParseObjCMethodPrototype() is re-used for
2363536b443bc50d58a79f14fca9b6842541a434854Steve Naroff      // method definitions.
237b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_after_method_proto,
238b6d74a158a9c002e3c0fcbda8ad8d0ccbb2e5088Chris Lattner                       "", tok::semi);
239294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      continue;
240294494e1cce92043562b4680c613df7fd028c02eSteve Naroff    }
2411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
242e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Ignore excess semicolons.
243e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::semi)) {
244294494e1cce92043562b4680c613df7fd028c02eSteve Naroff      ConsumeToken();
245e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
246e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
2471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
248bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // If we got to the end of the file, exit the loop.
249e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.is(tok::eof))
250e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian      break;
2511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
252e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // If we don't have an @ directive, parse it as a function definition.
253e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    if (Tok.isNot(tok::at)) {
2541fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // The code below does not consume '}'s because it is afraid of eating the
2551fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // end of a namespace.  Because of the way this code is structured, an
2561fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      // erroneous r_brace would cause an infinite loop if not handled here.
2571fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner      if (Tok.is(tok::r_brace))
2581fd80116b49782c367ff5d5f50a8b76dd8a5d7f7Chris Lattner        break;
2591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2604985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // FIXME: as the name implies, this rule allows function definitions.
2614985aceceb9b9261b876b515d32726175c13a775Steve Naroff      // We could pass a flag or check for functions during semantic analysis.
262682bf92db408a6cbc3d37b5496a99b6ef85041ecChris Lattner      allTUVariables.push_back(ParseDeclarationOrFunctionDefinition());
263e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      continue;
264e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    }
2651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
266e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    // Otherwise, we have an @ directive, eat the @.
267e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner    SourceLocation AtLoc = ConsumeToken(); // the "@"
268a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    tok::ObjCKeywordKind DirectiveKind = Tok.getObjCKeywordID();
2691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
270a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    if (DirectiveKind == tok::objc_end) { // @end -> terminate list
271e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      AtEndLoc = AtLoc;
272e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      break;
273bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    }
2741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
275bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    // Eat the identifier.
276bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken();
277bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
278a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    switch (DirectiveKind) {
279a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    default:
280bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: If someone forgets an @end on a protocol, this loop will
281bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // continue to eat up tons of stuff and spew lots of nonsense errors.  It
282bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // would probably be better to bail out if we saw an @class or @interface
283bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // or something like that.
284f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      Diag(AtLoc, diag::err_objc_illegal_interface_qual);
285bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // Skip until we see an '@' or '}' or ';'.
286a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      SkipUntil(tok::r_brace, tok::at);
287a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
289a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_required:
290a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_optional:
291a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      // This is only valid on protocols.
292bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner      // FIXME: Should this check for ObjC2 being enabled?
293e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      if (contextKey != tok::objc_protocol)
294bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        Diag(AtLoc, diag::err_objc_directive_only_in_protocol);
295a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      else
296bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner        MethodImplKind = DirectiveKind;
297a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
2981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
299a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner    case tok::objc_property:
300f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      if (!getLang().ObjC2)
301f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner        Diag(AtLoc, diag::err_objc_propertoes_require_objc2);
302f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner
303e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      ObjCDeclSpec OCDS;
3041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // Parse property attribute list, if any.
3058ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.is(tok::l_paren))
306e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner        ParseObjCPropertyAttribute(OCDS);
3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
308bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      struct ObjCPropertyCallback : FieldCallback {
309bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        Parser &P;
310bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy IDecl;
311bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        llvm::SmallVectorImpl<DeclPtrTy> &Props;
312bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        ObjCDeclSpec &OCDS;
313bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        SourceLocation AtLoc;
314bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        tok::ObjCKeywordKind MethodImplKind;
315bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
316bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        ObjCPropertyCallback(Parser &P, DeclPtrTy IDecl,
317bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                             llvm::SmallVectorImpl<DeclPtrTy> &Props,
318bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                             ObjCDeclSpec &OCDS, SourceLocation AtLoc,
319bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                             tok::ObjCKeywordKind MethodImplKind) :
320bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          P(P), IDecl(IDecl), Props(Props), OCDS(OCDS), AtLoc(AtLoc),
321bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          MethodImplKind(MethodImplKind) {
322bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        }
323bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
324bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy invoke(FieldDeclarator &FD) {
325bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          if (FD.D.getIdentifier() == 0) {
326bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            P.Diag(AtLoc, diag::err_objc_property_requires_field_name)
327bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall              << FD.D.getSourceRange();
328bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            return DeclPtrTy();
329bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          }
330bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          if (FD.BitfieldSize) {
331bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            P.Diag(AtLoc, diag::err_objc_property_bitfield)
332bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall              << FD.D.getSourceRange();
333bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            return DeclPtrTy();
334bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          }
335bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
336bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          // Install the property declarator into interfaceDecl.
337bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          IdentifierInfo *SelName =
338bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            OCDS.getGetterName() ? OCDS.getGetterName() : FD.D.getIdentifier();
339bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
340bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          Selector GetterSel =
341bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            P.PP.getSelectorTable().getNullarySelector(SelName);
342bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          IdentifierInfo *SetterName = OCDS.getSetterName();
343bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          Selector SetterSel;
344bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          if (SetterName)
345bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            SetterSel = P.PP.getSelectorTable().getSelector(1, &SetterName);
346bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          else
347bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            SetterSel = SelectorTable::constructSetterName(P.PP.getIdentifierTable(),
348bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                                           P.PP.getSelectorTable(),
349bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                                           FD.D.getIdentifier());
350bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          bool isOverridingProperty = false;
351bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          DeclPtrTy Property =
352bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            P.Actions.ActOnProperty(P.CurScope, AtLoc, FD, OCDS,
353bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                    GetterSel, SetterSel, IDecl,
354bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                    &isOverridingProperty,
355bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                    MethodImplKind);
356bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          if (!isOverridingProperty)
357bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall            Props.push_back(Property);
358bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
359bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          return Property;
360bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        }
361bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      } Callback(*this, interfaceDecl, allProperties,
362bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                 OCDS, AtLoc, MethodImplKind);
363bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
364e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      // Parse all the comma separated declarators.
365e82a10fbba9e33b253119c7c1e0a9801caef486dChris Lattner      DeclSpec DS;
366bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ParseStructDeclaration(DS, Callback);
3671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
368a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner      ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list, "",
369a1fed7e3beebf9bb1bc85123f283be3eb631c120Chris Lattner                       tok::at);
370a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner      break;
371f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff    }
372294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
373bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner
374bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // We break out of the big loop in two cases: when we see @end or when we see
375bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // EOF.  In the former case, eat the @end.  In the later case, emit an error.
376bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  if (Tok.isObjCAtKeyword(tok::objc_end))
377bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    ConsumeToken(); // the "end" identifier
378bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  else
379bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner    Diag(Tok, diag::err_objc_missing_end);
3801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
381a2449b2bf739545494241e189b59587d5ca5c2c1Chris Lattner  // Insert collected methods declarations into the @interface object.
382bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  // This passes in an invalid SourceLocation for AtEndLoc when EOF is hit.
3838a779314870760848e61da2c428a78971fe3f1c3Ted Kremenek  Actions.ActOnAtEnd(AtEndLoc, interfaceDecl,
3841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                     allMethods.data(), allMethods.size(),
385beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allProperties.data(), allProperties.size(),
386beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                     allTUVariables.data(), allTUVariables.size());
387294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
388294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
389d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   Parse property attribute declarations.
390d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
391d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attr-decl: '(' property-attrlist ')'
392d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attrlist:
393d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attribute
394d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     property-attrlist ',' property-attribute
395d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///   property-attribute:
396d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     getter '=' identifier
397d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     setter '=' identifier ':'
398d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readonly
399d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     readwrite
400d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     assign
401d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     retain
402d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     copy
403d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///     nonatomic
404d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian///
4057caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnervoid Parser::ParseObjCPropertyAttribute(ObjCDeclSpec &DS) {
4068ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner  assert(Tok.getKind() == tok::l_paren);
407dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner  SourceLocation LHSLoc = ConsumeParen(); // consume '('
4081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
409cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner  while (1) {
410ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    if (Tok.is(tok::code_completion)) {
411ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff      Actions.CodeCompleteObjCProperty(CurScope, DS);
412ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff      ConsumeToken();
413ece8e71d12b6f4cb2dc501297afef126dab8ad74Steve Naroff    }
414d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian    const IdentifierInfo *II = Tok.getIdentifierInfo();
4151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
416f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    // If this is not an identifier at all, bail out early.
417f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    if (II == 0) {
418f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      MatchRHSPunctuation(tok::r_paren, LHSLoc);
419f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner      return;
420f6ed85533583dae18a44ddc4be6cfc4d68749e36Chris Lattner    }
4211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
422156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    SourceLocation AttrName = ConsumeToken(); // consume last attribute name
4231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
42492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    if (II->isStr("readonly"))
425e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readonly);
42692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("assign"))
427e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_assign);
42892e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("readwrite"))
429e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_readwrite);
43092e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("retain"))
431e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_retain);
43292e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("copy"))
433e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_copy);
43492e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("nonatomic"))
435e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_nonatomic);
43692e62b02226410bcad8584541b8f1ff4d35ebab9Chris Lattner    else if (II->isStr("getter") || II->isStr("setter")) {
437e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner      // getter/setter require extra treatment.
438156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      if (ExpectAndConsume(tok::equal, diag::err_objc_expected_equal, "",
439156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                           tok::r_paren))
440dd5b5f2bb73d037745940431b71eb98393d12d4fChris Lattner        return;
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4428ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      if (Tok.isNot(tok::identifier)) {
4431ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(Tok, diag::err_expected_ident);
4448ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        SkipUntil(tok::r_paren);
4458ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        return;
4468ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
448e013d685c6689ac7ae103ee88acf573422d1ed6aDaniel Dunbar      if (II->getNameStart()[0] == 's') {
4498ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_setter);
4508ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setSetterName(Tok.getIdentifierInfo());
451156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
4521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
453156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        if (ExpectAndConsume(tok::colon, diag::err_expected_colon, "",
454156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner                             tok::r_paren))
4558ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner          return;
4568ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      } else {
4578ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setPropertyAttributes(ObjCDeclSpec::DQ_PR_getter);
4588ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner        DS.setGetterName(Tok.getIdentifierInfo());
459156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner        ConsumeToken();  // consume method name
4608ca329c00e72f301cbaaa42229b20a2f5bc793e5Chris Lattner      }
461e00da7c2f47d4a3d9615c1056a8a65e459113de3Chris Lattner    } else {
462a9500f05ba6c09bbd84d342236863833067cd816Chris Lattner      Diag(AttrName, diag::err_objc_expected_property_attr) << II;
463cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      SkipUntil(tok::r_paren);
464cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner      return;
465cd9f4b31c4fe5b77b5519cc17b4583fab912bad1Chris Lattner    }
4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
467156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    if (Tok.isNot(tok::comma))
468156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner      break;
4691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
470156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner    ConsumeToken();
471d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian  }
4721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
473156b061e4918a5e7ecd8eb317975de0e6be2688bChris Lattner  MatchRHSPunctuation(tok::r_paren, LHSLoc);
474d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian}
475d0f97d1716a138a8d9e0df8e5af77334663723d8Fariborz Jahanian
4763536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-proto:
4771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-instance-method objc-method-decl objc-method-attributes[opt]
4783536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///     objc-class-method objc-method-decl objc-method-attributes[opt]
479294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
480294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-instance-method: '-'
481294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-class-method: '+'
482294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
4834985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-method-attributes:         [OBJC2]
4844985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     __attribute__((deprecated))
4854985aceceb9b9261b876b515d32726175c13a775Steve Naroff///
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpParser::DeclPtrTy Parser::ParseObjCMethodPrototype(DeclPtrTy IDecl,
487b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
488df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert((Tok.is(tok::minus) || Tok.is(tok::plus)) && "expected +/-");
489294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
4901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  tok::TokenKind methodType = Tok.getKind();
491bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation mLoc = ConsumeToken();
4921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
493b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodDecl(mLoc, methodType, IDecl,MethodImplKind);
4943536b443bc50d58a79f14fca9b6842541a434854Steve Naroff  // Since this rule is used for both method declarations and definitions,
4952bd42fadafddc8acf744b57a970bdc96a077c617Steve Naroff  // the caller is (optionally) responsible for consuming the ';'.
496f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return MDecl;
497294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
498294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
499294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-selector:
500294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     identifier
501294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     one of
502294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       enum struct union if else while do for switch case default
503294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       break continue return goto asm sizeof typeof __alignof
504294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       unsigned long const short volatile signed restrict _Complex
505294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///       in out inout bycopy byref oneway int char float double void _Bool
506294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
5072fc5c2428ecb450a3256c8316b93b8655cb76a0fChris LattnerIdentifierInfo *Parser::ParseObjCSelectorPiece(SourceLocation &SelectorLoc) {
508ff38491c18b060526d754765b952f4a497a89416Chris Lattner  switch (Tok.getKind()) {
509ff38491c18b060526d754765b952f4a497a89416Chris Lattner  default:
510ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return 0;
511ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::identifier:
512ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_asm:
513ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw_auto:
5149298d9655aed28b2d9f6cc65c81401b209f03fdcChris Lattner  case tok::kw_bool:
515ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_break:
516ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_case:
517ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_catch:
518ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_char:
519ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_class:
520ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const:
521ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_const_cast:
522ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_continue:
523ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_default:
524ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_delete:
525ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_do:
526ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_double:
527ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_dynamic_cast:
528ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_else:
529ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_enum:
530ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_explicit:
531ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_export:
532ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_extern:
533ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_false:
534ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_float:
535ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_for:
536ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_friend:
537ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_goto:
538ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_if:
539ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_inline:
540ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_int:
541ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_long:
542ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_mutable:
543ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_namespace:
544ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_new:
545ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_operator:
546ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_private:
547ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_protected:
548ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_public:
549ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_register:
550ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_reinterpret_cast:
551ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_restrict:
552ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_return:
553ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_short:
554ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_signed:
555ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_sizeof:
556ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static:
557ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_static_cast:
558ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_struct:
559ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_switch:
560ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_template:
561ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_this:
562ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_throw:
563ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_true:
564ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_try:
565ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typedef:
566ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeid:
567ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typename:
568ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_typeof:
569ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_union:
570ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_unsigned:
571ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_using:
572ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_virtual:
573ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_void:
574ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_volatile:
575ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_wchar_t:
576ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw_while:
577ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Bool:
578ff38491c18b060526d754765b952f4a497a89416Chris Lattner  case tok::kw__Complex:
579ef048ef393960728bdc82cd5c45035bde7013b6aAnders Carlsson  case tok::kw___alignof:
580ff38491c18b060526d754765b952f4a497a89416Chris Lattner    IdentifierInfo *II = Tok.getIdentifierInfo();
5814b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SelectorLoc = ConsumeToken();
582ff38491c18b060526d754765b952f4a497a89416Chris Lattner    return II;
583d064951b0dcc95f8604d0d69ae82d9ecbd38c796Fariborz Jahanian  }
584294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
585294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
5860196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///  objc-for-collection-in: 'in'
5870196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian///
588335a2d4122e41343fe11a775889b8bec5b14be60Fariborz Jahanianbool Parser::isTokIdentifier_in() const {
5893ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // FIXME: May have to do additional look-ahead to only allow for
5903ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // valid tokens following an 'in'; such as an identifier, unary operators,
5913ba5a0f90a03d5e13d02cbee9abd2a1ba01b18bcFariborz Jahanian  // '[' etc.
5921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return (getLang().ObjC2 && Tok.is(tok::identifier) &&
5935ffb14b7e88e587cd2f78dcc3a966a64108920f0Chris Lattner          Tok.getIdentifierInfo() == ObjCTypeQuals[objc_in]);
5940196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian}
5950196cab54007ff072ec2642da8911c6b7e8d3fb5Fariborz Jahanian
596a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek/// ParseObjCTypeQualifierList - This routine parses the objective-c's type
597e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// qualifier list and builds their bitmask representation in the input
598e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner/// argument.
599294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
600294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-type-qualifiers:
601294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifier
602294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-qualifiers objc-type-qualifier
603294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
604a526c5c67e5a0473c340903ee542ce570119665fTed Kremenekvoid Parser::ParseObjCTypeQualifierList(ObjCDeclSpec &DS) {
605e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  while (1) {
606cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isNot(tok::identifier))
607e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      return;
6081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
609e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    const IdentifierInfo *II = Tok.getIdentifierInfo();
610e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    for (unsigned i = 0; i != objc_NumQuals; ++i) {
611a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      if (II != ObjCTypeQuals[i])
612e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner        continue;
6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
614a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      ObjCDeclSpec::ObjCDeclQualifier Qual;
615e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      switch (i) {
616e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      default: assert(0 && "Unknown decl qualifier");
617a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_in:     Qual = ObjCDeclSpec::DQ_In; break;
618a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_out:    Qual = ObjCDeclSpec::DQ_Out; break;
619a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_inout:  Qual = ObjCDeclSpec::DQ_Inout; break;
620a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_oneway: Qual = ObjCDeclSpec::DQ_Oneway; break;
621a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_bycopy: Qual = ObjCDeclSpec::DQ_Bycopy; break;
622a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      case objc_byref:  Qual = ObjCDeclSpec::DQ_Byref; break;
623e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      }
624a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek      DS.setObjCDeclQualifier(Qual);
625e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      ConsumeToken();
626e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      II = 0;
627e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner      break;
628e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    }
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
630e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    // If this wasn't a recognized qualifier, bail out.
631e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner    if (II) return;
632e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner  }
633e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner}
634e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner
635e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///   objc-type-name:
636e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] type-name ')'
637e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///     '(' objc-type-qualifiers[opt] ')'
638e8b724d481c9547de2ee6f442be594b38ada452dChris Lattner///
639a526c5c67e5a0473c340903ee542ce570119665fTed KremenekParser::TypeTy *Parser::ParseObjCTypeName(ObjCDeclSpec &DS) {
640df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_paren) && "expected (");
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6424a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  SourceLocation LParenLoc = ConsumeParen();
643e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  SourceLocation TypeStartLoc = Tok.getLocation();
6441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
64519d74e1494fe399f0e2a94e9419c095f8214851bFariborz Jahanian  // Parse type qualifiers, in, inout, etc.
646a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ParseObjCTypeQualifierList(DS);
6474fa7afd07421e7276d1717e4fdf43a5fdd65a622Steve Naroff
6484a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  TypeTy *Ty = 0;
649809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (isTypeSpecifierQualifier()) {
650809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    TypeResult TypeSpec = ParseTypeName();
651809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    if (!TypeSpec.isInvalid())
652809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor      Ty = TypeSpec.get();
653809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  }
6541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6554a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  if (Tok.is(tok::r_paren))
6564a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    ConsumeParen();
6574a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  else if (Tok.getLocation() == TypeStartLoc) {
658e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // If we didn't eat any tokens, then this isn't a type.
6591ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_type);
6604a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    SkipUntil(tok::r_paren);
6614a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner  } else {
6624a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // Otherwise, we found *something*, but didn't get a ')' in the right
6634a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    // place.  Emit an error then return what we have as the type.
6644a76b292c9c3f60a257636e21d76b6ce1c12f8c4Chris Lattner    MatchRHSPunctuation(tok::r_paren, LParenLoc);
665294494e1cce92043562b4680c613df7fd028c02eSteve Naroff  }
666f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff  return Ty;
667294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
668294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
669294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-method-decl:
670294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-selector
6714985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-keyword-selector objc-parmlist[opt]
672294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-type-name objc-selector
6734985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-type-name objc-keyword-selector objc-parmlist[opt]
674294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
675294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-selector:
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     objc-keyword-decl
677294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     objc-keyword-selector objc-keyword-decl
678294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
679294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///   objc-keyword-decl:
6807ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-type-name objc-keyword-attributes[opt] identifier
6817ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     objc-selector ':' objc-keyword-attributes[opt] identifier
6827ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-type-name objc-keyword-attributes[opt] identifier
6837ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     ':' objc-keyword-attributes[opt] identifier
684294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6854985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parmlist:
6864985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms objc-ellipsis[opt]
687294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6884985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-parms:
6894985aceceb9b9261b876b515d32726175c13a775Steve Naroff///     objc-parms , parameter-declaration
690294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6914985aceceb9b9261b876b515d32726175c13a775Steve Naroff///   objc-ellipsis:
692294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///     , ...
693294494e1cce92043562b4680c613df7fd028c02eSteve Naroff///
6947ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///   objc-keyword-attributes:         [OBJC2]
6957ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///     __attribute__((unused))
6967ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff///
697b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDecl(SourceLocation mLoc,
698b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              tok::TokenKind mType,
699b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                              DeclPtrTy IDecl,
700b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                          tok::ObjCKeywordKind MethodImplKind) {
70154abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  ParsingDeclRAIIObject PD(*this);
70254abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall
703e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  // Parse the return type if present.
704ff38491c18b060526d754765b952f4a497a89416Chris Lattner  TypeTy *ReturnType = 0;
705a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCDeclSpec DSRet;
706df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::l_paren))
707f1de0ca05e2df6f23bd559028693a12d1ebdaaf6Fariborz Jahanian    ReturnType = ParseObjCTypeName(DSRet);
7081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
709bef1185418705e16012b3dd50cd7c260c8d6b79cSteve Naroff  SourceLocation selLoc;
7102fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(selLoc);
711e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner
71284c431088693e216193094d1dbf327a01173f57fSteve Naroff  // An unnamed colon is valid.
71384c431088693e216193094d1dbf327a01173f57fSteve Naroff  if (!SelIdent && Tok.isNot(tok::colon)) { // missing selector name.
7141ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_selector_for_method)
7151ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner      << SourceRange(mLoc, Tok.getLocation());
716e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    // Skip until we get a ; or {}.
717e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner    SkipUntil(tok::r_brace);
718b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
719e8904e992ca5e821b199c4577e8b5e5b17a33b1dChris Lattner  }
7201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
721439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian  llvm::SmallVector<Declarator, 8> CargNames;
722df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::colon)) {
723ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist after the method, parse them.
724ff38491c18b060526d754765b952f4a497a89416Chris Lattner    AttributeList *MethodAttrs = 0;
7251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
726ff38491c18b060526d754765b952f4a497a89416Chris Lattner      MethodAttrs = ParseAttributes();
7271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
728ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Selector Sel = PP.getSelectorTable().getNullarySelector(SelIdent);
72954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    DeclPtrTy Result
73054abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall         = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
7311f7b6f88f18d7f6b10265acec5d41c4ed1897487Fariborz Jahanian                                          mType, IDecl, DSRet, ReturnType, Sel,
7321c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                          0, CargNames, MethodAttrs,
7331c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                          MethodImplKind);
73454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    PD.complete(Result);
73554abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall    return Result;
736ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
737f28b264437053fb0deacc9ba02b18a0966f7290aSteve Naroff
73868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
739e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner  llvm::SmallVector<Action::ObjCArgInfo, 12> ArgInfos;
7401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
741ff38491c18b060526d754765b952f4a497a89416Chris Lattner  while (1) {
742e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    Action::ObjCArgInfo ArgInfo;
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
744ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Each iteration parses a single keyword argument.
745df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::colon)) {
746ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_colon);
747ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
748ff38491c18b060526d754765b952f4a497a89416Chris Lattner    }
749ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the ':'.
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
751e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Type = 0;
752e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    if (Tok.is(tok::l_paren)) // Parse the argument type if present.
753e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner      ArgInfo.Type = ParseObjCTypeName(ArgInfo.DeclSpec);
754e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
755ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // If attributes exist before the argument name, parse them.
756e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.ArgAttrs = 0;
757df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
758e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner      ArgInfo.ArgAttrs = ParseAttributes();
7597ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff
760df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
761ff38491c18b060526d754765b952f4a497a89416Chris Lattner      Diag(Tok, diag::err_expected_ident); // missing argument name.
762ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
7634985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
7641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
765e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.Name = Tok.getIdentifierInfo();
766e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfo.NameLoc = Tok.getLocation();
767ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken(); // Eat the identifier.
7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
769e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    ArgInfos.push_back(ArgInfo);
770e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner    KeyIdents.push_back(SelIdent);
771e294d3fbaffcbc0cf5f16067ab31d2b2763d25e9Chris Lattner
772ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // Check for another keyword selector.
7734b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian    SourceLocation Loc;
7742fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner    SelIdent = ParseObjCSelectorPiece(Loc);
775df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (!SelIdent && Tok.isNot(tok::colon))
776ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
777ff38491c18b060526d754765b952f4a497a89416Chris Lattner    // We have a selector or a colon, continue parsing.
778ff38491c18b060526d754765b952f4a497a89416Chris Lattner  }
7791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
780335eafa5be51f6440672a74c73d588af72e96732Steve Naroff  bool isVariadic = false;
7811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
782ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // Parse the (optional) parameter list.
783df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::comma)) {
784ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ConsumeToken();
785df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::ellipsis)) {
786335eafa5be51f6440672a74c73d588af72e96732Steve Naroff      isVariadic = true;
7874985aceceb9b9261b876b515d32726175c13a775Steve Naroff      ConsumeToken();
788ff38491c18b060526d754765b952f4a497a89416Chris Lattner      break;
7894985aceceb9b9261b876b515d32726175c13a775Steve Naroff    }
790ff38491c18b060526d754765b952f4a497a89416Chris Lattner    DeclSpec DS;
791ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarationSpecifiers(DS);
7921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Parse the declarator.
793ff38491c18b060526d754765b952f4a497a89416Chris Lattner    Declarator ParmDecl(DS, Declarator::PrototypeContext);
794ff38491c18b060526d754765b952f4a497a89416Chris Lattner    ParseDeclarator(ParmDecl);
795439c65892cc8629bbf541e0b8bda6b64cbcc4e6bFariborz Jahanian    CargNames.push_back(ParmDecl);
7964985aceceb9b9261b876b515d32726175c13a775Steve Naroff  }
7971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
798ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // FIXME: Add support for optional parmameter list...
799e3a2ca7e30601cdd31c77a830f4cc487851e8096Fariborz Jahanian  // If attributes exist after the method, parse them.
800ff38491c18b060526d754765b952f4a497a89416Chris Lattner  AttributeList *MethodAttrs = 0;
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (getLang().ObjC2 && Tok.is(tok::kw___attribute))
802ff38491c18b060526d754765b952f4a497a89416Chris Lattner    MethodAttrs = ParseAttributes();
8031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8043688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian  if (KeyIdents.size() == 0)
8053688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian    return DeclPtrTy();
806ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(KeyIdents.size(),
807ff38491c18b060526d754765b952f4a497a89416Chris Lattner                                                   &KeyIdents[0]);
80854abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  DeclPtrTy Result
80954abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall       = Actions.ActOnMethodDeclaration(mLoc, Tok.getLocation(),
8103688fc679389d67b6755e62406998f9ea84d886aFariborz Jahanian                                        mType, IDecl, DSRet, ReturnType, Sel,
8111c6a3cc88177c67498fccdf14cfdf09959214e41Ted Kremenek                                        &ArgInfos[0], CargNames, MethodAttrs,
812335eafa5be51f6440672a74c73d588af72e96732Steve Naroff                                        MethodImplKind, isVariadic);
81354abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  PD.complete(Result);
81454abf7d4fa3123b8324c09d2a4dfb789fd818403John McCall  return Result;
815294494e1cce92043562b4680c613df7fd028c02eSteve Naroff}
816294494e1cce92043562b4680c613df7fd028c02eSteve Naroff
817dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-refs:
818dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '<' identifier-list '>'
819dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
8207caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattnerbool Parser::
821b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParseObjCProtocolReferences(llvm::SmallVectorImpl<Action::DeclPtrTy> &Protocols,
82271b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            llvm::SmallVectorImpl<SourceLocation> &ProtocolLocs,
82371b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            bool WarnOnDeclarations,
82471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                            SourceLocation &LAngleLoc, SourceLocation &EndLoc) {
825e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  assert(Tok.is(tok::less) && "expected <");
8261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
82771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  LAngleLoc = ConsumeToken(); // the "<"
8281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
829e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  llvm::SmallVector<IdentifierLocPair, 8> ProtocolIdents;
8301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
831e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  while (1) {
832e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::identifier)) {
833e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      Diag(Tok, diag::err_expected_ident);
834e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      SkipUntil(tok::greater);
835e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      return true;
836e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    }
837e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ProtocolIdents.push_back(std::make_pair(Tok.getIdentifierInfo(),
838e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                       Tok.getLocation()));
83971b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis    ProtocolLocs.push_back(Tok.getLocation());
840e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
8411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
842e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    if (Tok.isNot(tok::comma))
843e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner      break;
844e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    ConsumeToken();
845e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
8461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
847e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Consume the '>'.
848e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  if (Tok.isNot(tok::greater)) {
849e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Diag(Tok, diag::err_expected_greater);
850e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    return true;
851e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  }
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
853e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  EndLoc = ConsumeAnyToken();
8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
855e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  // Convert the list of protocols identifiers into a list of protocol decls.
856e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  Actions.FindProtocolDeclaration(WarnOnDeclarations,
857e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  &ProtocolIdents[0], ProtocolIdents.size(),
858e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner                                  Protocols);
859e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner  return false;
860e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner}
861e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner
862dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-instance-variables:
863dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     '{' objc-instance-variable-decl-list[opt] '}'
864dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
865dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl-list:
866dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-visibility-spec
867dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl ';'
868dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     ';'
869dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-visibility-spec
870dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list objc-instance-variable-decl ';'
871dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-instance-variable-decl-list ';'
872dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
873dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-visibility-spec:
874dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @private
875dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protected
876dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @public
877ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff///     @package [OBJC2]
878dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
879dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-instance-variable-decl:
8801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     struct-declaration
881dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
882b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattnervoid Parser::ParseObjCClassInstanceVariables(DeclPtrTy interfaceDecl,
88360fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff                                             SourceLocation atLoc) {
884df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  assert(Tok.is(tok::l_brace) && "expected {");
885b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 32> AllIvarDecls;
886e13594279a952537ac903325efff57e3edca79d9Chris Lattner
8871a0d31a3d7f14ddc6370ba912c778aece6c12cf0Douglas Gregor  ParseScope ClassScope(this, Scope::DeclScope|Scope::ClassScope);
88872de6676bd30f9081ee4166bbe07b4c270258ce6Douglas Gregor
889ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  SourceLocation LBraceLoc = ConsumeBrace(); // the "{"
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
891aa847fe3c88ee5e8d180e48537c58b805d48d95dFariborz Jahanian  tok::ObjCKeywordKind visibility = tok::objc_protected;
892ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  // While we still have something to read, read the instance variables.
893df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
894ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Each iteration of this loop reads one objc-instance-variable-decl.
8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
896ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Check for extraneous top-level semicolon.
897df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
898c2253f5ca170984fcd4f30f8823148e8cb71336bChris Lattner      Diag(Tok, diag::ext_extra_struct_semi)
899c2253f5ca170984fcd4f30f8823148e8cb71336bChris Lattner        << CodeModificationHint::CreateRemoval(SourceRange(Tok.getLocation()));
900ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
901ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      continue;
902ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
9031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
904ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    // Set the default visibility to private.
905df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::at)) { // parse objc-visibility-spec
906ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken(); // eat the @ sign
907861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff      switch (Tok.getObjCKeywordID()) {
908ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_private:
909ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_public:
910ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_protected:
911ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      case tok::objc_package:
912861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff        visibility = Tok.getObjCKeywordID();
913ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        ConsumeToken();
9141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        continue;
915ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      default:
916ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        Diag(Tok, diag::err_objc_illegal_visibility_spec);
917ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff        continue;
918ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      }
919ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
9201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
921bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    struct ObjCIvarCallback : FieldCallback {
922bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      Parser &P;
923bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy IDecl;
924bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      tok::ObjCKeywordKind visibility;
925bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls;
926bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
927bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      ObjCIvarCallback(Parser &P, DeclPtrTy IDecl, tok::ObjCKeywordKind V,
928bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                       llvm::SmallVectorImpl<DeclPtrTy> &AllIvarDecls) :
929bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        P(P), IDecl(IDecl), visibility(V), AllIvarDecls(AllIvarDecls) {
930bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
931bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
932bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      DeclPtrTy invoke(FieldDeclarator &FD) {
933bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        // Install the declarator into the interface decl.
934bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        DeclPtrTy Field
935bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall          = P.Actions.ActOnIvar(P.CurScope,
936bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                FD.D.getDeclSpec().getSourceRange().getBegin(),
937bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall                                IDecl, FD.D, FD.BitfieldSize, visibility);
938bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        AllIvarDecls.push_back(Field);
939bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall        return Field;
940bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall      }
941bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    } Callback(*this, interfaceDecl, visibility, AllIvarDecls);
942bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall
943e13594279a952537ac903325efff57e3edca79d9Chris Lattner    // Parse all the comma separated declarators.
944e13594279a952537ac903325efff57e3edca79d9Chris Lattner    DeclSpec DS;
945bdd563ec391b0a83fc6d04b8a8ea3022aa702f74John McCall    ParseStructDeclaration(DS, Callback);
9461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
947df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::semi)) {
948ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      ConsumeToken();
949ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    } else {
950ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      Diag(Tok, diag::err_expected_semi_decl_list);
951ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      // Skip to end of block or statement
952ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff      SkipUntil(tok::r_brace, true, true);
953ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff    }
954ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  }
95560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  SourceLocation RBraceLoc = MatchRHSPunctuation(tok::r_brace, LBraceLoc);
9568749be53f53384e7846502791ceda6c657228d07Steve Naroff  // Call ActOnFields() even if we don't have any decls. This is useful
9578749be53f53384e7846502791ceda6c657228d07Steve Naroff  // for code rewriting tools that need to be aware of the empty list.
9588749be53f53384e7846502791ceda6c657228d07Steve Naroff  Actions.ActOnFields(CurScope, atLoc, interfaceDecl,
959beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                      AllIvarDecls.data(), AllIvarDecls.size(),
9601bfe1c2129771c06fb58ae5e8c079ae30e138309Daniel Dunbar                      LBraceLoc, RBraceLoc, 0);
961ddbff78fb719a645b04bd27099fa6ec8c4693b3cSteve Naroff  return;
9625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
963dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
964dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-declaration:
965dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-definition
966dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-protocol-forward-reference
967dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
968dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-definition:
9691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///     @protocol identifier
9701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-protocol-refs[opt]
9711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///       objc-interface-decl-list
972dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @end
973dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
974dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-protocol-forward-reference:
975dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @protocol identifier-list ';'
976dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
977dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   "@protocol identifier ;" should be resolved as "@protocol
9783536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   identifier-list ;": objc-interface-decl-list may not start with a
979dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   semicolon in the first alternative if objc-protocol-refs are omitted.
980b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtProtocolDeclaration(SourceLocation AtLoc,
981b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                                      AttributeList *attrList) {
982861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_protocol) &&
9837ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff         "ParseObjCAtProtocolDeclaration(): Expected @protocol");
9847ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  ConsumeToken(); // the "protocol" identifier
9851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
986df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
9877ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    Diag(Tok, diag::err_expected_ident); // missing protocol name.
988b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
9897ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
9907ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Save the protocol name, then consume it.
9917ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  IdentifierInfo *protocolName = Tok.getIdentifierInfo();
9927ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  SourceLocation nameLoc = ConsumeToken();
9931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
994df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::semi)) { // forward declaration of one protocol.
9957caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    IdentifierLocPair ProtoInfo(protocolName, nameLoc);
9967ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    ConsumeToken();
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    return Actions.ActOnForwardProtocolDeclaration(AtLoc, &ProtoInfo, 1,
998bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
9997ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  }
10001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1001df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::comma)) { // list of forward declarations.
10027caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    llvm::SmallVector<IdentifierLocPair, 8> ProtocolRefs;
10037caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner    ProtocolRefs.push_back(std::make_pair(protocolName, nameLoc));
10047caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
10057ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Parse the list of forward declarations.
10067ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    while (1) {
10077ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the ','
1008df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
10097ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        Diag(Tok, diag::err_expected_ident);
10107ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        SkipUntil(tok::semi);
1011b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner        return DeclPtrTy();
10127ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      }
10137caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner      ProtocolRefs.push_back(IdentifierLocPair(Tok.getIdentifierInfo(),
10147caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner                                               Tok.getLocation()));
10157ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff      ConsumeToken(); // the identifier
10161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1017df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::comma))
10187ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff        break;
10197ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    }
10207ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    // Consume the ';'.
10217ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff    if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after, "@protocol"))
1022b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
10231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1024e440eb8158e71deb1e4ab11618ae3d680aac6da1Steve Naroff    return Actions.ActOnForwardProtocolDeclaration(AtLoc,
10251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                   &ProtocolRefs[0],
1026bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   ProtocolRefs.size(),
1027bc1c877fe28fb6a825f0b226a0a2da99e713ea03Fariborz Jahanian                                                   attrList);
10287caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  }
10291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10307ef58fdbefddf693910e6403a71b3d367444c897Steve Naroff  // Last, and definitely not least, parse a protocol declaration.
103171b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  SourceLocation LAngleLoc, EndProtoLoc;
10327caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner
1033b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  llvm::SmallVector<DeclPtrTy, 8> ProtocolRefs;
103471b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis  llvm::SmallVector<SourceLocation, 8> ProtocolLocs;
10357caeabd868d46cf4e68478c6e9136dae4e735d21Chris Lattner  if (Tok.is(tok::less) &&
103671b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis      ParseObjCProtocolReferences(ProtocolRefs, ProtocolLocs, false,
103771b0addffbdeed29cc062c962e236c34107755d6Argyrios Kyrtzidis                                  LAngleLoc, EndProtoLoc))
1038b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1040b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ProtoType =
1041e13b9595dc1e2f4288bec34f3412359f648e84a5Chris Lattner    Actions.ActOnStartProtocolInterface(AtLoc, protocolName, nameLoc,
1042beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.data(),
1043beaaccd8e2a8748f77b66e2b330fb9136937e14cJay Foad                                        ProtocolRefs.size(),
1044246e70f69cb8aeb67225c54690f1c6b25abd5a86Daniel Dunbar                                        EndProtoLoc, attrList);
104525e077d59a8e8e43b65882b69610a3d5e2aaf53cFariborz Jahanian  ParseObjCInterfaceDeclList(ProtoType, tok::objc_protocol);
1046bc662afa1cb9b61cb1e7808bb1463dd6291b8095Chris Lattner  return ProtoType;
1047dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff}
1048dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff
1049dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-implementation:
1050dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-class-implementation-prologue
1051dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     objc-category-implementation-prologue
1052dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1053dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-class-implementation-prologue:
1054dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier objc-superclass[opt]
1055dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///       objc-class-instance-variables[opt]
1056dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///
1057dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///   objc-category-implementation-prologue:
1058dac269b65eed82182fc3e96566dedd6562dfe11eSteve Naroff///     @implementation identifier ( identifier )
1059b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtImplementationDeclaration(
1060ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation atLoc) {
1061ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_implementation) &&
1062ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtImplementationDeclaration(): Expected @implementation");
1063ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "implementation" identifier
10641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1065df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1066ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing class or category name.
1067b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1068ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1069ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class or category name - consume it.
1070ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *nameId = Tok.getIdentifierInfo();
1071ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation nameLoc = ConsumeToken(); // consume class or category name
10721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (Tok.is(tok::l_paren)) {
1074ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // we have a category implementation.
1075ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation lparenLoc = ConsumeParen();
1076ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    SourceLocation categoryLoc, rparenLoc;
1077ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    IdentifierInfo *categoryId = 0;
10781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1079df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::identifier)) {
1080ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryId = Tok.getIdentifierInfo();
1081ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      categoryLoc = ConsumeToken();
1082ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    } else {
1083ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing category name.
1084b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    }
1086df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::r_paren)) {
1087ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_rparen);
1088ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      SkipUntil(tok::r_paren, false); // don't stop at ';'
1089b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1090ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1091ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    rparenLoc = ConsumeParen();
1092b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    DeclPtrTy ImplCatType = Actions.ActOnStartCategoryImplementation(
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    atLoc, nameId, nameLoc, categoryId,
10948f3fde00ad4d4f943321e338b914ae4740711c84Fariborz Jahanian                                    categoryLoc);
1095a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    ObjCImpDecl = ImplCatType;
1096b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1097ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1098ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // We have a class implementation
1099ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  SourceLocation superClassLoc;
1100ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian  IdentifierInfo *superClassId = 0;
1101df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1102ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    // We have a super class
1103ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1104df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::identifier)) {
1105ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      Diag(Tok, diag::err_expected_ident); // missing super class name.
1106b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1107ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1108ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassId = Tok.getIdentifierInfo();
1109ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian    superClassLoc = ConsumeToken(); // Consume super class name
1110ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1111b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy ImplClsType = Actions.ActOnStartClassImplementation(
1112cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner                                  atLoc, nameId, nameLoc,
1113ccb4f314248fb2202637d3290f2b17af5646da08Fariborz Jahanian                                  superClassId, superClassLoc);
11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
111560fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff  if (Tok.is(tok::l_brace)) // we have ivars
111660fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff    ParseObjCClassInstanceVariables(ImplClsType/*FIXME*/, atLoc);
1117a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek  ObjCImpDecl = ImplClsType;
11181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1119b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
11205f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
112160fcceeedbfc8b4a99cb942e2bc5aeb9e2f92a1fSteve Naroff
1122b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtEndDeclaration(SourceLocation atLoc) {
1123ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_end) &&
1124ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCAtEndDeclaration(): Expected @end");
1125b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy Result = ObjCImpDecl;
1126ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  ConsumeToken(); // the "end" identifier
1127a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  if (ObjCImpDecl) {
1128a526c5c67e5a0473c340903ee542ce570119665fTed Kremenek    Actions.ActOnAtEnd(atLoc, ObjCImpDecl);
1129b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    ObjCImpDecl = DeclPtrTy();
1130a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  }
113194cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian  else
113294cdb25a49bd3716ab56ef6de0ea57d29e686bf2Fariborz Jahanian    Diag(atLoc, diag::warn_expected_implementation); // missing @implementation
1133a6e3ac514c924879699c6b0b1201028f0091044fFariborz Jahanian  return Result;
11345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
1135e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian
1136e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///   compatibility-alias-decl:
1137e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///     @compatibility_alias alias-name  class-name ';'
1138e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian///
1139b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCAtAliasDeclaration(SourceLocation atLoc) {
1140e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_compatibility_alias) &&
1141e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian         "ParseObjCAtAliasDeclaration(): Expected @compatibility_alias");
1142e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  ConsumeToken(); // consume compatibility_alias
1143df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1144e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1145b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1146e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1147243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *aliasId = Tok.getIdentifierInfo();
1148243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation aliasLoc = ConsumeToken(); // consume alias-name
1149df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1150e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1151b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1152e992af01d14e2e31037562c123af0a71ae1ed374Fariborz Jahanian  }
1153243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  IdentifierInfo *classId = Tok.getIdentifierInfo();
1154243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  SourceLocation classLoc = ConsumeToken(); // consume class-name;
1155243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  if (Tok.isNot(tok::semi)) {
11561ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@compatibility_alias";
1157b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1158243b64b0001172405ff803c61bdcaa8e98ec1552Fariborz Jahanian  }
1159b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return Actions.ActOnCompatiblityAlias(atLoc, aliasId, aliasLoc,
1160b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner                                        classId, classLoc);
11615f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
11625f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer
1163ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-synthesis:
1164ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @synthesize property-ivar-list ';'
1165ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1166ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar-list:
1167ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar
1168ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-ivar-list ',' property-ivar
1169ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1170ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-ivar:
1171ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1172ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier '=' identifier
1173ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1174b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertySynthesize(SourceLocation atLoc) {
1175ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_synthesize) &&
1176ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@synthesize'");
1177f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume synthesize
1178df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1179ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1180b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1181ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
11821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1183df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::identifier)) {
1184f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyIvar = 0;
1185f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1186f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1187df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.is(tok::equal)) {
1188ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      // property '=' ivar-name
1189ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume '='
1190df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::identifier)) {
1191ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        Diag(Tok, diag::err_expected_ident);
1192ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian        break;
1193ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      }
1194f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian      propertyIvar = Tok.getIdentifierInfo();
1195ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      ConsumeToken(); // consume ivar-name
1196ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    }
1197f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, true, ObjCImpDecl,
1198f624f8186d8fe474350051c6d3f00b2c204fbeaeFariborz Jahanian                                  propertyId, propertyIvar);
1199df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1200ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1201ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1202ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1203df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
12041ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@synthesize";
1205d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian  else
1206d3fdcb5a1bf6bd5e54e18579c054ea3c292a0e28Fariborz Jahanian    ConsumeToken(); // consume ';'
1207b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1208ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
1209ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1210ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-dynamic:
1211ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     @dynamic  property-list
1212ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1213ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///   property-list:
1214ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     identifier
1215ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///     property-list ',' identifier
1216ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1217b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCPropertyDynamic(SourceLocation atLoc) {
1218ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  assert(Tok.isObjCAtKeyword(tok::objc_dynamic) &&
1219ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian         "ParseObjCPropertyDynamic(): Expected '@dynamic'");
1220ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  SourceLocation loc = ConsumeToken(); // consume dynamic
1221df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::identifier)) {
1222ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    Diag(Tok, diag::err_expected_ident);
1223b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner    return DeclPtrTy();
1224ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1225df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::identifier)) {
1226c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    IdentifierInfo *propertyId = Tok.getIdentifierInfo();
1227c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    SourceLocation propertyLoc = ConsumeToken(); // consume property name
1228c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian    Actions.ActOnPropertyImplDecl(atLoc, propertyLoc, false, ObjCImpDecl,
1229c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian                                  propertyId, 0);
1230c35b9e4e2efad727538c848cf30d4b0eb1031dc9Fariborz Jahanian
1231df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    if (Tok.isNot(tok::comma))
1232ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian      break;
1233ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken(); // consume ','
1234ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1235df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi))
12361ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_semi_after) << "@dynamic";
1237b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  return DeclPtrTy();
1238ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian}
12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1240397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-throw-statement:
1241397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    throw expression[opt];
1242397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
124343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCThrowStmt(SourceLocation atLoc) {
124415faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningExprResult Res(Actions);
1245397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume throw
1246df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::semi)) {
124739f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian    Res = ParseExpression();
12480e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Res.isInvalid()) {
1249397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      SkipUntil(tok::semi);
125043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl      return StmtError();
1251397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1252397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
125339f8f159c488a900e5958d5aab3e467af9ec8a2bFariborz Jahanian  ConsumeToken(); // consume ';'
1254e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff  return Actions.ActOnObjCAtThrowStmt(atLoc, move(Res), CurScope);
1255397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1256397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian
1257c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian/// objc-synchronized-statement:
125878a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian///   @synchronized '(' expression ')' compound-statement
1259c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian///
126043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult
126143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::ParseObjCSynchronizedStmt(SourceLocation atLoc) {
1262fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeToken(); // consume synchronized
1263fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::l_paren)) {
12641ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lparen_after) << "@synchronized";
126543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1266fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1267fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // '('
12682f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
12690e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
1270fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SkipUntil(tok::semi);
127143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1272fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1273fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  if (Tok.isNot(tok::r_paren)) {
12741ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
127543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1276fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  }
1277fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian  ConsumeParen();  // ')'
127878a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  if (Tok.isNot(tok::l_brace)) {
12791ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
128043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
128178a677bbb5fa115fa0995b5783adeeefad67167eFariborz Jahanian  }
12823ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // Enter a scope to hold everything within the compound stmt.  Compound
12833ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff  // statements can always hold declarations.
12848935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::DeclScope);
12853ac438c383a4a9a73c76a05c76ec5d02f10a3c52Steve Naroff
128661364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult SynchBody(ParseCompoundStatementBody());
12870e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
12888935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
12890e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (SynchBody.isInvalid())
1290fa3ee8e6776634caf064ba5928ca7699d317a280Fariborz Jahanian    SynchBody = Actions.ActOnNullStmt(Tok.getLocation());
129176ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtSynchronizedStmt(atLoc, move(Res), move(SynchBody));
1292c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian}
1293c385c90c68dfa376650e2facfbb444b2ec9bd110Fariborz Jahanian
1294397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-try-catch-statement:
1295397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt]
1296397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @try compound-statement objc-catch-list[opt] @finally compound-statement
1297397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
1298397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  objc-catch-list:
1299397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    @catch ( parameter-declaration ) compound-statement
1300397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///    objc-catch-list @catch ( catch-parameter-declaration ) compound-statement
1301397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///  catch-parameter-declaration:
1302397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     parameter-declaration
1303397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///     '...' [OBJC2]
1304397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian///
130543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCTryStmt(SourceLocation atLoc) {
1306397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  bool catch_or_finally_seen = false;
130743bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl
1308397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  ConsumeToken(); // consume try
1309df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
13101ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner    Diag(Tok, diag::err_expected_lbrace);
131143bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1312397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
131315faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult CatchStmts(Actions);
131415faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl  OwningStmtResult FinallyStmt(Actions);
13158935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope TryScope(this, Scope::DeclScope);
131661364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult TryBody(ParseCompoundStatementBody());
13178935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  TryScope.Exit();
13180e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (TryBody.isInvalid())
1319bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian    TryBody = Actions.ActOnNullStmt(Tok.getLocation());
1320a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl
1321df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  while (Tok.is(tok::at)) {
13226b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // At this point, we need to lookahead to determine if this @ is the start
13236b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // of an @catch or @finally.  We don't want to consume the @ token if this
13246b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    // is an @try or @encode or something else.
13256b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    Token AfterAt = GetLookAheadToken(1);
13266b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    if (!AfterAt.isObjCAtKeyword(tok::objc_catch) &&
13276b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner        !AfterAt.isObjCAtKeyword(tok::objc_finally))
13286b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      break;
13290e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1330161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian    SourceLocation AtCatchFinallyLoc = ConsumeToken();
1331cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner    if (Tok.isObjCAtKeyword(tok::objc_catch)) {
1332b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      DeclPtrTy FirstPart;
13333b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian      ConsumeToken(); // consume catch
1334df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.is(tok::l_paren)) {
1335397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian        ConsumeParen();
1336e21dd6ffef4585fa43cd3586ed971217d65bf56cSteve Naroff        ParseScope CatchScope(this, Scope::DeclScope|Scope::AtCatchScope);
1337df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner        if (Tok.isNot(tok::ellipsis)) {
1338397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          DeclSpec DS;
1339397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ParseDeclarationSpecifiers(DS);
134043bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl          // For some odd reason, the name of the exception variable is
13411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // optional. As a result, we need to use "PrototypeContext", because
13427ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // we must accept either 'declarator' or 'abstract-declarator' here.
13437ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          Declarator ParmDecl(DS, Declarator::PrototypeContext);
13447ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          ParseDeclarator(ParmDecl);
13457ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff
13467ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // Inform the actions module about the parameter declarator, so it
13477ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          // gets added to the current scope.
13487ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff          FirstPart = Actions.ActOnParamDeclarator(CurScope, ParmDecl);
134964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff        } else
1350397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian          ConsumeToken(); // consume '...'
13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
135293a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        SourceLocation RParenLoc;
13531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
135493a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        if (Tok.is(tok::r_paren))
135593a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          RParenLoc = ConsumeParen();
135693a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff        else // Skip over garbage, until we get to ')'.  Eat the ')'.
135793a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff          SkipUntil(tok::r_paren, true, false);
135893a259500186fa7270f66cb460c5f5728e5680aeSteve Naroff
135915faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl        OwningStmtResult CatchBody(Actions, true);
1360c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        if (Tok.is(tok::l_brace))
1361c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          CatchBody = ParseCompoundStatementBody();
1362c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        else
1363c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner          Diag(Tok, diag::err_expected_lbrace);
13640e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        if (CatchBody.isInvalid())
13653b1191d7eaf2f4984564e01ab84b6713a9d80e70Fariborz Jahanian          CatchBody = Actions.ActOnNullStmt(Tok.getLocation());
13660e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl        CatchStmts = Actions.ActOnObjCAtCatchStmt(AtCatchFinallyLoc,
13677ba138abd329e591a8f6d5001f60dd7082f71b3bSteve Naroff                        RParenLoc, FirstPart, move(CatchBody),
136876ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                        move(CatchStmts));
136964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      } else {
13701ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner        Diag(AtCatchFinallyLoc, diag::err_expected_lparen_after)
13711ab3b96de160e4fbffec2a776e284a48a3bb543dChris Lattner          << "@catch clause";
137243bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl        return StmtError();
1373397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      }
1374397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
13756b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    } else {
13766b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner      assert(Tok.isObjCAtKeyword(tok::objc_finally) && "Lookahead confused?");
137764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff      ConsumeToken(); // consume finally
13788935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor      ParseScope FinallyScope(this, Scope::DeclScope);
13790e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
138015faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl      OwningStmtResult FinallyBody(Actions, true);
1381c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      if (Tok.is(tok::l_brace))
1382c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        FinallyBody = ParseCompoundStatementBody();
1383c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner      else
1384c1b3ba5ae08316fe43e541c4fb02921fc3e80b21Chris Lattner        Diag(Tok, diag::err_expected_lbrace);
13850e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (FinallyBody.isInvalid())
1386161a9c5afaafb4d527b7efba9675a8b2cbbe32e0Fariborz Jahanian        FinallyBody = Actions.ActOnNullStmt(Tok.getLocation());
13870e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      FinallyStmt = Actions.ActOnObjCAtFinallyStmt(AtCatchFinallyLoc,
138876ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                                   move(FinallyBody));
1389397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      catch_or_finally_seen = true;
1390397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian      break;
1391397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    }
1392397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian  }
1393bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  if (!catch_or_finally_seen) {
1394397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian    Diag(atLoc, diag::err_missing_catch_finally);
139543bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
1396bd49a647afd9cc534fef13cadf652d4e9c396e2bFariborz Jahanian  }
139776ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl  return Actions.ActOnObjCAtTryStmt(atLoc, move(TryBody), move(CatchStmts),
139876ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                    move(FinallyStmt));
1399397fcc117e5631db53879fbfcca66966088f3f07Fariborz Jahanian}
1400ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
14013536b443bc50d58a79f14fca9b6842541a434854Steve Naroff///   objc-method-def: objc-method-proto ';'[opt] '{' body '}'
1402ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian///
1403b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris LattnerParser::DeclPtrTy Parser::ParseObjCMethodDefinition() {
1404b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner  DeclPtrTy MDecl = ParseObjCMethodPrototype(ObjCImpDecl);
14051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
140649f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner  PrettyStackTraceActionsDecl CrashInfo(MDecl, Tok.getLocation(), Actions,
140749f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        PP.getSourceManager(),
140849f28ca787d8db7cac3c8898334f70ea55374c98Chris Lattner                                        "parsing Objective-C method");
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1410ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  // parse optional ';'
1411209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  if (Tok.is(tok::semi)) {
1412209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian    if (ObjCImpDecl)
1413209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian      Diag(Tok, diag::warn_semicolon_before_method_nody);
1414ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian    ConsumeToken();
1415209a8c2fa23636f6d065d618e7078e164903f5cdFariborz Jahanian  }
1416ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian
1417409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // We should have an opening brace now.
1418df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::l_brace)) {
1419da323adbb99cee19a203ead852d5d9bfebb23fb7Steve Naroff    Diag(Tok, diag::err_expected_method_body);
14201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1421409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // Skip over garbage, until we get to '{'.  Don't eat the '{'.
1422409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    SkipUntil(tok::l_brace, true, true);
14231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1424409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    // If we didn't find the '{', bail out.
1425409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff    if (Tok.isNot(tok::l_brace))
1426b28317a8e5e0e2953d1e5406d753d6c3c7f1e7d2Chris Lattner      return DeclPtrTy();
1427ac00b7f4a933e60e2f0afd83092339160adc140cFariborz Jahanian  }
1428409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  SourceLocation BraceLoc = Tok.getLocation();
14291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1430409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Enter a scope for the method body.
14318935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  ParseScope BodyScope(this, Scope::FnScope|Scope::DeclScope);
14321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1433409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Tell the actions module that we have entered a method definition with the
1434394f3f483fa4e7b472630cfcd03f7840520958c5Steve Naroff  // specified Declarator for the method.
1435ebf6443a4f493233f7e8d92b3a991848c8b1c00dSteve Naroff  Actions.ActOnStartOfObjCMethodDef(CurScope, MDecl);
143661364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
143761364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl  OwningStmtResult FnBody(ParseCompoundStatementBody());
143861364dddc33383e62cfe3b841dbc0f471280d95bSebastian Redl
1439409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // If the function body could not be parsed, make a bogus compoundstmt.
14400e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (FnBody.isInvalid())
1441a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl    FnBody = Actions.ActOnCompoundStmt(BraceLoc, BraceLoc,
1442a60528cdac7deee3991c2b48af4df4f315e49e9dSebastian Redl                                       MultiStmtArg(Actions), false);
1443798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
144432ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  // TODO: Pass argument information.
144532ce8376efb7e0d70e5f7e8fcf685130293f412bSteve Naroff  Actions.ActOnFinishFunctionBody(MDecl, move(FnBody));
14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1447409be835b68344e0de56f99ef9a1e12760bc69eeSteve Naroff  // Leave the function body scope.
14488935b8b49053122ddd3ab4cd59af0fe5eb9c23cfDouglas Gregor  BodyScope.Exit();
1449798d119415323ebcd029ffe1e0fb442a4ca8adbbSebastian Redl
145071c0a951d08dc7a2a057df8c15f22b36f6aa47c7Steve Naroff  return MDecl;
14515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer}
14525508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
145343bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian RedlParser::OwningStmtResult Parser::ParseObjCAtStatement(SourceLocation AtLoc) {
145464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  if (Tok.isObjCAtKeyword(tok::objc_try)) {
14556b884508c3bc97cc9df9516adb92fbf88dd0a2e4Chris Lattner    return ParseObjCTryStmt(AtLoc);
145664515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  } else if (Tok.isObjCAtKeyword(tok::objc_throw))
145764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCThrowStmt(AtLoc);
145864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  else if (Tok.isObjCAtKeyword(tok::objc_synchronized))
145964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    return ParseObjCSynchronizedStmt(AtLoc);
1460d8c4e15138e69a51754cc259c8a592cc47950c8eSebastian Redl  OwningExprResult Res(ParseExpressionWithLeadingAt(AtLoc));
14610e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
146264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // If the expression is invalid, skip ahead to the next semicolon. Not
146364515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // doing this opens us up to the possibility of infinite loops if
146464515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    // ParseExpression does not consume any tokens.
146564515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff    SkipUntil(tok::semi);
146643bc2a0973ffe404fabba6f8280cd6bad2c69fcbSebastian Redl    return StmtError();
146764515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  }
146864515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  // Otherwise, eat the semicolon.
146964515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff  ExpectAndConsume(tok::semi, diag::err_expected_semi_after_expr);
14706b1d283fe879fb11d7ce7a69feecf66e77b0eaf3Anders Carlsson  return Actions.ActOnExprStmt(Actions.FullExpr(Res));
147164515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff}
147264515f31850024a263e8f55f81e9ea4b39925cfaSteve Naroff
14731d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCAtExpression(SourceLocation AtLoc) {
14745508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  switch (Tok.getKind()) {
1475b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::string_literal:    // primary-expression: string-literal
1476b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  case tok::wide_string_literal:
14771d922960e083906a586609ac6978678147250177Sebastian Redl    return ParsePostfixExpressionSuffix(ParseObjCStringLiteral(AtLoc));
1478b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  default:
14794fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    if (Tok.getIdentifierInfo() == 0)
14801d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
14812f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl
14824fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    switch (Tok.getIdentifierInfo()->getObjCKeywordID()) {
14834fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_encode:
14841d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCEncodeExpression(AtLoc));
14854fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_protocol:
14861d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCProtocolExpression(AtLoc));
14874fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    case tok::objc_selector:
14881d922960e083906a586609ac6978678147250177Sebastian Redl      return ParsePostfixExpressionSuffix(ParseObjCSelectorExpression(AtLoc));
14894fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    default:
14901d922960e083906a586609ac6978678147250177Sebastian Redl      return ExprError(Diag(AtLoc, diag::err_unexpected_at));
14914fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    }
14925508518a2702b00be3b15a26d772bde968972f54Anders Carlsson  }
14935508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
14945508518a2702b00be3b15a26d772bde968972f54Anders Carlsson
14951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-message-expr:
14960ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     '[' objc-receiver objc-message-args ']'
14970ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
14980ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-receiver:
14990ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     expression
15000ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     class-name
15010ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     type-name
15021d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCMessageExpression() {
1503699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  assert(Tok.is(tok::l_square) && "'[' expected");
1504699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation LBracLoc = ConsumeBracket(); // consume '['
1505699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
1506699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  // Parse receiver
150714dd98a7952c9559e0d17fff8272bf3be67135afChris Lattner  if (isTokObjCMessageIdentifierReceiver()) {
1508699b66138ac307a32e238463e0eff513ff17d337Chris Lattner    IdentifierInfo *ReceiverName = Tok.getIdentifierInfo();
1509d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    if (ReceiverName != Ident_super || GetLookAheadToken(1).isNot(tok::period)) {
1510d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      SourceLocation NameLoc = ConsumeToken();
1511d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian      return ParseObjCMessageExpressionBody(LBracLoc, NameLoc, ReceiverName,
1512d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian                                            ExprArg(Actions));
1513d2869925b5f10e00b13fbf3f41bbb17e4c9adbe0Fariborz Jahanian    }
1514699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
1515699b66138ac307a32e238463e0eff513ff17d337Chris Lattner
15162f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl  OwningExprResult Res(ParseExpression());
15170e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  if (Res.isInvalid()) {
15185c749428a9938d5e2e9564b1c9b7a9252c30ee27Chris Lattner    SkipUntil(tok::r_square);
15191d922960e083906a586609ac6978678147250177Sebastian Redl    return move(Res);
1520699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  }
15211d922960e083906a586609ac6978678147250177Sebastian Redl
15220e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl  return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
152376ad2e85575722e8a38a2bd4648ab4304d9fcd24Sebastian Redl                                        0, move(Res));
1524699b66138ac307a32e238463e0eff513ff17d337Chris Lattner}
15251d922960e083906a586609ac6978678147250177Sebastian Redl
1526699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// ParseObjCMessageExpressionBody - Having parsed "'[' objc-receiver", parse
1527699b66138ac307a32e238463e0eff513ff17d337Chris Lattner/// the rest of a message expression.
15281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
15290ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-message-args:
15300ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-selector
15310ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list
15320ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
15330ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordarg-list:
15340ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg
15350ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     objc-keywordarg-list objc-keywordarg
15360ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
15371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///   objc-keywordarg:
15380ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     selector-name[opt] ':' objc-keywordexpr
15390ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
15400ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   objc-keywordexpr:
15410ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list
15420ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///
15430ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///   nonempty-expr-list:
15440ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     assignment-expression
15450ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian///     nonempty-expr-list , assignment-expression
15461d922960e083906a586609ac6978678147250177Sebastian Redl///
15471d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
1548699b66138ac307a32e238463e0eff513ff17d337Chris LattnerParser::ParseObjCMessageExpressionBody(SourceLocation LBracLoc,
15495cb93b8bf009c4b0ae09b71ba85f54b2a7ea8022Steve Naroff                                       SourceLocation NameLoc,
1550699b66138ac307a32e238463e0eff513ff17d337Chris Lattner                                       IdentifierInfo *ReceiverName,
15511d922960e083906a586609ac6978678147250177Sebastian Redl                                       ExprArg ReceiverExpr) {
1552a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  // Parse objc-selector
15534b6c9051c6522894978c9ba6a819a659d102db36Fariborz Jahanian  SourceLocation Loc;
15542fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *selIdent = ParseObjCSelectorPiece(Loc);
155568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1556ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson  SourceLocation SelectorLoc = Loc;
15571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
155868d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1559a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector KeyExprs(Actions);
156068d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff
1561df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.is(tok::colon)) {
1562a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    while (1) {
1563a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // Each iteration parses a single keyword argument.
156468d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      KeyIdents.push_back(selIdent);
156537387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff
1566df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (Tok.isNot(tok::colon)) {
1567a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        Diag(Tok, diag::err_expected_colon);
15684fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
15694fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15704fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
15714fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
15721d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError();
1573a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      }
15741d922960e083906a586609ac6978678147250177Sebastian Redl
157568d331a78e655d97294e94fcfa63f92cc1f40578Steve Naroff      ConsumeToken(); // Eat the ':'.
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ':'
15772f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
15780e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
15794fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
15804fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
15814fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
15824fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
15831d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
158437387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      }
15851d922960e083906a586609ac6978678147250177Sebastian Redl
158637387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // We have a valid expression.
1587effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
15881d922960e083906a586609ac6978678147250177Sebastian Redl
158937387c932855c6d58d70bdd705cd3a9fdcd2a931Steve Naroff      // Check for another keyword selector.
15902fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      selIdent = ParseObjCSelectorPiece(Loc);
1591df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner      if (!selIdent && Tok.isNot(tok::colon))
1592a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian        break;
1593a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian      // We have a selector or a colon, continue parsing.
1594a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1595a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    // Parse the, optional, argument list, comma separated.
1596df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner    while (Tok.is(tok::comma)) {
159749f109c786f99eb7468dac3976db083a65493444Steve Naroff      ConsumeToken(); // Eat the ','.
15981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      ///  Parse the expression after ','
15992f7ece7c77eb17e24e8f0f4e1b7fb01aa5111f96Sebastian Redl      OwningExprResult Res(ParseAssignmentExpression());
16000e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl      if (Res.isInvalid()) {
16014fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // We must manually skip to a ']', otherwise the expression skipper will
16024fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // stop at the ']' when it skips to the ';'.  We want it to skip beyond
16034fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        // the enclosing expression.
16044fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner        SkipUntil(tok::r_square);
16051d922960e083906a586609ac6978678147250177Sebastian Redl        return move(Res);
160649f109c786f99eb7468dac3976db083a65493444Steve Naroff      }
16070e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
160849f109c786f99eb7468dac3976db083a65493444Steve Naroff      // We have a valid expression.
1609effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl      KeyExprs.push_back(Res.release());
1610a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    }
1611a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  } else if (!selIdent) {
1612a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_ident); // missing selector name.
16131d922960e083906a586609ac6978678147250177Sebastian Redl
16144fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
16154fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
16164fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
16174fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
16181d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1619a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
16201d922960e083906a586609ac6978678147250177Sebastian Redl
1621df19526177bc6d0a3ea4d1ae97497869f60563dbChris Lattner  if (Tok.isNot(tok::r_square)) {
1622a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian    Diag(Tok, diag::err_expected_rsquare);
16234fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // We must manually skip to a ']', otherwise the expression skipper will
16244fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // stop at the ']' when it skips to the ';'.  We want it to skip beyond
16254fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    // the enclosing expression.
16264fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner    SkipUntil(tok::r_square);
16271d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError();
1628a65ff6c81f5b278000839988bb532114fd8c6797Fariborz Jahanian  }
16291d922960e083906a586609ac6978678147250177Sebastian Redl
1630699b66138ac307a32e238463e0eff513ff17d337Chris Lattner  SourceLocation RBracLoc = ConsumeBracket(); // consume ']'
16311d922960e083906a586609ac6978678147250177Sebastian Redl
163229238a0bf7cbf5b396efb451a0adb5fe4aa037caSteve Naroff  unsigned nKeys = KeyIdents.size();
1633ff38491c18b060526d754765b952f4a497a89416Chris Lattner  if (nKeys == 0)
1634ff38491c18b060526d754765b952f4a497a89416Chris Lattner    KeyIdents.push_back(selIdent);
1635ff38491c18b060526d754765b952f4a497a89416Chris Lattner  Selector Sel = PP.getSelectorTable().getSelector(nKeys, &KeyIdents[0]);
16361d922960e083906a586609ac6978678147250177Sebastian Redl
1637ff38491c18b060526d754765b952f4a497a89416Chris Lattner  // We've just parsed a keyword message.
16381d922960e083906a586609ac6978678147250177Sebastian Redl  if (ReceiverName)
16391d922960e083906a586609ac6978678147250177Sebastian Redl    return Owned(Actions.ActOnClassMessage(CurScope, ReceiverName, Sel,
16401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                           LBracLoc, NameLoc, SelectorLoc,
1641ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                           RBracLoc,
16421d922960e083906a586609ac6978678147250177Sebastian Redl                                           KeyExprs.take(), KeyExprs.size()));
16431d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ActOnInstanceMessage(ReceiverExpr.release(), Sel,
1644ff975cfab9ada27df86038286d1678084aeb3428Anders Carlsson                                            LBracLoc, SelectorLoc, RBracLoc,
16451d922960e083906a586609ac6978678147250177Sebastian Redl                                            KeyExprs.take(), KeyExprs.size()));
16460ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian}
16470ccb27ded12fd03eb6818a880f50901bb70254feFariborz Jahanian
16481d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult Parser::ParseObjCStringLiteral(SourceLocation AtLoc) {
164920df9b7ab9388b2a488c5b1293cd196b1e073b4eSebastian Redl  OwningExprResult Res(ParseStringLiteralExpression());
16501d922960e083906a586609ac6978678147250177Sebastian Redl  if (Res.isInvalid()) return move(Res);
16511d922960e083906a586609ac6978678147250177Sebastian Redl
1652b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // @"foo" @"bar" is a valid concatenated string.  Eat any subsequent string
1653b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // expressions.  At this point, we know that the only valid thing that starts
1654b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  // with '@' is an @"".
1655b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  llvm::SmallVector<SourceLocation, 4> AtLocs;
1656a55e52c0802cae3b7c366a05c461d3d15074c1a3Sebastian Redl  ExprVector AtStrings(Actions);
1657b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  AtLocs.push_back(AtLoc);
1658effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl  AtStrings.push_back(Res.release());
16590e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1660b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  while (Tok.is(tok::at)) {
1661b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner    AtLocs.push_back(ConsumeToken()); // eat the @.
1662b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
166315faa7fdfb496489dec9470aa5eb699b29ecdaccSebastian Redl    // Invalid unless there is a string literal.
166497cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    if (!isTokenStringLiteral())
166597cf6eb380016db868866faf27a086cd55a316d4Chris Lattner      return ExprError(Diag(Tok, diag::err_objc_concat_string));
1666b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner
166797cf6eb380016db868866faf27a086cd55a316d4Chris Lattner    OwningExprResult Lit(ParseStringLiteralExpression());
16680e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl    if (Lit.isInvalid())
16691d922960e083906a586609ac6978678147250177Sebastian Redl      return move(Lit);
16700e9eabca263e8922bec0e2b38c8670eba9a39a1fSebastian Redl
1671effa8d1c97b00a3f53e972b0e61d9aade5ea1c57Sebastian Redl    AtStrings.push_back(Lit.release());
1672b3a99cd5bcaeff0c5ff6a60788b5eb68e52a3953Chris Lattner  }
16731d922960e083906a586609ac6978678147250177Sebastian Redl
16741d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCStringLiteral(&AtLocs[0], AtStrings.take(),
16751d922960e083906a586609ac6978678147250177Sebastian Redl                                              AtStrings.size()));
16765508518a2702b00be3b15a26d772bde968972f54Anders Carlsson}
1677f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson
1678f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///    objc-encode-expression:
1679f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson///      @encode ( type-name )
16801d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
16811d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCEncodeExpression(SourceLocation AtLoc) {
1682861cf3effdc0fbc97d401539bc3050da76b2476fSteve Naroff  assert(Tok.isObjCAtKeyword(tok::objc_encode) && "Not an @encode expression!");
16831d922960e083906a586609ac6978678147250177Sebastian Redl
1684f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation EncLoc = ConsumeToken();
16851d922960e083906a586609ac6978678147250177Sebastian Redl
16864fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
16871d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@encode");
16881d922960e083906a586609ac6978678147250177Sebastian Redl
1689f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson  SourceLocation LParenLoc = ConsumeParen();
16901d922960e083906a586609ac6978678147250177Sebastian Redl
1691809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  TypeResult Ty = ParseTypeName();
16921d922960e083906a586609ac6978678147250177Sebastian Redl
16934988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
16941d922960e083906a586609ac6978678147250177Sebastian Redl
1695809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor  if (Ty.isInvalid())
1696809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor    return ExprError();
1697809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor
16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return Owned(Actions.ParseObjCEncodeExpression(AtLoc, EncLoc, LParenLoc,
1699809070a886684cb5b92eb0e00a6581ab1fa6b17aDouglas Gregor                                                 Ty.get(), RParenLoc));
1700f9bcf01f82dfd2688f81e57bcc6300c9b13c51a6Anders Carlsson}
170129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
170229b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///     objc-protocol-expression
170329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson///       @protocol ( protocol-name )
17041d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
17051d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCProtocolExpression(SourceLocation AtLoc) {
170629b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation ProtoLoc = ConsumeToken();
17071d922960e083906a586609ac6978678147250177Sebastian Redl
17084fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
17091d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@protocol");
17101d922960e083906a586609ac6978678147250177Sebastian Redl
171129b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  SourceLocation LParenLoc = ConsumeParen();
17121d922960e083906a586609ac6978678147250177Sebastian Redl
17134fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::identifier))
17141d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
17151d922960e083906a586609ac6978678147250177Sebastian Redl
1716390d50a725497e99247dc104a7d2c2a255d3af14Fariborz Jahanian  IdentifierInfo *protocolId = Tok.getIdentifierInfo();
171729b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson  ConsumeToken();
17181d922960e083906a586609ac6978678147250177Sebastian Redl
17194988ae3fda10743c8ed8a98cdcb5a783362587b4Anders Carlsson  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
172029b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson
17211d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCProtocolExpression(protocolId, AtLoc, ProtoLoc,
17221d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
172329b2cb1ff1a3dd78edd38e2f43ee7041d3e4ec3cAnders Carlsson}
1724a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian
1725a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///     objc-selector-expression
1726a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian///       @selector '(' objc-keyword-selector ')'
17271d922960e083906a586609ac6978678147250177Sebastian RedlParser::OwningExprResult
17281d922960e083906a586609ac6978678147250177Sebastian RedlParser::ParseObjCSelectorExpression(SourceLocation AtLoc) {
1729a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation SelectorLoc = ConsumeToken();
17301d922960e083906a586609ac6978678147250177Sebastian Redl
17314fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner  if (Tok.isNot(tok::l_paren))
17321d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_lparen_after) << "@selector");
17331d922960e083906a586609ac6978678147250177Sebastian Redl
1734b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  llvm::SmallVector<IdentifierInfo *, 12> KeyIdents;
1735a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation LParenLoc = ConsumeParen();
1736a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation sLoc;
17372fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner  IdentifierInfo *SelIdent = ParseObjCSelectorPiece(sLoc);
17381d922960e083906a586609ac6978678147250177Sebastian Redl  if (!SelIdent && Tok.isNot(tok::colon)) // missing selector name.
17391d922960e083906a586609ac6978678147250177Sebastian Redl    return ExprError(Diag(Tok, diag::err_expected_ident));
17401d922960e083906a586609ac6978678147250177Sebastian Redl
1741b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian  KeyIdents.push_back(SelIdent);
1742887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  unsigned nColons = 0;
1743887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  if (Tok.isNot(tok::r_paren)) {
1744a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    while (1) {
17454fef81d718ca1b91ce2adef52db91a35f86e9bbdChris Lattner      if (Tok.isNot(tok::colon))
17461d922960e083906a586609ac6978678147250177Sebastian Redl        return ExprError(Diag(Tok, diag::err_expected_colon));
17471d922960e083906a586609ac6978678147250177Sebastian Redl
1748cb53b361bce341c8591333c6997f62e480acc0b4Chris Lattner      nColons++;
1749a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      ConsumeToken(); // Eat the ':'.
1750a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (Tok.is(tok::r_paren))
1751a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1752a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      // Check for another keyword selector.
1753a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      SourceLocation Loc;
17542fc5c2428ecb450a3256c8316b93b8655cb76a0fChris Lattner      SelIdent = ParseObjCSelectorPiece(Loc);
1755b62f6813406a03bf8a371c4e46c9fad51d102121Fariborz Jahanian      KeyIdents.push_back(SelIdent);
1756a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian      if (!SelIdent && Tok.isNot(tok::colon))
1757a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian        break;
1758a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian    }
1759887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  }
1760a0818e3cd7d59d05e6da41015033b5574c3d7893Fariborz Jahanian  SourceLocation RParenLoc = MatchRHSPunctuation(tok::r_paren, LParenLoc);
1761887407e71fd58de452361b77d72970e32b20ebe0Steve Naroff  Selector Sel = PP.getSelectorTable().getSelector(nColons, &KeyIdents[0]);
17621d922960e083906a586609ac6978678147250177Sebastian Redl  return Owned(Actions.ParseObjCSelectorExpression(Sel, AtLoc, SelectorLoc,
17631d922960e083906a586609ac6978678147250177Sebastian Redl                                                   LParenLoc, RParenLoc));
176458065b2d8038a4e9a91ea4813bd1774c0f6efacbGabor Greif }
1765