Migrate the responsibility for turning the receiver name in an

Objective-C class message expression into a type from the parser
(which was doing so in two places) to Action::getObjCMessageKind()
which, in the case of Sema, reduces the number of name lookups we need
to perform.

llvm-svn: 102026
This commit is contained in:
Douglas Gregor 2010-04-21 20:38:13 +00:00
parent 4bb6e92902
commit e5798dcb41
6 changed files with 65 additions and 66 deletions

View File

@ -2373,9 +2373,7 @@ public:
/// ///
/// \param S The scope in which the message send occurs. /// \param S The scope in which the message send occurs.
/// ///
/// \param Name The identifier following the '['. This identifier /// \param Name The identifier following the '['.
/// may be modified by the action, if, for example, typo-correction
/// finds a different class name.
/// ///
/// \param NameLoc The location of the identifier. /// \param NameLoc The location of the identifier.
/// ///
@ -2383,12 +2381,16 @@ public:
/// ///
/// \param HasTrailingDot Whether the name is followed by a period. /// \param HasTrailingDot Whether the name is followed by a period.
/// ///
/// \param ReceiverType If this routine returns \c ObjCClassMessage,
/// this argument will be set to the receiver type.
///
/// \returns The kind of message send. /// \returns The kind of message send.
virtual ObjCMessageKind getObjCMessageKind(Scope *S, virtual ObjCMessageKind getObjCMessageKind(Scope *S,
IdentifierInfo *&Name, IdentifierInfo *Name,
SourceLocation NameLoc, SourceLocation NameLoc,
bool IsSuper, bool IsSuper,
bool HasTrailingDot); bool HasTrailingDot,
TypeTy *&ReceiverType);
/// \brief Parsed a message send to 'super'. /// \brief Parsed a message send to 'super'.
/// ///

View File

@ -27,15 +27,30 @@ ActionBase::~ActionBase() {}
Action::~Action() {} Action::~Action() {}
Action::ObjCMessageKind Action::getObjCMessageKind(Scope *S, Action::ObjCMessageKind Action::getObjCMessageKind(Scope *S,
IdentifierInfo *&Name, IdentifierInfo *Name,
SourceLocation NameLoc, SourceLocation NameLoc,
bool IsSuper, bool IsSuper,
bool HasTrailingDot) { bool HasTrailingDot,
TypeTy *&ReceiverType) {
ReceiverType = 0;
if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope()) if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
return ObjCSuperMessage; return ObjCSuperMessage;
if (getTypeName(*Name, NameLoc, S)) if (TypeTy *TyName = getTypeName(*Name, NameLoc, S)) {
DeclSpec DS;
const char *PrevSpec = 0;
unsigned DiagID = 0;
if (!DS.SetTypeSpecType(DeclSpec::TST_typename, NameLoc, PrevSpec,
DiagID, TyName)) {
DS.SetRangeEnd(NameLoc);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
TypeResult Ty = ActOnTypeName(S, DeclaratorInfo);
if (!Ty.isInvalid())
ReceiverType = Ty.get();
}
return ObjCClassMessage; return ObjCClassMessage;
}
return ObjCInstanceMessage; return ObjCInstanceMessage;
} }

View File

@ -130,15 +130,17 @@ Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
if (getLang().ObjC1 && Tok.is(tok::identifier)) { if (getLang().ObjC1 && Tok.is(tok::identifier)) {
IdentifierInfo *II = Tok.getIdentifierInfo(); IdentifierInfo *II = Tok.getIdentifierInfo();
SourceLocation IILoc = Tok.getLocation(); SourceLocation IILoc = Tok.getLocation();
TypeTy *ReceiverType;
// Three cases. This is a message send to a type: [type foo] // Three cases. This is a message send to a type: [type foo]
// This is a message send to super: [super foo] // This is a message send to super: [super foo]
// This is a message sent to an expr: [super.bar foo] // This is a message sent to an expr: [super.bar foo]
switch (Action::ObjCMessageKind Kind switch (Action::ObjCMessageKind Kind
= Actions.getObjCMessageKind(CurScope, II, IILoc, = Actions.getObjCMessageKind(CurScope, II, IILoc,
II == Ident_super, II == Ident_super,
NextToken().is(tok::period))) { NextToken().is(tok::period),
ReceiverType)) {
case Action::ObjCSuperMessage: case Action::ObjCSuperMessage:
case Action::ObjCClassMessage: { case Action::ObjCClassMessage:
// If we have exactly one array designator, this used the GNU // If we have exactly one array designator, this used the GNU
// 'designation: array-designator' extension, otherwise there should be no // 'designation: array-designator' extension, otherwise there should be no
// designators at all! // designators at all!
@ -154,36 +156,16 @@ Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
ConsumeToken(), ConsumeToken(),
0, 0,
ExprArg(Actions)); ExprArg(Actions));
ConsumeToken(); // the identifier
// FIXME: This code is redundant with ParseObjCMessageExpr. if (!ReceiverType) {
// Create the type that corresponds to the identifier (which
// names an Objective-C class).
TypeTy *Type = 0;
if (TypeTy *TyName = Actions.getTypeName(*II, IILoc, CurScope)) {
DeclSpec DS;
const char *PrevSpec = 0;
unsigned DiagID = 0;
if (!DS.SetTypeSpecType(DeclSpec::TST_typename, IILoc, PrevSpec,
DiagID, TyName)) {
DS.SetRangeEnd(IILoc);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
TypeResult Ty = Actions.ActOnTypeName(CurScope, DeclaratorInfo);
if (!Ty.isInvalid())
Type = Ty.get();
}
}
ConsumeToken(); // The identifier.
if (!Type) {
SkipUntil(tok::r_square); SkipUntil(tok::r_square);
return ExprError(); return ExprError();
} }
return ParseAssignmentExprWithObjCMessageExprStart(StartLoc, return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
SourceLocation(), SourceLocation(),
Type, ReceiverType,
ExprArg(Actions)); ExprArg(Actions));
}
case Action::ObjCInstanceMessage: case Action::ObjCInstanceMessage:
// Fall through; we'll just parse the expression and // Fall through; we'll just parse the expression and

View File

@ -1726,40 +1726,26 @@ Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
if (Tok.is(tok::identifier)) { if (Tok.is(tok::identifier)) {
IdentifierInfo *Name = Tok.getIdentifierInfo(); IdentifierInfo *Name = Tok.getIdentifierInfo();
SourceLocation NameLoc = Tok.getLocation(); SourceLocation NameLoc = Tok.getLocation();
TypeTy *ReceiverType;
switch (Actions.getObjCMessageKind(CurScope, Name, NameLoc, switch (Actions.getObjCMessageKind(CurScope, Name, NameLoc,
Name == Ident_super, Name == Ident_super,
NextToken().is(tok::period))) { NextToken().is(tok::period),
ReceiverType)) {
case Action::ObjCSuperMessage: case Action::ObjCSuperMessage:
return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0, return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
ExprArg(Actions)); ExprArg(Actions));
case Action::ObjCClassMessage: { case Action::ObjCClassMessage:
// Create the type that corresponds to the identifier (which if (!ReceiverType) {
// names an Objective-C class).
TypeTy *Type = 0;
if (TypeTy *TyName = Actions.getTypeName(*Name, NameLoc, CurScope)) {
DeclSpec DS;
const char *PrevSpec = 0;
unsigned DiagID = 0;
if (!DS.SetTypeSpecType(DeclSpec::TST_typename, NameLoc, PrevSpec,
DiagID, TyName)) {
DS.SetRangeEnd(NameLoc);
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
TypeResult Ty = Actions.ActOnTypeName(CurScope, DeclaratorInfo);
if (!Ty.isInvalid())
Type = Ty.get();
}
}
ConsumeToken(); // The identifier.
if (!Type) {
SkipUntil(tok::r_square); SkipUntil(tok::r_square);
return ExprError(); return ExprError();
} }
return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), Type, ConsumeToken(); // the type name
return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
ReceiverType,
ExprArg(Actions)); ExprArg(Actions));
}
case Action::ObjCInstanceMessage: case Action::ObjCInstanceMessage:
// Fall through to parse an expression. // Fall through to parse an expression.

View File

@ -3867,10 +3867,11 @@ public:
SourceLocation propertyNameLoc); SourceLocation propertyNameLoc);
virtual ObjCMessageKind getObjCMessageKind(Scope *S, virtual ObjCMessageKind getObjCMessageKind(Scope *S,
IdentifierInfo *&Name, IdentifierInfo *Name,
SourceLocation NameLoc, SourceLocation NameLoc,
bool IsSuper, bool IsSuper,
bool HasTrailingDot); bool HasTrailingDot,
TypeTy *&ReceiverType);
virtual OwningExprResult ActOnSuperMessage(Scope *S, SourceLocation SuperLoc, virtual OwningExprResult ActOnSuperMessage(Scope *S, SourceLocation SuperLoc,
Selector Sel, Selector Sel,

View File

@ -503,10 +503,13 @@ ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
} }
Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S, Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
IdentifierInfo *&Name, IdentifierInfo *Name,
SourceLocation NameLoc, SourceLocation NameLoc,
bool IsSuper, bool IsSuper,
bool HasTrailingDot) { bool HasTrailingDot,
TypeTy *&ReceiverType) {
ReceiverType = 0;
// If the identifier is "super" and there is no trailing dot, we're // If the identifier is "super" and there is no trailing dot, we're
// messaging super. // messaging super.
if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope()) if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
@ -541,11 +544,19 @@ Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
// We found something. If it's a type, then we have a class // We found something. If it's a type, then we have a class
// message. Otherwise, it's an instance message. // message. Otherwise, it's an instance message.
NamedDecl *ND = Result.getFoundDecl(); NamedDecl *ND = Result.getFoundDecl();
if (isa<ObjCInterfaceDecl>(ND) || isa<TypeDecl>(ND) || QualType T;
isa<UnresolvedUsingTypenameDecl>(ND)) if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
return ObjCClassMessage; T = Context.getObjCInterfaceType(Class);
else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
T = Context.getTypeDeclType(Type);
else
return ObjCInstanceMessage;
return ObjCInstanceMessage; // We have a class message, and T is the type we're
// messaging. Build source-location information for it.
TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
ReceiverType = CreateLocInfoType(T, TSInfo).getAsOpaquePtr();
return ObjCClassMessage;
} }
} }
@ -561,7 +572,7 @@ Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
// If we found a declaration, correct when it refers to an Objective-C // If we found a declaration, correct when it refers to an Objective-C
// class. // class.
NamedDecl *ND = Result.getFoundDecl(); NamedDecl *ND = Result.getFoundDecl();
if (isa<ObjCInterfaceDecl>(ND)) { if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Diag(NameLoc, diag::err_unknown_receiver_suggest) Diag(NameLoc, diag::err_unknown_receiver_suggest)
<< Name << Result.getLookupName() << Name << Result.getLookupName()
<< FixItHint::CreateReplacement(SourceRange(NameLoc), << FixItHint::CreateReplacement(SourceRange(NameLoc),
@ -569,7 +580,9 @@ Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
Diag(ND->getLocation(), diag::note_previous_decl) Diag(ND->getLocation(), diag::note_previous_decl)
<< Corrected; << Corrected;
Name = ND->getIdentifier(); QualType T = Context.getObjCInterfaceType(Class);
TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
ReceiverType = CreateLocInfoType(T, TSInfo).getAsOpaquePtr();
return ObjCClassMessage; return ObjCClassMessage;
} }
} else if (Result.empty() && Corrected.getAsIdentifierInfo() && } else if (Result.empty() && Corrected.getAsIdentifierInfo() &&