#include "clang/Sema/SemaInternal.h"
#include "clang/Sema/Lookup.h"
#include "clang/Sema/Scope.h"
#include "clang/Sema/ScopeInfo.h"
#include "clang/Sema/Initialization.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/ExprObjC.h"
#include "clang/AST/TypeLoc.h"
#include "llvm/ADT/SmallString.h"
#include "clang/Lex/Preprocessor.h"
using namespace clang;
using namespace sema;
ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
Expr **strings,
unsigned NumStrings) {
StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings);
StringLiteral *S = Strings[0];
if (NumStrings != 1) {
llvm::SmallString<128> StrBuf;
llvm::SmallVector<SourceLocation, 8> StrLocs;
for (unsigned i = 0; i != NumStrings; ++i) {
S = Strings[i];
if (S->isWide()) {
Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant)
<< S->getSourceRange();
return true;
}
StrBuf += S->getString();
StrLocs.append(S->tokloc_begin(), S->tokloc_end());
}
S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(),
false, false,
Context.getPointerType(Context.CharTy),
&StrLocs[0], StrLocs.size());
}
if (CheckObjCString(S))
return true;
QualType Ty = Context.getObjCConstantStringInterface();
if (!Ty.isNull()) {
Ty = Context.getObjCObjectPointerType(Ty);
} else if (getLangOptions().NoConstantCFStrings) {
IdentifierInfo *NSIdent=0;
std::string StringClass(getLangOptions().ObjCConstantStringClass);
if (StringClass.empty())
NSIdent = &Context.Idents.get("NSConstantString");
else
NSIdent = &Context.Idents.get(StringClass);
NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
LookupOrdinaryName);
if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
Context.setObjCConstantStringInterface(StrIF);
Ty = Context.getObjCConstantStringInterface();
Ty = Context.getObjCObjectPointerType(Ty);
} else {
Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent
<< S->getSourceRange();
Ty = Context.getObjCIdType();
}
} else {
IdentifierInfo *NSIdent = &Context.Idents.get("NSString");
NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0],
LookupOrdinaryName);
if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
Context.setObjCConstantStringInterface(StrIF);
Ty = Context.getObjCConstantStringInterface();
Ty = Context.getObjCObjectPointerType(Ty);
} else {
Ty = Context.getObjCIdType();
}
}
return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]);
}
Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
TypeSourceInfo *EncodedTypeInfo,
SourceLocation RParenLoc) {
QualType EncodedType = EncodedTypeInfo->getType();
QualType StrTy;
if (EncodedType->isDependentType())
StrTy = Context.DependentTy;
else {
std::string Str;
Context.getObjCEncodingForType(EncodedType, Str);
StrTy = Context.CharTy;
if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings)
StrTy.addConst();
StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1),
ArrayType::Normal, 0);
}
return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
}
ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
SourceLocation EncodeLoc,
SourceLocation LParenLoc,
ParsedType ty,
SourceLocation RParenLoc) {
TypeSourceInfo *TInfo;
QualType EncodedType = GetTypeFromParser(ty, &TInfo);
if (!TInfo)
TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
PP.getLocForEndOfToken(LParenLoc));
return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
}
ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
SourceLocation AtLoc,
SourceLocation SelLoc,
SourceLocation LParenLoc,
SourceLocation RParenLoc) {
ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
SourceRange(LParenLoc, RParenLoc), false, false);
if (!Method)
Method = LookupFactoryMethodInGlobalPool(Sel,
SourceRange(LParenLoc, RParenLoc));
if (!Method)
Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
llvm::DenseMap<Selector, SourceLocation>::iterator Pos
= ReferencedSelectors.find(Sel);
if (Pos == ReferencedSelectors.end())
ReferencedSelectors.insert(std::make_pair(Sel, SelLoc));
QualType Ty = Context.getObjCSelType();
return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
}
ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
SourceLocation AtLoc,
SourceLocation ProtoLoc,
SourceLocation LParenLoc,
SourceLocation RParenLoc) {
ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc);
if (!PDecl) {
Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
return true;
}
QualType Ty = Context.getObjCProtoType();
if (Ty.isNull())
return true;
Ty = Context.getObjCObjectPointerType(Ty);
return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc);
}
ObjCMethodDecl *Sema::tryCaptureObjCSelf() {
DeclContext *DC = CurContext;
while (true) {
if (isa<BlockDecl>(DC)) DC = cast<BlockDecl>(DC)->getDeclContext();
else if (isa<EnumDecl>(DC)) DC = cast<EnumDecl>(DC)->getDeclContext();
else break;
}
ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC);
if (!method)
return 0;
ImplicitParamDecl *self = method->getSelfDecl();
assert(self && "capturing 'self' in non-definition?");
for (unsigned idx = FunctionScopes.size() - 1;
isa<BlockScopeInfo>(FunctionScopes[idx]);
--idx) {
BlockScopeInfo *blockScope = cast<BlockScopeInfo>(FunctionScopes[idx]);
unsigned &captureIndex = blockScope->CaptureMap[self];
if (captureIndex) break;
bool nested = isa<BlockScopeInfo>(FunctionScopes[idx-1]);
blockScope->Captures.push_back(
BlockDecl::Capture(self, false, nested, 0));
captureIndex = blockScope->Captures.size(); }
return method;
}
bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs,
Selector Sel, ObjCMethodDecl *Method,
bool isClassMessage,
SourceLocation lbrac, SourceLocation rbrac,
QualType &ReturnType, ExprValueKind &VK) {
if (!Method) {
for (unsigned i = 0; i != NumArgs; i++) {
if (Args[i]->isTypeDependent())
continue;
ExprResult Result = DefaultArgumentPromotion(Args[i]);
if (Result.isInvalid())
return true;
Args[i] = Result.take();
}
unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found :
diag::warn_inst_method_not_found;
Diag(lbrac, DiagID)
<< Sel << isClassMessage << SourceRange(lbrac, rbrac);
ReturnType = Context.getObjCIdType();
VK = VK_RValue;
return false;
}
ReturnType = Method->getSendResultType();
VK = Expr::getValueKindForType(Method->getResultType());
unsigned NumNamedArgs = Sel.getNumArgs();
if (Method->param_size() > Sel.getNumArgs())
NumNamedArgs = Method->param_size();
if (NumArgs < NumNamedArgs) {
Diag(lbrac, diag::err_typecheck_call_too_few_args)
<< 2 << NumNamedArgs << NumArgs;
return false;
}
bool IsError = false;
for (unsigned i = 0; i < NumNamedArgs; i++) {
if (Args[i]->isTypeDependent())
continue;
Expr *argExpr = Args[i];
ParmVarDecl *Param = Method->param_begin()[i];
assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
Param->getType(),
PDiag(diag::err_call_incomplete_argument)
<< argExpr->getSourceRange()))
return true;
InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
Param);
ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr));
if (ArgE.isInvalid())
IsError = true;
else
Args[i] = ArgE.takeAs<Expr>();
}
if (Method->isVariadic()) {
for (unsigned i = NumNamedArgs; i < NumArgs; ++i) {
if (Args[i]->isTypeDependent())
continue;
ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0);
IsError |= Arg.isInvalid();
Args[i] = Arg.take();
}
} else {
if (NumArgs != NumNamedArgs) {
Diag(Args[NumNamedArgs]->getLocStart(),
diag::err_typecheck_call_too_many_args)
<< 2 << NumNamedArgs << NumArgs
<< Method->getSourceRange()
<< SourceRange(Args[NumNamedArgs]->getLocStart(),
Args[NumArgs-1]->getLocEnd());
}
}
for (specific_attr_iterator<NonNullAttr>
i = Method->specific_attr_begin<NonNullAttr>(),
e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) {
CheckNonNullArguments(*i, Args, lbrac);
}
DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs);
return IsError;
}
bool Sema::isSelfExpr(Expr *RExpr) {
DeclContext *DC = CurContext;
while (isa<BlockDecl>(DC))
DC = DC->getParent();
if (DC && !isa<ObjCMethodDecl>(DC))
return false;
if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RExpr))
if (ICE->getCastKind() == CK_LValueToRValue)
RExpr = ICE->getSubExpr();
if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr))
if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self"))
return true;
return false;
}
ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel,
ObjCInterfaceDecl *ClassDecl) {
ObjCMethodDecl *Method = 0;
while (ClassDecl && !Method) {
if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Method = ImpDecl->getClassMethod(Sel);
if (!Method)
Method = ClassDecl->getCategoryClassMethod(Sel);
if (!Method && !ClassDecl->getSuperClass()) {
Method = ClassDecl->lookupInstanceMethod(Sel);
if (!Method)
Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
}
ClassDecl = ClassDecl->getSuperClass();
}
return Method;
}
ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel,
ObjCInterfaceDecl *ClassDecl) {
ObjCMethodDecl *Method = 0;
while (ClassDecl && !Method) {
if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Method = ImpDecl->getInstanceMethod(Sel);
if (!Method)
Method = ClassDecl->getCategoryInstanceMethod(Sel);
ClassDecl = ClassDecl->getSuperClass();
}
return Method;
}
ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
const ObjCObjectPointerType *OPT,
bool Instance)
{
ObjCMethodDecl *MD = 0;
for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
E = OPT->qual_end(); I != E; ++I) {
ObjCProtocolDecl *PROTO = (*I);
if ((MD = PROTO->lookupMethod(Sel, Instance))) {
return MD;
}
}
return 0;
}
ExprResult Sema::
HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
Expr *BaseExpr, DeclarationName MemberName,
SourceLocation MemberLoc,
SourceLocation SuperLoc, QualType SuperType,
bool Super) {
const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
ObjCInterfaceDecl *IFace = IFaceT->getDecl();
if (MemberName.getNameKind() != DeclarationName::Identifier) {
Diag(MemberLoc, diag::err_invalid_property_name)
<< MemberName << QualType(OPT, 0);
return ExprError();
}
IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
if (IFace->isForwardDecl()) {
Diag(MemberLoc, diag::err_property_not_found_forward_class)
<< MemberName << QualType(OPT, 0);
Diag(IFace->getLocation(), diag::note_forward_class);
return ExprError();
}
if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) {
if (DiagnoseUseOfDecl(PD, MemberLoc))
return ExprError();
QualType ResTy = PD->getType();
ResTy = ResTy.getNonLValueExprType(Context);
Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))
ResTy = Getter->getResultType();
if (Super)
return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
VK_LValue, OK_ObjCProperty,
MemberLoc,
SuperLoc, SuperType));
else
return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy,
VK_LValue, OK_ObjCProperty,
MemberLoc, BaseExpr));
}
for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(),
E = OPT->qual_end(); I != E; ++I)
if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) {
if (DiagnoseUseOfDecl(PD, MemberLoc))
return ExprError();
if (Super)
return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
VK_LValue,
OK_ObjCProperty,
MemberLoc,
SuperLoc, SuperType));
else
return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(),
VK_LValue,
OK_ObjCProperty,
MemberLoc,
BaseExpr));
}
Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
if (!Getter)
Getter = LookupMethodInQualifiedType(Sel, OPT, true);
if (!Getter)
Getter = IFace->lookupPrivateMethod(Sel);
if (!Getter)
Getter = IFace->getCategoryInstanceMethod(Sel);
if (Getter) {
if (DiagnoseUseOfDecl(Getter, MemberLoc))
return ExprError();
}
Selector SetterSel =
SelectorTable::constructSetterName(PP.getIdentifierTable(),
PP.getSelectorTable(), Member);
ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
if (!Setter)
Setter = LookupMethodInQualifiedType(SetterSel, OPT, true);
if (!Setter) {
Setter = IFace->lookupPrivateMethod(SetterSel);
}
if (!Setter)
Setter = IFace->getCategoryInstanceMethod(SetterSel);
if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
return ExprError();
if (Getter || Setter) {
QualType PType;
if (Getter)
PType = Getter->getSendResultType();
else {
ParmVarDecl *ArgDecl = *Setter->param_begin();
PType = ArgDecl->getType();
}
ExprValueKind VK = VK_LValue;
ExprObjectKind OK = OK_ObjCProperty;
if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() &&
PType->isVoidType())
VK = VK_RValue, OK = OK_Ordinary;
if (Super)
return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
PType, VK, OK,
MemberLoc,
SuperLoc, SuperType));
else
return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
PType, VK, OK,
MemberLoc, BaseExpr));
}
LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName);
if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) &&
Res.getAsSingle<ObjCPropertyDecl>()) {
DeclarationName TypoResult = Res.getLookupName();
Diag(MemberLoc, diag::err_property_not_found_suggest)
<< MemberName << QualType(OPT, 0) << TypoResult
<< FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString());
ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>();
Diag(Property->getLocation(), diag::note_previous_decl)
<< Property->getDeclName();
return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc,
SuperLoc, SuperType, Super);
}
ObjCInterfaceDecl *ClassDeclared;
if (ObjCIvarDecl *Ivar =
IFace->lookupInstanceVariable(Member, ClassDeclared)) {
QualType T = Ivar->getType();
if (const ObjCObjectPointerType * OBJPT =
T->getAsObjCInterfacePointerType()) {
const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType();
if (ObjCInterfaceDecl *IFace = IFaceT->getDecl())
if (IFace->isForwardDecl()) {
Diag(MemberLoc, diag::err_property_not_as_forward_class)
<< MemberName << IFace;
Diag(IFace->getLocation(), diag::note_forward_class);
return ExprError();
}
}
}
Diag(MemberLoc, diag::err_property_not_found)
<< MemberName << QualType(OPT, 0);
if (Setter)
Diag(Setter->getLocation(), diag::note_getter_unavailable)
<< MemberName << BaseExpr->getSourceRange();
return ExprError();
}
ExprResult Sema::
ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
IdentifierInfo &propertyName,
SourceLocation receiverNameLoc,
SourceLocation propertyNameLoc) {
IdentifierInfo *receiverNamePtr = &receiverName;
ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
receiverNameLoc);
if (IFace == 0) {
if (receiverNamePtr->isStr("super")) {
if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) {
if (CurMethod->isInstanceMethod()) {
QualType T =
Context.getObjCInterfaceType(CurMethod->getClassInterface());
T = Context.getObjCObjectPointerType(T);
return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(),
0, &propertyName,
propertyNameLoc,
receiverNameLoc, T, true);
}
IFace = CurMethod->getClassInterface()->getSuperClass();
}
}
if (IFace == 0) {
Diag(receiverNameLoc, diag::err_expected_ident_or_lparen);
return ExprError();
}
}
Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName);
ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel);
if (!Getter)
if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Getter = ImpDecl->getClassMethod(Sel);
if (Getter) {
if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
return ExprError();
}
Selector SetterSel =
SelectorTable::constructSetterName(PP.getIdentifierTable(),
PP.getSelectorTable(), &propertyName);
ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
if (!Setter) {
if (ObjCMethodDecl *CurMeth = getCurMethodDecl())
if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface())
if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation())
Setter = ImpDecl->getClassMethod(SetterSel);
}
if (!Setter)
Setter = IFace->getCategoryClassMethod(SetterSel);
if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
return ExprError();
if (Getter || Setter) {
QualType PType;
ExprValueKind VK = VK_LValue;
if (Getter) {
PType = Getter->getSendResultType();
if (!getLangOptions().CPlusPlus &&
!PType.hasQualifiers() && PType->isVoidType())
VK = VK_RValue;
} else {
for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(),
E = Setter->param_end(); PI != E; ++PI)
PType = (*PI)->getType();
VK = VK_LValue;
}
ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty);
return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter,
PType, VK, OK,
propertyNameLoc,
receiverNameLoc, IFace));
}
return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
<< &propertyName << Context.getObjCInterfaceType(IFace));
}
Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
IdentifierInfo *Name,
SourceLocation NameLoc,
bool IsSuper,
bool HasTrailingDot,
ParsedType &ReceiverType) {
ReceiverType = ParsedType();
if (IsSuper && S->isInObjcMethodScope())
return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
LookupName(Result, S);
switch (Result.getResultKind()) {
case LookupResult::NotFound:
if (ObjCMethodDecl *Method = getCurMethodDecl()) {
ObjCInterfaceDecl *ClassDeclared;
if (Method->getClassInterface()->lookupInstanceVariable(Name,
ClassDeclared))
return ObjCInstanceMessage;
}
break;
case LookupResult::NotFoundInCurrentInstantiation:
case LookupResult::FoundOverloaded:
case LookupResult::FoundUnresolvedValue:
case LookupResult::Ambiguous:
Result.suppressDiagnostics();
return ObjCInstanceMessage;
case LookupResult::Found: {
if (HasTrailingDot)
return ObjCInstanceMessage;
NamedDecl *ND = Result.getFoundDecl();
QualType T;
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
T = Context.getObjCInterfaceType(Class);
else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND))
T = Context.getTypeDeclType(Type);
else
return ObjCInstanceMessage;
TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
ReceiverType = CreateParsedType(T, TSInfo);
return ObjCClassMessage;
}
}
CorrectTypoContext CTC = CTC_Expression;
if (ObjCMethodDecl *Method = getCurMethodDecl())
if (Method->getClassInterface() &&
Method->getClassInterface()->getSuperClass())
CTC = CTC_ObjCMessageReceiver;
if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) {
if (Result.isSingleResult()) {
NamedDecl *ND = Result.getFoundDecl();
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) {
Diag(NameLoc, diag::err_unknown_receiver_suggest)
<< Name << Result.getLookupName()
<< FixItHint::CreateReplacement(SourceRange(NameLoc),
ND->getNameAsString());
Diag(ND->getLocation(), diag::note_previous_decl)
<< Corrected;
QualType T = Context.getObjCInterfaceType(Class);
TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
ReceiverType = CreateParsedType(T, TSInfo);
return ObjCClassMessage;
}
} else if (Result.empty() && Corrected.getAsIdentifierInfo() &&
Corrected.getAsIdentifierInfo()->isStr("super")) {
Diag(NameLoc, diag::err_unknown_receiver_suggest)
<< Name << Corrected
<< FixItHint::CreateReplacement(SourceRange(NameLoc), "super");
return ObjCSuperMessage;
}
}
return ObjCInstanceMessage;
}
ExprResult Sema::ActOnSuperMessage(Scope *S,
SourceLocation SuperLoc,
Selector Sel,
SourceLocation LBracLoc,
SourceLocation SelectorLoc,
SourceLocation RBracLoc,
MultiExprArg Args) {
ObjCMethodDecl *Method = tryCaptureObjCSelf();
if (!Method) {
Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
return ExprError();
}
ObjCInterfaceDecl *Class = Method->getClassInterface();
if (!Class) {
Diag(SuperLoc, diag::error_no_super_class_message)
<< Method->getDeclName();
return ExprError();
}
ObjCInterfaceDecl *Super = Class->getSuperClass();
if (!Super) {
Diag(SuperLoc, diag::error_root_class_cannot_use_super)
<< Class->getIdentifier();
return ExprError();
}
if (Method->isInstanceMethod()) {
QualType SuperTy = Context.getObjCInterfaceType(Super);
SuperTy = Context.getObjCObjectPointerType(SuperTy);
return BuildInstanceMessage(0, SuperTy, SuperLoc,
Sel, 0,
LBracLoc, SelectorLoc, RBracLoc, move(Args));
}
return BuildClassMessage(0,
Context.getObjCInterfaceType(Super),
SuperLoc, Sel, 0,
LBracLoc, SelectorLoc, RBracLoc, move(Args));
}
ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
QualType ReceiverType,
SourceLocation SuperLoc,
Selector Sel,
ObjCMethodDecl *Method,
SourceLocation LBracLoc,
SourceLocation SelectorLoc,
SourceLocation RBracLoc,
MultiExprArg ArgsIn) {
SourceLocation Loc = SuperLoc.isValid()? SuperLoc
: ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
if (LBracLoc.isInvalid()) {
Diag(Loc, diag::err_missing_open_square_message_send)
<< FixItHint::CreateInsertion(Loc, "[");
LBracLoc = Loc;
}
if (ReceiverType->isDependentType()) {
unsigned NumArgs = ArgsIn.size();
Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
assert(SuperLoc.isInvalid() && "Message to super with dependent type");
return Owned(ObjCMessageExpr::Create(Context, ReceiverType,
VK_RValue, LBracLoc, ReceiverTypeInfo,
Sel, SelectorLoc, 0,
Args, NumArgs, RBracLoc));
}
ObjCInterfaceDecl *Class = 0;
const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
if (!ClassType || !(Class = ClassType->getInterface())) {
Diag(Loc, diag::err_invalid_receiver_class_message)
<< ReceiverType;
return ExprError();
}
assert(Class && "We don't know which class we're messaging?");
(void)DiagnoseUseOfDecl(Class, Loc);
if (!Method) {
if (Class->isForwardDecl()) {
Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName();
Method = LookupFactoryMethodInGlobalPool(Sel,
SourceRange(LBracLoc, RBracLoc));
if (Method)
Diag(Method->getLocation(), diag::note_method_sent_forward_class)
<< Method->getDeclName();
}
if (!Method)
Method = Class->lookupClassMethod(Sel);
if (!Method)
Method = LookupPrivateClassMethod(Sel, Class);
if (Method && DiagnoseUseOfDecl(Method, Loc))
return ExprError();
}
QualType ReturnType;
ExprValueKind VK = VK_RValue;
unsigned NumArgs = ArgsIn.size();
Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true,
LBracLoc, RBracLoc, ReturnType, VK))
return ExprError();
if (Method && !Method->getResultType()->isVoidType() &&
RequireCompleteType(LBracLoc, Method->getResultType(),
diag::err_illegal_message_expr_incomplete_type))
return ExprError();
Expr *Result;
if (SuperLoc.isValid())
Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
SuperLoc, false,
ReceiverType, Sel, SelectorLoc,
Method, Args, NumArgs, RBracLoc);
else
Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
ReceiverTypeInfo, Sel, SelectorLoc,
Method, Args, NumArgs, RBracLoc);
return MaybeBindToTemporary(Result);
}
ExprResult Sema::ActOnClassMessage(Scope *S,
ParsedType Receiver,
Selector Sel,
SourceLocation LBracLoc,
SourceLocation SelectorLoc,
SourceLocation RBracLoc,
MultiExprArg Args) {
TypeSourceInfo *ReceiverTypeInfo;
QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
if (ReceiverType.isNull())
return ExprError();
if (!ReceiverTypeInfo)
ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
SourceLocation(), Sel, 0,
LBracLoc, SelectorLoc, RBracLoc, move(Args));
}
ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
QualType ReceiverType,
SourceLocation SuperLoc,
Selector Sel,
ObjCMethodDecl *Method,
SourceLocation LBracLoc,
SourceLocation SelectorLoc,
SourceLocation RBracLoc,
MultiExprArg ArgsIn) {
SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart();
if (LBracLoc.isInvalid()) {
Diag(Loc, diag::err_missing_open_square_message_send)
<< FixItHint::CreateInsertion(Loc, "[");
LBracLoc = Loc;
}
if (Receiver) {
if (Receiver->isTypeDependent()) {
unsigned NumArgs = ArgsIn.size();
Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
assert(SuperLoc.isInvalid() && "Message to super with dependent type");
return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy,
VK_RValue, LBracLoc, Receiver, Sel,
SelectorLoc, 0,
Args, NumArgs, RBracLoc));
}
ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver);
if (Result.isInvalid())
return ExprError();
Receiver = Result.take();
ReceiverType = Receiver->getType();
}
if (!Method) {
bool receiverIsId = ReceiverType->isObjCIdType();
if (receiverIsId || ReceiverType->isBlockPointerType() ||
(Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
Method = LookupInstanceMethodInGlobalPool(Sel,
SourceRange(LBracLoc, RBracLoc),
receiverIsId);
if (!Method)
Method = LookupFactoryMethodInGlobalPool(Sel,
SourceRange(LBracLoc, RBracLoc),
receiverIsId);
} else if (ReceiverType->isObjCClassType() ||
ReceiverType->isObjCQualifiedClassType()) {
if (const ObjCObjectPointerType *QClassTy
= ReceiverType->getAsObjCQualifiedClassType()) {
Method = LookupMethodInQualifiedType(Sel, QClassTy, false);
if (!Method) {
Method = LookupMethodInQualifiedType(Sel, QClassTy, true);
if (Method) {
Diag(Loc, diag::warn_instance_method_on_class_found)
<< Method->getSelector() << Sel;
Diag(Method->getLocation(), diag::note_method_declared_at);
}
}
} else {
if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
Method = ClassDecl->lookupClassMethod(Sel);
if (!Method)
Method = LookupPrivateClassMethod(Sel, ClassDecl);
}
if (Method && DiagnoseUseOfDecl(Method, Loc))
return ExprError();
}
if (!Method) {
if (!Receiver || !isSelfExpr(Receiver)) {
Method = LookupFactoryMethodInGlobalPool(Sel,
SourceRange(LBracLoc, RBracLoc),
true);
if (!Method) {
Method = LookupInstanceMethodInGlobalPool(Sel,
SourceRange(LBracLoc, RBracLoc),
true);
if (Method)
if (const ObjCInterfaceDecl *ID =
dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
if (ID->getSuperClass())
Diag(Loc, diag::warn_root_inst_method_not_found)
<< Sel << SourceRange(LBracLoc, RBracLoc);
}
}
}
}
}
} else {
ObjCInterfaceDecl* ClassDecl = 0;
if (const ObjCObjectPointerType *QIdTy
= ReceiverType->getAsObjCQualifiedIdType()) {
Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
if (!Method)
Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
} else if (const ObjCObjectPointerType *OCIType
= ReceiverType->getAsObjCInterfacePointerType()) {
ClassDecl = OCIType->getInterfaceDecl();
Method = ClassDecl->lookupInstanceMethod(Sel);
if (!Method)
Method = LookupMethodInQualifiedType(Sel, OCIType, true);
const ObjCInterfaceDecl *forwardClass = 0;
if (!Method) {
Method = LookupPrivateInstanceMethod(Sel, ClassDecl);
if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
if (OCIType->qual_empty()) {
Method = LookupInstanceMethodInGlobalPool(Sel,
SourceRange(LBracLoc, RBracLoc));
if (OCIType->getInterfaceDecl()->isForwardDecl())
forwardClass = OCIType->getInterfaceDecl();
if (Method && !forwardClass)
Diag(Loc, diag::warn_maynot_respond)
<< OCIType->getInterfaceDecl()->getIdentifier() << Sel;
}
}
}
if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass))
return ExprError();
} else if (!Context.getObjCIdType().isNull() &&
(ReceiverType->isPointerType() ||
ReceiverType->isIntegerType())) {
Diag(Loc, diag::warn_bad_receiver_type)
<< ReceiverType
<< Receiver->getSourceRange();
if (ReceiverType->isPointerType())
Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
CK_BitCast).take();
else {
bool IsNull = Receiver->isNullPointerConstant(Context,
Expr::NPC_ValueDependentIsNull);
Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
IsNull ? CK_NullToPointer : CK_IntegralToPointer).take();
}
ReceiverType = Receiver->getType();
}
else {
ExprResult ReceiverRes;
if (getLangOptions().CPlusPlus)
ReceiverRes = PerformContextuallyConvertToObjCId(Receiver);
if (ReceiverRes.isUsable()) {
Receiver = ReceiverRes.take();
if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) {
Receiver = ICE->getSubExpr();
ReceiverType = Receiver->getType();
}
return BuildInstanceMessage(Receiver,
ReceiverType,
SuperLoc,
Sel,
Method,
LBracLoc,
SelectorLoc,
RBracLoc,
move(ArgsIn));
} else {
Diag(Loc, diag::err_bad_receiver_type)
<< ReceiverType << Receiver->getSourceRange();
return ExprError();
}
}
}
}
unsigned NumArgs = ArgsIn.size();
Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release());
QualType ReturnType;
ExprValueKind VK = VK_RValue;
bool ClassMessage = (ReceiverType->isObjCClassType() ||
ReceiverType->isObjCQualifiedClassType());
if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, ClassMessage,
LBracLoc, RBracLoc, ReturnType, VK))
return ExprError();
if (Method && !Method->getResultType()->isVoidType() &&
RequireCompleteType(LBracLoc, Method->getResultType(),
diag::err_illegal_message_expr_incomplete_type))
return ExprError();
Expr *Result;
if (SuperLoc.isValid())
Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
SuperLoc, true,
ReceiverType, Sel, SelectorLoc, Method,
Args, NumArgs, RBracLoc);
else
Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
Receiver, Sel, SelectorLoc, Method,
Args, NumArgs, RBracLoc);
return MaybeBindToTemporary(Result);
}
ExprResult Sema::ActOnInstanceMessage(Scope *S,
Expr *Receiver,
Selector Sel,
SourceLocation LBracLoc,
SourceLocation SelectorLoc,
SourceLocation RBracLoc,
MultiExprArg Args) {
if (!Receiver)
return ExprError();
return BuildInstanceMessage(Receiver, Receiver->getType(),
SourceLocation(), Sel, 0,
LBracLoc, SelectorLoc, RBracLoc, move(Args));
}