Skip to content

Commit e5798dc

Browse files
committed
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
1 parent 4bb6e92 commit e5798dc

File tree

6 files changed

+66
-67
lines changed

6 files changed

+66
-67
lines changed

clang/include/clang/Parse/Action.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2373,22 +2373,24 @@ class Action : public ActionBase {
23732373
///
23742374
/// \param S The scope in which the message send occurs.
23752375
///
2376-
/// \param Name The identifier following the '['. This identifier
2377-
/// may be modified by the action, if, for example, typo-correction
2378-
/// finds a different class name.
2376+
/// \param Name The identifier following the '['.
23792377
///
23802378
/// \param NameLoc The location of the identifier.
23812379
///
23822380
/// \param IsSuper Whether the name is the pseudo-keyword "super".
23832381
///
23842382
/// \param HasTrailingDot Whether the name is followed by a period.
23852383
///
2384+
/// \param ReceiverType If this routine returns \c ObjCClassMessage,
2385+
/// this argument will be set to the receiver type.
2386+
///
23862387
/// \returns The kind of message send.
23872388
virtual ObjCMessageKind getObjCMessageKind(Scope *S,
2388-
IdentifierInfo *&Name,
2389+
IdentifierInfo *Name,
23892390
SourceLocation NameLoc,
23902391
bool IsSuper,
2391-
bool HasTrailingDot);
2392+
bool HasTrailingDot,
2393+
TypeTy *&ReceiverType);
23922394

23932395
/// \brief Parsed a message send to 'super'.
23942396
///

clang/lib/Parse/MinimalAction.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,30 @@ ActionBase::~ActionBase() {}
2727
Action::~Action() {}
2828

2929
Action::ObjCMessageKind Action::getObjCMessageKind(Scope *S,
30-
IdentifierInfo *&Name,
30+
IdentifierInfo *Name,
3131
SourceLocation NameLoc,
3232
bool IsSuper,
33-
bool HasTrailingDot) {
33+
bool HasTrailingDot,
34+
TypeTy *&ReceiverType) {
35+
ReceiverType = 0;
36+
3437
if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
3538
return ObjCSuperMessage;
3639

37-
if (getTypeName(*Name, NameLoc, S))
40+
if (TypeTy *TyName = getTypeName(*Name, NameLoc, S)) {
41+
DeclSpec DS;
42+
const char *PrevSpec = 0;
43+
unsigned DiagID = 0;
44+
if (!DS.SetTypeSpecType(DeclSpec::TST_typename, NameLoc, PrevSpec,
45+
DiagID, TyName)) {
46+
DS.SetRangeEnd(NameLoc);
47+
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
48+
TypeResult Ty = ActOnTypeName(S, DeclaratorInfo);
49+
if (!Ty.isInvalid())
50+
ReceiverType = Ty.get();
51+
}
3852
return ObjCClassMessage;
53+
}
3954

4055
return ObjCInstanceMessage;
4156
}

clang/lib/Parse/ParseInit.cpp

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,17 @@ Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
130130
if (getLang().ObjC1 && Tok.is(tok::identifier)) {
131131
IdentifierInfo *II = Tok.getIdentifierInfo();
132132
SourceLocation IILoc = Tok.getLocation();
133+
TypeTy *ReceiverType;
133134
// Three cases. This is a message send to a type: [type foo]
134135
// This is a message send to super: [super foo]
135136
// This is a message sent to an expr: [super.bar foo]
136137
switch (Action::ObjCMessageKind Kind
137138
= Actions.getObjCMessageKind(CurScope, II, IILoc,
138139
II == Ident_super,
139-
NextToken().is(tok::period))) {
140+
NextToken().is(tok::period),
141+
ReceiverType)) {
140142
case Action::ObjCSuperMessage:
141-
case Action::ObjCClassMessage: {
143+
case Action::ObjCClassMessage:
142144
// If we have exactly one array designator, this used the GNU
143145
// 'designation: array-designator' extension, otherwise there should be no
144146
// designators at all!
@@ -154,36 +156,16 @@ Parser::OwningExprResult Parser::ParseInitializerWithPotentialDesignator() {
154156
ConsumeToken(),
155157
0,
156158
ExprArg(Actions));
157-
158-
// FIXME: This code is redundant with ParseObjCMessageExpr.
159-
// Create the type that corresponds to the identifier (which
160-
// names an Objective-C class).
161-
TypeTy *Type = 0;
162-
if (TypeTy *TyName = Actions.getTypeName(*II, IILoc, CurScope)) {
163-
DeclSpec DS;
164-
const char *PrevSpec = 0;
165-
unsigned DiagID = 0;
166-
if (!DS.SetTypeSpecType(DeclSpec::TST_typename, IILoc, PrevSpec,
167-
DiagID, TyName)) {
168-
DS.SetRangeEnd(IILoc);
169-
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
170-
TypeResult Ty = Actions.ActOnTypeName(CurScope, DeclaratorInfo);
171-
if (!Ty.isInvalid())
172-
Type = Ty.get();
173-
}
174-
}
175-
176-
ConsumeToken(); // The identifier.
177-
if (!Type) {
159+
ConsumeToken(); // the identifier
160+
if (!ReceiverType) {
178161
SkipUntil(tok::r_square);
179162
return ExprError();
180163
}
181164

182165
return ParseAssignmentExprWithObjCMessageExprStart(StartLoc,
183166
SourceLocation(),
184-
Type,
167+
ReceiverType,
185168
ExprArg(Actions));
186-
}
187169

188170
case Action::ObjCInstanceMessage:
189171
// Fall through; we'll just parse the expression and

clang/lib/Parse/ParseObjc.cpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,40 +1726,26 @@ Parser::OwningExprResult Parser::ParseObjCMessageExpression() {
17261726
if (Tok.is(tok::identifier)) {
17271727
IdentifierInfo *Name = Tok.getIdentifierInfo();
17281728
SourceLocation NameLoc = Tok.getLocation();
1729+
TypeTy *ReceiverType;
17291730
switch (Actions.getObjCMessageKind(CurScope, Name, NameLoc,
17301731
Name == Ident_super,
1731-
NextToken().is(tok::period))) {
1732+
NextToken().is(tok::period),
1733+
ReceiverType)) {
17321734
case Action::ObjCSuperMessage:
17331735
return ParseObjCMessageExpressionBody(LBracLoc, ConsumeToken(), 0,
17341736
ExprArg(Actions));
17351737

1736-
case Action::ObjCClassMessage: {
1737-
// Create the type that corresponds to the identifier (which
1738-
// names an Objective-C class).
1739-
TypeTy *Type = 0;
1740-
if (TypeTy *TyName = Actions.getTypeName(*Name, NameLoc, CurScope)) {
1741-
DeclSpec DS;
1742-
const char *PrevSpec = 0;
1743-
unsigned DiagID = 0;
1744-
if (!DS.SetTypeSpecType(DeclSpec::TST_typename, NameLoc, PrevSpec,
1745-
DiagID, TyName)) {
1746-
DS.SetRangeEnd(NameLoc);
1747-
Declarator DeclaratorInfo(DS, Declarator::TypeNameContext);
1748-
TypeResult Ty = Actions.ActOnTypeName(CurScope, DeclaratorInfo);
1749-
if (!Ty.isInvalid())
1750-
Type = Ty.get();
1751-
}
1752-
}
1753-
1754-
ConsumeToken(); // The identifier.
1755-
if (!Type) {
1738+
case Action::ObjCClassMessage:
1739+
if (!ReceiverType) {
17561740
SkipUntil(tok::r_square);
17571741
return ExprError();
17581742
}
17591743

1760-
return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(), Type,
1744+
ConsumeToken(); // the type name
1745+
1746+
return ParseObjCMessageExpressionBody(LBracLoc, SourceLocation(),
1747+
ReceiverType,
17611748
ExprArg(Actions));
1762-
}
17631749

17641750
case Action::ObjCInstanceMessage:
17651751
// Fall through to parse an expression.

clang/lib/Sema/Sema.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3867,10 +3867,11 @@ class Sema : public Action {
38673867
SourceLocation propertyNameLoc);
38683868

38693869
virtual ObjCMessageKind getObjCMessageKind(Scope *S,
3870-
IdentifierInfo *&Name,
3870+
IdentifierInfo *Name,
38713871
SourceLocation NameLoc,
38723872
bool IsSuper,
3873-
bool HasTrailingDot);
3873+
bool HasTrailingDot,
3874+
TypeTy *&ReceiverType);
38743875

38753876
virtual OwningExprResult ActOnSuperMessage(Scope *S, SourceLocation SuperLoc,
38763877
Selector Sel,

clang/lib/Sema/SemaExprObjC.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -503,10 +503,13 @@ ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
503503
}
504504

505505
Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
506-
IdentifierInfo *&Name,
506+
IdentifierInfo *Name,
507507
SourceLocation NameLoc,
508508
bool IsSuper,
509-
bool HasTrailingDot) {
509+
bool HasTrailingDot,
510+
TypeTy *&ReceiverType) {
511+
ReceiverType = 0;
512+
510513
// If the identifier is "super" and there is no trailing dot, we're
511514
// messaging super.
512515
if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope())
@@ -541,11 +544,19 @@ Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
541544
// We found something. If it's a type, then we have a class
542545
// message. Otherwise, it's an instance message.
543546
NamedDecl *ND = Result.getFoundDecl();
544-
if (isa<ObjCInterfaceDecl>(ND) || isa<TypeDecl>(ND) ||
545-
isa<UnresolvedUsingTypenameDecl>(ND))
546-
return ObjCClassMessage;
547-
548-
return ObjCInstanceMessage;
547+
QualType T;
548+
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
549+
T = Context.getObjCInterfaceType(Class);
550+
else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
551+
T = Context.getTypeDeclType(Type);
552+
else
553+
return ObjCInstanceMessage;
554+
555+
// We have a class message, and T is the type we're
556+
// messaging. Build source-location information for it.
557+
TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
558+
ReceiverType = CreateLocInfoType(T, TSInfo).getAsOpaquePtr();
559+
return ObjCClassMessage;
549560
}
550561
}
551562

@@ -561,15 +572,17 @@ Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
561572
// If we found a declaration, correct when it refers to an Objective-C
562573
// class.
563574
NamedDecl *ND = Result.getFoundDecl();
564-
if (isa<ObjCInterfaceDecl>(ND)) {
575+
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
565576
Diag(NameLoc, diag::err_unknown_receiver_suggest)
566577
<< Name << Result.getLookupName()
567578
<< FixItHint::CreateReplacement(SourceRange(NameLoc),
568579
ND->getNameAsString());
569580
Diag(ND->getLocation(), diag::note_previous_decl)
570581
<< Corrected;
571582

572-
Name = ND->getIdentifier();
583+
QualType T = Context.getObjCInterfaceType(Class);
584+
TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
585+
ReceiverType = CreateLocInfoType(T, TSInfo).getAsOpaquePtr();
573586
return ObjCClassMessage;
574587
}
575588
} else if (Result.empty() && Corrected.getAsIdentifierInfo() &&

0 commit comments

Comments
 (0)