2008-01-05 06:32:30 +08:00
|
|
|
//===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for Objective-C expressions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-05-30 05:12:08 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2009-02-18 14:48:40 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2008-01-05 06:32:30 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
|
2009-02-18 14:48:40 +08:00
|
|
|
ExprTy **strings,
|
2008-01-05 06:32:30 +08:00
|
|
|
unsigned NumStrings) {
|
2009-02-18 14:48:40 +08:00
|
|
|
StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
|
|
|
|
|
2009-02-18 14:13:04 +08:00
|
|
|
// Most ObjC strings are formed out of a single piece. However, we *can*
|
|
|
|
// have strings formed out of multiple @ strings with multiple pptokens in
|
|
|
|
// each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one
|
|
|
|
// StringLiteral for ObjCStringLiteral to hold onto.
|
2009-02-18 14:48:40 +08:00
|
|
|
StringLiteral *S = Strings[0];
|
2009-02-18 14:13:04 +08:00
|
|
|
|
|
|
|
// If we have a multi-part string, merge it all together.
|
|
|
|
if (NumStrings != 1) {
|
2008-01-05 06:32:30 +08:00
|
|
|
// Concatenate objc strings.
|
2009-02-18 14:48:40 +08:00
|
|
|
llvm::SmallString<128> StrBuf;
|
|
|
|
llvm::SmallVector<SourceLocation, 8> StrLocs;
|
2009-02-18 13:49:11 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != NumStrings; ++i) {
|
2009-02-18 14:48:40 +08:00
|
|
|
S = Strings[i];
|
|
|
|
|
|
|
|
// ObjC strings can't be wide.
|
2009-02-18 14:13:04 +08:00
|
|
|
if (S->isWide()) {
|
|
|
|
Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
|
|
|
|
<< S->getSourceRange();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-18 14:48:40 +08:00
|
|
|
// Get the string data.
|
|
|
|
StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength());
|
|
|
|
|
|
|
|
// Get the locations of the string tokens.
|
|
|
|
StrLocs.append(S->tokloc_begin(), S->tokloc_end());
|
|
|
|
|
|
|
|
// Free the temporary string.
|
2009-02-07 09:47:29 +08:00
|
|
|
S->Destroy(Context);
|
2008-01-05 06:32:30 +08:00
|
|
|
}
|
2009-02-18 14:48:40 +08:00
|
|
|
|
|
|
|
// Create the aggregate string with the appropriate content and location
|
|
|
|
// information.
|
|
|
|
S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false,
|
2009-02-18 14:40:38 +08:00
|
|
|
Context.getPointerType(Context.CharTy),
|
2009-02-18 14:48:40 +08:00
|
|
|
&StrLocs[0], StrLocs.size());
|
2008-01-05 06:32:30 +08:00
|
|
|
}
|
|
|
|
|
2009-02-18 14:01:06 +08:00
|
|
|
// Verify that this composite string is acceptable for ObjC strings.
|
|
|
|
if (CheckObjCString(S))
|
2008-01-05 06:32:30 +08:00
|
|
|
return true;
|
2009-02-18 14:06:56 +08:00
|
|
|
|
|
|
|
// Initialize the constant string interface lazily. This assumes
|
|
|
|
// the NSConstantString interface is seen in this translation unit.
|
|
|
|
QualType Ty = Context.getObjCConstantStringInterface();
|
|
|
|
if (!Ty.isNull()) {
|
|
|
|
Ty = Context.getPointerType(Ty);
|
2008-06-22 05:44:18 +08:00
|
|
|
} else {
|
2009-02-18 14:06:56 +08:00
|
|
|
IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString");
|
|
|
|
NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName);
|
|
|
|
if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
|
|
|
|
Context.setObjCConstantStringInterface(StrIF);
|
|
|
|
Ty = Context.getObjCConstantStringInterface();
|
|
|
|
Ty = Context.getPointerType(Ty);
|
|
|
|
} else {
|
|
|
|
// If there is no NSConstantString interface defined then treat constant
|
|
|
|
// strings as untyped objects and let the runtime figure it out later.
|
|
|
|
Ty = Context.getObjCIdType();
|
|
|
|
}
|
2008-06-22 05:44:18 +08:00
|
|
|
}
|
2009-02-18 14:06:56 +08:00
|
|
|
|
2009-02-18 14:13:04 +08:00
|
|
|
return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
|
2008-01-05 06:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
|
|
|
|
SourceLocation EncodeLoc,
|
|
|
|
SourceLocation LParenLoc,
|
2009-02-18 14:06:56 +08:00
|
|
|
TypeTy *ty,
|
2008-01-05 06:32:30 +08:00
|
|
|
SourceLocation RParenLoc) {
|
2009-02-18 14:06:56 +08:00
|
|
|
QualType EncodedType = QualType::getFromOpaquePtr(ty);
|
2008-01-05 06:32:30 +08:00
|
|
|
|
2009-02-25 06:18:39 +08:00
|
|
|
std::string Str;
|
|
|
|
Context.getObjCEncodingForType(EncodedType, Str);
|
|
|
|
|
|
|
|
// The type of @encode is the same as the type of the corresponding string,
|
|
|
|
// which is an array type.
|
|
|
|
QualType StrTy = Context.CharTy;
|
|
|
|
// A C++ string literal has a const-qualified element type (C++ 2.13.4p1).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
StrTy.addConst();
|
|
|
|
StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
|
|
|
|
ArrayType::Normal, 0);
|
|
|
|
|
|
|
|
return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc);
|
2008-01-05 06:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
|
|
|
|
SourceLocation AtLoc,
|
|
|
|
SourceLocation SelLoc,
|
|
|
|
SourceLocation LParenLoc,
|
|
|
|
SourceLocation RParenLoc) {
|
2009-02-18 14:06:56 +08:00
|
|
|
QualType Ty = Context.getObjCSelType();
|
|
|
|
return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
|
2008-01-05 06:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
|
|
|
|
SourceLocation AtLoc,
|
|
|
|
SourceLocation ProtoLoc,
|
|
|
|
SourceLocation LParenLoc,
|
|
|
|
SourceLocation RParenLoc) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId];
|
2008-01-05 06:32:30 +08:00
|
|
|
if (!PDecl) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
|
2008-01-05 06:32:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-18 14:06:56 +08:00
|
|
|
QualType Ty = Context.getObjCProtoType();
|
|
|
|
if (Ty.isNull())
|
2008-01-05 06:32:30 +08:00
|
|
|
return true;
|
2009-02-18 14:06:56 +08:00
|
|
|
Ty = Context.getPointerType(Ty);
|
|
|
|
return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
|
2008-01-05 06:32:30 +08:00
|
|
|
}
|
|
|
|
|
2008-09-11 08:50:25 +08:00
|
|
|
bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
|
|
|
|
Selector Sel, ObjCMethodDecl *Method,
|
2008-11-24 11:33:13 +08:00
|
|
|
bool isClassMessage,
|
2008-09-11 08:01:56 +08:00
|
|
|
SourceLocation lbrac, SourceLocation rbrac,
|
|
|
|
QualType &ReturnType) {
|
|
|
|
if (!Method) {
|
2008-09-11 08:04:36 +08:00
|
|
|
// Apply default argument promotion as for (C99 6.5.2.2p6).
|
|
|
|
for (unsigned i = 0; i != NumArgs; i++)
|
|
|
|
DefaultArgumentPromotion(Args[i]);
|
|
|
|
|
2008-11-24 11:33:13 +08:00
|
|
|
unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
|
|
|
|
diag::warn_inst_method_not_found;
|
|
|
|
Diag(lbrac, DiagID)
|
|
|
|
<< Sel << isClassMessage << SourceRange(lbrac, rbrac);
|
2008-09-11 08:01:56 +08:00
|
|
|
ReturnType = Context.getObjCIdType();
|
|
|
|
return false;
|
|
|
|
}
|
2008-11-24 11:33:13 +08:00
|
|
|
|
|
|
|
ReturnType = Method->getResultType();
|
2008-09-11 08:01:56 +08:00
|
|
|
|
2008-09-11 08:50:25 +08:00
|
|
|
unsigned NumNamedArgs = Sel.getNumArgs();
|
|
|
|
assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!");
|
|
|
|
|
2008-01-05 06:32:30 +08:00
|
|
|
bool anyIncompatibleArgs = false;
|
2008-09-11 08:50:25 +08:00
|
|
|
for (unsigned i = 0; i < NumNamedArgs; i++) {
|
2008-01-05 06:32:30 +08:00
|
|
|
Expr *argExpr = Args[i];
|
|
|
|
assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
|
|
|
|
|
2009-02-21 02:43:26 +08:00
|
|
|
QualType lhsType = Method->param_begin()[i]->getType();
|
2008-01-05 06:32:30 +08:00
|
|
|
QualType rhsType = argExpr->getType();
|
|
|
|
|
|
|
|
// If necessary, apply function/array conversion. C99 6.7.5.3p[7,8].
|
2008-04-03 01:17:33 +08:00
|
|
|
if (lhsType->isArrayType())
|
|
|
|
lhsType = Context.getArrayDecayedType(lhsType);
|
2008-01-05 06:32:30 +08:00
|
|
|
else if (lhsType->isFunctionType())
|
|
|
|
lhsType = Context.getPointerType(lhsType);
|
|
|
|
|
2008-04-03 01:17:33 +08:00
|
|
|
AssignConvertType Result =
|
|
|
|
CheckSingleAssignmentConstraints(lhsType, argExpr);
|
2008-01-05 06:32:30 +08:00
|
|
|
if (Args[i] != argExpr) // The expression was converted.
|
|
|
|
Args[i] = argExpr; // Make sure we store the converted expression.
|
|
|
|
|
|
|
|
anyIncompatibleArgs |=
|
|
|
|
DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType,
|
|
|
|
argExpr, "sending");
|
|
|
|
}
|
2008-09-11 08:50:25 +08:00
|
|
|
|
|
|
|
// Promote additional arguments to variadic methods.
|
|
|
|
if (Method->isVariadic()) {
|
2009-01-17 00:48:51 +08:00
|
|
|
for (unsigned i = NumNamedArgs; i < NumArgs; ++i)
|
|
|
|
DefaultVariadicArgumentPromotion(Args[i], VariadicMethod);
|
2008-09-11 08:50:25 +08:00
|
|
|
} else {
|
|
|
|
// Check for extra arguments to non-variadic methods.
|
|
|
|
if (NumArgs != NumNamedArgs) {
|
|
|
|
Diag(Args[NumNamedArgs]->getLocStart(),
|
2008-11-19 13:08:23 +08:00
|
|
|
diag::err_typecheck_call_too_many_args)
|
2008-11-22 02:44:24 +08:00
|
|
|
<< 2 /*method*/ << Method->getSourceRange()
|
2008-11-19 13:08:23 +08:00
|
|
|
<< SourceRange(Args[NumNamedArgs]->getLocStart(),
|
|
|
|
Args[NumArgs-1]->getLocEnd());
|
2008-09-11 08:50:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-05 06:32:30 +08:00
|
|
|
return anyIncompatibleArgs;
|
|
|
|
}
|
|
|
|
|
2009-03-04 23:11:40 +08:00
|
|
|
bool Sema::isSelfExpr(Expr *RExpr) {
|
|
|
|
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
|
|
|
|
if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-02-26 23:55:06 +08:00
|
|
|
// Helper method for ActOnClassMethod/ActOnInstanceMethod.
|
|
|
|
// Will search "local" class/category implementations for a method decl.
|
|
|
|
// Returns 0 if no method is found.
|
|
|
|
ObjCMethodDecl *Sema::LookupPrivateMethod(Selector Sel,
|
|
|
|
ObjCInterfaceDecl *ClassDecl) {
|
|
|
|
ObjCMethodDecl *Method = 0;
|
|
|
|
|
|
|
|
if (ObjCImplementationDecl *ImpDecl =
|
|
|
|
ObjCImplementations[ClassDecl->getIdentifier()])
|
|
|
|
Method = ImpDecl->getClassMethod(Sel);
|
|
|
|
|
|
|
|
// Look through local category implementations associated with the class.
|
|
|
|
if (!Method) {
|
|
|
|
for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
|
|
|
|
if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
|
|
|
|
Method = ObjCCategoryImpls[i]->getClassMethod(Sel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Method;
|
|
|
|
}
|
|
|
|
|
2008-01-05 06:32:30 +08:00
|
|
|
// ActOnClassMessage - used for both unary and keyword messages.
|
|
|
|
// ArgExprs is optional - if it is present, the number of expressions
|
|
|
|
// is obtained from Sel.getNumArgs().
|
|
|
|
Sema::ExprResult Sema::ActOnClassMessage(
|
|
|
|
Scope *S,
|
|
|
|
IdentifierInfo *receiverName, Selector Sel,
|
2009-02-15 02:21:46 +08:00
|
|
|
SourceLocation lbrac, SourceLocation receiverLoc,
|
|
|
|
SourceLocation selectorLoc, SourceLocation rbrac,
|
2008-11-19 23:54:23 +08:00
|
|
|
ExprTy **Args, unsigned NumArgs)
|
2008-01-05 06:32:30 +08:00
|
|
|
{
|
|
|
|
assert(receiverName && "missing receiver class name");
|
|
|
|
|
|
|
|
Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl* ClassDecl = 0;
|
2008-07-25 03:44:33 +08:00
|
|
|
bool isSuper = false;
|
|
|
|
|
2008-11-20 13:35:30 +08:00
|
|
|
if (receiverName->isStr("super")) {
|
2008-11-19 23:54:23 +08:00
|
|
|
if (getCurMethodDecl()) {
|
|
|
|
isSuper = true;
|
2009-01-08 05:01:41 +08:00
|
|
|
ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface();
|
|
|
|
if (!OID)
|
|
|
|
return Diag(lbrac, diag::error_no_super_class_message)
|
|
|
|
<< getCurMethodDecl()->getDeclName();
|
|
|
|
ClassDecl = OID->getSuperClass();
|
2008-11-19 23:54:23 +08:00
|
|
|
if (!ClassDecl)
|
2009-01-08 05:01:41 +08:00
|
|
|
return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName();
|
2009-01-10 01:18:27 +08:00
|
|
|
if (getCurMethodDecl()->isInstanceMethod()) {
|
2008-11-19 23:54:23 +08:00
|
|
|
QualType superTy = Context.getObjCInterfaceType(ClassDecl);
|
|
|
|
superTy = Context.getPointerType(superTy);
|
2009-02-07 09:47:29 +08:00
|
|
|
ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(),
|
|
|
|
superTy);
|
2008-11-19 23:54:23 +08:00
|
|
|
// We are really in an instance method, redirect.
|
2009-02-15 02:21:46 +08:00
|
|
|
return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
|
|
|
|
selectorLoc, rbrac, Args, NumArgs);
|
2008-11-19 23:54:23 +08:00
|
|
|
}
|
|
|
|
// We are sending a message to 'super' within a class method. Do nothing,
|
|
|
|
// the receiver will pass through as 'super' (how convenient:-).
|
|
|
|
} else {
|
|
|
|
// 'super' has been used outside a method context. If a variable named
|
|
|
|
// 'super' has been declared, redirect. If not, produce a diagnostic.
|
2009-02-05 01:27:36 +08:00
|
|
|
NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName);
|
2008-11-19 23:54:23 +08:00
|
|
|
ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl);
|
|
|
|
if (VD) {
|
2009-02-07 09:47:29 +08:00
|
|
|
ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(),
|
|
|
|
receiverLoc);
|
2008-11-19 23:54:23 +08:00
|
|
|
// We are really in an instance method, redirect.
|
2009-02-15 02:21:46 +08:00
|
|
|
return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac,
|
|
|
|
selectorLoc, rbrac, Args, NumArgs);
|
2008-11-19 23:54:23 +08:00
|
|
|
}
|
2008-11-24 05:45:46 +08:00
|
|
|
return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName;
|
2008-11-19 23:54:23 +08:00
|
|
|
}
|
2008-01-05 06:32:30 +08:00
|
|
|
} else
|
|
|
|
ClassDecl = getObjCInterfaceDecl(receiverName);
|
|
|
|
|
2008-07-26 03:39:00 +08:00
|
|
|
// The following code allows for the following GCC-ism:
|
2008-06-05 07:08:38 +08:00
|
|
|
//
|
|
|
|
// typedef XCElementDisplayRect XCElementGraphicsRect;
|
|
|
|
//
|
|
|
|
// @implementation XCRASlice
|
|
|
|
// - whatever { // Note that XCElementGraphicsRect is a typedef name.
|
|
|
|
// _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init];
|
|
|
|
// }
|
|
|
|
//
|
2008-07-26 03:39:00 +08:00
|
|
|
// If necessary, the following lookup could move to getObjCInterfaceDecl().
|
|
|
|
if (!ClassDecl) {
|
2009-02-05 01:27:36 +08:00
|
|
|
NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName);
|
2008-07-26 03:39:00 +08:00
|
|
|
if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) {
|
|
|
|
const ObjCInterfaceType *OCIT;
|
|
|
|
OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType();
|
2009-01-17 04:35:09 +08:00
|
|
|
if (!OCIT)
|
|
|
|
return Diag(receiverLoc, diag::err_invalid_receiver_to_message);
|
|
|
|
ClassDecl = OCIT->getDecl();
|
2008-07-26 03:39:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(ClassDecl && "missing interface declaration");
|
2008-06-05 07:08:38 +08:00
|
|
|
ObjCMethodDecl *Method = 0;
|
2008-01-05 06:32:30 +08:00
|
|
|
QualType returnType;
|
2008-07-26 03:39:00 +08:00
|
|
|
Method = ClassDecl->lookupClassMethod(Sel);
|
|
|
|
|
|
|
|
// If we have an implementation in scope, check "private" methods.
|
2009-02-26 23:55:06 +08:00
|
|
|
if (!Method)
|
|
|
|
Method = LookupPrivateMethod(Sel, ClassDecl);
|
|
|
|
|
2008-07-26 03:39:00 +08:00
|
|
|
// Before we give up, check if the selector is an instance method.
|
2009-03-05 01:50:39 +08:00
|
|
|
// But only in the root. This matches gcc's behaviour and what the
|
|
|
|
// runtime expects.
|
|
|
|
if (!Method) {
|
|
|
|
ObjCInterfaceDecl *Root = ClassDecl;
|
|
|
|
while (Root->getSuperClass())
|
|
|
|
Root = Root->getSuperClass();
|
|
|
|
Method = Root->lookupInstanceMethod(Sel);
|
|
|
|
}
|
2008-07-26 03:39:00 +08:00
|
|
|
|
2009-02-19 05:56:37 +08:00
|
|
|
if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
|
|
|
|
return true;
|
2009-02-15 03:08:58 +08:00
|
|
|
|
2008-11-24 11:33:13 +08:00
|
|
|
if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true,
|
2008-09-11 08:01:56 +08:00
|
|
|
lbrac, rbrac, returnType))
|
|
|
|
return true;
|
2008-06-24 23:50:53 +08:00
|
|
|
|
|
|
|
// If we have the ObjCInterfaceDecl* for the class that is receiving
|
|
|
|
// the message, use that to construct the ObjCMessageExpr. Otherwise
|
|
|
|
// pass on the IdentifierInfo* for the class.
|
2008-07-25 03:44:33 +08:00
|
|
|
// FIXME: need to do a better job handling 'super' usage within a class
|
|
|
|
// For now, we simply pass the "super" identifier through (which isn't
|
|
|
|
// consistent with instance methods.
|
2008-07-26 03:39:00 +08:00
|
|
|
if (isSuper)
|
2009-02-07 09:47:29 +08:00
|
|
|
return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method,
|
|
|
|
lbrac, rbrac, ArgExprs, NumArgs);
|
2008-06-24 23:50:53 +08:00
|
|
|
else
|
2009-02-07 09:47:29 +08:00
|
|
|
return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method,
|
|
|
|
lbrac, rbrac, ArgExprs, NumArgs);
|
2008-01-05 06:32:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActOnInstanceMessage - used for both unary and keyword messages.
|
|
|
|
// ArgExprs is optional - if it is present, the number of expressions
|
|
|
|
// is obtained from Sel.getNumArgs().
|
2008-07-21 14:31:05 +08:00
|
|
|
Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel,
|
2008-07-27 06:17:49 +08:00
|
|
|
SourceLocation lbrac,
|
2009-02-15 02:21:46 +08:00
|
|
|
SourceLocation receiverLoc,
|
2008-07-27 06:17:49 +08:00
|
|
|
SourceLocation rbrac,
|
|
|
|
ExprTy **Args, unsigned NumArgs) {
|
2008-01-05 06:32:30 +08:00
|
|
|
assert(receiver && "missing receiver expression");
|
|
|
|
|
|
|
|
Expr **ArgExprs = reinterpret_cast<Expr **>(Args);
|
|
|
|
Expr *RExpr = static_cast<Expr *>(receiver);
|
|
|
|
QualType returnType;
|
2008-05-31 10:19:15 +08:00
|
|
|
|
2008-07-27 06:17:49 +08:00
|
|
|
QualType ReceiverCType =
|
|
|
|
Context.getCanonicalType(RExpr->getType()).getUnqualifiedType();
|
2008-11-18 06:29:32 +08:00
|
|
|
|
|
|
|
// Handle messages to 'super'.
|
2009-02-23 23:40:48 +08:00
|
|
|
if (isa<ObjCSuperExpr>(RExpr)) {
|
2008-11-18 06:29:32 +08:00
|
|
|
ObjCMethodDecl *Method = 0;
|
|
|
|
if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
|
|
|
|
// If we have an interface in scope, check 'super' methods.
|
|
|
|
if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
|
|
|
|
if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass())
|
|
|
|
Method = SuperDecl->lookupInstanceMethod(Sel);
|
|
|
|
}
|
2009-02-15 02:21:46 +08:00
|
|
|
|
2009-02-19 05:56:37 +08:00
|
|
|
if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
|
|
|
|
return true;
|
2009-02-15 03:08:58 +08:00
|
|
|
|
2008-11-24 11:33:13 +08:00
|
|
|
if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
|
2008-11-18 06:29:32 +08:00
|
|
|
lbrac, rbrac, returnType))
|
|
|
|
return true;
|
2009-02-07 09:47:29 +08:00
|
|
|
return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
|
|
|
|
rbrac, ArgExprs, NumArgs);
|
2008-11-18 06:29:32 +08:00
|
|
|
}
|
|
|
|
|
2008-07-21 13:57:44 +08:00
|
|
|
// Handle messages to id.
|
2008-09-30 00:51:41 +08:00
|
|
|
if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) ||
|
|
|
|
ReceiverCType->getAsBlockPointerType()) {
|
2008-09-30 22:38:43 +08:00
|
|
|
ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(
|
|
|
|
Sel, SourceRange(lbrac,rbrac));
|
2008-02-01 14:57:39 +08:00
|
|
|
if (!Method)
|
|
|
|
Method = FactoryMethodPool[Sel].Method;
|
2008-11-24 11:33:13 +08:00
|
|
|
if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
|
2008-09-11 08:01:56 +08:00
|
|
|
lbrac, rbrac, returnType))
|
|
|
|
return true;
|
2009-02-07 09:47:29 +08:00
|
|
|
return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
|
|
|
|
rbrac, ArgExprs, NumArgs);
|
2008-07-21 13:57:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle messages to Class.
|
2008-07-27 06:17:49 +08:00
|
|
|
if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) {
|
2008-07-21 14:12:56 +08:00
|
|
|
ObjCMethodDecl *Method = 0;
|
2009-03-04 23:11:40 +08:00
|
|
|
|
2008-07-21 14:44:27 +08:00
|
|
|
if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
|
2009-02-23 10:25:40 +08:00
|
|
|
if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
|
|
|
|
// First check the public methods in the class interface.
|
|
|
|
Method = ClassDecl->lookupClassMethod(Sel);
|
|
|
|
|
2009-02-26 23:55:06 +08:00
|
|
|
if (!Method)
|
|
|
|
Method = LookupPrivateMethod(Sel, ClassDecl);
|
2009-03-05 01:50:39 +08:00
|
|
|
// Before we give up, check if the selector is an instance method.
|
|
|
|
// But only in the root. This matches gcc's behaviour and what the
|
|
|
|
// runtime expects.
|
|
|
|
if (!Method) {
|
|
|
|
ObjCInterfaceDecl *Root = ClassDecl;
|
|
|
|
while (Root->getSuperClass())
|
|
|
|
Root = Root->getSuperClass();
|
|
|
|
Method = Root->lookupInstanceMethod(Sel);
|
|
|
|
}
|
2009-02-23 10:25:40 +08:00
|
|
|
}
|
2009-02-19 05:56:37 +08:00
|
|
|
if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
|
|
|
|
return true;
|
2009-02-23 10:25:40 +08:00
|
|
|
}
|
2009-03-04 23:11:40 +08:00
|
|
|
if (!Method) {
|
|
|
|
// If not messaging 'self', look for any factory method named 'Sel'.
|
|
|
|
if (!isSelfExpr(RExpr)) {
|
|
|
|
Method = FactoryMethodPool[Sel].Method;
|
2009-03-05 01:50:39 +08:00
|
|
|
if (!Method) {
|
2009-03-04 23:11:40 +08:00
|
|
|
Method = LookupInstanceMethodInGlobalPool(
|
|
|
|
Sel, SourceRange(lbrac,rbrac));
|
2009-03-05 01:50:39 +08:00
|
|
|
if (Method)
|
|
|
|
Diag(receiverLoc, diag::warn_maynot_respond) << Sel;
|
|
|
|
}
|
2009-03-04 23:11:40 +08:00
|
|
|
}
|
|
|
|
}
|
2008-11-24 11:33:13 +08:00
|
|
|
if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
|
2008-09-11 08:01:56 +08:00
|
|
|
lbrac, rbrac, returnType))
|
|
|
|
return true;
|
2009-02-07 09:47:29 +08:00
|
|
|
return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
|
|
|
|
rbrac, ArgExprs, NumArgs);
|
2008-07-21 13:57:44 +08:00
|
|
|
}
|
|
|
|
|
2008-07-21 14:12:56 +08:00
|
|
|
ObjCMethodDecl *Method = 0;
|
2008-07-21 13:57:44 +08:00
|
|
|
ObjCInterfaceDecl* ClassDecl = 0;
|
2008-07-21 14:12:56 +08:00
|
|
|
|
|
|
|
// We allow sending a message to a qualified ID ("id<foo>"), which is ok as
|
|
|
|
// long as one of the protocols implements the selector (if not, warn).
|
2008-07-27 06:17:49 +08:00
|
|
|
if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) {
|
2009-02-22 05:17:01 +08:00
|
|
|
// Search protocols for instance methods.
|
2008-07-21 13:57:44 +08:00
|
|
|
for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
|
|
|
|
ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
|
|
|
|
if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel)))
|
|
|
|
break;
|
|
|
|
}
|
2009-02-22 05:17:01 +08:00
|
|
|
// Check for GCC extension "Class<foo>".
|
|
|
|
} else if (ObjCQualifiedClassType *QIT =
|
|
|
|
dyn_cast<ObjCQualifiedClassType>(ReceiverCType)) {
|
|
|
|
// Search protocols for class methods.
|
|
|
|
for (unsigned i = 0; i < QIT->getNumProtocols(); i++) {
|
|
|
|
ObjCProtocolDecl *PDecl = QIT->getProtocols(i);
|
|
|
|
if (PDecl && (Method = PDecl->lookupClassMethod(Sel)))
|
|
|
|
break;
|
|
|
|
}
|
2009-02-23 23:40:48 +08:00
|
|
|
} else if (const ObjCInterfaceType *OCIType =
|
2008-07-27 06:17:49 +08:00
|
|
|
ReceiverCType->getAsPointerToObjCInterfaceType()) {
|
2008-07-21 14:12:56 +08:00
|
|
|
// We allow sending a message to a pointer to an interface (an object).
|
2008-02-01 14:43:02 +08:00
|
|
|
|
2009-02-23 23:40:48 +08:00
|
|
|
ClassDecl = OCIType->getDecl();
|
2008-09-30 22:38:43 +08:00
|
|
|
// FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be
|
|
|
|
// faster than the following method (which can do *many* linear searches).
|
|
|
|
// The idea is to add class info to InstanceMethodPool.
|
2008-07-21 13:57:44 +08:00
|
|
|
Method = ClassDecl->lookupInstanceMethod(Sel);
|
|
|
|
|
|
|
|
if (!Method) {
|
|
|
|
// Search protocol qualifiers.
|
2009-02-23 23:40:48 +08:00
|
|
|
for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(),
|
|
|
|
E = OCIType->qual_end(); QI != E; ++QI) {
|
2008-07-21 13:57:44 +08:00
|
|
|
if ((Method = (*QI)->lookupInstanceMethod(Sel)))
|
2008-01-05 06:32:30 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-02-23 03:35:57 +08:00
|
|
|
if (!Method) {
|
|
|
|
// If we have an implementation in scope, check "private" methods.
|
2009-03-04 06:19:15 +08:00
|
|
|
if (ClassDecl) {
|
2009-02-23 03:35:57 +08:00
|
|
|
if (ObjCImplementationDecl *ImpDecl =
|
|
|
|
ObjCImplementations[ClassDecl->getIdentifier()])
|
|
|
|
Method = ImpDecl->getInstanceMethod(Sel);
|
2009-03-04 06:19:15 +08:00
|
|
|
// Look through local category implementations associated with the class.
|
|
|
|
if (!Method) {
|
|
|
|
for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) {
|
|
|
|
if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl)
|
|
|
|
Method = ObjCCategoryImpls[i]->getInstanceMethod(Sel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-04 23:11:40 +08:00
|
|
|
if (!isSelfExpr(RExpr)) {
|
|
|
|
// If we still haven't found a method, look in the global pool. This
|
|
|
|
// behavior isn't very desirable, however we need it for GCC
|
|
|
|
// compatibility. FIXME: should we deviate??
|
|
|
|
if (!Method && OCIType->qual_empty()) {
|
|
|
|
Method = LookupInstanceMethodInGlobalPool(
|
|
|
|
Sel, SourceRange(lbrac,rbrac));
|
|
|
|
if (Method && !OCIType->getDecl()->isForwardDecl())
|
|
|
|
Diag(lbrac, diag::warn_maynot_respond)
|
|
|
|
<< OCIType->getDecl()->getIdentifier()->getName() << Sel;
|
|
|
|
}
|
2009-03-04 06:19:15 +08:00
|
|
|
}
|
2009-02-23 03:35:57 +08:00
|
|
|
}
|
2009-02-19 05:56:37 +08:00
|
|
|
if (Method && DiagnoseUseOfDecl(Method, receiverLoc))
|
|
|
|
return true;
|
2008-07-21 14:12:56 +08:00
|
|
|
} else {
|
2009-03-02 01:14:31 +08:00
|
|
|
Diag(lbrac, diag::warn_bad_receiver_type)
|
2008-11-24 14:25:27 +08:00
|
|
|
<< RExpr->getType() << RExpr->getSourceRange();
|
2008-07-21 14:12:56 +08:00
|
|
|
return true;
|
2008-07-21 13:57:44 +08:00
|
|
|
}
|
|
|
|
|
2008-11-24 11:33:13 +08:00
|
|
|
if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false,
|
2008-09-11 08:01:56 +08:00
|
|
|
lbrac, rbrac, returnType))
|
|
|
|
return true;
|
2009-02-07 09:47:29 +08:00
|
|
|
return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac,
|
|
|
|
rbrac, ArgExprs, NumArgs);
|
2008-01-05 06:32:30 +08:00
|
|
|
}
|
2008-04-07 13:30:13 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
|
|
|
|
/// inheritance hierarchy of 'rProto'.
|
|
|
|
static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
|
|
|
|
ObjCProtocolDecl *rProto) {
|
|
|
|
if (lProto == rProto)
|
|
|
|
return true;
|
2008-07-22 05:32:27 +08:00
|
|
|
for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(),
|
|
|
|
E = rProto->protocol_end(); PI != E; ++PI)
|
|
|
|
if (ProtocolCompatibleWithProtocol(lProto, *PI))
|
2008-04-07 13:30:13 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ClassImplementsProtocol - Checks that 'lProto' protocol
|
|
|
|
/// has been implemented in IDecl class, its super class or categories (if
|
|
|
|
/// lookupCategory is true).
|
|
|
|
static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
|
|
|
|
ObjCInterfaceDecl *IDecl,
|
2008-06-05 03:00:03 +08:00
|
|
|
bool lookupCategory,
|
|
|
|
bool RHSIsQualifiedID = false) {
|
2008-04-07 13:30:13 +08:00
|
|
|
|
|
|
|
// 1st, look up the class.
|
2008-07-22 02:19:38 +08:00
|
|
|
const ObjCList<ObjCProtocolDecl> &Protocols =
|
|
|
|
IDecl->getReferencedProtocols();
|
|
|
|
|
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(),
|
|
|
|
E = Protocols.end(); PI != E; ++PI) {
|
|
|
|
if (ProtocolCompatibleWithProtocol(lProto, *PI))
|
2008-04-07 13:30:13 +08:00
|
|
|
return true;
|
2008-06-05 03:00:03 +08:00
|
|
|
// This is dubious and is added to be compatible with gcc.
|
|
|
|
// In gcc, it is also allowed assigning a protocol-qualified 'id'
|
|
|
|
// type to a LHS object when protocol in qualified LHS is in list
|
|
|
|
// of protocols in the rhs 'id' object. This IMO, should be a bug.
|
2008-06-05 04:48:08 +08:00
|
|
|
// FIXME: Treat this as an extension, and flag this as an error when
|
|
|
|
// GCC extensions are not enabled.
|
2008-07-22 02:19:38 +08:00
|
|
|
if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto))
|
2008-06-05 03:00:03 +08:00
|
|
|
return true;
|
2008-04-07 13:30:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 2nd, look up the category.
|
|
|
|
if (lookupCategory)
|
|
|
|
for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
|
|
|
|
CDecl = CDecl->getNextClassCategory()) {
|
2008-07-22 05:32:27 +08:00
|
|
|
for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(),
|
|
|
|
E = CDecl->protocol_end(); PI != E; ++PI)
|
|
|
|
if (ProtocolCompatibleWithProtocol(lProto, *PI))
|
2008-04-07 13:30:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3rd, look up the super class(s)
|
|
|
|
if (IDecl->getSuperClass())
|
|
|
|
return
|
2008-06-05 03:00:03 +08:00
|
|
|
ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory,
|
|
|
|
RHSIsQualifiedID);
|
2008-04-07 13:30:13 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-20 10:09:31 +08:00
|
|
|
/// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an
|
|
|
|
/// ObjCQualifiedIDType.
|
2009-03-03 23:43:24 +08:00
|
|
|
/// FIXME: Move to ASTContext::typesAreCompatible() and friends.
|
2008-04-07 13:30:13 +08:00
|
|
|
bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs,
|
|
|
|
bool compare) {
|
|
|
|
// Allow id<P..> and an 'id' or void* type in all cases.
|
|
|
|
if (const PointerType *PT = lhs->getAsPointerType()) {
|
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
2009-02-13 01:52:19 +08:00
|
|
|
if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
|
2008-04-07 13:30:13 +08:00
|
|
|
return true;
|
|
|
|
} else if (const PointerType *PT = rhs->getAsPointerType()) {
|
|
|
|
QualType PointeeTy = PT->getPointeeType();
|
2009-02-13 01:52:19 +08:00
|
|
|
if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType())
|
2008-04-07 13:30:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-20 10:09:31 +08:00
|
|
|
if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) {
|
|
|
|
const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
|
|
|
|
const ObjCQualifiedInterfaceType *rhsQI = 0;
|
2008-06-01 10:43:50 +08:00
|
|
|
QualType rtype;
|
|
|
|
|
2008-04-20 10:09:31 +08:00
|
|
|
if (!rhsQID) {
|
|
|
|
// Not comparing two ObjCQualifiedIdType's?
|
|
|
|
if (!rhs->isPointerType()) return false;
|
2008-06-01 10:43:50 +08:00
|
|
|
|
|
|
|
rtype = rhs->getAsPointerType()->getPointeeType();
|
2008-04-07 13:30:13 +08:00
|
|
|
rhsQI = rtype->getAsObjCQualifiedInterfaceType();
|
2008-04-20 10:09:31 +08:00
|
|
|
if (rhsQI == 0) {
|
2008-06-01 10:43:50 +08:00
|
|
|
// If the RHS is a unqualified interface pointer "NSString*",
|
|
|
|
// make sure we check the class hierarchy.
|
2008-04-20 10:09:31 +08:00
|
|
|
if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
|
|
|
|
ObjCInterfaceDecl *rhsID = IT->getDecl();
|
|
|
|
for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
|
|
|
|
// when comparing an id<P> on lhs with a static type on rhs,
|
|
|
|
// see if static class implements all of id's protocols, directly or
|
|
|
|
// through its super class and categories.
|
|
|
|
if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-04-07 13:30:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE;
|
2008-06-01 10:43:50 +08:00
|
|
|
if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *").
|
2008-04-07 13:30:13 +08:00
|
|
|
RHSProtoI = rhsQI->qual_begin();
|
|
|
|
RHSProtoE = rhsQI->qual_end();
|
2008-06-01 10:43:50 +08:00
|
|
|
} else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *").
|
2008-04-07 13:30:13 +08:00
|
|
|
RHSProtoI = rhsQID->qual_begin();
|
|
|
|
RHSProtoE = rhsQID->qual_end();
|
2008-04-20 10:09:31 +08:00
|
|
|
} else {
|
|
|
|
return false;
|
2008-04-07 13:30:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
|
|
|
|
ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
|
|
|
|
bool match = false;
|
|
|
|
|
|
|
|
// when comparing an id<P> on lhs with a static type on rhs,
|
|
|
|
// see if static class implements all of id's protocols, directly or
|
|
|
|
// through its super class and categories.
|
2008-04-20 10:09:31 +08:00
|
|
|
for (; RHSProtoI != RHSProtoE; ++RHSProtoI) {
|
|
|
|
ObjCProtocolDecl *rhsProto = *RHSProtoI;
|
|
|
|
if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
|
2008-12-17 04:15:50 +08:00
|
|
|
(compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
|
2008-04-07 13:30:13 +08:00
|
|
|
match = true;
|
2008-04-20 10:09:31 +08:00
|
|
|
break;
|
2008-04-07 13:30:13 +08:00
|
|
|
}
|
|
|
|
}
|
2008-06-01 10:43:50 +08:00
|
|
|
if (rhsQI) {
|
|
|
|
// If the RHS is a qualified interface pointer "NSString<P>*",
|
|
|
|
// make sure we check the class hierarchy.
|
|
|
|
if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) {
|
|
|
|
ObjCInterfaceDecl *rhsID = IT->getDecl();
|
|
|
|
for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) {
|
|
|
|
// when comparing an id<P> on lhs with a static type on rhs,
|
|
|
|
// see if static class implements all of id's protocols, directly or
|
|
|
|
// through its super class and categories.
|
|
|
|
if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-04-07 13:30:13 +08:00
|
|
|
if (!match)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-04-20 10:09:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType();
|
|
|
|
assert(rhsQID && "One of the LHS/RHS should be id<x>");
|
2008-04-07 13:30:13 +08:00
|
|
|
|
2008-04-20 10:09:31 +08:00
|
|
|
if (!lhs->isPointerType())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QualType ltype = lhs->getAsPointerType()->getPointeeType();
|
|
|
|
if (const ObjCQualifiedInterfaceType *lhsQI =
|
|
|
|
ltype->getAsObjCQualifiedInterfaceType()) {
|
|
|
|
ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin();
|
|
|
|
ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end();
|
|
|
|
for (; LHSProtoI != LHSProtoE; ++LHSProtoI) {
|
|
|
|
bool match = false;
|
|
|
|
ObjCProtocolDecl *lhsProto = *LHSProtoI;
|
2008-04-07 13:30:13 +08:00
|
|
|
for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
|
|
|
|
ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
|
2008-04-20 10:09:31 +08:00
|
|
|
if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
|
2008-12-17 04:15:50 +08:00
|
|
|
(compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) {
|
2008-04-07 13:30:13 +08:00
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-04-20 10:09:31 +08:00
|
|
|
if (!match)
|
|
|
|
return false;
|
2008-04-07 13:30:13 +08:00
|
|
|
}
|
2008-04-20 10:09:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) {
|
|
|
|
// for static type vs. qualified 'id' type, check that class implements
|
|
|
|
// all of 'id's protocols.
|
|
|
|
ObjCInterfaceDecl *lhsID = IT->getDecl();
|
|
|
|
for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
|
|
|
|
ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
|
2008-06-05 03:00:03 +08:00
|
|
|
if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true))
|
2008-04-20 10:09:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2008-04-07 13:30:13 +08:00
|
|
|
}
|
2008-04-20 10:09:31 +08:00
|
|
|
return false;
|
2008-04-07 13:30:13 +08:00
|
|
|
}
|
|
|
|
|