Home | History | Annotate | Line # | Download | only in Sema
      1      1.1  joerg //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===//
      2      1.1  joerg //
      3      1.1  joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4      1.1  joerg // See https://llvm.org/LICENSE.txt for license information.
      5      1.1  joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6      1.1  joerg //
      7      1.1  joerg //===----------------------------------------------------------------------===//
      8      1.1  joerg //
      9      1.1  joerg //  This file implements semantic analysis for Objective-C expressions.
     10      1.1  joerg //
     11      1.1  joerg //===----------------------------------------------------------------------===//
     12      1.1  joerg 
     13      1.1  joerg #include "clang/AST/ASTContext.h"
     14      1.1  joerg #include "clang/AST/DeclObjC.h"
     15      1.1  joerg #include "clang/AST/ExprObjC.h"
     16      1.1  joerg #include "clang/AST/StmtVisitor.h"
     17      1.1  joerg #include "clang/AST/TypeLoc.h"
     18      1.1  joerg #include "clang/Analysis/DomainSpecific/CocoaConventions.h"
     19  1.1.1.2  joerg #include "clang/Basic/Builtins.h"
     20      1.1  joerg #include "clang/Edit/Commit.h"
     21      1.1  joerg #include "clang/Edit/Rewriters.h"
     22      1.1  joerg #include "clang/Lex/Preprocessor.h"
     23      1.1  joerg #include "clang/Sema/Initialization.h"
     24      1.1  joerg #include "clang/Sema/Lookup.h"
     25      1.1  joerg #include "clang/Sema/Scope.h"
     26      1.1  joerg #include "clang/Sema/ScopeInfo.h"
     27  1.1.1.2  joerg #include "clang/Sema/SemaInternal.h"
     28      1.1  joerg #include "llvm/ADT/SmallString.h"
     29      1.1  joerg #include "llvm/Support/ConvertUTF.h"
     30      1.1  joerg 
     31      1.1  joerg using namespace clang;
     32      1.1  joerg using namespace sema;
     33      1.1  joerg using llvm::makeArrayRef;
     34      1.1  joerg 
     35      1.1  joerg ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs,
     36      1.1  joerg                                         ArrayRef<Expr *> Strings) {
     37      1.1  joerg   // Most ObjC strings are formed out of a single piece.  However, we *can*
     38      1.1  joerg   // have strings formed out of multiple @ strings with multiple pptokens in
     39      1.1  joerg   // each one, e.g. @"foo" "bar" @"baz" "qux"   which need to be turned into one
     40      1.1  joerg   // StringLiteral for ObjCStringLiteral to hold onto.
     41      1.1  joerg   StringLiteral *S = cast<StringLiteral>(Strings[0]);
     42      1.1  joerg 
     43      1.1  joerg   // If we have a multi-part string, merge it all together.
     44      1.1  joerg   if (Strings.size() != 1) {
     45      1.1  joerg     // Concatenate objc strings.
     46      1.1  joerg     SmallString<128> StrBuf;
     47      1.1  joerg     SmallVector<SourceLocation, 8> StrLocs;
     48      1.1  joerg 
     49      1.1  joerg     for (Expr *E : Strings) {
     50      1.1  joerg       S = cast<StringLiteral>(E);
     51      1.1  joerg 
     52      1.1  joerg       // ObjC strings can't be wide or UTF.
     53      1.1  joerg       if (!S->isAscii()) {
     54      1.1  joerg         Diag(S->getBeginLoc(), diag::err_cfstring_literal_not_string_constant)
     55      1.1  joerg             << S->getSourceRange();
     56      1.1  joerg         return true;
     57      1.1  joerg       }
     58      1.1  joerg 
     59      1.1  joerg       // Append the string.
     60      1.1  joerg       StrBuf += S->getString();
     61      1.1  joerg 
     62      1.1  joerg       // Get the locations of the string tokens.
     63      1.1  joerg       StrLocs.append(S->tokloc_begin(), S->tokloc_end());
     64      1.1  joerg     }
     65      1.1  joerg 
     66      1.1  joerg     // Create the aggregate string with the appropriate content and location
     67      1.1  joerg     // information.
     68      1.1  joerg     const ConstantArrayType *CAT = Context.getAsConstantArrayType(S->getType());
     69      1.1  joerg     assert(CAT && "String literal not of constant array type!");
     70      1.1  joerg     QualType StrTy = Context.getConstantArrayType(
     71      1.1  joerg         CAT->getElementType(), llvm::APInt(32, StrBuf.size() + 1), nullptr,
     72      1.1  joerg         CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers());
     73      1.1  joerg     S = StringLiteral::Create(Context, StrBuf, StringLiteral::Ascii,
     74      1.1  joerg                               /*Pascal=*/false, StrTy, &StrLocs[0],
     75      1.1  joerg                               StrLocs.size());
     76      1.1  joerg   }
     77      1.1  joerg 
     78      1.1  joerg   return BuildObjCStringLiteral(AtLocs[0], S);
     79      1.1  joerg }
     80      1.1  joerg 
     81      1.1  joerg ExprResult Sema::BuildObjCStringLiteral(SourceLocation AtLoc, StringLiteral *S){
     82      1.1  joerg   // Verify that this composite string is acceptable for ObjC strings.
     83      1.1  joerg   if (CheckObjCString(S))
     84      1.1  joerg     return true;
     85      1.1  joerg 
     86      1.1  joerg   // Initialize the constant string interface lazily. This assumes
     87      1.1  joerg   // the NSString interface is seen in this translation unit. Note: We
     88      1.1  joerg   // don't use NSConstantString, since the runtime team considers this
     89      1.1  joerg   // interface private (even though it appears in the header files).
     90      1.1  joerg   QualType Ty = Context.getObjCConstantStringInterface();
     91      1.1  joerg   if (!Ty.isNull()) {
     92      1.1  joerg     Ty = Context.getObjCObjectPointerType(Ty);
     93      1.1  joerg   } else if (getLangOpts().NoConstantCFStrings) {
     94      1.1  joerg     IdentifierInfo *NSIdent=nullptr;
     95      1.1  joerg     std::string StringClass(getLangOpts().ObjCConstantStringClass);
     96      1.1  joerg 
     97      1.1  joerg     if (StringClass.empty())
     98      1.1  joerg       NSIdent = &Context.Idents.get("NSConstantString");
     99      1.1  joerg     else
    100      1.1  joerg       NSIdent = &Context.Idents.get(StringClass);
    101      1.1  joerg 
    102      1.1  joerg     NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc,
    103      1.1  joerg                                      LookupOrdinaryName);
    104      1.1  joerg     if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
    105      1.1  joerg       Context.setObjCConstantStringInterface(StrIF);
    106      1.1  joerg       Ty = Context.getObjCConstantStringInterface();
    107      1.1  joerg       Ty = Context.getObjCObjectPointerType(Ty);
    108      1.1  joerg     } else {
    109      1.1  joerg       // If there is no NSConstantString interface defined then treat this
    110      1.1  joerg       // as error and recover from it.
    111      1.1  joerg       Diag(S->getBeginLoc(), diag::err_no_nsconstant_string_class)
    112      1.1  joerg           << NSIdent << S->getSourceRange();
    113      1.1  joerg       Ty = Context.getObjCIdType();
    114      1.1  joerg     }
    115      1.1  joerg   } else {
    116      1.1  joerg     IdentifierInfo *NSIdent = NSAPIObj->getNSClassId(NSAPI::ClassId_NSString);
    117      1.1  joerg     NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc,
    118      1.1  joerg                                      LookupOrdinaryName);
    119      1.1  joerg     if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) {
    120      1.1  joerg       Context.setObjCConstantStringInterface(StrIF);
    121      1.1  joerg       Ty = Context.getObjCConstantStringInterface();
    122      1.1  joerg       Ty = Context.getObjCObjectPointerType(Ty);
    123      1.1  joerg     } else {
    124      1.1  joerg       // If there is no NSString interface defined, implicitly declare
    125      1.1  joerg       // a @class NSString; and use that instead. This is to make sure
    126      1.1  joerg       // type of an NSString literal is represented correctly, instead of
    127      1.1  joerg       // being an 'id' type.
    128      1.1  joerg       Ty = Context.getObjCNSStringType();
    129      1.1  joerg       if (Ty.isNull()) {
    130      1.1  joerg         ObjCInterfaceDecl *NSStringIDecl =
    131      1.1  joerg           ObjCInterfaceDecl::Create (Context,
    132      1.1  joerg                                      Context.getTranslationUnitDecl(),
    133      1.1  joerg                                      SourceLocation(), NSIdent,
    134      1.1  joerg                                      nullptr, nullptr, SourceLocation());
    135      1.1  joerg         Ty = Context.getObjCInterfaceType(NSStringIDecl);
    136      1.1  joerg         Context.setObjCNSStringType(Ty);
    137      1.1  joerg       }
    138      1.1  joerg       Ty = Context.getObjCObjectPointerType(Ty);
    139      1.1  joerg     }
    140      1.1  joerg   }
    141      1.1  joerg 
    142      1.1  joerg   return new (Context) ObjCStringLiteral(S, Ty, AtLoc);
    143      1.1  joerg }
    144      1.1  joerg 
    145      1.1  joerg /// Emits an error if the given method does not exist, or if the return
    146      1.1  joerg /// type is not an Objective-C object.
    147      1.1  joerg static bool validateBoxingMethod(Sema &S, SourceLocation Loc,
    148      1.1  joerg                                  const ObjCInterfaceDecl *Class,
    149      1.1  joerg                                  Selector Sel, const ObjCMethodDecl *Method) {
    150      1.1  joerg   if (!Method) {
    151      1.1  joerg     // FIXME: Is there a better way to avoid quotes than using getName()?
    152      1.1  joerg     S.Diag(Loc, diag::err_undeclared_boxing_method) << Sel << Class->getName();
    153      1.1  joerg     return false;
    154      1.1  joerg   }
    155      1.1  joerg 
    156      1.1  joerg   // Make sure the return type is reasonable.
    157      1.1  joerg   QualType ReturnType = Method->getReturnType();
    158      1.1  joerg   if (!ReturnType->isObjCObjectPointerType()) {
    159      1.1  joerg     S.Diag(Loc, diag::err_objc_literal_method_sig)
    160      1.1  joerg       << Sel;
    161      1.1  joerg     S.Diag(Method->getLocation(), diag::note_objc_literal_method_return)
    162      1.1  joerg       << ReturnType;
    163      1.1  joerg     return false;
    164      1.1  joerg   }
    165      1.1  joerg 
    166      1.1  joerg   return true;
    167      1.1  joerg }
    168      1.1  joerg 
    169      1.1  joerg /// Maps ObjCLiteralKind to NSClassIdKindKind
    170      1.1  joerg static NSAPI::NSClassIdKindKind ClassKindFromLiteralKind(
    171      1.1  joerg                                             Sema::ObjCLiteralKind LiteralKind) {
    172      1.1  joerg   switch (LiteralKind) {
    173      1.1  joerg     case Sema::LK_Array:
    174      1.1  joerg       return NSAPI::ClassId_NSArray;
    175      1.1  joerg     case Sema::LK_Dictionary:
    176      1.1  joerg       return NSAPI::ClassId_NSDictionary;
    177      1.1  joerg     case Sema::LK_Numeric:
    178      1.1  joerg       return NSAPI::ClassId_NSNumber;
    179      1.1  joerg     case Sema::LK_String:
    180      1.1  joerg       return NSAPI::ClassId_NSString;
    181      1.1  joerg     case Sema::LK_Boxed:
    182      1.1  joerg       return NSAPI::ClassId_NSValue;
    183      1.1  joerg 
    184      1.1  joerg     // there is no corresponding matching
    185      1.1  joerg     // between LK_None/LK_Block and NSClassIdKindKind
    186      1.1  joerg     case Sema::LK_Block:
    187      1.1  joerg     case Sema::LK_None:
    188      1.1  joerg       break;
    189      1.1  joerg   }
    190      1.1  joerg   llvm_unreachable("LiteralKind can't be converted into a ClassKind");
    191      1.1  joerg }
    192      1.1  joerg 
    193      1.1  joerg /// Validates ObjCInterfaceDecl availability.
    194      1.1  joerg /// ObjCInterfaceDecl, used to create ObjC literals, should be defined
    195      1.1  joerg /// if clang not in a debugger mode.
    196      1.1  joerg static bool ValidateObjCLiteralInterfaceDecl(Sema &S, ObjCInterfaceDecl *Decl,
    197      1.1  joerg                                             SourceLocation Loc,
    198      1.1  joerg                                             Sema::ObjCLiteralKind LiteralKind) {
    199      1.1  joerg   if (!Decl) {
    200      1.1  joerg     NSAPI::NSClassIdKindKind Kind = ClassKindFromLiteralKind(LiteralKind);
    201      1.1  joerg     IdentifierInfo *II = S.NSAPIObj->getNSClassId(Kind);
    202      1.1  joerg     S.Diag(Loc, diag::err_undeclared_objc_literal_class)
    203      1.1  joerg       << II->getName() << LiteralKind;
    204      1.1  joerg     return false;
    205      1.1  joerg   } else if (!Decl->hasDefinition() && !S.getLangOpts().DebuggerObjCLiteral) {
    206      1.1  joerg     S.Diag(Loc, diag::err_undeclared_objc_literal_class)
    207      1.1  joerg       << Decl->getName() << LiteralKind;
    208      1.1  joerg     S.Diag(Decl->getLocation(), diag::note_forward_class);
    209      1.1  joerg     return false;
    210      1.1  joerg   }
    211      1.1  joerg 
    212      1.1  joerg   return true;
    213      1.1  joerg }
    214      1.1  joerg 
    215      1.1  joerg /// Looks up ObjCInterfaceDecl of a given NSClassIdKindKind.
    216      1.1  joerg /// Used to create ObjC literals, such as NSDictionary (@{}),
    217      1.1  joerg /// NSArray (@[]) and Boxed Expressions (@())
    218      1.1  joerg static ObjCInterfaceDecl *LookupObjCInterfaceDeclForLiteral(Sema &S,
    219      1.1  joerg                                             SourceLocation Loc,
    220      1.1  joerg                                             Sema::ObjCLiteralKind LiteralKind) {
    221      1.1  joerg   NSAPI::NSClassIdKindKind ClassKind = ClassKindFromLiteralKind(LiteralKind);
    222      1.1  joerg   IdentifierInfo *II = S.NSAPIObj->getNSClassId(ClassKind);
    223      1.1  joerg   NamedDecl *IF = S.LookupSingleName(S.TUScope, II, Loc,
    224      1.1  joerg                                      Sema::LookupOrdinaryName);
    225      1.1  joerg   ObjCInterfaceDecl *ID = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
    226      1.1  joerg   if (!ID && S.getLangOpts().DebuggerObjCLiteral) {
    227      1.1  joerg     ASTContext &Context = S.Context;
    228      1.1  joerg     TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
    229      1.1  joerg     ID = ObjCInterfaceDecl::Create (Context, TU, SourceLocation(), II,
    230      1.1  joerg                                     nullptr, nullptr, SourceLocation());
    231      1.1  joerg   }
    232      1.1  joerg 
    233      1.1  joerg   if (!ValidateObjCLiteralInterfaceDecl(S, ID, Loc, LiteralKind)) {
    234      1.1  joerg     ID = nullptr;
    235      1.1  joerg   }
    236      1.1  joerg 
    237      1.1  joerg   return ID;
    238      1.1  joerg }
    239      1.1  joerg 
    240      1.1  joerg /// Retrieve the NSNumber factory method that should be used to create
    241      1.1  joerg /// an Objective-C literal for the given type.
    242      1.1  joerg static ObjCMethodDecl *getNSNumberFactoryMethod(Sema &S, SourceLocation Loc,
    243      1.1  joerg                                                 QualType NumberType,
    244      1.1  joerg                                                 bool isLiteral = false,
    245      1.1  joerg                                                 SourceRange R = SourceRange()) {
    246      1.1  joerg   Optional<NSAPI::NSNumberLiteralMethodKind> Kind =
    247      1.1  joerg       S.NSAPIObj->getNSNumberFactoryMethodKind(NumberType);
    248      1.1  joerg 
    249      1.1  joerg   if (!Kind) {
    250      1.1  joerg     if (isLiteral) {
    251      1.1  joerg       S.Diag(Loc, diag::err_invalid_nsnumber_type)
    252      1.1  joerg         << NumberType << R;
    253      1.1  joerg     }
    254      1.1  joerg     return nullptr;
    255      1.1  joerg   }
    256      1.1  joerg 
    257      1.1  joerg   // If we already looked up this method, we're done.
    258      1.1  joerg   if (S.NSNumberLiteralMethods[*Kind])
    259      1.1  joerg     return S.NSNumberLiteralMethods[*Kind];
    260      1.1  joerg 
    261      1.1  joerg   Selector Sel = S.NSAPIObj->getNSNumberLiteralSelector(*Kind,
    262      1.1  joerg                                                         /*Instance=*/false);
    263      1.1  joerg 
    264      1.1  joerg   ASTContext &CX = S.Context;
    265      1.1  joerg 
    266      1.1  joerg   // Look up the NSNumber class, if we haven't done so already. It's cached
    267      1.1  joerg   // in the Sema instance.
    268      1.1  joerg   if (!S.NSNumberDecl) {
    269      1.1  joerg     S.NSNumberDecl = LookupObjCInterfaceDeclForLiteral(S, Loc,
    270      1.1  joerg                                                        Sema::LK_Numeric);
    271      1.1  joerg     if (!S.NSNumberDecl) {
    272      1.1  joerg       return nullptr;
    273      1.1  joerg     }
    274      1.1  joerg   }
    275      1.1  joerg 
    276      1.1  joerg   if (S.NSNumberPointer.isNull()) {
    277      1.1  joerg     // generate the pointer to NSNumber type.
    278      1.1  joerg     QualType NSNumberObject = CX.getObjCInterfaceType(S.NSNumberDecl);
    279      1.1  joerg     S.NSNumberPointer = CX.getObjCObjectPointerType(NSNumberObject);
    280      1.1  joerg   }
    281      1.1  joerg 
    282      1.1  joerg   // Look for the appropriate method within NSNumber.
    283      1.1  joerg   ObjCMethodDecl *Method = S.NSNumberDecl->lookupClassMethod(Sel);
    284      1.1  joerg   if (!Method && S.getLangOpts().DebuggerObjCLiteral) {
    285      1.1  joerg     // create a stub definition this NSNumber factory method.
    286      1.1  joerg     TypeSourceInfo *ReturnTInfo = nullptr;
    287      1.1  joerg     Method =
    288      1.1  joerg         ObjCMethodDecl::Create(CX, SourceLocation(), SourceLocation(), Sel,
    289      1.1  joerg                                S.NSNumberPointer, ReturnTInfo, S.NSNumberDecl,
    290      1.1  joerg                                /*isInstance=*/false, /*isVariadic=*/false,
    291      1.1  joerg                                /*isPropertyAccessor=*/false,
    292  1.1.1.2  joerg                                /*isSynthesizedAccessorStub=*/false,
    293      1.1  joerg                                /*isImplicitlyDeclared=*/true,
    294      1.1  joerg                                /*isDefined=*/false, ObjCMethodDecl::Required,
    295      1.1  joerg                                /*HasRelatedResultType=*/false);
    296      1.1  joerg     ParmVarDecl *value = ParmVarDecl::Create(S.Context, Method,
    297      1.1  joerg                                              SourceLocation(), SourceLocation(),
    298      1.1  joerg                                              &CX.Idents.get("value"),
    299      1.1  joerg                                              NumberType, /*TInfo=*/nullptr,
    300      1.1  joerg                                              SC_None, nullptr);
    301      1.1  joerg     Method->setMethodParams(S.Context, value, None);
    302      1.1  joerg   }
    303      1.1  joerg 
    304      1.1  joerg   if (!validateBoxingMethod(S, Loc, S.NSNumberDecl, Sel, Method))
    305      1.1  joerg     return nullptr;
    306      1.1  joerg 
    307      1.1  joerg   // Note: if the parameter type is out-of-line, we'll catch it later in the
    308      1.1  joerg   // implicit conversion.
    309      1.1  joerg 
    310      1.1  joerg   S.NSNumberLiteralMethods[*Kind] = Method;
    311      1.1  joerg   return Method;
    312      1.1  joerg }
    313      1.1  joerg 
    314      1.1  joerg /// BuildObjCNumericLiteral - builds an ObjCBoxedExpr AST node for the
    315      1.1  joerg /// numeric literal expression. Type of the expression will be "NSNumber *".
    316      1.1  joerg ExprResult Sema::BuildObjCNumericLiteral(SourceLocation AtLoc, Expr *Number) {
    317      1.1  joerg   // Determine the type of the literal.
    318      1.1  joerg   QualType NumberType = Number->getType();
    319      1.1  joerg   if (CharacterLiteral *Char = dyn_cast<CharacterLiteral>(Number)) {
    320      1.1  joerg     // In C, character literals have type 'int'. That's not the type we want
    321      1.1  joerg     // to use to determine the Objective-c literal kind.
    322      1.1  joerg     switch (Char->getKind()) {
    323      1.1  joerg     case CharacterLiteral::Ascii:
    324      1.1  joerg     case CharacterLiteral::UTF8:
    325      1.1  joerg       NumberType = Context.CharTy;
    326      1.1  joerg       break;
    327      1.1  joerg 
    328      1.1  joerg     case CharacterLiteral::Wide:
    329      1.1  joerg       NumberType = Context.getWideCharType();
    330      1.1  joerg       break;
    331      1.1  joerg 
    332      1.1  joerg     case CharacterLiteral::UTF16:
    333      1.1  joerg       NumberType = Context.Char16Ty;
    334      1.1  joerg       break;
    335      1.1  joerg 
    336      1.1  joerg     case CharacterLiteral::UTF32:
    337      1.1  joerg       NumberType = Context.Char32Ty;
    338      1.1  joerg       break;
    339      1.1  joerg     }
    340      1.1  joerg   }
    341      1.1  joerg 
    342      1.1  joerg   // Look for the appropriate method within NSNumber.
    343      1.1  joerg   // Construct the literal.
    344      1.1  joerg   SourceRange NR(Number->getSourceRange());
    345      1.1  joerg   ObjCMethodDecl *Method = getNSNumberFactoryMethod(*this, AtLoc, NumberType,
    346      1.1  joerg                                                     true, NR);
    347      1.1  joerg   if (!Method)
    348      1.1  joerg     return ExprError();
    349      1.1  joerg 
    350      1.1  joerg   // Convert the number to the type that the parameter expects.
    351      1.1  joerg   ParmVarDecl *ParamDecl = Method->parameters()[0];
    352      1.1  joerg   InitializedEntity Entity = InitializedEntity::InitializeParameter(Context,
    353      1.1  joerg                                                                     ParamDecl);
    354      1.1  joerg   ExprResult ConvertedNumber = PerformCopyInitialization(Entity,
    355      1.1  joerg                                                          SourceLocation(),
    356      1.1  joerg                                                          Number);
    357      1.1  joerg   if (ConvertedNumber.isInvalid())
    358      1.1  joerg     return ExprError();
    359      1.1  joerg   Number = ConvertedNumber.get();
    360      1.1  joerg 
    361      1.1  joerg   // Use the effective source range of the literal, including the leading '@'.
    362      1.1  joerg   return MaybeBindToTemporary(
    363      1.1  joerg            new (Context) ObjCBoxedExpr(Number, NSNumberPointer, Method,
    364      1.1  joerg                                        SourceRange(AtLoc, NR.getEnd())));
    365      1.1  joerg }
    366      1.1  joerg 
    367      1.1  joerg ExprResult Sema::ActOnObjCBoolLiteral(SourceLocation AtLoc,
    368      1.1  joerg                                       SourceLocation ValueLoc,
    369      1.1  joerg                                       bool Value) {
    370      1.1  joerg   ExprResult Inner;
    371      1.1  joerg   if (getLangOpts().CPlusPlus) {
    372      1.1  joerg     Inner = ActOnCXXBoolLiteral(ValueLoc, Value? tok::kw_true : tok::kw_false);
    373      1.1  joerg   } else {
    374      1.1  joerg     // C doesn't actually have a way to represent literal values of type
    375      1.1  joerg     // _Bool. So, we'll use 0/1 and implicit cast to _Bool.
    376      1.1  joerg     Inner = ActOnIntegerConstant(ValueLoc, Value? 1 : 0);
    377      1.1  joerg     Inner = ImpCastExprToType(Inner.get(), Context.BoolTy,
    378      1.1  joerg                               CK_IntegralToBoolean);
    379      1.1  joerg   }
    380      1.1  joerg 
    381      1.1  joerg   return BuildObjCNumericLiteral(AtLoc, Inner.get());
    382      1.1  joerg }
    383      1.1  joerg 
    384      1.1  joerg /// Check that the given expression is a valid element of an Objective-C
    385      1.1  joerg /// collection literal.
    386      1.1  joerg static ExprResult CheckObjCCollectionLiteralElement(Sema &S, Expr *Element,
    387      1.1  joerg                                                     QualType T,
    388      1.1  joerg                                                     bool ArrayLiteral = false) {
    389      1.1  joerg   // If the expression is type-dependent, there's nothing for us to do.
    390      1.1  joerg   if (Element->isTypeDependent())
    391      1.1  joerg     return Element;
    392      1.1  joerg 
    393      1.1  joerg   ExprResult Result = S.CheckPlaceholderExpr(Element);
    394      1.1  joerg   if (Result.isInvalid())
    395      1.1  joerg     return ExprError();
    396      1.1  joerg   Element = Result.get();
    397      1.1  joerg 
    398      1.1  joerg   // In C++, check for an implicit conversion to an Objective-C object pointer
    399      1.1  joerg   // type.
    400      1.1  joerg   if (S.getLangOpts().CPlusPlus && Element->getType()->isRecordType()) {
    401      1.1  joerg     InitializedEntity Entity
    402      1.1  joerg       = InitializedEntity::InitializeParameter(S.Context, T,
    403      1.1  joerg                                                /*Consumed=*/false);
    404      1.1  joerg     InitializationKind Kind = InitializationKind::CreateCopy(
    405      1.1  joerg         Element->getBeginLoc(), SourceLocation());
    406      1.1  joerg     InitializationSequence Seq(S, Entity, Kind, Element);
    407      1.1  joerg     if (!Seq.Failed())
    408      1.1  joerg       return Seq.Perform(S, Entity, Kind, Element);
    409      1.1  joerg   }
    410      1.1  joerg 
    411      1.1  joerg   Expr *OrigElement = Element;
    412      1.1  joerg 
    413      1.1  joerg   // Perform lvalue-to-rvalue conversion.
    414      1.1  joerg   Result = S.DefaultLvalueConversion(Element);
    415      1.1  joerg   if (Result.isInvalid())
    416      1.1  joerg     return ExprError();
    417      1.1  joerg   Element = Result.get();
    418      1.1  joerg 
    419      1.1  joerg   // Make sure that we have an Objective-C pointer type or block.
    420      1.1  joerg   if (!Element->getType()->isObjCObjectPointerType() &&
    421      1.1  joerg       !Element->getType()->isBlockPointerType()) {
    422      1.1  joerg     bool Recovered = false;
    423      1.1  joerg 
    424      1.1  joerg     // If this is potentially an Objective-C numeric literal, add the '@'.
    425      1.1  joerg     if (isa<IntegerLiteral>(OrigElement) ||
    426      1.1  joerg         isa<CharacterLiteral>(OrigElement) ||
    427      1.1  joerg         isa<FloatingLiteral>(OrigElement) ||
    428      1.1  joerg         isa<ObjCBoolLiteralExpr>(OrigElement) ||
    429      1.1  joerg         isa<CXXBoolLiteralExpr>(OrigElement)) {
    430      1.1  joerg       if (S.NSAPIObj->getNSNumberFactoryMethodKind(OrigElement->getType())) {
    431      1.1  joerg         int Which = isa<CharacterLiteral>(OrigElement) ? 1
    432      1.1  joerg                   : (isa<CXXBoolLiteralExpr>(OrigElement) ||
    433      1.1  joerg                      isa<ObjCBoolLiteralExpr>(OrigElement)) ? 2
    434      1.1  joerg                   : 3;
    435      1.1  joerg 
    436      1.1  joerg         S.Diag(OrigElement->getBeginLoc(), diag::err_box_literal_collection)
    437      1.1  joerg             << Which << OrigElement->getSourceRange()
    438      1.1  joerg             << FixItHint::CreateInsertion(OrigElement->getBeginLoc(), "@");
    439      1.1  joerg 
    440      1.1  joerg         Result =
    441      1.1  joerg             S.BuildObjCNumericLiteral(OrigElement->getBeginLoc(), OrigElement);
    442      1.1  joerg         if (Result.isInvalid())
    443      1.1  joerg           return ExprError();
    444      1.1  joerg 
    445      1.1  joerg         Element = Result.get();
    446      1.1  joerg         Recovered = true;
    447      1.1  joerg       }
    448      1.1  joerg     }
    449      1.1  joerg     // If this is potentially an Objective-C string literal, add the '@'.
    450      1.1  joerg     else if (StringLiteral *String = dyn_cast<StringLiteral>(OrigElement)) {
    451      1.1  joerg       if (String->isAscii()) {
    452      1.1  joerg         S.Diag(OrigElement->getBeginLoc(), diag::err_box_literal_collection)
    453      1.1  joerg             << 0 << OrigElement->getSourceRange()
    454      1.1  joerg             << FixItHint::CreateInsertion(OrigElement->getBeginLoc(), "@");
    455      1.1  joerg 
    456      1.1  joerg         Result = S.BuildObjCStringLiteral(OrigElement->getBeginLoc(), String);
    457      1.1  joerg         if (Result.isInvalid())
    458      1.1  joerg           return ExprError();
    459      1.1  joerg 
    460      1.1  joerg         Element = Result.get();
    461      1.1  joerg         Recovered = true;
    462      1.1  joerg       }
    463      1.1  joerg     }
    464      1.1  joerg 
    465      1.1  joerg     if (!Recovered) {
    466      1.1  joerg       S.Diag(Element->getBeginLoc(), diag::err_invalid_collection_element)
    467      1.1  joerg           << Element->getType();
    468      1.1  joerg       return ExprError();
    469      1.1  joerg     }
    470      1.1  joerg   }
    471      1.1  joerg   if (ArrayLiteral)
    472      1.1  joerg     if (ObjCStringLiteral *getString =
    473      1.1  joerg           dyn_cast<ObjCStringLiteral>(OrigElement)) {
    474      1.1  joerg       if (StringLiteral *SL = getString->getString()) {
    475      1.1  joerg         unsigned numConcat = SL->getNumConcatenated();
    476      1.1  joerg         if (numConcat > 1) {
    477      1.1  joerg           // Only warn if the concatenated string doesn't come from a macro.
    478      1.1  joerg           bool hasMacro = false;
    479      1.1  joerg           for (unsigned i = 0; i < numConcat ; ++i)
    480      1.1  joerg             if (SL->getStrTokenLoc(i).isMacroID()) {
    481      1.1  joerg               hasMacro = true;
    482      1.1  joerg               break;
    483      1.1  joerg             }
    484      1.1  joerg           if (!hasMacro)
    485      1.1  joerg             S.Diag(Element->getBeginLoc(),
    486      1.1  joerg                    diag::warn_concatenated_nsarray_literal)
    487      1.1  joerg                 << Element->getType();
    488      1.1  joerg         }
    489      1.1  joerg       }
    490      1.1  joerg     }
    491      1.1  joerg 
    492      1.1  joerg   // Make sure that the element has the type that the container factory
    493      1.1  joerg   // function expects.
    494      1.1  joerg   return S.PerformCopyInitialization(
    495      1.1  joerg       InitializedEntity::InitializeParameter(S.Context, T,
    496      1.1  joerg                                              /*Consumed=*/false),
    497      1.1  joerg       Element->getBeginLoc(), Element);
    498      1.1  joerg }
    499      1.1  joerg 
    500      1.1  joerg ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) {
    501      1.1  joerg   if (ValueExpr->isTypeDependent()) {
    502      1.1  joerg     ObjCBoxedExpr *BoxedExpr =
    503      1.1  joerg       new (Context) ObjCBoxedExpr(ValueExpr, Context.DependentTy, nullptr, SR);
    504      1.1  joerg     return BoxedExpr;
    505      1.1  joerg   }
    506      1.1  joerg   ObjCMethodDecl *BoxingMethod = nullptr;
    507      1.1  joerg   QualType BoxedType;
    508      1.1  joerg   // Convert the expression to an RValue, so we can check for pointer types...
    509      1.1  joerg   ExprResult RValue = DefaultFunctionArrayLvalueConversion(ValueExpr);
    510      1.1  joerg   if (RValue.isInvalid()) {
    511      1.1  joerg     return ExprError();
    512      1.1  joerg   }
    513      1.1  joerg   SourceLocation Loc = SR.getBegin();
    514      1.1  joerg   ValueExpr = RValue.get();
    515      1.1  joerg   QualType ValueType(ValueExpr->getType());
    516      1.1  joerg   if (const PointerType *PT = ValueType->getAs<PointerType>()) {
    517      1.1  joerg     QualType PointeeType = PT->getPointeeType();
    518      1.1  joerg     if (Context.hasSameUnqualifiedType(PointeeType, Context.CharTy)) {
    519      1.1  joerg 
    520      1.1  joerg       if (!NSStringDecl) {
    521      1.1  joerg         NSStringDecl = LookupObjCInterfaceDeclForLiteral(*this, Loc,
    522      1.1  joerg                                                          Sema::LK_String);
    523      1.1  joerg         if (!NSStringDecl) {
    524      1.1  joerg           return ExprError();
    525      1.1  joerg         }
    526      1.1  joerg         QualType NSStringObject = Context.getObjCInterfaceType(NSStringDecl);
    527      1.1  joerg         NSStringPointer = Context.getObjCObjectPointerType(NSStringObject);
    528      1.1  joerg       }
    529      1.1  joerg 
    530      1.1  joerg       // The boxed expression can be emitted as a compile time constant if it is
    531      1.1  joerg       // a string literal whose character encoding is compatible with UTF-8.
    532      1.1  joerg       if (auto *CE = dyn_cast<ImplicitCastExpr>(ValueExpr))
    533      1.1  joerg         if (CE->getCastKind() == CK_ArrayToPointerDecay)
    534      1.1  joerg           if (auto *SL =
    535      1.1  joerg                   dyn_cast<StringLiteral>(CE->getSubExpr()->IgnoreParens())) {
    536      1.1  joerg             assert((SL->isAscii() || SL->isUTF8()) &&
    537      1.1  joerg                    "unexpected character encoding");
    538      1.1  joerg             StringRef Str = SL->getString();
    539      1.1  joerg             const llvm::UTF8 *StrBegin = Str.bytes_begin();
    540      1.1  joerg             const llvm::UTF8 *StrEnd = Str.bytes_end();
    541      1.1  joerg             // Check that this is a valid UTF-8 string.
    542      1.1  joerg             if (llvm::isLegalUTF8String(&StrBegin, StrEnd)) {
    543      1.1  joerg               BoxedType = Context.getAttributedType(
    544      1.1  joerg                   AttributedType::getNullabilityAttrKind(
    545      1.1  joerg                       NullabilityKind::NonNull),
    546      1.1  joerg                   NSStringPointer, NSStringPointer);
    547      1.1  joerg               return new (Context) ObjCBoxedExpr(CE, BoxedType, nullptr, SR);
    548      1.1  joerg             }
    549      1.1  joerg 
    550      1.1  joerg             Diag(SL->getBeginLoc(), diag::warn_objc_boxing_invalid_utf8_string)
    551      1.1  joerg                 << NSStringPointer << SL->getSourceRange();
    552      1.1  joerg           }
    553      1.1  joerg 
    554      1.1  joerg       if (!StringWithUTF8StringMethod) {
    555      1.1  joerg         IdentifierInfo *II = &Context.Idents.get("stringWithUTF8String");
    556      1.1  joerg         Selector stringWithUTF8String = Context.Selectors.getUnarySelector(II);
    557      1.1  joerg 
    558      1.1  joerg         // Look for the appropriate method within NSString.
    559      1.1  joerg         BoxingMethod = NSStringDecl->lookupClassMethod(stringWithUTF8String);
    560      1.1  joerg         if (!BoxingMethod && getLangOpts().DebuggerObjCLiteral) {
    561      1.1  joerg           // Debugger needs to work even if NSString hasn't been defined.
    562      1.1  joerg           TypeSourceInfo *ReturnTInfo = nullptr;
    563      1.1  joerg           ObjCMethodDecl *M = ObjCMethodDecl::Create(
    564      1.1  joerg               Context, SourceLocation(), SourceLocation(), stringWithUTF8String,
    565      1.1  joerg               NSStringPointer, ReturnTInfo, NSStringDecl,
    566      1.1  joerg               /*isInstance=*/false, /*isVariadic=*/false,
    567      1.1  joerg               /*isPropertyAccessor=*/false,
    568  1.1.1.2  joerg               /*isSynthesizedAccessorStub=*/false,
    569      1.1  joerg               /*isImplicitlyDeclared=*/true,
    570      1.1  joerg               /*isDefined=*/false, ObjCMethodDecl::Required,
    571      1.1  joerg               /*HasRelatedResultType=*/false);
    572      1.1  joerg           QualType ConstCharType = Context.CharTy.withConst();
    573      1.1  joerg           ParmVarDecl *value =
    574      1.1  joerg             ParmVarDecl::Create(Context, M,
    575      1.1  joerg                                 SourceLocation(), SourceLocation(),
    576      1.1  joerg                                 &Context.Idents.get("value"),
    577      1.1  joerg                                 Context.getPointerType(ConstCharType),
    578      1.1  joerg                                 /*TInfo=*/nullptr,
    579      1.1  joerg                                 SC_None, nullptr);
    580      1.1  joerg           M->setMethodParams(Context, value, None);
    581      1.1  joerg           BoxingMethod = M;
    582      1.1  joerg         }
    583      1.1  joerg 
    584      1.1  joerg         if (!validateBoxingMethod(*this, Loc, NSStringDecl,
    585      1.1  joerg                                   stringWithUTF8String, BoxingMethod))
    586      1.1  joerg            return ExprError();
    587      1.1  joerg 
    588      1.1  joerg         StringWithUTF8StringMethod = BoxingMethod;
    589      1.1  joerg       }
    590      1.1  joerg 
    591      1.1  joerg       BoxingMethod = StringWithUTF8StringMethod;
    592      1.1  joerg       BoxedType = NSStringPointer;
    593      1.1  joerg       // Transfer the nullability from method's return type.
    594      1.1  joerg       Optional<NullabilityKind> Nullability =
    595      1.1  joerg           BoxingMethod->getReturnType()->getNullability(Context);
    596      1.1  joerg       if (Nullability)
    597      1.1  joerg         BoxedType = Context.getAttributedType(
    598      1.1  joerg             AttributedType::getNullabilityAttrKind(*Nullability), BoxedType,
    599      1.1  joerg             BoxedType);
    600      1.1  joerg     }
    601      1.1  joerg   } else if (ValueType->isBuiltinType()) {
    602      1.1  joerg     // The other types we support are numeric, char and BOOL/bool. We could also
    603      1.1  joerg     // provide limited support for structure types, such as NSRange, NSRect, and
    604      1.1  joerg     // NSSize. See NSValue (NSValueGeometryExtensions) in <Foundation/NSGeometry.h>
    605      1.1  joerg     // for more details.
    606      1.1  joerg 
    607      1.1  joerg     // Check for a top-level character literal.
    608      1.1  joerg     if (const CharacterLiteral *Char =
    609      1.1  joerg         dyn_cast<CharacterLiteral>(ValueExpr->IgnoreParens())) {
    610      1.1  joerg       // In C, character literals have type 'int'. That's not the type we want
    611      1.1  joerg       // to use to determine the Objective-c literal kind.
    612      1.1  joerg       switch (Char->getKind()) {
    613      1.1  joerg       case CharacterLiteral::Ascii:
    614      1.1  joerg       case CharacterLiteral::UTF8:
    615      1.1  joerg         ValueType = Context.CharTy;
    616      1.1  joerg         break;
    617      1.1  joerg 
    618      1.1  joerg       case CharacterLiteral::Wide:
    619      1.1  joerg         ValueType = Context.getWideCharType();
    620      1.1  joerg         break;
    621      1.1  joerg 
    622      1.1  joerg       case CharacterLiteral::UTF16:
    623      1.1  joerg         ValueType = Context.Char16Ty;
    624      1.1  joerg         break;
    625      1.1  joerg 
    626      1.1  joerg       case CharacterLiteral::UTF32:
    627      1.1  joerg         ValueType = Context.Char32Ty;
    628      1.1  joerg         break;
    629      1.1  joerg       }
    630      1.1  joerg     }
    631      1.1  joerg     // FIXME:  Do I need to do anything special with BoolTy expressions?
    632      1.1  joerg 
    633      1.1  joerg     // Look for the appropriate method within NSNumber.
    634      1.1  joerg     BoxingMethod = getNSNumberFactoryMethod(*this, Loc, ValueType);
    635      1.1  joerg     BoxedType = NSNumberPointer;
    636      1.1  joerg   } else if (const EnumType *ET = ValueType->getAs<EnumType>()) {
    637      1.1  joerg     if (!ET->getDecl()->isComplete()) {
    638      1.1  joerg       Diag(Loc, diag::err_objc_incomplete_boxed_expression_type)
    639      1.1  joerg         << ValueType << ValueExpr->getSourceRange();
    640      1.1  joerg       return ExprError();
    641      1.1  joerg     }
    642      1.1  joerg 
    643      1.1  joerg     BoxingMethod = getNSNumberFactoryMethod(*this, Loc,
    644      1.1  joerg                                             ET->getDecl()->getIntegerType());
    645      1.1  joerg     BoxedType = NSNumberPointer;
    646      1.1  joerg   } else if (ValueType->isObjCBoxableRecordType()) {
    647      1.1  joerg     // Support for structure types, that marked as objc_boxable
    648      1.1  joerg     // struct __attribute__((objc_boxable)) s { ... };
    649      1.1  joerg 
    650      1.1  joerg     // Look up the NSValue class, if we haven't done so already. It's cached
    651      1.1  joerg     // in the Sema instance.
    652      1.1  joerg     if (!NSValueDecl) {
    653      1.1  joerg       NSValueDecl = LookupObjCInterfaceDeclForLiteral(*this, Loc,
    654      1.1  joerg                                                       Sema::LK_Boxed);
    655      1.1  joerg       if (!NSValueDecl) {
    656      1.1  joerg         return ExprError();
    657      1.1  joerg       }
    658      1.1  joerg 
    659      1.1  joerg       // generate the pointer to NSValue type.
    660      1.1  joerg       QualType NSValueObject = Context.getObjCInterfaceType(NSValueDecl);
    661      1.1  joerg       NSValuePointer = Context.getObjCObjectPointerType(NSValueObject);
    662      1.1  joerg     }
    663      1.1  joerg 
    664      1.1  joerg     if (!ValueWithBytesObjCTypeMethod) {
    665      1.1  joerg       IdentifierInfo *II[] = {
    666      1.1  joerg         &Context.Idents.get("valueWithBytes"),
    667      1.1  joerg         &Context.Idents.get("objCType")
    668      1.1  joerg       };
    669      1.1  joerg       Selector ValueWithBytesObjCType = Context.Selectors.getSelector(2, II);
    670      1.1  joerg 
    671      1.1  joerg       // Look for the appropriate method within NSValue.
    672      1.1  joerg       BoxingMethod = NSValueDecl->lookupClassMethod(ValueWithBytesObjCType);
    673      1.1  joerg       if (!BoxingMethod && getLangOpts().DebuggerObjCLiteral) {
    674      1.1  joerg         // Debugger needs to work even if NSValue hasn't been defined.
    675      1.1  joerg         TypeSourceInfo *ReturnTInfo = nullptr;
    676      1.1  joerg         ObjCMethodDecl *M = ObjCMethodDecl::Create(
    677  1.1.1.2  joerg             Context, SourceLocation(), SourceLocation(), ValueWithBytesObjCType,
    678  1.1.1.2  joerg             NSValuePointer, ReturnTInfo, NSValueDecl,
    679  1.1.1.2  joerg             /*isInstance=*/false,
    680  1.1.1.2  joerg             /*isVariadic=*/false,
    681  1.1.1.2  joerg             /*isPropertyAccessor=*/false,
    682  1.1.1.2  joerg             /*isSynthesizedAccessorStub=*/false,
    683  1.1.1.2  joerg             /*isImplicitlyDeclared=*/true,
    684  1.1.1.2  joerg             /*isDefined=*/false, ObjCMethodDecl::Required,
    685  1.1.1.2  joerg             /*HasRelatedResultType=*/false);
    686      1.1  joerg 
    687      1.1  joerg         SmallVector<ParmVarDecl *, 2> Params;
    688      1.1  joerg 
    689      1.1  joerg         ParmVarDecl *bytes =
    690      1.1  joerg         ParmVarDecl::Create(Context, M,
    691      1.1  joerg                             SourceLocation(), SourceLocation(),
    692      1.1  joerg                             &Context.Idents.get("bytes"),
    693      1.1  joerg                             Context.VoidPtrTy.withConst(),
    694      1.1  joerg                             /*TInfo=*/nullptr,
    695      1.1  joerg                             SC_None, nullptr);
    696      1.1  joerg         Params.push_back(bytes);
    697      1.1  joerg 
    698      1.1  joerg         QualType ConstCharType = Context.CharTy.withConst();
    699      1.1  joerg         ParmVarDecl *type =
    700      1.1  joerg         ParmVarDecl::Create(Context, M,
    701      1.1  joerg                             SourceLocation(), SourceLocation(),
    702      1.1  joerg                             &Context.Idents.get("type"),
    703      1.1  joerg                             Context.getPointerType(ConstCharType),
    704      1.1  joerg                             /*TInfo=*/nullptr,
    705      1.1  joerg                             SC_None, nullptr);
    706      1.1  joerg         Params.push_back(type);
    707      1.1  joerg 
    708      1.1  joerg         M->setMethodParams(Context, Params, None);
    709      1.1  joerg         BoxingMethod = M;
    710      1.1  joerg       }
    711      1.1  joerg 
    712      1.1  joerg       if (!validateBoxingMethod(*this, Loc, NSValueDecl,
    713      1.1  joerg                                 ValueWithBytesObjCType, BoxingMethod))
    714      1.1  joerg         return ExprError();
    715      1.1  joerg 
    716      1.1  joerg       ValueWithBytesObjCTypeMethod = BoxingMethod;
    717      1.1  joerg     }
    718      1.1  joerg 
    719      1.1  joerg     if (!ValueType.isTriviallyCopyableType(Context)) {
    720      1.1  joerg       Diag(Loc, diag::err_objc_non_trivially_copyable_boxed_expression_type)
    721      1.1  joerg         << ValueType << ValueExpr->getSourceRange();
    722      1.1  joerg       return ExprError();
    723      1.1  joerg     }
    724      1.1  joerg 
    725      1.1  joerg     BoxingMethod = ValueWithBytesObjCTypeMethod;
    726      1.1  joerg     BoxedType = NSValuePointer;
    727      1.1  joerg   }
    728      1.1  joerg 
    729      1.1  joerg   if (!BoxingMethod) {
    730      1.1  joerg     Diag(Loc, diag::err_objc_illegal_boxed_expression_type)
    731      1.1  joerg       << ValueType << ValueExpr->getSourceRange();
    732      1.1  joerg     return ExprError();
    733      1.1  joerg   }
    734      1.1  joerg 
    735      1.1  joerg   DiagnoseUseOfDecl(BoxingMethod, Loc);
    736      1.1  joerg 
    737      1.1  joerg   ExprResult ConvertedValueExpr;
    738      1.1  joerg   if (ValueType->isObjCBoxableRecordType()) {
    739      1.1  joerg     InitializedEntity IE = InitializedEntity::InitializeTemporary(ValueType);
    740      1.1  joerg     ConvertedValueExpr = PerformCopyInitialization(IE, ValueExpr->getExprLoc(),
    741      1.1  joerg                                                    ValueExpr);
    742      1.1  joerg   } else {
    743      1.1  joerg     // Convert the expression to the type that the parameter requires.
    744      1.1  joerg     ParmVarDecl *ParamDecl = BoxingMethod->parameters()[0];
    745      1.1  joerg     InitializedEntity IE = InitializedEntity::InitializeParameter(Context,
    746      1.1  joerg                                                                   ParamDecl);
    747      1.1  joerg     ConvertedValueExpr = PerformCopyInitialization(IE, SourceLocation(),
    748      1.1  joerg                                                    ValueExpr);
    749      1.1  joerg   }
    750      1.1  joerg 
    751      1.1  joerg   if (ConvertedValueExpr.isInvalid())
    752      1.1  joerg     return ExprError();
    753      1.1  joerg   ValueExpr = ConvertedValueExpr.get();
    754      1.1  joerg 
    755      1.1  joerg   ObjCBoxedExpr *BoxedExpr =
    756      1.1  joerg     new (Context) ObjCBoxedExpr(ValueExpr, BoxedType,
    757      1.1  joerg                                       BoxingMethod, SR);
    758      1.1  joerg   return MaybeBindToTemporary(BoxedExpr);
    759      1.1  joerg }
    760      1.1  joerg 
    761      1.1  joerg /// Build an ObjC subscript pseudo-object expression, given that
    762      1.1  joerg /// that's supported by the runtime.
    763      1.1  joerg ExprResult Sema::BuildObjCSubscriptExpression(SourceLocation RB, Expr *BaseExpr,
    764      1.1  joerg                                         Expr *IndexExpr,
    765      1.1  joerg                                         ObjCMethodDecl *getterMethod,
    766      1.1  joerg                                         ObjCMethodDecl *setterMethod) {
    767      1.1  joerg   assert(!LangOpts.isSubscriptPointerArithmetic());
    768      1.1  joerg 
    769      1.1  joerg   // We can't get dependent types here; our callers should have
    770      1.1  joerg   // filtered them out.
    771      1.1  joerg   assert((!BaseExpr->isTypeDependent() && !IndexExpr->isTypeDependent()) &&
    772      1.1  joerg          "base or index cannot have dependent type here");
    773      1.1  joerg 
    774      1.1  joerg   // Filter out placeholders in the index.  In theory, overloads could
    775      1.1  joerg   // be preserved here, although that might not actually work correctly.
    776      1.1  joerg   ExprResult Result = CheckPlaceholderExpr(IndexExpr);
    777      1.1  joerg   if (Result.isInvalid())
    778      1.1  joerg     return ExprError();
    779      1.1  joerg   IndexExpr = Result.get();
    780      1.1  joerg 
    781      1.1  joerg   // Perform lvalue-to-rvalue conversion on the base.
    782      1.1  joerg   Result = DefaultLvalueConversion(BaseExpr);
    783      1.1  joerg   if (Result.isInvalid())
    784      1.1  joerg     return ExprError();
    785      1.1  joerg   BaseExpr = Result.get();
    786      1.1  joerg 
    787      1.1  joerg   // Build the pseudo-object expression.
    788      1.1  joerg   return new (Context) ObjCSubscriptRefExpr(
    789      1.1  joerg       BaseExpr, IndexExpr, Context.PseudoObjectTy, VK_LValue, OK_ObjCSubscript,
    790      1.1  joerg       getterMethod, setterMethod, RB);
    791      1.1  joerg }
    792      1.1  joerg 
    793      1.1  joerg ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) {
    794      1.1  joerg   SourceLocation Loc = SR.getBegin();
    795      1.1  joerg 
    796      1.1  joerg   if (!NSArrayDecl) {
    797      1.1  joerg     NSArrayDecl = LookupObjCInterfaceDeclForLiteral(*this, Loc,
    798      1.1  joerg                                                     Sema::LK_Array);
    799      1.1  joerg     if (!NSArrayDecl) {
    800      1.1  joerg       return ExprError();
    801      1.1  joerg     }
    802      1.1  joerg   }
    803      1.1  joerg 
    804      1.1  joerg   // Find the arrayWithObjects:count: method, if we haven't done so already.
    805      1.1  joerg   QualType IdT = Context.getObjCIdType();
    806      1.1  joerg   if (!ArrayWithObjectsMethod) {
    807      1.1  joerg     Selector
    808      1.1  joerg       Sel = NSAPIObj->getNSArraySelector(NSAPI::NSArr_arrayWithObjectsCount);
    809      1.1  joerg     ObjCMethodDecl *Method = NSArrayDecl->lookupClassMethod(Sel);
    810      1.1  joerg     if (!Method && getLangOpts().DebuggerObjCLiteral) {
    811      1.1  joerg       TypeSourceInfo *ReturnTInfo = nullptr;
    812      1.1  joerg       Method = ObjCMethodDecl::Create(
    813      1.1  joerg           Context, SourceLocation(), SourceLocation(), Sel, IdT, ReturnTInfo,
    814      1.1  joerg           Context.getTranslationUnitDecl(), false /*Instance*/,
    815      1.1  joerg           false /*isVariadic*/,
    816  1.1.1.2  joerg           /*isPropertyAccessor=*/false, /*isSynthesizedAccessorStub=*/false,
    817      1.1  joerg           /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
    818      1.1  joerg           ObjCMethodDecl::Required, false);
    819      1.1  joerg       SmallVector<ParmVarDecl *, 2> Params;
    820      1.1  joerg       ParmVarDecl *objects = ParmVarDecl::Create(Context, Method,
    821      1.1  joerg                                                  SourceLocation(),
    822      1.1  joerg                                                  SourceLocation(),
    823      1.1  joerg                                                  &Context.Idents.get("objects"),
    824      1.1  joerg                                                  Context.getPointerType(IdT),
    825      1.1  joerg                                                  /*TInfo=*/nullptr,
    826      1.1  joerg                                                  SC_None, nullptr);
    827      1.1  joerg       Params.push_back(objects);
    828      1.1  joerg       ParmVarDecl *cnt = ParmVarDecl::Create(Context, Method,
    829      1.1  joerg                                              SourceLocation(),
    830      1.1  joerg                                              SourceLocation(),
    831      1.1  joerg                                              &Context.Idents.get("cnt"),
    832      1.1  joerg                                              Context.UnsignedLongTy,
    833      1.1  joerg                                              /*TInfo=*/nullptr, SC_None,
    834      1.1  joerg                                              nullptr);
    835      1.1  joerg       Params.push_back(cnt);
    836      1.1  joerg       Method->setMethodParams(Context, Params, None);
    837      1.1  joerg     }
    838      1.1  joerg 
    839      1.1  joerg     if (!validateBoxingMethod(*this, Loc, NSArrayDecl, Sel, Method))
    840      1.1  joerg       return ExprError();
    841      1.1  joerg 
    842      1.1  joerg     // Dig out the type that all elements should be converted to.
    843      1.1  joerg     QualType T = Method->parameters()[0]->getType();
    844      1.1  joerg     const PointerType *PtrT = T->getAs<PointerType>();
    845      1.1  joerg     if (!PtrT ||
    846      1.1  joerg         !Context.hasSameUnqualifiedType(PtrT->getPointeeType(), IdT)) {
    847      1.1  joerg       Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
    848      1.1  joerg         << Sel;
    849      1.1  joerg       Diag(Method->parameters()[0]->getLocation(),
    850      1.1  joerg            diag::note_objc_literal_method_param)
    851      1.1  joerg         << 0 << T
    852      1.1  joerg         << Context.getPointerType(IdT.withConst());
    853      1.1  joerg       return ExprError();
    854      1.1  joerg     }
    855      1.1  joerg 
    856      1.1  joerg     // Check that the 'count' parameter is integral.
    857      1.1  joerg     if (!Method->parameters()[1]->getType()->isIntegerType()) {
    858      1.1  joerg       Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
    859      1.1  joerg         << Sel;
    860      1.1  joerg       Diag(Method->parameters()[1]->getLocation(),
    861      1.1  joerg            diag::note_objc_literal_method_param)
    862      1.1  joerg         << 1
    863      1.1  joerg         << Method->parameters()[1]->getType()
    864      1.1  joerg         << "integral";
    865      1.1  joerg       return ExprError();
    866      1.1  joerg     }
    867      1.1  joerg 
    868      1.1  joerg     // We've found a good +arrayWithObjects:count: method. Save it!
    869      1.1  joerg     ArrayWithObjectsMethod = Method;
    870      1.1  joerg   }
    871      1.1  joerg 
    872      1.1  joerg   QualType ObjectsType = ArrayWithObjectsMethod->parameters()[0]->getType();
    873      1.1  joerg   QualType RequiredType = ObjectsType->castAs<PointerType>()->getPointeeType();
    874      1.1  joerg 
    875      1.1  joerg   // Check that each of the elements provided is valid in a collection literal,
    876      1.1  joerg   // performing conversions as necessary.
    877      1.1  joerg   Expr **ElementsBuffer = Elements.data();
    878      1.1  joerg   for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
    879      1.1  joerg     ExprResult Converted = CheckObjCCollectionLiteralElement(*this,
    880      1.1  joerg                                                              ElementsBuffer[I],
    881      1.1  joerg                                                              RequiredType, true);
    882      1.1  joerg     if (Converted.isInvalid())
    883      1.1  joerg       return ExprError();
    884      1.1  joerg 
    885      1.1  joerg     ElementsBuffer[I] = Converted.get();
    886      1.1  joerg   }
    887      1.1  joerg 
    888      1.1  joerg   QualType Ty
    889      1.1  joerg     = Context.getObjCObjectPointerType(
    890      1.1  joerg                                     Context.getObjCInterfaceType(NSArrayDecl));
    891      1.1  joerg 
    892      1.1  joerg   return MaybeBindToTemporary(
    893      1.1  joerg            ObjCArrayLiteral::Create(Context, Elements, Ty,
    894      1.1  joerg                                     ArrayWithObjectsMethod, SR));
    895      1.1  joerg }
    896      1.1  joerg 
    897  1.1.1.2  joerg /// Check for duplicate keys in an ObjC dictionary literal. For instance:
    898  1.1.1.2  joerg ///   NSDictionary *nd = @{ @"foo" : @"bar", @"foo" : @"baz" };
    899  1.1.1.2  joerg static void
    900  1.1.1.2  joerg CheckObjCDictionaryLiteralDuplicateKeys(Sema &S,
    901  1.1.1.2  joerg                                         ObjCDictionaryLiteral *Literal) {
    902  1.1.1.2  joerg   if (Literal->isValueDependent() || Literal->isTypeDependent())
    903  1.1.1.2  joerg     return;
    904  1.1.1.2  joerg 
    905  1.1.1.2  joerg   // NSNumber has quite relaxed equality semantics (for instance, @YES is
    906  1.1.1.2  joerg   // considered equal to @1.0). For now, ignore floating points and just do a
    907  1.1.1.2  joerg   // bit-width and sign agnostic integer compare.
    908  1.1.1.2  joerg   struct APSIntCompare {
    909  1.1.1.2  joerg     bool operator()(const llvm::APSInt &LHS, const llvm::APSInt &RHS) const {
    910  1.1.1.2  joerg       return llvm::APSInt::compareValues(LHS, RHS) < 0;
    911  1.1.1.2  joerg     }
    912  1.1.1.2  joerg   };
    913  1.1.1.2  joerg 
    914  1.1.1.2  joerg   llvm::DenseMap<StringRef, SourceLocation> StringKeys;
    915  1.1.1.2  joerg   std::map<llvm::APSInt, SourceLocation, APSIntCompare> IntegralKeys;
    916  1.1.1.2  joerg 
    917  1.1.1.2  joerg   auto checkOneKey = [&](auto &Map, const auto &Key, SourceLocation Loc) {
    918  1.1.1.2  joerg     auto Pair = Map.insert({Key, Loc});
    919  1.1.1.2  joerg     if (!Pair.second) {
    920  1.1.1.2  joerg       S.Diag(Loc, diag::warn_nsdictionary_duplicate_key);
    921  1.1.1.2  joerg       S.Diag(Pair.first->second, diag::note_nsdictionary_duplicate_key_here);
    922  1.1.1.2  joerg     }
    923  1.1.1.2  joerg   };
    924  1.1.1.2  joerg 
    925  1.1.1.2  joerg   for (unsigned Idx = 0, End = Literal->getNumElements(); Idx != End; ++Idx) {
    926  1.1.1.2  joerg     Expr *Key = Literal->getKeyValueElement(Idx).Key->IgnoreParenImpCasts();
    927  1.1.1.2  joerg 
    928  1.1.1.2  joerg     if (auto *StrLit = dyn_cast<ObjCStringLiteral>(Key)) {
    929  1.1.1.2  joerg       StringRef Bytes = StrLit->getString()->getBytes();
    930  1.1.1.2  joerg       SourceLocation Loc = StrLit->getExprLoc();
    931  1.1.1.2  joerg       checkOneKey(StringKeys, Bytes, Loc);
    932  1.1.1.2  joerg     }
    933  1.1.1.2  joerg 
    934  1.1.1.2  joerg     if (auto *BE = dyn_cast<ObjCBoxedExpr>(Key)) {
    935  1.1.1.2  joerg       Expr *Boxed = BE->getSubExpr();
    936  1.1.1.2  joerg       SourceLocation Loc = BE->getExprLoc();
    937  1.1.1.2  joerg 
    938  1.1.1.2  joerg       // Check for @("foo").
    939  1.1.1.2  joerg       if (auto *Str = dyn_cast<StringLiteral>(Boxed->IgnoreParenImpCasts())) {
    940  1.1.1.2  joerg         checkOneKey(StringKeys, Str->getBytes(), Loc);
    941  1.1.1.2  joerg         continue;
    942  1.1.1.2  joerg       }
    943  1.1.1.2  joerg 
    944  1.1.1.2  joerg       Expr::EvalResult Result;
    945  1.1.1.2  joerg       if (Boxed->EvaluateAsInt(Result, S.getASTContext(),
    946  1.1.1.2  joerg                                Expr::SE_AllowSideEffects)) {
    947  1.1.1.2  joerg         checkOneKey(IntegralKeys, Result.Val.getInt(), Loc);
    948  1.1.1.2  joerg       }
    949  1.1.1.2  joerg     }
    950  1.1.1.2  joerg   }
    951  1.1.1.2  joerg }
    952  1.1.1.2  joerg 
    953      1.1  joerg ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR,
    954      1.1  joerg                               MutableArrayRef<ObjCDictionaryElement> Elements) {
    955      1.1  joerg   SourceLocation Loc = SR.getBegin();
    956      1.1  joerg 
    957      1.1  joerg   if (!NSDictionaryDecl) {
    958      1.1  joerg     NSDictionaryDecl = LookupObjCInterfaceDeclForLiteral(*this, Loc,
    959      1.1  joerg                                                          Sema::LK_Dictionary);
    960      1.1  joerg     if (!NSDictionaryDecl) {
    961      1.1  joerg       return ExprError();
    962      1.1  joerg     }
    963      1.1  joerg   }
    964      1.1  joerg 
    965      1.1  joerg   // Find the dictionaryWithObjects:forKeys:count: method, if we haven't done
    966      1.1  joerg   // so already.
    967      1.1  joerg   QualType IdT = Context.getObjCIdType();
    968      1.1  joerg   if (!DictionaryWithObjectsMethod) {
    969      1.1  joerg     Selector Sel = NSAPIObj->getNSDictionarySelector(
    970      1.1  joerg                                NSAPI::NSDict_dictionaryWithObjectsForKeysCount);
    971      1.1  joerg     ObjCMethodDecl *Method = NSDictionaryDecl->lookupClassMethod(Sel);
    972      1.1  joerg     if (!Method && getLangOpts().DebuggerObjCLiteral) {
    973  1.1.1.2  joerg       Method = ObjCMethodDecl::Create(
    974  1.1.1.2  joerg           Context, SourceLocation(), SourceLocation(), Sel, IdT,
    975  1.1.1.2  joerg           nullptr /*TypeSourceInfo */, Context.getTranslationUnitDecl(),
    976  1.1.1.2  joerg           false /*Instance*/, false /*isVariadic*/,
    977  1.1.1.2  joerg           /*isPropertyAccessor=*/false,
    978  1.1.1.2  joerg           /*isSynthesizedAccessorStub=*/false,
    979  1.1.1.2  joerg           /*isImplicitlyDeclared=*/true, /*isDefined=*/false,
    980  1.1.1.2  joerg           ObjCMethodDecl::Required, false);
    981      1.1  joerg       SmallVector<ParmVarDecl *, 3> Params;
    982      1.1  joerg       ParmVarDecl *objects = ParmVarDecl::Create(Context, Method,
    983      1.1  joerg                                                  SourceLocation(),
    984      1.1  joerg                                                  SourceLocation(),
    985      1.1  joerg                                                  &Context.Idents.get("objects"),
    986      1.1  joerg                                                  Context.getPointerType(IdT),
    987      1.1  joerg                                                  /*TInfo=*/nullptr, SC_None,
    988      1.1  joerg                                                  nullptr);
    989      1.1  joerg       Params.push_back(objects);
    990      1.1  joerg       ParmVarDecl *keys = ParmVarDecl::Create(Context, Method,
    991      1.1  joerg                                               SourceLocation(),
    992      1.1  joerg                                               SourceLocation(),
    993      1.1  joerg                                               &Context.Idents.get("keys"),
    994      1.1  joerg                                               Context.getPointerType(IdT),
    995      1.1  joerg                                               /*TInfo=*/nullptr, SC_None,
    996      1.1  joerg                                               nullptr);
    997      1.1  joerg       Params.push_back(keys);
    998      1.1  joerg       ParmVarDecl *cnt = ParmVarDecl::Create(Context, Method,
    999      1.1  joerg                                              SourceLocation(),
   1000      1.1  joerg                                              SourceLocation(),
   1001      1.1  joerg                                              &Context.Idents.get("cnt"),
   1002      1.1  joerg                                              Context.UnsignedLongTy,
   1003      1.1  joerg                                              /*TInfo=*/nullptr, SC_None,
   1004      1.1  joerg                                              nullptr);
   1005      1.1  joerg       Params.push_back(cnt);
   1006      1.1  joerg       Method->setMethodParams(Context, Params, None);
   1007      1.1  joerg     }
   1008      1.1  joerg 
   1009      1.1  joerg     if (!validateBoxingMethod(*this, SR.getBegin(), NSDictionaryDecl, Sel,
   1010      1.1  joerg                               Method))
   1011      1.1  joerg        return ExprError();
   1012      1.1  joerg 
   1013      1.1  joerg     // Dig out the type that all values should be converted to.
   1014      1.1  joerg     QualType ValueT = Method->parameters()[0]->getType();
   1015      1.1  joerg     const PointerType *PtrValue = ValueT->getAs<PointerType>();
   1016      1.1  joerg     if (!PtrValue ||
   1017      1.1  joerg         !Context.hasSameUnqualifiedType(PtrValue->getPointeeType(), IdT)) {
   1018      1.1  joerg       Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
   1019      1.1  joerg         << Sel;
   1020      1.1  joerg       Diag(Method->parameters()[0]->getLocation(),
   1021      1.1  joerg            diag::note_objc_literal_method_param)
   1022      1.1  joerg         << 0 << ValueT
   1023      1.1  joerg         << Context.getPointerType(IdT.withConst());
   1024      1.1  joerg       return ExprError();
   1025      1.1  joerg     }
   1026      1.1  joerg 
   1027      1.1  joerg     // Dig out the type that all keys should be converted to.
   1028      1.1  joerg     QualType KeyT = Method->parameters()[1]->getType();
   1029      1.1  joerg     const PointerType *PtrKey = KeyT->getAs<PointerType>();
   1030      1.1  joerg     if (!PtrKey ||
   1031      1.1  joerg         !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(),
   1032      1.1  joerg                                         IdT)) {
   1033      1.1  joerg       bool err = true;
   1034      1.1  joerg       if (PtrKey) {
   1035      1.1  joerg         if (QIDNSCopying.isNull()) {
   1036      1.1  joerg           // key argument of selector is id<NSCopying>?
   1037      1.1  joerg           if (ObjCProtocolDecl *NSCopyingPDecl =
   1038      1.1  joerg               LookupProtocol(&Context.Idents.get("NSCopying"), SR.getBegin())) {
   1039      1.1  joerg             ObjCProtocolDecl *PQ[] = {NSCopyingPDecl};
   1040      1.1  joerg             QIDNSCopying =
   1041      1.1  joerg               Context.getObjCObjectType(Context.ObjCBuiltinIdTy, { },
   1042      1.1  joerg                                         llvm::makeArrayRef(
   1043      1.1  joerg                                           (ObjCProtocolDecl**) PQ,
   1044      1.1  joerg                                           1),
   1045      1.1  joerg                                         false);
   1046      1.1  joerg             QIDNSCopying = Context.getObjCObjectPointerType(QIDNSCopying);
   1047      1.1  joerg           }
   1048      1.1  joerg         }
   1049      1.1  joerg         if (!QIDNSCopying.isNull())
   1050      1.1  joerg           err = !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(),
   1051      1.1  joerg                                                 QIDNSCopying);
   1052      1.1  joerg       }
   1053      1.1  joerg 
   1054      1.1  joerg       if (err) {
   1055      1.1  joerg         Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
   1056      1.1  joerg           << Sel;
   1057      1.1  joerg         Diag(Method->parameters()[1]->getLocation(),
   1058      1.1  joerg              diag::note_objc_literal_method_param)
   1059      1.1  joerg           << 1 << KeyT
   1060      1.1  joerg           << Context.getPointerType(IdT.withConst());
   1061      1.1  joerg         return ExprError();
   1062      1.1  joerg       }
   1063      1.1  joerg     }
   1064      1.1  joerg 
   1065      1.1  joerg     // Check that the 'count' parameter is integral.
   1066      1.1  joerg     QualType CountType = Method->parameters()[2]->getType();
   1067      1.1  joerg     if (!CountType->isIntegerType()) {
   1068      1.1  joerg       Diag(SR.getBegin(), diag::err_objc_literal_method_sig)
   1069      1.1  joerg         << Sel;
   1070      1.1  joerg       Diag(Method->parameters()[2]->getLocation(),
   1071      1.1  joerg            diag::note_objc_literal_method_param)
   1072      1.1  joerg         << 2 << CountType
   1073      1.1  joerg         << "integral";
   1074      1.1  joerg       return ExprError();
   1075      1.1  joerg     }
   1076      1.1  joerg 
   1077      1.1  joerg     // We've found a good +dictionaryWithObjects:keys:count: method; save it!
   1078      1.1  joerg     DictionaryWithObjectsMethod = Method;
   1079      1.1  joerg   }
   1080      1.1  joerg 
   1081      1.1  joerg   QualType ValuesT = DictionaryWithObjectsMethod->parameters()[0]->getType();
   1082      1.1  joerg   QualType ValueT = ValuesT->castAs<PointerType>()->getPointeeType();
   1083      1.1  joerg   QualType KeysT = DictionaryWithObjectsMethod->parameters()[1]->getType();
   1084      1.1  joerg   QualType KeyT = KeysT->castAs<PointerType>()->getPointeeType();
   1085      1.1  joerg 
   1086      1.1  joerg   // Check that each of the keys and values provided is valid in a collection
   1087      1.1  joerg   // literal, performing conversions as necessary.
   1088      1.1  joerg   bool HasPackExpansions = false;
   1089      1.1  joerg   for (ObjCDictionaryElement &Element : Elements) {
   1090      1.1  joerg     // Check the key.
   1091      1.1  joerg     ExprResult Key = CheckObjCCollectionLiteralElement(*this, Element.Key,
   1092      1.1  joerg                                                        KeyT);
   1093      1.1  joerg     if (Key.isInvalid())
   1094      1.1  joerg       return ExprError();
   1095      1.1  joerg 
   1096      1.1  joerg     // Check the value.
   1097      1.1  joerg     ExprResult Value
   1098      1.1  joerg       = CheckObjCCollectionLiteralElement(*this, Element.Value, ValueT);
   1099      1.1  joerg     if (Value.isInvalid())
   1100      1.1  joerg       return ExprError();
   1101      1.1  joerg 
   1102      1.1  joerg     Element.Key = Key.get();
   1103      1.1  joerg     Element.Value = Value.get();
   1104      1.1  joerg 
   1105      1.1  joerg     if (Element.EllipsisLoc.isInvalid())
   1106      1.1  joerg       continue;
   1107      1.1  joerg 
   1108      1.1  joerg     if (!Element.Key->containsUnexpandedParameterPack() &&
   1109      1.1  joerg         !Element.Value->containsUnexpandedParameterPack()) {
   1110      1.1  joerg       Diag(Element.EllipsisLoc,
   1111      1.1  joerg            diag::err_pack_expansion_without_parameter_packs)
   1112      1.1  joerg           << SourceRange(Element.Key->getBeginLoc(),
   1113      1.1  joerg                          Element.Value->getEndLoc());
   1114      1.1  joerg       return ExprError();
   1115      1.1  joerg     }
   1116      1.1  joerg 
   1117      1.1  joerg     HasPackExpansions = true;
   1118      1.1  joerg   }
   1119      1.1  joerg 
   1120  1.1.1.2  joerg   QualType Ty = Context.getObjCObjectPointerType(
   1121  1.1.1.2  joerg       Context.getObjCInterfaceType(NSDictionaryDecl));
   1122  1.1.1.2  joerg 
   1123  1.1.1.2  joerg   auto *Literal =
   1124  1.1.1.2  joerg       ObjCDictionaryLiteral::Create(Context, Elements, HasPackExpansions, Ty,
   1125  1.1.1.2  joerg                                     DictionaryWithObjectsMethod, SR);
   1126  1.1.1.2  joerg   CheckObjCDictionaryLiteralDuplicateKeys(*this, Literal);
   1127  1.1.1.2  joerg   return MaybeBindToTemporary(Literal);
   1128      1.1  joerg }
   1129      1.1  joerg 
   1130      1.1  joerg ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc,
   1131      1.1  joerg                                       TypeSourceInfo *EncodedTypeInfo,
   1132      1.1  joerg                                       SourceLocation RParenLoc) {
   1133      1.1  joerg   QualType EncodedType = EncodedTypeInfo->getType();
   1134      1.1  joerg   QualType StrTy;
   1135      1.1  joerg   if (EncodedType->isDependentType())
   1136      1.1  joerg     StrTy = Context.DependentTy;
   1137      1.1  joerg   else {
   1138      1.1  joerg     if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled.
   1139      1.1  joerg         !EncodedType->isVoidType()) // void is handled too.
   1140      1.1  joerg       if (RequireCompleteType(AtLoc, EncodedType,
   1141      1.1  joerg                               diag::err_incomplete_type_objc_at_encode,
   1142      1.1  joerg                               EncodedTypeInfo->getTypeLoc()))
   1143      1.1  joerg         return ExprError();
   1144      1.1  joerg 
   1145      1.1  joerg     std::string Str;
   1146      1.1  joerg     QualType NotEncodedT;
   1147      1.1  joerg     Context.getObjCEncodingForType(EncodedType, Str, nullptr, &NotEncodedT);
   1148      1.1  joerg     if (!NotEncodedT.isNull())
   1149      1.1  joerg       Diag(AtLoc, diag::warn_incomplete_encoded_type)
   1150      1.1  joerg         << EncodedType << NotEncodedT;
   1151      1.1  joerg 
   1152      1.1  joerg     // The type of @encode is the same as the type of the corresponding string,
   1153      1.1  joerg     // which is an array type.
   1154      1.1  joerg     StrTy = Context.getStringLiteralArrayType(Context.CharTy, Str.size());
   1155      1.1  joerg   }
   1156      1.1  joerg 
   1157      1.1  joerg   return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc);
   1158      1.1  joerg }
   1159      1.1  joerg 
   1160      1.1  joerg ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc,
   1161      1.1  joerg                                            SourceLocation EncodeLoc,
   1162      1.1  joerg                                            SourceLocation LParenLoc,
   1163      1.1  joerg                                            ParsedType ty,
   1164      1.1  joerg                                            SourceLocation RParenLoc) {
   1165      1.1  joerg   // FIXME: Preserve type source info ?
   1166      1.1  joerg   TypeSourceInfo *TInfo;
   1167      1.1  joerg   QualType EncodedType = GetTypeFromParser(ty, &TInfo);
   1168      1.1  joerg   if (!TInfo)
   1169      1.1  joerg     TInfo = Context.getTrivialTypeSourceInfo(EncodedType,
   1170      1.1  joerg                                              getLocForEndOfToken(LParenLoc));
   1171      1.1  joerg 
   1172      1.1  joerg   return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc);
   1173      1.1  joerg }
   1174      1.1  joerg 
   1175      1.1  joerg static bool HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
   1176      1.1  joerg                                                SourceLocation AtLoc,
   1177      1.1  joerg                                                SourceLocation LParenLoc,
   1178      1.1  joerg                                                SourceLocation RParenLoc,
   1179      1.1  joerg                                                ObjCMethodDecl *Method,
   1180      1.1  joerg                                                ObjCMethodList &MethList) {
   1181      1.1  joerg   ObjCMethodList *M = &MethList;
   1182      1.1  joerg   bool Warned = false;
   1183      1.1  joerg   for (M = M->getNext(); M; M=M->getNext()) {
   1184      1.1  joerg     ObjCMethodDecl *MatchingMethodDecl = M->getMethod();
   1185      1.1  joerg     if (MatchingMethodDecl == Method ||
   1186      1.1  joerg         isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()) ||
   1187      1.1  joerg         MatchingMethodDecl->getSelector() != Method->getSelector())
   1188      1.1  joerg       continue;
   1189      1.1  joerg     if (!S.MatchTwoMethodDeclarations(Method,
   1190      1.1  joerg                                       MatchingMethodDecl, Sema::MMS_loose)) {
   1191      1.1  joerg       if (!Warned) {
   1192      1.1  joerg         Warned = true;
   1193      1.1  joerg         S.Diag(AtLoc, diag::warn_multiple_selectors)
   1194      1.1  joerg           << Method->getSelector() << FixItHint::CreateInsertion(LParenLoc, "(")
   1195      1.1  joerg           << FixItHint::CreateInsertion(RParenLoc, ")");
   1196      1.1  joerg         S.Diag(Method->getLocation(), diag::note_method_declared_at)
   1197      1.1  joerg           << Method->getDeclName();
   1198      1.1  joerg       }
   1199      1.1  joerg       S.Diag(MatchingMethodDecl->getLocation(), diag::note_method_declared_at)
   1200      1.1  joerg         << MatchingMethodDecl->getDeclName();
   1201      1.1  joerg     }
   1202      1.1  joerg   }
   1203      1.1  joerg   return Warned;
   1204      1.1  joerg }
   1205      1.1  joerg 
   1206      1.1  joerg static void DiagnoseMismatchedSelectors(Sema &S, SourceLocation AtLoc,
   1207      1.1  joerg                                         ObjCMethodDecl *Method,
   1208      1.1  joerg                                         SourceLocation LParenLoc,
   1209      1.1  joerg                                         SourceLocation RParenLoc,
   1210      1.1  joerg                                         bool WarnMultipleSelectors) {
   1211      1.1  joerg   if (!WarnMultipleSelectors ||
   1212      1.1  joerg       S.Diags.isIgnored(diag::warn_multiple_selectors, SourceLocation()))
   1213      1.1  joerg     return;
   1214      1.1  joerg   bool Warned = false;
   1215      1.1  joerg   for (Sema::GlobalMethodPool::iterator b = S.MethodPool.begin(),
   1216      1.1  joerg        e = S.MethodPool.end(); b != e; b++) {
   1217      1.1  joerg     // first, instance methods
   1218      1.1  joerg     ObjCMethodList &InstMethList = b->second.first;
   1219      1.1  joerg     if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc,
   1220      1.1  joerg                                                       Method, InstMethList))
   1221      1.1  joerg       Warned = true;
   1222      1.1  joerg 
   1223      1.1  joerg     // second, class methods
   1224      1.1  joerg     ObjCMethodList &ClsMethList = b->second.second;
   1225      1.1  joerg     if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc,
   1226      1.1  joerg                                                       Method, ClsMethList) || Warned)
   1227      1.1  joerg       return;
   1228      1.1  joerg   }
   1229      1.1  joerg }
   1230      1.1  joerg 
   1231  1.1.1.2  joerg static ObjCMethodDecl *LookupDirectMethodInMethodList(Sema &S, Selector Sel,
   1232  1.1.1.2  joerg                                                       ObjCMethodList &MethList,
   1233  1.1.1.2  joerg                                                       bool &onlyDirect,
   1234  1.1.1.2  joerg                                                       bool &anyDirect) {
   1235  1.1.1.2  joerg   (void)Sel;
   1236  1.1.1.2  joerg   ObjCMethodList *M = &MethList;
   1237  1.1.1.2  joerg   ObjCMethodDecl *DirectMethod = nullptr;
   1238  1.1.1.2  joerg   for (; M; M = M->getNext()) {
   1239  1.1.1.2  joerg     ObjCMethodDecl *Method = M->getMethod();
   1240  1.1.1.2  joerg     if (!Method)
   1241  1.1.1.2  joerg       continue;
   1242  1.1.1.2  joerg     assert(Method->getSelector() == Sel && "Method with wrong selector in method list");
   1243  1.1.1.2  joerg     if (Method->isDirectMethod()) {
   1244  1.1.1.2  joerg       anyDirect = true;
   1245  1.1.1.2  joerg       DirectMethod = Method;
   1246  1.1.1.2  joerg     } else
   1247  1.1.1.2  joerg       onlyDirect = false;
   1248  1.1.1.2  joerg   }
   1249  1.1.1.2  joerg 
   1250  1.1.1.2  joerg   return DirectMethod;
   1251  1.1.1.2  joerg }
   1252  1.1.1.2  joerg 
   1253  1.1.1.2  joerg // Search the global pool for (potentially) direct methods matching the given
   1254  1.1.1.2  joerg // selector. If a non-direct method is found, set \param onlyDirect to false. If
   1255  1.1.1.2  joerg // a direct method is found, set \param anyDirect to true. Returns a direct
   1256  1.1.1.2  joerg // method, if any.
   1257  1.1.1.2  joerg static ObjCMethodDecl *LookupDirectMethodInGlobalPool(Sema &S, Selector Sel,
   1258  1.1.1.2  joerg                                                       bool &onlyDirect,
   1259  1.1.1.2  joerg                                                       bool &anyDirect) {
   1260  1.1.1.2  joerg   auto Iter = S.MethodPool.find(Sel);
   1261  1.1.1.2  joerg   if (Iter == S.MethodPool.end())
   1262  1.1.1.2  joerg     return nullptr;
   1263  1.1.1.2  joerg 
   1264  1.1.1.2  joerg   ObjCMethodDecl *DirectInstance = LookupDirectMethodInMethodList(
   1265  1.1.1.2  joerg       S, Sel, Iter->second.first, onlyDirect, anyDirect);
   1266  1.1.1.2  joerg   ObjCMethodDecl *DirectClass = LookupDirectMethodInMethodList(
   1267  1.1.1.2  joerg       S, Sel, Iter->second.second, onlyDirect, anyDirect);
   1268  1.1.1.2  joerg 
   1269  1.1.1.2  joerg   return DirectInstance ? DirectInstance : DirectClass;
   1270  1.1.1.2  joerg }
   1271  1.1.1.2  joerg 
   1272  1.1.1.2  joerg static ObjCMethodDecl *findMethodInCurrentClass(Sema &S, Selector Sel) {
   1273  1.1.1.2  joerg   auto *CurMD = S.getCurMethodDecl();
   1274  1.1.1.2  joerg   if (!CurMD)
   1275  1.1.1.2  joerg     return nullptr;
   1276  1.1.1.2  joerg   ObjCInterfaceDecl *IFace = CurMD->getClassInterface();
   1277  1.1.1.2  joerg 
   1278  1.1.1.2  joerg   // The language enforce that only one direct method is present in a given
   1279  1.1.1.2  joerg   // class, so we just need to find one method in the current class to know
   1280  1.1.1.2  joerg   // whether Sel is potentially direct in this context.
   1281  1.1.1.2  joerg   if (ObjCMethodDecl *MD = IFace->lookupMethod(Sel, /*isInstance=*/true))
   1282  1.1.1.2  joerg     return MD;
   1283  1.1.1.2  joerg   if (ObjCMethodDecl *MD = IFace->lookupPrivateMethod(Sel, /*isInstance=*/true))
   1284  1.1.1.2  joerg     return MD;
   1285  1.1.1.2  joerg   if (ObjCMethodDecl *MD = IFace->lookupMethod(Sel, /*isInstance=*/false))
   1286  1.1.1.2  joerg     return MD;
   1287  1.1.1.2  joerg   if (ObjCMethodDecl *MD = IFace->lookupPrivateMethod(Sel, /*isInstance=*/false))
   1288  1.1.1.2  joerg     return MD;
   1289  1.1.1.2  joerg 
   1290  1.1.1.2  joerg   return nullptr;
   1291  1.1.1.2  joerg }
   1292  1.1.1.2  joerg 
   1293      1.1  joerg ExprResult Sema::ParseObjCSelectorExpression(Selector Sel,
   1294      1.1  joerg                                              SourceLocation AtLoc,
   1295      1.1  joerg                                              SourceLocation SelLoc,
   1296      1.1  joerg                                              SourceLocation LParenLoc,
   1297      1.1  joerg                                              SourceLocation RParenLoc,
   1298      1.1  joerg                                              bool WarnMultipleSelectors) {
   1299      1.1  joerg   ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel,
   1300      1.1  joerg                              SourceRange(LParenLoc, RParenLoc));
   1301      1.1  joerg   if (!Method)
   1302      1.1  joerg     Method = LookupFactoryMethodInGlobalPool(Sel,
   1303      1.1  joerg                                           SourceRange(LParenLoc, RParenLoc));
   1304      1.1  joerg   if (!Method) {
   1305      1.1  joerg     if (const ObjCMethodDecl *OM = SelectorsForTypoCorrection(Sel)) {
   1306      1.1  joerg       Selector MatchedSel = OM->getSelector();
   1307      1.1  joerg       SourceRange SelectorRange(LParenLoc.getLocWithOffset(1),
   1308      1.1  joerg                                 RParenLoc.getLocWithOffset(-1));
   1309      1.1  joerg       Diag(SelLoc, diag::warn_undeclared_selector_with_typo)
   1310      1.1  joerg         << Sel << MatchedSel
   1311      1.1  joerg         << FixItHint::CreateReplacement(SelectorRange, MatchedSel.getAsString());
   1312      1.1  joerg 
   1313      1.1  joerg     } else
   1314      1.1  joerg         Diag(SelLoc, diag::warn_undeclared_selector) << Sel;
   1315  1.1.1.2  joerg   } else {
   1316      1.1  joerg     DiagnoseMismatchedSelectors(*this, AtLoc, Method, LParenLoc, RParenLoc,
   1317      1.1  joerg                                 WarnMultipleSelectors);
   1318      1.1  joerg 
   1319  1.1.1.2  joerg     bool onlyDirect = true;
   1320  1.1.1.2  joerg     bool anyDirect = false;
   1321  1.1.1.2  joerg     ObjCMethodDecl *GlobalDirectMethod =
   1322  1.1.1.2  joerg         LookupDirectMethodInGlobalPool(*this, Sel, onlyDirect, anyDirect);
   1323  1.1.1.2  joerg 
   1324  1.1.1.2  joerg     if (onlyDirect) {
   1325  1.1.1.2  joerg       Diag(AtLoc, diag::err_direct_selector_expression)
   1326  1.1.1.2  joerg           << Method->getSelector();
   1327  1.1.1.2  joerg       Diag(Method->getLocation(), diag::note_direct_method_declared_at)
   1328  1.1.1.2  joerg           << Method->getDeclName();
   1329  1.1.1.2  joerg     } else if (anyDirect) {
   1330  1.1.1.2  joerg       // If we saw any direct methods, see if we see a direct member of the
   1331  1.1.1.2  joerg       // current class. If so, the @selector will likely be used to refer to
   1332  1.1.1.2  joerg       // this direct method.
   1333  1.1.1.2  joerg       ObjCMethodDecl *LikelyTargetMethod = findMethodInCurrentClass(*this, Sel);
   1334  1.1.1.2  joerg       if (LikelyTargetMethod && LikelyTargetMethod->isDirectMethod()) {
   1335  1.1.1.2  joerg         Diag(AtLoc, diag::warn_potentially_direct_selector_expression) << Sel;
   1336  1.1.1.2  joerg         Diag(LikelyTargetMethod->getLocation(),
   1337  1.1.1.2  joerg              diag::note_direct_method_declared_at)
   1338  1.1.1.2  joerg             << LikelyTargetMethod->getDeclName();
   1339  1.1.1.2  joerg       } else if (!LikelyTargetMethod) {
   1340  1.1.1.2  joerg         // Otherwise, emit the "strict" variant of this diagnostic, unless
   1341  1.1.1.2  joerg         // LikelyTargetMethod is non-direct.
   1342  1.1.1.2  joerg         Diag(AtLoc, diag::warn_strict_potentially_direct_selector_expression)
   1343  1.1.1.2  joerg             << Sel;
   1344  1.1.1.2  joerg         Diag(GlobalDirectMethod->getLocation(),
   1345  1.1.1.2  joerg              diag::note_direct_method_declared_at)
   1346  1.1.1.2  joerg             << GlobalDirectMethod->getDeclName();
   1347  1.1.1.2  joerg       }
   1348  1.1.1.2  joerg     }
   1349  1.1.1.2  joerg   }
   1350  1.1.1.2  joerg 
   1351      1.1  joerg   if (Method &&
   1352      1.1  joerg       Method->getImplementationControl() != ObjCMethodDecl::Optional &&
   1353      1.1  joerg       !getSourceManager().isInSystemHeader(Method->getLocation()))
   1354      1.1  joerg     ReferencedSelectors.insert(std::make_pair(Sel, AtLoc));
   1355      1.1  joerg 
   1356      1.1  joerg   // In ARC, forbid the user from using @selector for
   1357      1.1  joerg   // retain/release/autorelease/dealloc/retainCount.
   1358      1.1  joerg   if (getLangOpts().ObjCAutoRefCount) {
   1359      1.1  joerg     switch (Sel.getMethodFamily()) {
   1360      1.1  joerg     case OMF_retain:
   1361      1.1  joerg     case OMF_release:
   1362      1.1  joerg     case OMF_autorelease:
   1363      1.1  joerg     case OMF_retainCount:
   1364      1.1  joerg     case OMF_dealloc:
   1365      1.1  joerg       Diag(AtLoc, diag::err_arc_illegal_selector) <<
   1366      1.1  joerg         Sel << SourceRange(LParenLoc, RParenLoc);
   1367      1.1  joerg       break;
   1368      1.1  joerg 
   1369      1.1  joerg     case OMF_None:
   1370      1.1  joerg     case OMF_alloc:
   1371      1.1  joerg     case OMF_copy:
   1372      1.1  joerg     case OMF_finalize:
   1373      1.1  joerg     case OMF_init:
   1374      1.1  joerg     case OMF_mutableCopy:
   1375      1.1  joerg     case OMF_new:
   1376      1.1  joerg     case OMF_self:
   1377      1.1  joerg     case OMF_initialize:
   1378      1.1  joerg     case OMF_performSelector:
   1379      1.1  joerg       break;
   1380      1.1  joerg     }
   1381      1.1  joerg   }
   1382      1.1  joerg   QualType Ty = Context.getObjCSelType();
   1383      1.1  joerg   return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc);
   1384      1.1  joerg }
   1385      1.1  joerg 
   1386      1.1  joerg ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId,
   1387      1.1  joerg                                              SourceLocation AtLoc,
   1388      1.1  joerg                                              SourceLocation ProtoLoc,
   1389      1.1  joerg                                              SourceLocation LParenLoc,
   1390      1.1  joerg                                              SourceLocation ProtoIdLoc,
   1391      1.1  joerg                                              SourceLocation RParenLoc) {
   1392      1.1  joerg   ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoIdLoc);
   1393      1.1  joerg   if (!PDecl) {
   1394      1.1  joerg     Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId;
   1395      1.1  joerg     return true;
   1396      1.1  joerg   }
   1397  1.1.1.2  joerg   if (PDecl->isNonRuntimeProtocol())
   1398  1.1.1.2  joerg     Diag(ProtoLoc, diag::err_objc_non_runtime_protocol_in_protocol_expr)
   1399  1.1.1.2  joerg         << PDecl;
   1400      1.1  joerg   if (!PDecl->hasDefinition()) {
   1401      1.1  joerg     Diag(ProtoLoc, diag::err_atprotocol_protocol) << PDecl;
   1402      1.1  joerg     Diag(PDecl->getLocation(), diag::note_entity_declared_at) << PDecl;
   1403      1.1  joerg   } else {
   1404      1.1  joerg     PDecl = PDecl->getDefinition();
   1405      1.1  joerg   }
   1406      1.1  joerg 
   1407      1.1  joerg   QualType Ty = Context.getObjCProtoType();
   1408      1.1  joerg   if (Ty.isNull())
   1409      1.1  joerg     return true;
   1410      1.1  joerg   Ty = Context.getObjCObjectPointerType(Ty);
   1411      1.1  joerg   return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, ProtoIdLoc, RParenLoc);
   1412      1.1  joerg }
   1413      1.1  joerg 
   1414      1.1  joerg /// Try to capture an implicit reference to 'self'.
   1415      1.1  joerg ObjCMethodDecl *Sema::tryCaptureObjCSelf(SourceLocation Loc) {
   1416      1.1  joerg   DeclContext *DC = getFunctionLevelDeclContext();
   1417      1.1  joerg 
   1418      1.1  joerg   // If we're not in an ObjC method, error out.  Note that, unlike the
   1419      1.1  joerg   // C++ case, we don't require an instance method --- class methods
   1420      1.1  joerg   // still have a 'self', and we really do still need to capture it!
   1421      1.1  joerg   ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC);
   1422      1.1  joerg   if (!method)
   1423      1.1  joerg     return nullptr;
   1424      1.1  joerg 
   1425      1.1  joerg   tryCaptureVariable(method->getSelfDecl(), Loc);
   1426      1.1  joerg 
   1427      1.1  joerg   return method;
   1428      1.1  joerg }
   1429      1.1  joerg 
   1430      1.1  joerg static QualType stripObjCInstanceType(ASTContext &Context, QualType T) {
   1431      1.1  joerg   QualType origType = T;
   1432      1.1  joerg   if (auto nullability = AttributedType::stripOuterNullability(T)) {
   1433      1.1  joerg     if (T == Context.getObjCInstanceType()) {
   1434      1.1  joerg       return Context.getAttributedType(
   1435      1.1  joerg                AttributedType::getNullabilityAttrKind(*nullability),
   1436      1.1  joerg                Context.getObjCIdType(),
   1437      1.1  joerg                Context.getObjCIdType());
   1438      1.1  joerg     }
   1439      1.1  joerg 
   1440      1.1  joerg     return origType;
   1441      1.1  joerg   }
   1442      1.1  joerg 
   1443      1.1  joerg   if (T == Context.getObjCInstanceType())
   1444      1.1  joerg     return Context.getObjCIdType();
   1445      1.1  joerg 
   1446      1.1  joerg   return origType;
   1447      1.1  joerg }
   1448      1.1  joerg 
   1449      1.1  joerg /// Determine the result type of a message send based on the receiver type,
   1450      1.1  joerg /// method, and the kind of message send.
   1451      1.1  joerg ///
   1452      1.1  joerg /// This is the "base" result type, which will still need to be adjusted
   1453      1.1  joerg /// to account for nullability.
   1454      1.1  joerg static QualType getBaseMessageSendResultType(Sema &S,
   1455      1.1  joerg                                              QualType ReceiverType,
   1456      1.1  joerg                                              ObjCMethodDecl *Method,
   1457      1.1  joerg                                              bool isClassMessage,
   1458      1.1  joerg                                              bool isSuperMessage) {
   1459      1.1  joerg   assert(Method && "Must have a method");
   1460      1.1  joerg   if (!Method->hasRelatedResultType())
   1461      1.1  joerg     return Method->getSendResultType(ReceiverType);
   1462      1.1  joerg 
   1463      1.1  joerg   ASTContext &Context = S.Context;
   1464      1.1  joerg 
   1465      1.1  joerg   // Local function that transfers the nullability of the method's
   1466      1.1  joerg   // result type to the returned result.
   1467      1.1  joerg   auto transferNullability = [&](QualType type) -> QualType {
   1468      1.1  joerg     // If the method's result type has nullability, extract it.
   1469      1.1  joerg     if (auto nullability = Method->getSendResultType(ReceiverType)
   1470      1.1  joerg                              ->getNullability(Context)){
   1471      1.1  joerg       // Strip off any outer nullability sugar from the provided type.
   1472      1.1  joerg       (void)AttributedType::stripOuterNullability(type);
   1473      1.1  joerg 
   1474      1.1  joerg       // Form a new attributed type using the method result type's nullability.
   1475      1.1  joerg       return Context.getAttributedType(
   1476      1.1  joerg                AttributedType::getNullabilityAttrKind(*nullability),
   1477      1.1  joerg                type,
   1478      1.1  joerg                type);
   1479      1.1  joerg     }
   1480      1.1  joerg 
   1481      1.1  joerg     return type;
   1482      1.1  joerg   };
   1483      1.1  joerg 
   1484      1.1  joerg   // If a method has a related return type:
   1485      1.1  joerg   //   - if the method found is an instance method, but the message send
   1486      1.1  joerg   //     was a class message send, T is the declared return type of the method
   1487      1.1  joerg   //     found
   1488      1.1  joerg   if (Method->isInstanceMethod() && isClassMessage)
   1489      1.1  joerg     return stripObjCInstanceType(Context,
   1490      1.1  joerg                                  Method->getSendResultType(ReceiverType));
   1491      1.1  joerg 
   1492      1.1  joerg   //   - if the receiver is super, T is a pointer to the class of the
   1493      1.1  joerg   //     enclosing method definition
   1494      1.1  joerg   if (isSuperMessage) {
   1495      1.1  joerg     if (ObjCMethodDecl *CurMethod = S.getCurMethodDecl())
   1496      1.1  joerg       if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface()) {
   1497      1.1  joerg         return transferNullability(
   1498      1.1  joerg                  Context.getObjCObjectPointerType(
   1499      1.1  joerg                    Context.getObjCInterfaceType(Class)));
   1500      1.1  joerg       }
   1501      1.1  joerg   }
   1502      1.1  joerg 
   1503      1.1  joerg   //   - if the receiver is the name of a class U, T is a pointer to U
   1504      1.1  joerg   if (ReceiverType->getAsObjCInterfaceType())
   1505      1.1  joerg     return transferNullability(Context.getObjCObjectPointerType(ReceiverType));
   1506      1.1  joerg   //   - if the receiver is of type Class or qualified Class type,
   1507      1.1  joerg   //     T is the declared return type of the method.
   1508      1.1  joerg   if (ReceiverType->isObjCClassType() ||
   1509      1.1  joerg       ReceiverType->isObjCQualifiedClassType())
   1510      1.1  joerg     return stripObjCInstanceType(Context,
   1511      1.1  joerg                                  Method->getSendResultType(ReceiverType));
   1512      1.1  joerg 
   1513      1.1  joerg   //   - if the receiver is id, qualified id, Class, or qualified Class, T
   1514      1.1  joerg   //     is the receiver type, otherwise
   1515      1.1  joerg   //   - T is the type of the receiver expression.
   1516      1.1  joerg   return transferNullability(ReceiverType);
   1517      1.1  joerg }
   1518      1.1  joerg 
   1519      1.1  joerg QualType Sema::getMessageSendResultType(const Expr *Receiver,
   1520      1.1  joerg                                         QualType ReceiverType,
   1521      1.1  joerg                                         ObjCMethodDecl *Method,
   1522      1.1  joerg                                         bool isClassMessage,
   1523      1.1  joerg                                         bool isSuperMessage) {
   1524      1.1  joerg   // Produce the result type.
   1525      1.1  joerg   QualType resultType = getBaseMessageSendResultType(*this, ReceiverType,
   1526      1.1  joerg                                                      Method,
   1527      1.1  joerg                                                      isClassMessage,
   1528      1.1  joerg                                                      isSuperMessage);
   1529      1.1  joerg 
   1530      1.1  joerg   // If this is a class message, ignore the nullability of the receiver.
   1531      1.1  joerg   if (isClassMessage) {
   1532      1.1  joerg     // In a class method, class messages to 'self' that return instancetype can
   1533      1.1  joerg     // be typed as the current class.  We can safely do this in ARC because self
   1534      1.1  joerg     // can't be reassigned, and we do it unsafely outside of ARC because in
   1535      1.1  joerg     // practice people never reassign self in class methods and there's some
   1536      1.1  joerg     // virtue in not being aggressively pedantic.
   1537      1.1  joerg     if (Receiver && Receiver->isObjCSelfExpr()) {
   1538      1.1  joerg       assert(ReceiverType->isObjCClassType() && "expected a Class self");
   1539      1.1  joerg       QualType T = Method->getSendResultType(ReceiverType);
   1540      1.1  joerg       AttributedType::stripOuterNullability(T);
   1541      1.1  joerg       if (T == Context.getObjCInstanceType()) {
   1542      1.1  joerg         const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(
   1543      1.1  joerg             cast<ImplicitParamDecl>(
   1544      1.1  joerg                 cast<DeclRefExpr>(Receiver->IgnoreParenImpCasts())->getDecl())
   1545      1.1  joerg                 ->getDeclContext());
   1546      1.1  joerg         assert(MD->isClassMethod() && "expected a class method");
   1547      1.1  joerg         QualType NewResultType = Context.getObjCObjectPointerType(
   1548      1.1  joerg             Context.getObjCInterfaceType(MD->getClassInterface()));
   1549      1.1  joerg         if (auto Nullability = resultType->getNullability(Context))
   1550      1.1  joerg           NewResultType = Context.getAttributedType(
   1551      1.1  joerg               AttributedType::getNullabilityAttrKind(*Nullability),
   1552      1.1  joerg               NewResultType, NewResultType);
   1553      1.1  joerg         return NewResultType;
   1554      1.1  joerg       }
   1555      1.1  joerg     }
   1556      1.1  joerg     return resultType;
   1557      1.1  joerg   }
   1558      1.1  joerg 
   1559      1.1  joerg   // There is nothing left to do if the result type cannot have a nullability
   1560      1.1  joerg   // specifier.
   1561      1.1  joerg   if (!resultType->canHaveNullability())
   1562      1.1  joerg     return resultType;
   1563      1.1  joerg 
   1564      1.1  joerg   // Map the nullability of the result into a table index.
   1565      1.1  joerg   unsigned receiverNullabilityIdx = 0;
   1566  1.1.1.2  joerg   if (Optional<NullabilityKind> nullability =
   1567  1.1.1.2  joerg           ReceiverType->getNullability(Context)) {
   1568  1.1.1.2  joerg     if (*nullability == NullabilityKind::NullableResult)
   1569  1.1.1.2  joerg       nullability = NullabilityKind::Nullable;
   1570      1.1  joerg     receiverNullabilityIdx = 1 + static_cast<unsigned>(*nullability);
   1571  1.1.1.2  joerg   }
   1572      1.1  joerg 
   1573      1.1  joerg   unsigned resultNullabilityIdx = 0;
   1574  1.1.1.2  joerg   if (Optional<NullabilityKind> nullability =
   1575  1.1.1.2  joerg           resultType->getNullability(Context)) {
   1576  1.1.1.2  joerg     if (*nullability == NullabilityKind::NullableResult)
   1577  1.1.1.2  joerg       nullability = NullabilityKind::Nullable;
   1578      1.1  joerg     resultNullabilityIdx = 1 + static_cast<unsigned>(*nullability);
   1579  1.1.1.2  joerg   }
   1580      1.1  joerg 
   1581      1.1  joerg   // The table of nullability mappings, indexed by the receiver's nullability
   1582      1.1  joerg   // and then the result type's nullability.
   1583      1.1  joerg   static const uint8_t None = 0;
   1584      1.1  joerg   static const uint8_t NonNull = 1;
   1585      1.1  joerg   static const uint8_t Nullable = 2;
   1586      1.1  joerg   static const uint8_t Unspecified = 3;
   1587      1.1  joerg   static const uint8_t nullabilityMap[4][4] = {
   1588      1.1  joerg     //                  None        NonNull       Nullable    Unspecified
   1589      1.1  joerg     /* None */        { None,       None,         Nullable,   None },
   1590      1.1  joerg     /* NonNull */     { None,       NonNull,      Nullable,   Unspecified },
   1591      1.1  joerg     /* Nullable */    { Nullable,   Nullable,     Nullable,   Nullable },
   1592      1.1  joerg     /* Unspecified */ { None,       Unspecified,  Nullable,   Unspecified }
   1593      1.1  joerg   };
   1594      1.1  joerg 
   1595      1.1  joerg   unsigned newResultNullabilityIdx
   1596      1.1  joerg     = nullabilityMap[receiverNullabilityIdx][resultNullabilityIdx];
   1597      1.1  joerg   if (newResultNullabilityIdx == resultNullabilityIdx)
   1598      1.1  joerg     return resultType;
   1599      1.1  joerg 
   1600      1.1  joerg   // Strip off the existing nullability. This removes as little type sugar as
   1601      1.1  joerg   // possible.
   1602      1.1  joerg   do {
   1603      1.1  joerg     if (auto attributed = dyn_cast<AttributedType>(resultType.getTypePtr())) {
   1604      1.1  joerg       resultType = attributed->getModifiedType();
   1605      1.1  joerg     } else {
   1606      1.1  joerg       resultType = resultType.getDesugaredType(Context);
   1607      1.1  joerg     }
   1608      1.1  joerg   } while (resultType->getNullability(Context));
   1609      1.1  joerg 
   1610      1.1  joerg   // Add nullability back if needed.
   1611      1.1  joerg   if (newResultNullabilityIdx > 0) {
   1612      1.1  joerg     auto newNullability
   1613      1.1  joerg       = static_cast<NullabilityKind>(newResultNullabilityIdx-1);
   1614      1.1  joerg     return Context.getAttributedType(
   1615      1.1  joerg              AttributedType::getNullabilityAttrKind(newNullability),
   1616      1.1  joerg              resultType, resultType);
   1617      1.1  joerg   }
   1618      1.1  joerg 
   1619      1.1  joerg   return resultType;
   1620      1.1  joerg }
   1621      1.1  joerg 
   1622      1.1  joerg /// Look for an ObjC method whose result type exactly matches the given type.
   1623      1.1  joerg static const ObjCMethodDecl *
   1624      1.1  joerg findExplicitInstancetypeDeclarer(const ObjCMethodDecl *MD,
   1625      1.1  joerg                                  QualType instancetype) {
   1626      1.1  joerg   if (MD->getReturnType() == instancetype)
   1627      1.1  joerg     return MD;
   1628      1.1  joerg 
   1629      1.1  joerg   // For these purposes, a method in an @implementation overrides a
   1630      1.1  joerg   // declaration in the @interface.
   1631      1.1  joerg   if (const ObjCImplDecl *impl =
   1632      1.1  joerg         dyn_cast<ObjCImplDecl>(MD->getDeclContext())) {
   1633      1.1  joerg     const ObjCContainerDecl *iface;
   1634      1.1  joerg     if (const ObjCCategoryImplDecl *catImpl =
   1635      1.1  joerg           dyn_cast<ObjCCategoryImplDecl>(impl)) {
   1636      1.1  joerg       iface = catImpl->getCategoryDecl();
   1637      1.1  joerg     } else {
   1638      1.1  joerg       iface = impl->getClassInterface();
   1639      1.1  joerg     }
   1640      1.1  joerg 
   1641      1.1  joerg     const ObjCMethodDecl *ifaceMD =
   1642      1.1  joerg       iface->getMethod(MD->getSelector(), MD->isInstanceMethod());
   1643      1.1  joerg     if (ifaceMD) return findExplicitInstancetypeDeclarer(ifaceMD, instancetype);
   1644      1.1  joerg   }
   1645      1.1  joerg 
   1646      1.1  joerg   SmallVector<const ObjCMethodDecl *, 4> overrides;
   1647      1.1  joerg   MD->getOverriddenMethods(overrides);
   1648      1.1  joerg   for (unsigned i = 0, e = overrides.size(); i != e; ++i) {
   1649      1.1  joerg     if (const ObjCMethodDecl *result =
   1650      1.1  joerg           findExplicitInstancetypeDeclarer(overrides[i], instancetype))
   1651      1.1  joerg       return result;
   1652      1.1  joerg   }
   1653      1.1  joerg 
   1654      1.1  joerg   return nullptr;
   1655      1.1  joerg }
   1656      1.1  joerg 
   1657      1.1  joerg void Sema::EmitRelatedResultTypeNoteForReturn(QualType destType) {
   1658      1.1  joerg   // Only complain if we're in an ObjC method and the required return
   1659      1.1  joerg   // type doesn't match the method's declared return type.
   1660      1.1  joerg   ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurContext);
   1661      1.1  joerg   if (!MD || !MD->hasRelatedResultType() ||
   1662      1.1  joerg       Context.hasSameUnqualifiedType(destType, MD->getReturnType()))
   1663      1.1  joerg     return;
   1664      1.1  joerg 
   1665      1.1  joerg   // Look for a method overridden by this method which explicitly uses
   1666      1.1  joerg   // 'instancetype'.
   1667      1.1  joerg   if (const ObjCMethodDecl *overridden =
   1668      1.1  joerg         findExplicitInstancetypeDeclarer(MD, Context.getObjCInstanceType())) {
   1669      1.1  joerg     SourceRange range = overridden->getReturnTypeSourceRange();
   1670      1.1  joerg     SourceLocation loc = range.getBegin();
   1671      1.1  joerg     if (loc.isInvalid())
   1672      1.1  joerg       loc = overridden->getLocation();
   1673      1.1  joerg     Diag(loc, diag::note_related_result_type_explicit)
   1674      1.1  joerg       << /*current method*/ 1 << range;
   1675      1.1  joerg     return;
   1676      1.1  joerg   }
   1677      1.1  joerg 
   1678      1.1  joerg   // Otherwise, if we have an interesting method family, note that.
   1679      1.1  joerg   // This should always trigger if the above didn't.
   1680      1.1  joerg   if (ObjCMethodFamily family = MD->getMethodFamily())
   1681      1.1  joerg     Diag(MD->getLocation(), diag::note_related_result_type_family)
   1682      1.1  joerg       << /*current method*/ 1
   1683      1.1  joerg       << family;
   1684      1.1  joerg }
   1685      1.1  joerg 
   1686      1.1  joerg void Sema::EmitRelatedResultTypeNote(const Expr *E) {
   1687      1.1  joerg   E = E->IgnoreParenImpCasts();
   1688      1.1  joerg   const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E);
   1689      1.1  joerg   if (!MsgSend)
   1690      1.1  joerg     return;
   1691      1.1  joerg 
   1692      1.1  joerg   const ObjCMethodDecl *Method = MsgSend->getMethodDecl();
   1693      1.1  joerg   if (!Method)
   1694      1.1  joerg     return;
   1695      1.1  joerg 
   1696      1.1  joerg   if (!Method->hasRelatedResultType())
   1697      1.1  joerg     return;
   1698      1.1  joerg 
   1699      1.1  joerg   if (Context.hasSameUnqualifiedType(
   1700      1.1  joerg           Method->getReturnType().getNonReferenceType(), MsgSend->getType()))
   1701      1.1  joerg     return;
   1702      1.1  joerg 
   1703      1.1  joerg   if (!Context.hasSameUnqualifiedType(Method->getReturnType(),
   1704      1.1  joerg                                       Context.getObjCInstanceType()))
   1705      1.1  joerg     return;
   1706      1.1  joerg 
   1707      1.1  joerg   Diag(Method->getLocation(), diag::note_related_result_type_inferred)
   1708      1.1  joerg     << Method->isInstanceMethod() << Method->getSelector()
   1709      1.1  joerg     << MsgSend->getType();
   1710      1.1  joerg }
   1711      1.1  joerg 
   1712      1.1  joerg bool Sema::CheckMessageArgumentTypes(
   1713      1.1  joerg     const Expr *Receiver, QualType ReceiverType, MultiExprArg Args,
   1714      1.1  joerg     Selector Sel, ArrayRef<SourceLocation> SelectorLocs, ObjCMethodDecl *Method,
   1715      1.1  joerg     bool isClassMessage, bool isSuperMessage, SourceLocation lbrac,
   1716      1.1  joerg     SourceLocation rbrac, SourceRange RecRange, QualType &ReturnType,
   1717      1.1  joerg     ExprValueKind &VK) {
   1718      1.1  joerg   SourceLocation SelLoc;
   1719      1.1  joerg   if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
   1720      1.1  joerg     SelLoc = SelectorLocs.front();
   1721      1.1  joerg   else
   1722      1.1  joerg     SelLoc = lbrac;
   1723      1.1  joerg 
   1724      1.1  joerg   if (!Method) {
   1725      1.1  joerg     // Apply default argument promotion as for (C99 6.5.2.2p6).
   1726      1.1  joerg     for (unsigned i = 0, e = Args.size(); i != e; i++) {
   1727      1.1  joerg       if (Args[i]->isTypeDependent())
   1728      1.1  joerg         continue;
   1729      1.1  joerg 
   1730      1.1  joerg       ExprResult result;
   1731      1.1  joerg       if (getLangOpts().DebuggerSupport) {
   1732      1.1  joerg         QualType paramTy; // ignored
   1733      1.1  joerg         result = checkUnknownAnyArg(SelLoc, Args[i], paramTy);
   1734      1.1  joerg       } else {
   1735      1.1  joerg         result = DefaultArgumentPromotion(Args[i]);
   1736      1.1  joerg       }
   1737      1.1  joerg       if (result.isInvalid())
   1738      1.1  joerg         return true;
   1739      1.1  joerg       Args[i] = result.get();
   1740      1.1  joerg     }
   1741      1.1  joerg 
   1742      1.1  joerg     unsigned DiagID;
   1743      1.1  joerg     if (getLangOpts().ObjCAutoRefCount)
   1744      1.1  joerg       DiagID = diag::err_arc_method_not_found;
   1745      1.1  joerg     else
   1746      1.1  joerg       DiagID = isClassMessage ? diag::warn_class_method_not_found
   1747      1.1  joerg                               : diag::warn_inst_method_not_found;
   1748      1.1  joerg     if (!getLangOpts().DebuggerSupport) {
   1749      1.1  joerg       const ObjCMethodDecl *OMD = SelectorsForTypoCorrection(Sel, ReceiverType);
   1750      1.1  joerg       if (OMD && !OMD->isInvalidDecl()) {
   1751      1.1  joerg         if (getLangOpts().ObjCAutoRefCount)
   1752      1.1  joerg           DiagID = diag::err_method_not_found_with_typo;
   1753      1.1  joerg         else
   1754      1.1  joerg           DiagID = isClassMessage ? diag::warn_class_method_not_found_with_typo
   1755      1.1  joerg                                   : diag::warn_instance_method_not_found_with_typo;
   1756      1.1  joerg         Selector MatchedSel = OMD->getSelector();
   1757      1.1  joerg         SourceRange SelectorRange(SelectorLocs.front(), SelectorLocs.back());
   1758      1.1  joerg         if (MatchedSel.isUnarySelector())
   1759      1.1  joerg           Diag(SelLoc, DiagID)
   1760      1.1  joerg             << Sel<< isClassMessage << MatchedSel
   1761      1.1  joerg             << FixItHint::CreateReplacement(SelectorRange, MatchedSel.getAsString());
   1762      1.1  joerg         else
   1763      1.1  joerg           Diag(SelLoc, DiagID) << Sel<< isClassMessage << MatchedSel;
   1764      1.1  joerg       }
   1765      1.1  joerg       else
   1766      1.1  joerg         Diag(SelLoc, DiagID)
   1767      1.1  joerg           << Sel << isClassMessage << SourceRange(SelectorLocs.front(),
   1768      1.1  joerg                                                 SelectorLocs.back());
   1769      1.1  joerg       // Find the class to which we are sending this message.
   1770  1.1.1.2  joerg       if (auto *ObjPT = ReceiverType->getAs<ObjCObjectPointerType>()) {
   1771  1.1.1.2  joerg         if (ObjCInterfaceDecl *ThisClass = ObjPT->getInterfaceDecl()) {
   1772      1.1  joerg           Diag(ThisClass->getLocation(), diag::note_receiver_class_declared);
   1773      1.1  joerg           if (!RecRange.isInvalid())
   1774      1.1  joerg             if (ThisClass->lookupClassMethod(Sel))
   1775  1.1.1.2  joerg               Diag(RecRange.getBegin(), diag::note_receiver_expr_here)
   1776  1.1.1.2  joerg                   << FixItHint::CreateReplacement(RecRange,
   1777  1.1.1.2  joerg                                                   ThisClass->getNameAsString());
   1778      1.1  joerg         }
   1779      1.1  joerg       }
   1780      1.1  joerg     }
   1781      1.1  joerg 
   1782      1.1  joerg     // In debuggers, we want to use __unknown_anytype for these
   1783      1.1  joerg     // results so that clients can cast them.
   1784      1.1  joerg     if (getLangOpts().DebuggerSupport) {
   1785      1.1  joerg       ReturnType = Context.UnknownAnyTy;
   1786      1.1  joerg     } else {
   1787      1.1  joerg       ReturnType = Context.getObjCIdType();
   1788      1.1  joerg     }
   1789      1.1  joerg     VK = VK_RValue;
   1790      1.1  joerg     return false;
   1791      1.1  joerg   }
   1792      1.1  joerg 
   1793      1.1  joerg   ReturnType = getMessageSendResultType(Receiver, ReceiverType, Method,
   1794      1.1  joerg                                         isClassMessage, isSuperMessage);
   1795      1.1  joerg   VK = Expr::getValueKindForType(Method->getReturnType());
   1796      1.1  joerg 
   1797      1.1  joerg   unsigned NumNamedArgs = Sel.getNumArgs();
   1798      1.1  joerg   // Method might have more arguments than selector indicates. This is due
   1799      1.1  joerg   // to addition of c-style arguments in method.
   1800      1.1  joerg   if (Method->param_size() > Sel.getNumArgs())
   1801      1.1  joerg     NumNamedArgs = Method->param_size();
   1802      1.1  joerg   // FIXME. This need be cleaned up.
   1803      1.1  joerg   if (Args.size() < NumNamedArgs) {
   1804      1.1  joerg     Diag(SelLoc, diag::err_typecheck_call_too_few_args)
   1805      1.1  joerg       << 2 << NumNamedArgs << static_cast<unsigned>(Args.size());
   1806      1.1  joerg     return false;
   1807      1.1  joerg   }
   1808      1.1  joerg 
   1809      1.1  joerg   // Compute the set of type arguments to be substituted into each parameter
   1810      1.1  joerg   // type.
   1811      1.1  joerg   Optional<ArrayRef<QualType>> typeArgs
   1812      1.1  joerg     = ReceiverType->getObjCSubstitutions(Method->getDeclContext());
   1813      1.1  joerg   bool IsError = false;
   1814      1.1  joerg   for (unsigned i = 0; i < NumNamedArgs; i++) {
   1815      1.1  joerg     // We can't do any type-checking on a type-dependent argument.
   1816      1.1  joerg     if (Args[i]->isTypeDependent())
   1817      1.1  joerg       continue;
   1818      1.1  joerg 
   1819      1.1  joerg     Expr *argExpr = Args[i];
   1820      1.1  joerg 
   1821      1.1  joerg     ParmVarDecl *param = Method->parameters()[i];
   1822      1.1  joerg     assert(argExpr && "CheckMessageArgumentTypes(): missing expression");
   1823      1.1  joerg 
   1824  1.1.1.2  joerg     if (param->hasAttr<NoEscapeAttr>() &&
   1825  1.1.1.2  joerg         param->getType()->isBlockPointerType())
   1826      1.1  joerg       if (auto *BE = dyn_cast<BlockExpr>(
   1827      1.1  joerg               argExpr->IgnoreParenNoopCasts(Context)))
   1828      1.1  joerg         BE->getBlockDecl()->setDoesNotEscape();
   1829      1.1  joerg 
   1830      1.1  joerg     // Strip the unbridged-cast placeholder expression off unless it's
   1831      1.1  joerg     // a consumed argument.
   1832      1.1  joerg     if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) &&
   1833      1.1  joerg         !param->hasAttr<CFConsumedAttr>())
   1834      1.1  joerg       argExpr = stripARCUnbridgedCast(argExpr);
   1835      1.1  joerg 
   1836      1.1  joerg     // If the parameter is __unknown_anytype, infer its type
   1837      1.1  joerg     // from the argument.
   1838      1.1  joerg     if (param->getType() == Context.UnknownAnyTy) {
   1839      1.1  joerg       QualType paramType;
   1840      1.1  joerg       ExprResult argE = checkUnknownAnyArg(SelLoc, argExpr, paramType);
   1841      1.1  joerg       if (argE.isInvalid()) {
   1842      1.1  joerg         IsError = true;
   1843      1.1  joerg       } else {
   1844      1.1  joerg         Args[i] = argE.get();
   1845      1.1  joerg 
   1846      1.1  joerg         // Update the parameter type in-place.
   1847      1.1  joerg         param->setType(paramType);
   1848      1.1  joerg       }
   1849      1.1  joerg       continue;
   1850      1.1  joerg     }
   1851      1.1  joerg 
   1852      1.1  joerg     QualType origParamType = param->getType();
   1853      1.1  joerg     QualType paramType = param->getType();
   1854      1.1  joerg     if (typeArgs)
   1855      1.1  joerg       paramType = paramType.substObjCTypeArgs(
   1856      1.1  joerg                     Context,
   1857      1.1  joerg                     *typeArgs,
   1858      1.1  joerg                     ObjCSubstitutionContext::Parameter);
   1859      1.1  joerg 
   1860      1.1  joerg     if (RequireCompleteType(argExpr->getSourceRange().getBegin(),
   1861      1.1  joerg                             paramType,
   1862      1.1  joerg                             diag::err_call_incomplete_argument, argExpr))
   1863      1.1  joerg       return true;
   1864      1.1  joerg 
   1865      1.1  joerg     InitializedEntity Entity
   1866      1.1  joerg       = InitializedEntity::InitializeParameter(Context, param, paramType);
   1867      1.1  joerg     ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), argExpr);
   1868      1.1  joerg     if (ArgE.isInvalid())
   1869      1.1  joerg       IsError = true;
   1870      1.1  joerg     else {
   1871      1.1  joerg       Args[i] = ArgE.getAs<Expr>();
   1872      1.1  joerg 
   1873      1.1  joerg       // If we are type-erasing a block to a block-compatible
   1874      1.1  joerg       // Objective-C pointer type, we may need to extend the lifetime
   1875      1.1  joerg       // of the block object.
   1876      1.1  joerg       if (typeArgs && Args[i]->isRValue() && paramType->isBlockPointerType() &&
   1877      1.1  joerg           Args[i]->getType()->isBlockPointerType() &&
   1878      1.1  joerg           origParamType->isObjCObjectPointerType()) {
   1879      1.1  joerg         ExprResult arg = Args[i];
   1880      1.1  joerg         maybeExtendBlockObject(arg);
   1881      1.1  joerg         Args[i] = arg.get();
   1882      1.1  joerg       }
   1883      1.1  joerg     }
   1884      1.1  joerg   }
   1885      1.1  joerg 
   1886      1.1  joerg   // Promote additional arguments to variadic methods.
   1887      1.1  joerg   if (Method->isVariadic()) {
   1888      1.1  joerg     for (unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) {
   1889      1.1  joerg       if (Args[i]->isTypeDependent())
   1890      1.1  joerg         continue;
   1891      1.1  joerg 
   1892      1.1  joerg       ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod,
   1893      1.1  joerg                                                         nullptr);
   1894      1.1  joerg       IsError |= Arg.isInvalid();
   1895      1.1  joerg       Args[i] = Arg.get();
   1896      1.1  joerg     }
   1897      1.1  joerg   } else {
   1898      1.1  joerg     // Check for extra arguments to non-variadic methods.
   1899      1.1  joerg     if (Args.size() != NumNamedArgs) {
   1900      1.1  joerg       Diag(Args[NumNamedArgs]->getBeginLoc(),
   1901      1.1  joerg            diag::err_typecheck_call_too_many_args)
   1902      1.1  joerg           << 2 /*method*/ << NumNamedArgs << static_cast<unsigned>(Args.size())
   1903      1.1  joerg           << Method->getSourceRange()
   1904      1.1  joerg           << SourceRange(Args[NumNamedArgs]->getBeginLoc(),
   1905      1.1  joerg                          Args.back()->getEndLoc());
   1906      1.1  joerg     }
   1907      1.1  joerg   }
   1908      1.1  joerg 
   1909      1.1  joerg   DiagnoseSentinelCalls(Method, SelLoc, Args);
   1910      1.1  joerg 
   1911      1.1  joerg   // Do additional checkings on method.
   1912      1.1  joerg   IsError |= CheckObjCMethodCall(
   1913      1.1  joerg       Method, SelLoc, makeArrayRef(Args.data(), Args.size()));
   1914      1.1  joerg 
   1915      1.1  joerg   return IsError;
   1916      1.1  joerg }
   1917      1.1  joerg 
   1918      1.1  joerg bool Sema::isSelfExpr(Expr *RExpr) {
   1919      1.1  joerg   // 'self' is objc 'self' in an objc method only.
   1920      1.1  joerg   ObjCMethodDecl *Method =
   1921      1.1  joerg       dyn_cast_or_null<ObjCMethodDecl>(CurContext->getNonClosureAncestor());
   1922      1.1  joerg   return isSelfExpr(RExpr, Method);
   1923      1.1  joerg }
   1924      1.1  joerg 
   1925      1.1  joerg bool Sema::isSelfExpr(Expr *receiver, const ObjCMethodDecl *method) {
   1926      1.1  joerg   if (!method) return false;
   1927      1.1  joerg 
   1928      1.1  joerg   receiver = receiver->IgnoreParenLValueCasts();
   1929      1.1  joerg   if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver))
   1930      1.1  joerg     if (DRE->getDecl() == method->getSelfDecl())
   1931      1.1  joerg       return true;
   1932      1.1  joerg   return false;
   1933      1.1  joerg }
   1934      1.1  joerg 
   1935      1.1  joerg /// LookupMethodInType - Look up a method in an ObjCObjectType.
   1936      1.1  joerg ObjCMethodDecl *Sema::LookupMethodInObjectType(Selector sel, QualType type,
   1937      1.1  joerg                                                bool isInstance) {
   1938      1.1  joerg   const ObjCObjectType *objType = type->castAs<ObjCObjectType>();
   1939      1.1  joerg   if (ObjCInterfaceDecl *iface = objType->getInterface()) {
   1940      1.1  joerg     // Look it up in the main interface (and categories, etc.)
   1941      1.1  joerg     if (ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance))
   1942      1.1  joerg       return method;
   1943      1.1  joerg 
   1944      1.1  joerg     // Okay, look for "private" methods declared in any
   1945      1.1  joerg     // @implementations we've seen.
   1946      1.1  joerg     if (ObjCMethodDecl *method = iface->lookupPrivateMethod(sel, isInstance))
   1947      1.1  joerg       return method;
   1948      1.1  joerg   }
   1949      1.1  joerg 
   1950      1.1  joerg   // Check qualifiers.
   1951      1.1  joerg   for (const auto *I : objType->quals())
   1952      1.1  joerg     if (ObjCMethodDecl *method = I->lookupMethod(sel, isInstance))
   1953      1.1  joerg       return method;
   1954      1.1  joerg 
   1955      1.1  joerg   return nullptr;
   1956      1.1  joerg }
   1957      1.1  joerg 
   1958      1.1  joerg /// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier
   1959      1.1  joerg /// list of a qualified objective pointer type.
   1960      1.1  joerg ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
   1961      1.1  joerg                                               const ObjCObjectPointerType *OPT,
   1962      1.1  joerg                                               bool Instance)
   1963      1.1  joerg {
   1964      1.1  joerg   ObjCMethodDecl *MD = nullptr;
   1965      1.1  joerg   for (const auto *PROTO : OPT->quals()) {
   1966      1.1  joerg     if ((MD = PROTO->lookupMethod(Sel, Instance))) {
   1967      1.1  joerg       return MD;
   1968      1.1  joerg     }
   1969      1.1  joerg   }
   1970      1.1  joerg   return nullptr;
   1971      1.1  joerg }
   1972      1.1  joerg 
   1973      1.1  joerg /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
   1974      1.1  joerg /// objective C interface.  This is a property reference expression.
   1975      1.1  joerg ExprResult Sema::
   1976      1.1  joerg HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
   1977      1.1  joerg                           Expr *BaseExpr, SourceLocation OpLoc,
   1978      1.1  joerg                           DeclarationName MemberName,
   1979      1.1  joerg                           SourceLocation MemberLoc,
   1980      1.1  joerg                           SourceLocation SuperLoc, QualType SuperType,
   1981      1.1  joerg                           bool Super) {
   1982      1.1  joerg   const ObjCInterfaceType *IFaceT = OPT->getInterfaceType();
   1983      1.1  joerg   ObjCInterfaceDecl *IFace = IFaceT->getDecl();
   1984      1.1  joerg 
   1985      1.1  joerg   if (!MemberName.isIdentifier()) {
   1986      1.1  joerg     Diag(MemberLoc, diag::err_invalid_property_name)
   1987      1.1  joerg       << MemberName << QualType(OPT, 0);
   1988      1.1  joerg     return ExprError();
   1989      1.1  joerg   }
   1990      1.1  joerg 
   1991      1.1  joerg   IdentifierInfo *Member = MemberName.getAsIdentifierInfo();
   1992      1.1  joerg 
   1993      1.1  joerg   SourceRange BaseRange = Super? SourceRange(SuperLoc)
   1994      1.1  joerg                                : BaseExpr->getSourceRange();
   1995      1.1  joerg   if (RequireCompleteType(MemberLoc, OPT->getPointeeType(),
   1996      1.1  joerg                           diag::err_property_not_found_forward_class,
   1997      1.1  joerg                           MemberName, BaseRange))
   1998      1.1  joerg     return ExprError();
   1999      1.1  joerg 
   2000      1.1  joerg   if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(
   2001      1.1  joerg           Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) {
   2002      1.1  joerg     // Check whether we can reference this property.
   2003      1.1  joerg     if (DiagnoseUseOfDecl(PD, MemberLoc))
   2004      1.1  joerg       return ExprError();
   2005      1.1  joerg     if (Super)
   2006      1.1  joerg       return new (Context)
   2007      1.1  joerg           ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue,
   2008      1.1  joerg                               OK_ObjCProperty, MemberLoc, SuperLoc, SuperType);
   2009      1.1  joerg     else
   2010      1.1  joerg       return new (Context)
   2011      1.1  joerg           ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue,
   2012      1.1  joerg                               OK_ObjCProperty, MemberLoc, BaseExpr);
   2013      1.1  joerg   }
   2014      1.1  joerg   // Check protocols on qualified interfaces.
   2015      1.1  joerg   for (const auto *I : OPT->quals())
   2016      1.1  joerg     if (ObjCPropertyDecl *PD = I->FindPropertyDeclaration(
   2017      1.1  joerg             Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) {
   2018      1.1  joerg       // Check whether we can reference this property.
   2019      1.1  joerg       if (DiagnoseUseOfDecl(PD, MemberLoc))
   2020      1.1  joerg         return ExprError();
   2021      1.1  joerg 
   2022      1.1  joerg       if (Super)
   2023      1.1  joerg         return new (Context) ObjCPropertyRefExpr(
   2024      1.1  joerg             PD, Context.PseudoObjectTy, VK_LValue, OK_ObjCProperty, MemberLoc,
   2025      1.1  joerg             SuperLoc, SuperType);
   2026      1.1  joerg       else
   2027      1.1  joerg         return new (Context)
   2028      1.1  joerg             ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue,
   2029      1.1  joerg                                 OK_ObjCProperty, MemberLoc, BaseExpr);
   2030      1.1  joerg     }
   2031      1.1  joerg   // If that failed, look for an "implicit" property by seeing if the nullary
   2032      1.1  joerg   // selector is implemented.
   2033      1.1  joerg 
   2034      1.1  joerg   // FIXME: The logic for looking up nullary and unary selectors should be
   2035      1.1  joerg   // shared with the code in ActOnInstanceMessage.
   2036      1.1  joerg 
   2037      1.1  joerg   Selector Sel = PP.getSelectorTable().getNullarySelector(Member);
   2038      1.1  joerg   ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel);
   2039      1.1  joerg 
   2040      1.1  joerg   // May be found in property's qualified list.
   2041      1.1  joerg   if (!Getter)
   2042      1.1  joerg     Getter = LookupMethodInQualifiedType(Sel, OPT, true);
   2043      1.1  joerg 
   2044      1.1  joerg   // If this reference is in an @implementation, check for 'private' methods.
   2045      1.1  joerg   if (!Getter)
   2046      1.1  joerg     Getter = IFace->lookupPrivateMethod(Sel);
   2047      1.1  joerg 
   2048      1.1  joerg   if (Getter) {
   2049      1.1  joerg     // Check if we can reference this property.
   2050      1.1  joerg     if (DiagnoseUseOfDecl(Getter, MemberLoc))
   2051      1.1  joerg       return ExprError();
   2052      1.1  joerg   }
   2053      1.1  joerg   // If we found a getter then this may be a valid dot-reference, we
   2054      1.1  joerg   // will look for the matching setter, in case it is needed.
   2055      1.1  joerg   Selector SetterSel =
   2056      1.1  joerg     SelectorTable::constructSetterSelector(PP.getIdentifierTable(),
   2057      1.1  joerg                                            PP.getSelectorTable(), Member);
   2058      1.1  joerg   ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel);
   2059      1.1  joerg 
   2060      1.1  joerg   // May be found in property's qualified list.
   2061      1.1  joerg   if (!Setter)
   2062      1.1  joerg     Setter = LookupMethodInQualifiedType(SetterSel, OPT, true);
   2063      1.1  joerg 
   2064      1.1  joerg   if (!Setter) {
   2065      1.1  joerg     // If this reference is in an @implementation, also check for 'private'
   2066      1.1  joerg     // methods.
   2067      1.1  joerg     Setter = IFace->lookupPrivateMethod(SetterSel);
   2068      1.1  joerg   }
   2069      1.1  joerg 
   2070      1.1  joerg   if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc))
   2071      1.1  joerg     return ExprError();
   2072      1.1  joerg 
   2073      1.1  joerg   // Special warning if member name used in a property-dot for a setter accessor
   2074      1.1  joerg   // does not use a property with same name; e.g. obj.X = ... for a property with
   2075      1.1  joerg   // name 'x'.
   2076      1.1  joerg   if (Setter && Setter->isImplicit() && Setter->isPropertyAccessor() &&
   2077      1.1  joerg       !IFace->FindPropertyDeclaration(
   2078      1.1  joerg           Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) {
   2079      1.1  joerg       if (const ObjCPropertyDecl *PDecl = Setter->findPropertyDecl()) {
   2080      1.1  joerg         // Do not warn if user is using property-dot syntax to make call to
   2081      1.1  joerg         // user named setter.
   2082  1.1.1.2  joerg         if (!(PDecl->getPropertyAttributes() &
   2083  1.1.1.2  joerg               ObjCPropertyAttribute::kind_setter))
   2084      1.1  joerg           Diag(MemberLoc,
   2085      1.1  joerg                diag::warn_property_access_suggest)
   2086      1.1  joerg           << MemberName << QualType(OPT, 0) << PDecl->getName()
   2087      1.1  joerg           << FixItHint::CreateReplacement(MemberLoc, PDecl->getName());
   2088      1.1  joerg       }
   2089      1.1  joerg   }
   2090      1.1  joerg 
   2091      1.1  joerg   if (Getter || Setter) {
   2092      1.1  joerg     if (Super)
   2093      1.1  joerg       return new (Context)
   2094      1.1  joerg           ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue,
   2095      1.1  joerg                               OK_ObjCProperty, MemberLoc, SuperLoc, SuperType);
   2096      1.1  joerg     else
   2097      1.1  joerg       return new (Context)
   2098      1.1  joerg           ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue,
   2099      1.1  joerg                               OK_ObjCProperty, MemberLoc, BaseExpr);
   2100      1.1  joerg 
   2101      1.1  joerg   }
   2102      1.1  joerg 
   2103      1.1  joerg   // Attempt to correct for typos in property names.
   2104      1.1  joerg   DeclFilterCCC<ObjCPropertyDecl> CCC{};
   2105      1.1  joerg   if (TypoCorrection Corrected = CorrectTypo(
   2106      1.1  joerg           DeclarationNameInfo(MemberName, MemberLoc), LookupOrdinaryName,
   2107      1.1  joerg           nullptr, nullptr, CCC, CTK_ErrorRecovery, IFace, false, OPT)) {
   2108      1.1  joerg     DeclarationName TypoResult = Corrected.getCorrection();
   2109      1.1  joerg     if (TypoResult.isIdentifier() &&
   2110      1.1  joerg         TypoResult.getAsIdentifierInfo() == Member) {
   2111      1.1  joerg       // There is no need to try the correction if it is the same.
   2112      1.1  joerg       NamedDecl *ChosenDecl =
   2113      1.1  joerg         Corrected.isKeyword() ? nullptr : Corrected.getFoundDecl();
   2114      1.1  joerg       if (ChosenDecl && isa<ObjCPropertyDecl>(ChosenDecl))
   2115      1.1  joerg         if (cast<ObjCPropertyDecl>(ChosenDecl)->isClassProperty()) {
   2116      1.1  joerg           // This is a class property, we should not use the instance to
   2117      1.1  joerg           // access it.
   2118      1.1  joerg           Diag(MemberLoc, diag::err_class_property_found) << MemberName
   2119      1.1  joerg           << OPT->getInterfaceDecl()->getName()
   2120      1.1  joerg           << FixItHint::CreateReplacement(BaseExpr->getSourceRange(),
   2121      1.1  joerg                                           OPT->getInterfaceDecl()->getName());
   2122      1.1  joerg           return ExprError();
   2123      1.1  joerg         }
   2124      1.1  joerg     } else {
   2125      1.1  joerg       diagnoseTypo(Corrected, PDiag(diag::err_property_not_found_suggest)
   2126      1.1  joerg                                 << MemberName << QualType(OPT, 0));
   2127      1.1  joerg       return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc,
   2128      1.1  joerg                                        TypoResult, MemberLoc,
   2129      1.1  joerg                                        SuperLoc, SuperType, Super);
   2130      1.1  joerg     }
   2131      1.1  joerg   }
   2132      1.1  joerg   ObjCInterfaceDecl *ClassDeclared;
   2133      1.1  joerg   if (ObjCIvarDecl *Ivar =
   2134      1.1  joerg       IFace->lookupInstanceVariable(Member, ClassDeclared)) {
   2135      1.1  joerg     QualType T = Ivar->getType();
   2136      1.1  joerg     if (const ObjCObjectPointerType * OBJPT =
   2137      1.1  joerg         T->getAsObjCInterfacePointerType()) {
   2138      1.1  joerg       if (RequireCompleteType(MemberLoc, OBJPT->getPointeeType(),
   2139      1.1  joerg                               diag::err_property_not_as_forward_class,
   2140      1.1  joerg                               MemberName, BaseExpr))
   2141      1.1  joerg         return ExprError();
   2142      1.1  joerg     }
   2143      1.1  joerg     Diag(MemberLoc,
   2144      1.1  joerg          diag::err_ivar_access_using_property_syntax_suggest)
   2145      1.1  joerg     << MemberName << QualType(OPT, 0) << Ivar->getDeclName()
   2146      1.1  joerg     << FixItHint::CreateReplacement(OpLoc, "->");
   2147      1.1  joerg     return ExprError();
   2148      1.1  joerg   }
   2149      1.1  joerg 
   2150      1.1  joerg   Diag(MemberLoc, diag::err_property_not_found)
   2151      1.1  joerg     << MemberName << QualType(OPT, 0);
   2152      1.1  joerg   if (Setter)
   2153      1.1  joerg     Diag(Setter->getLocation(), diag::note_getter_unavailable)
   2154      1.1  joerg           << MemberName << BaseExpr->getSourceRange();
   2155      1.1  joerg   return ExprError();
   2156      1.1  joerg }
   2157      1.1  joerg 
   2158      1.1  joerg ExprResult Sema::
   2159      1.1  joerg ActOnClassPropertyRefExpr(IdentifierInfo &receiverName,
   2160      1.1  joerg                           IdentifierInfo &propertyName,
   2161      1.1  joerg                           SourceLocation receiverNameLoc,
   2162      1.1  joerg                           SourceLocation propertyNameLoc) {
   2163      1.1  joerg 
   2164      1.1  joerg   IdentifierInfo *receiverNamePtr = &receiverName;
   2165      1.1  joerg   ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr,
   2166      1.1  joerg                                                   receiverNameLoc);
   2167      1.1  joerg 
   2168      1.1  joerg   QualType SuperType;
   2169      1.1  joerg   if (!IFace) {
   2170      1.1  joerg     // If the "receiver" is 'super' in a method, handle it as an expression-like
   2171      1.1  joerg     // property reference.
   2172      1.1  joerg     if (receiverNamePtr->isStr("super")) {
   2173      1.1  joerg       if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf(receiverNameLoc)) {
   2174      1.1  joerg         if (auto classDecl = CurMethod->getClassInterface()) {
   2175      1.1  joerg           SuperType = QualType(classDecl->getSuperClassType(), 0);
   2176      1.1  joerg           if (CurMethod->isInstanceMethod()) {
   2177      1.1  joerg             if (SuperType.isNull()) {
   2178      1.1  joerg               // The current class does not have a superclass.
   2179      1.1  joerg               Diag(receiverNameLoc, diag::err_root_class_cannot_use_super)
   2180      1.1  joerg                 << CurMethod->getClassInterface()->getIdentifier();
   2181      1.1  joerg               return ExprError();
   2182      1.1  joerg             }
   2183      1.1  joerg             QualType T = Context.getObjCObjectPointerType(SuperType);
   2184      1.1  joerg 
   2185      1.1  joerg             return HandleExprPropertyRefExpr(T->castAs<ObjCObjectPointerType>(),
   2186      1.1  joerg                                              /*BaseExpr*/nullptr,
   2187      1.1  joerg                                              SourceLocation()/*OpLoc*/,
   2188      1.1  joerg                                              &propertyName,
   2189      1.1  joerg                                              propertyNameLoc,
   2190      1.1  joerg                                              receiverNameLoc, T, true);
   2191      1.1  joerg           }
   2192      1.1  joerg 
   2193      1.1  joerg           // Otherwise, if this is a class method, try dispatching to our
   2194      1.1  joerg           // superclass.
   2195      1.1  joerg           IFace = CurMethod->getClassInterface()->getSuperClass();
   2196      1.1  joerg         }
   2197      1.1  joerg       }
   2198      1.1  joerg     }
   2199      1.1  joerg 
   2200      1.1  joerg     if (!IFace) {
   2201      1.1  joerg       Diag(receiverNameLoc, diag::err_expected_either) << tok::identifier
   2202      1.1  joerg                                                        << tok::l_paren;
   2203      1.1  joerg       return ExprError();
   2204      1.1  joerg     }
   2205      1.1  joerg   }
   2206      1.1  joerg 
   2207      1.1  joerg   Selector GetterSel;
   2208      1.1  joerg   Selector SetterSel;
   2209      1.1  joerg   if (auto PD = IFace->FindPropertyDeclaration(
   2210      1.1  joerg           &propertyName, ObjCPropertyQueryKind::OBJC_PR_query_class)) {
   2211      1.1  joerg     GetterSel = PD->getGetterName();
   2212      1.1  joerg     SetterSel = PD->getSetterName();
   2213      1.1  joerg   } else {
   2214      1.1  joerg     GetterSel = PP.getSelectorTable().getNullarySelector(&propertyName);
   2215      1.1  joerg     SetterSel = SelectorTable::constructSetterSelector(
   2216      1.1  joerg         PP.getIdentifierTable(), PP.getSelectorTable(), &propertyName);
   2217      1.1  joerg   }
   2218      1.1  joerg 
   2219      1.1  joerg   // Search for a declared property first.
   2220      1.1  joerg   ObjCMethodDecl *Getter = IFace->lookupClassMethod(GetterSel);
   2221      1.1  joerg 
   2222      1.1  joerg   // If this reference is in an @implementation, check for 'private' methods.
   2223      1.1  joerg   if (!Getter)
   2224      1.1  joerg     Getter = IFace->lookupPrivateClassMethod(GetterSel);
   2225      1.1  joerg 
   2226      1.1  joerg   if (Getter) {
   2227      1.1  joerg     // FIXME: refactor/share with ActOnMemberReference().
   2228      1.1  joerg     // Check if we can reference this property.
   2229      1.1  joerg     if (DiagnoseUseOfDecl(Getter, propertyNameLoc))
   2230      1.1  joerg       return ExprError();
   2231      1.1  joerg   }
   2232      1.1  joerg 
   2233      1.1  joerg   // Look for the matching setter, in case it is needed.
   2234      1.1  joerg   ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel);
   2235      1.1  joerg   if (!Setter) {
   2236      1.1  joerg     // If this reference is in an @implementation, also check for 'private'
   2237      1.1  joerg     // methods.
   2238      1.1  joerg     Setter = IFace->lookupPrivateClassMethod(SetterSel);
   2239      1.1  joerg   }
   2240      1.1  joerg   // Look through local category implementations associated with the class.
   2241      1.1  joerg   if (!Setter)
   2242      1.1  joerg     Setter = IFace->getCategoryClassMethod(SetterSel);
   2243      1.1  joerg 
   2244      1.1  joerg   if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc))
   2245      1.1  joerg     return ExprError();
   2246      1.1  joerg 
   2247      1.1  joerg   if (Getter || Setter) {
   2248      1.1  joerg     if (!SuperType.isNull())
   2249      1.1  joerg       return new (Context)
   2250      1.1  joerg           ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue,
   2251      1.1  joerg                               OK_ObjCProperty, propertyNameLoc, receiverNameLoc,
   2252      1.1  joerg                               SuperType);
   2253      1.1  joerg 
   2254      1.1  joerg     return new (Context) ObjCPropertyRefExpr(
   2255      1.1  joerg         Getter, Setter, Context.PseudoObjectTy, VK_LValue, OK_ObjCProperty,
   2256      1.1  joerg         propertyNameLoc, receiverNameLoc, IFace);
   2257      1.1  joerg   }
   2258      1.1  joerg   return ExprError(Diag(propertyNameLoc, diag::err_property_not_found)
   2259      1.1  joerg                      << &propertyName << Context.getObjCInterfaceType(IFace));
   2260      1.1  joerg }
   2261      1.1  joerg 
   2262      1.1  joerg namespace {
   2263      1.1  joerg 
   2264      1.1  joerg class ObjCInterfaceOrSuperCCC final : public CorrectionCandidateCallback {
   2265      1.1  joerg  public:
   2266      1.1  joerg   ObjCInterfaceOrSuperCCC(ObjCMethodDecl *Method) {
   2267      1.1  joerg     // Determine whether "super" is acceptable in the current context.
   2268      1.1  joerg     if (Method && Method->getClassInterface())
   2269      1.1  joerg       WantObjCSuper = Method->getClassInterface()->getSuperClass();
   2270      1.1  joerg   }
   2271      1.1  joerg 
   2272      1.1  joerg   bool ValidateCandidate(const TypoCorrection &candidate) override {
   2273      1.1  joerg     return candidate.getCorrectionDeclAs<ObjCInterfaceDecl>() ||
   2274      1.1  joerg         candidate.isKeyword("super");
   2275      1.1  joerg   }
   2276      1.1  joerg 
   2277      1.1  joerg   std::unique_ptr<CorrectionCandidateCallback> clone() override {
   2278      1.1  joerg     return std::make_unique<ObjCInterfaceOrSuperCCC>(*this);
   2279      1.1  joerg   }
   2280      1.1  joerg };
   2281      1.1  joerg 
   2282      1.1  joerg } // end anonymous namespace
   2283      1.1  joerg 
   2284      1.1  joerg Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S,
   2285      1.1  joerg                                                IdentifierInfo *Name,
   2286      1.1  joerg                                                SourceLocation NameLoc,
   2287      1.1  joerg                                                bool IsSuper,
   2288      1.1  joerg                                                bool HasTrailingDot,
   2289      1.1  joerg                                                ParsedType &ReceiverType) {
   2290      1.1  joerg   ReceiverType = nullptr;
   2291      1.1  joerg 
   2292      1.1  joerg   // If the identifier is "super" and there is no trailing dot, we're
   2293      1.1  joerg   // messaging super. If the identifier is "super" and there is a
   2294      1.1  joerg   // trailing dot, it's an instance message.
   2295      1.1  joerg   if (IsSuper && S->isInObjcMethodScope())
   2296      1.1  joerg     return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage;
   2297      1.1  joerg 
   2298      1.1  joerg   LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName);
   2299      1.1  joerg   LookupName(Result, S);
   2300      1.1  joerg 
   2301      1.1  joerg   switch (Result.getResultKind()) {
   2302      1.1  joerg   case LookupResult::NotFound:
   2303      1.1  joerg     // Normal name lookup didn't find anything. If we're in an
   2304      1.1  joerg     // Objective-C method, look for ivars. If we find one, we're done!
   2305      1.1  joerg     // FIXME: This is a hack. Ivar lookup should be part of normal
   2306      1.1  joerg     // lookup.
   2307      1.1  joerg     if (ObjCMethodDecl *Method = getCurMethodDecl()) {
   2308      1.1  joerg       if (!Method->getClassInterface()) {
   2309      1.1  joerg         // Fall back: let the parser try to parse it as an instance message.
   2310      1.1  joerg         return ObjCInstanceMessage;
   2311      1.1  joerg       }
   2312      1.1  joerg 
   2313      1.1  joerg       ObjCInterfaceDecl *ClassDeclared;
   2314      1.1  joerg       if (Method->getClassInterface()->lookupInstanceVariable(Name,
   2315      1.1  joerg                                                               ClassDeclared))
   2316      1.1  joerg         return ObjCInstanceMessage;
   2317      1.1  joerg     }
   2318      1.1  joerg 
   2319      1.1  joerg     // Break out; we'll perform typo correction below.
   2320      1.1  joerg     break;
   2321      1.1  joerg 
   2322      1.1  joerg   case LookupResult::NotFoundInCurrentInstantiation:
   2323      1.1  joerg   case LookupResult::FoundOverloaded:
   2324      1.1  joerg   case LookupResult::FoundUnresolvedValue:
   2325      1.1  joerg   case LookupResult::Ambiguous:
   2326      1.1  joerg     Result.suppressDiagnostics();
   2327      1.1  joerg     return ObjCInstanceMessage;
   2328      1.1  joerg 
   2329      1.1  joerg   case LookupResult::Found: {
   2330      1.1  joerg     // If the identifier is a class or not, and there is a trailing dot,
   2331      1.1  joerg     // it's an instance message.
   2332      1.1  joerg     if (HasTrailingDot)
   2333      1.1  joerg       return ObjCInstanceMessage;
   2334      1.1  joerg     // We found something. If it's a type, then we have a class
   2335      1.1  joerg     // message. Otherwise, it's an instance message.
   2336      1.1  joerg     NamedDecl *ND = Result.getFoundDecl();
   2337      1.1  joerg     QualType T;
   2338      1.1  joerg     if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND))
   2339      1.1  joerg       T = Context.getObjCInterfaceType(Class);
   2340      1.1  joerg     else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) {
   2341      1.1  joerg       T = Context.getTypeDeclType(Type);
   2342      1.1  joerg       DiagnoseUseOfDecl(Type, NameLoc);
   2343      1.1  joerg     }
   2344      1.1  joerg     else
   2345      1.1  joerg       return ObjCInstanceMessage;
   2346      1.1  joerg 
   2347      1.1  joerg     //  We have a class message, and T is the type we're
   2348      1.1  joerg     //  messaging. Build source-location information for it.
   2349      1.1  joerg     TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
   2350      1.1  joerg     ReceiverType = CreateParsedType(T, TSInfo);
   2351      1.1  joerg     return ObjCClassMessage;
   2352      1.1  joerg   }
   2353      1.1  joerg   }
   2354      1.1  joerg 
   2355      1.1  joerg   ObjCInterfaceOrSuperCCC CCC(getCurMethodDecl());
   2356      1.1  joerg   if (TypoCorrection Corrected = CorrectTypo(
   2357      1.1  joerg           Result.getLookupNameInfo(), Result.getLookupKind(), S, nullptr, CCC,
   2358      1.1  joerg           CTK_ErrorRecovery, nullptr, false, nullptr, false)) {
   2359      1.1  joerg     if (Corrected.isKeyword()) {
   2360      1.1  joerg       // If we've found the keyword "super" (the only keyword that would be
   2361      1.1  joerg       // returned by CorrectTypo), this is a send to super.
   2362      1.1  joerg       diagnoseTypo(Corrected,
   2363      1.1  joerg                    PDiag(diag::err_unknown_receiver_suggest) << Name);
   2364      1.1  joerg       return ObjCSuperMessage;
   2365      1.1  joerg     } else if (ObjCInterfaceDecl *Class =
   2366      1.1  joerg                    Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
   2367      1.1  joerg       // If we found a declaration, correct when it refers to an Objective-C
   2368      1.1  joerg       // class.
   2369      1.1  joerg       diagnoseTypo(Corrected,
   2370      1.1  joerg                    PDiag(diag::err_unknown_receiver_suggest) << Name);
   2371      1.1  joerg       QualType T = Context.getObjCInterfaceType(Class);
   2372      1.1  joerg       TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc);
   2373      1.1  joerg       ReceiverType = CreateParsedType(T, TSInfo);
   2374      1.1  joerg       return ObjCClassMessage;
   2375      1.1  joerg     }
   2376      1.1  joerg   }
   2377      1.1  joerg 
   2378      1.1  joerg   // Fall back: let the parser try to parse it as an instance message.
   2379      1.1  joerg   return ObjCInstanceMessage;
   2380      1.1  joerg }
   2381      1.1  joerg 
   2382      1.1  joerg ExprResult Sema::ActOnSuperMessage(Scope *S,
   2383      1.1  joerg                                    SourceLocation SuperLoc,
   2384      1.1  joerg                                    Selector Sel,
   2385      1.1  joerg                                    SourceLocation LBracLoc,
   2386      1.1  joerg                                    ArrayRef<SourceLocation> SelectorLocs,
   2387      1.1  joerg                                    SourceLocation RBracLoc,
   2388      1.1  joerg                                    MultiExprArg Args) {
   2389      1.1  joerg   // Determine whether we are inside a method or not.
   2390      1.1  joerg   ObjCMethodDecl *Method = tryCaptureObjCSelf(SuperLoc);
   2391      1.1  joerg   if (!Method) {
   2392      1.1  joerg     Diag(SuperLoc, diag::err_invalid_receiver_to_message_super);
   2393      1.1  joerg     return ExprError();
   2394      1.1  joerg   }
   2395      1.1  joerg 
   2396      1.1  joerg   ObjCInterfaceDecl *Class = Method->getClassInterface();
   2397      1.1  joerg   if (!Class) {
   2398      1.1  joerg     Diag(SuperLoc, diag::err_no_super_class_message)
   2399      1.1  joerg       << Method->getDeclName();
   2400      1.1  joerg     return ExprError();
   2401      1.1  joerg   }
   2402      1.1  joerg 
   2403      1.1  joerg   QualType SuperTy(Class->getSuperClassType(), 0);
   2404      1.1  joerg   if (SuperTy.isNull()) {
   2405      1.1  joerg     // The current class does not have a superclass.
   2406      1.1  joerg     Diag(SuperLoc, diag::err_root_class_cannot_use_super)
   2407      1.1  joerg       << Class->getIdentifier();
   2408      1.1  joerg     return ExprError();
   2409      1.1  joerg   }
   2410      1.1  joerg 
   2411      1.1  joerg   // We are in a method whose class has a superclass, so 'super'
   2412      1.1  joerg   // is acting as a keyword.
   2413      1.1  joerg   if (Method->getSelector() == Sel)
   2414      1.1  joerg     getCurFunction()->ObjCShouldCallSuper = false;
   2415      1.1  joerg 
   2416      1.1  joerg   if (Method->isInstanceMethod()) {
   2417      1.1  joerg     // Since we are in an instance method, this is an instance
   2418      1.1  joerg     // message to the superclass instance.
   2419      1.1  joerg     SuperTy = Context.getObjCObjectPointerType(SuperTy);
   2420      1.1  joerg     return BuildInstanceMessage(nullptr, SuperTy, SuperLoc,
   2421      1.1  joerg                                 Sel, /*Method=*/nullptr,
   2422      1.1  joerg                                 LBracLoc, SelectorLocs, RBracLoc, Args);
   2423      1.1  joerg   }
   2424      1.1  joerg 
   2425      1.1  joerg   // Since we are in a class method, this is a class message to
   2426      1.1  joerg   // the superclass.
   2427      1.1  joerg   return BuildClassMessage(/*ReceiverTypeInfo=*/nullptr,
   2428      1.1  joerg                            SuperTy,
   2429      1.1  joerg                            SuperLoc, Sel, /*Method=*/nullptr,
   2430      1.1  joerg                            LBracLoc, SelectorLocs, RBracLoc, Args);
   2431      1.1  joerg }
   2432      1.1  joerg 
   2433      1.1  joerg ExprResult Sema::BuildClassMessageImplicit(QualType ReceiverType,
   2434      1.1  joerg                                            bool isSuperReceiver,
   2435      1.1  joerg                                            SourceLocation Loc,
   2436      1.1  joerg                                            Selector Sel,
   2437      1.1  joerg                                            ObjCMethodDecl *Method,
   2438      1.1  joerg                                            MultiExprArg Args) {
   2439      1.1  joerg   TypeSourceInfo *receiverTypeInfo = nullptr;
   2440      1.1  joerg   if (!ReceiverType.isNull())
   2441      1.1  joerg     receiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType);
   2442      1.1  joerg 
   2443      1.1  joerg   return BuildClassMessage(receiverTypeInfo, ReceiverType,
   2444      1.1  joerg                           /*SuperLoc=*/isSuperReceiver ? Loc : SourceLocation(),
   2445      1.1  joerg                            Sel, Method, Loc, Loc, Loc, Args,
   2446      1.1  joerg                            /*isImplicit=*/true);
   2447      1.1  joerg }
   2448      1.1  joerg 
   2449      1.1  joerg static void applyCocoaAPICheck(Sema &S, const ObjCMessageExpr *Msg,
   2450      1.1  joerg                                unsigned DiagID,
   2451      1.1  joerg                                bool (*refactor)(const ObjCMessageExpr *,
   2452      1.1  joerg                                               const NSAPI &, edit::Commit &)) {
   2453      1.1  joerg   SourceLocation MsgLoc = Msg->getExprLoc();
   2454      1.1  joerg   if (S.Diags.isIgnored(DiagID, MsgLoc))
   2455      1.1  joerg     return;
   2456      1.1  joerg 
   2457      1.1  joerg   SourceManager &SM = S.SourceMgr;
   2458      1.1  joerg   edit::Commit ECommit(SM, S.LangOpts);
   2459      1.1  joerg   if (refactor(Msg,*S.NSAPIObj, ECommit)) {
   2460  1.1.1.2  joerg     auto Builder = S.Diag(MsgLoc, DiagID)
   2461  1.1.1.2  joerg                    << Msg->getSelector() << Msg->getSourceRange();
   2462      1.1  joerg     // FIXME: Don't emit diagnostic at all if fixits are non-commitable.
   2463      1.1  joerg     if (!ECommit.isCommitable())
   2464      1.1  joerg       return;
   2465      1.1  joerg     for (edit::Commit::edit_iterator
   2466      1.1  joerg            I = ECommit.edit_begin(), E = ECommit.edit_end(); I != E; ++I) {
   2467      1.1  joerg       const edit::Commit::Edit &Edit = *I;
   2468      1.1  joerg       switch (Edit.Kind) {
   2469      1.1  joerg       case edit::Commit::Act_Insert:
   2470      1.1  joerg         Builder.AddFixItHint(FixItHint::CreateInsertion(Edit.OrigLoc,
   2471      1.1  joerg                                                         Edit.Text,
   2472      1.1  joerg                                                         Edit.BeforePrev));
   2473      1.1  joerg         break;
   2474      1.1  joerg       case edit::Commit::Act_InsertFromRange:
   2475      1.1  joerg         Builder.AddFixItHint(
   2476      1.1  joerg             FixItHint::CreateInsertionFromRange(Edit.OrigLoc,
   2477      1.1  joerg                                                 Edit.getInsertFromRange(SM),
   2478      1.1  joerg                                                 Edit.BeforePrev));
   2479      1.1  joerg         break;
   2480      1.1  joerg       case edit::Commit::Act_Remove:
   2481      1.1  joerg         Builder.AddFixItHint(FixItHint::CreateRemoval(Edit.getFileRange(SM)));
   2482      1.1  joerg         break;
   2483      1.1  joerg       }
   2484      1.1  joerg     }
   2485      1.1  joerg   }
   2486      1.1  joerg }
   2487      1.1  joerg 
   2488      1.1  joerg static void checkCocoaAPI(Sema &S, const ObjCMessageExpr *Msg) {
   2489      1.1  joerg   applyCocoaAPICheck(S, Msg, diag::warn_objc_redundant_literal_use,
   2490      1.1  joerg                      edit::rewriteObjCRedundantCallWithLiteral);
   2491      1.1  joerg }
   2492      1.1  joerg 
   2493      1.1  joerg static void checkFoundationAPI(Sema &S, SourceLocation Loc,
   2494      1.1  joerg                                const ObjCMethodDecl *Method,
   2495      1.1  joerg                                ArrayRef<Expr *> Args, QualType ReceiverType,
   2496      1.1  joerg                                bool IsClassObjectCall) {
   2497      1.1  joerg   // Check if this is a performSelector method that uses a selector that returns
   2498      1.1  joerg   // a record or a vector type.
   2499      1.1  joerg   if (Method->getSelector().getMethodFamily() != OMF_performSelector ||
   2500      1.1  joerg       Args.empty())
   2501      1.1  joerg     return;
   2502      1.1  joerg   const auto *SE = dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens());
   2503      1.1  joerg   if (!SE)
   2504      1.1  joerg     return;
   2505      1.1  joerg   ObjCMethodDecl *ImpliedMethod;
   2506      1.1  joerg   if (!IsClassObjectCall) {
   2507      1.1  joerg     const auto *OPT = ReceiverType->getAs<ObjCObjectPointerType>();
   2508      1.1  joerg     if (!OPT || !OPT->getInterfaceDecl())
   2509      1.1  joerg       return;
   2510      1.1  joerg     ImpliedMethod =
   2511      1.1  joerg         OPT->getInterfaceDecl()->lookupInstanceMethod(SE->getSelector());
   2512      1.1  joerg     if (!ImpliedMethod)
   2513      1.1  joerg       ImpliedMethod =
   2514      1.1  joerg           OPT->getInterfaceDecl()->lookupPrivateMethod(SE->getSelector());
   2515      1.1  joerg   } else {
   2516      1.1  joerg     const auto *IT = ReceiverType->getAs<ObjCInterfaceType>();
   2517      1.1  joerg     if (!IT)
   2518      1.1  joerg       return;
   2519      1.1  joerg     ImpliedMethod = IT->getDecl()->lookupClassMethod(SE->getSelector());
   2520      1.1  joerg     if (!ImpliedMethod)
   2521      1.1  joerg       ImpliedMethod =
   2522      1.1  joerg           IT->getDecl()->lookupPrivateClassMethod(SE->getSelector());
   2523      1.1  joerg   }
   2524      1.1  joerg   if (!ImpliedMethod)
   2525      1.1  joerg     return;
   2526      1.1  joerg   QualType Ret = ImpliedMethod->getReturnType();
   2527      1.1  joerg   if (Ret->isRecordType() || Ret->isVectorType() || Ret->isExtVectorType()) {
   2528      1.1  joerg     S.Diag(Loc, diag::warn_objc_unsafe_perform_selector)
   2529      1.1  joerg         << Method->getSelector()
   2530      1.1  joerg         << (!Ret->isRecordType()
   2531      1.1  joerg                 ? /*Vector*/ 2
   2532      1.1  joerg                 : Ret->isUnionType() ? /*Union*/ 1 : /*Struct*/ 0);
   2533      1.1  joerg     S.Diag(ImpliedMethod->getBeginLoc(),
   2534      1.1  joerg            diag::note_objc_unsafe_perform_selector_method_declared_here)
   2535      1.1  joerg         << ImpliedMethod->getSelector() << Ret;
   2536      1.1  joerg   }
   2537      1.1  joerg }
   2538      1.1  joerg 
   2539      1.1  joerg /// Diagnose use of %s directive in an NSString which is being passed
   2540      1.1  joerg /// as formatting string to formatting method.
   2541      1.1  joerg static void
   2542      1.1  joerg DiagnoseCStringFormatDirectiveInObjCAPI(Sema &S,
   2543      1.1  joerg                                         ObjCMethodDecl *Method,
   2544      1.1  joerg                                         Selector Sel,
   2545      1.1  joerg                                         Expr **Args, unsigned NumArgs) {
   2546      1.1  joerg   unsigned Idx = 0;
   2547      1.1  joerg   bool Format = false;
   2548      1.1  joerg   ObjCStringFormatFamily SFFamily = Sel.getStringFormatFamily();
   2549      1.1  joerg   if (SFFamily == ObjCStringFormatFamily::SFF_NSString) {
   2550      1.1  joerg     Idx = 0;
   2551      1.1  joerg     Format = true;
   2552      1.1  joerg   }
   2553      1.1  joerg   else if (Method) {
   2554      1.1  joerg     for (const auto *I : Method->specific_attrs<FormatAttr>()) {
   2555      1.1  joerg       if (S.GetFormatNSStringIdx(I, Idx)) {
   2556      1.1  joerg         Format = true;
   2557      1.1  joerg         break;
   2558      1.1  joerg       }
   2559      1.1  joerg     }
   2560      1.1  joerg   }
   2561      1.1  joerg   if (!Format || NumArgs <= Idx)
   2562      1.1  joerg     return;
   2563      1.1  joerg 
   2564      1.1  joerg   Expr *FormatExpr = Args[Idx];
   2565      1.1  joerg   if (ObjCStringLiteral *OSL =
   2566      1.1  joerg       dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) {
   2567      1.1  joerg     StringLiteral *FormatString = OSL->getString();
   2568      1.1  joerg     if (S.FormatStringHasSArg(FormatString)) {
   2569      1.1  joerg       S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string)
   2570      1.1  joerg         << "%s" << 0 << 0;
   2571      1.1  joerg       if (Method)
   2572      1.1  joerg         S.Diag(Method->getLocation(), diag::note_method_declared_at)
   2573      1.1  joerg           << Method->getDeclName();
   2574      1.1  joerg     }
   2575      1.1  joerg   }
   2576      1.1  joerg }
   2577      1.1  joerg 
   2578      1.1  joerg /// Build an Objective-C class message expression.
   2579      1.1  joerg ///
   2580      1.1  joerg /// This routine takes care of both normal class messages and
   2581      1.1  joerg /// class messages to the superclass.
   2582      1.1  joerg ///
   2583      1.1  joerg /// \param ReceiverTypeInfo Type source information that describes the
   2584      1.1  joerg /// receiver of this message. This may be NULL, in which case we are
   2585      1.1  joerg /// sending to the superclass and \p SuperLoc must be a valid source
   2586      1.1  joerg /// location.
   2587      1.1  joerg 
   2588      1.1  joerg /// \param ReceiverType The type of the object receiving the
   2589      1.1  joerg /// message. When \p ReceiverTypeInfo is non-NULL, this is the same
   2590      1.1  joerg /// type as that refers to. For a superclass send, this is the type of
   2591      1.1  joerg /// the superclass.
   2592      1.1  joerg ///
   2593      1.1  joerg /// \param SuperLoc The location of the "super" keyword in a
   2594      1.1  joerg /// superclass message.
   2595      1.1  joerg ///
   2596      1.1  joerg /// \param Sel The selector to which the message is being sent.
   2597      1.1  joerg ///
   2598      1.1  joerg /// \param Method The method that this class message is invoking, if
   2599      1.1  joerg /// already known.
   2600      1.1  joerg ///
   2601      1.1  joerg /// \param LBracLoc The location of the opening square bracket ']'.
   2602      1.1  joerg ///
   2603      1.1  joerg /// \param RBracLoc The location of the closing square bracket ']'.
   2604      1.1  joerg ///
   2605      1.1  joerg /// \param ArgsIn The message arguments.
   2606      1.1  joerg ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo,
   2607      1.1  joerg                                    QualType ReceiverType,
   2608      1.1  joerg                                    SourceLocation SuperLoc,
   2609      1.1  joerg                                    Selector Sel,
   2610      1.1  joerg                                    ObjCMethodDecl *Method,
   2611      1.1  joerg                                    SourceLocation LBracLoc,
   2612      1.1  joerg                                    ArrayRef<SourceLocation> SelectorLocs,
   2613      1.1  joerg                                    SourceLocation RBracLoc,
   2614      1.1  joerg                                    MultiExprArg ArgsIn,
   2615      1.1  joerg                                    bool isImplicit) {
   2616      1.1  joerg   SourceLocation Loc = SuperLoc.isValid()? SuperLoc
   2617      1.1  joerg     : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin();
   2618      1.1  joerg   if (LBracLoc.isInvalid()) {
   2619      1.1  joerg     Diag(Loc, diag::err_missing_open_square_message_send)
   2620      1.1  joerg       << FixItHint::CreateInsertion(Loc, "[");
   2621      1.1  joerg     LBracLoc = Loc;
   2622      1.1  joerg   }
   2623      1.1  joerg   ArrayRef<SourceLocation> SelectorSlotLocs;
   2624      1.1  joerg   if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
   2625      1.1  joerg     SelectorSlotLocs = SelectorLocs;
   2626      1.1  joerg   else
   2627      1.1  joerg     SelectorSlotLocs = Loc;
   2628      1.1  joerg   SourceLocation SelLoc = SelectorSlotLocs.front();
   2629      1.1  joerg 
   2630      1.1  joerg   if (ReceiverType->isDependentType()) {
   2631      1.1  joerg     // If the receiver type is dependent, we can't type-check anything
   2632      1.1  joerg     // at this point. Build a dependent expression.
   2633      1.1  joerg     unsigned NumArgs = ArgsIn.size();
   2634      1.1  joerg     Expr **Args = ArgsIn.data();
   2635      1.1  joerg     assert(SuperLoc.isInvalid() && "Message to super with dependent type");
   2636      1.1  joerg     return ObjCMessageExpr::Create(
   2637      1.1  joerg         Context, ReceiverType, VK_RValue, LBracLoc, ReceiverTypeInfo, Sel,
   2638      1.1  joerg         SelectorLocs, /*Method=*/nullptr, makeArrayRef(Args, NumArgs), RBracLoc,
   2639      1.1  joerg         isImplicit);
   2640      1.1  joerg   }
   2641      1.1  joerg 
   2642      1.1  joerg   // Find the class to which we are sending this message.
   2643      1.1  joerg   ObjCInterfaceDecl *Class = nullptr;
   2644      1.1  joerg   const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>();
   2645      1.1  joerg   if (!ClassType || !(Class = ClassType->getInterface())) {
   2646      1.1  joerg     Diag(Loc, diag::err_invalid_receiver_class_message)
   2647      1.1  joerg       << ReceiverType;
   2648      1.1  joerg     return ExprError();
   2649      1.1  joerg   }
   2650      1.1  joerg   assert(Class && "We don't know which class we're messaging?");
   2651      1.1  joerg   // objc++ diagnoses during typename annotation.
   2652      1.1  joerg   if (!getLangOpts().CPlusPlus)
   2653      1.1  joerg     (void)DiagnoseUseOfDecl(Class, SelectorSlotLocs);
   2654      1.1  joerg   // Find the method we are messaging.
   2655      1.1  joerg   if (!Method) {
   2656      1.1  joerg     SourceRange TypeRange
   2657      1.1  joerg       = SuperLoc.isValid()? SourceRange(SuperLoc)
   2658      1.1  joerg                           : ReceiverTypeInfo->getTypeLoc().getSourceRange();
   2659      1.1  joerg     if (RequireCompleteType(Loc, Context.getObjCInterfaceType(Class),
   2660      1.1  joerg                             (getLangOpts().ObjCAutoRefCount
   2661      1.1  joerg                                ? diag::err_arc_receiver_forward_class
   2662      1.1  joerg                                : diag::warn_receiver_forward_class),
   2663      1.1  joerg                             TypeRange)) {
   2664      1.1  joerg       // A forward class used in messaging is treated as a 'Class'
   2665      1.1  joerg       Method = LookupFactoryMethodInGlobalPool(Sel,
   2666      1.1  joerg                                                SourceRange(LBracLoc, RBracLoc));
   2667      1.1  joerg       if (Method && !getLangOpts().ObjCAutoRefCount)
   2668      1.1  joerg         Diag(Method->getLocation(), diag::note_method_sent_forward_class)
   2669      1.1  joerg           << Method->getDeclName();
   2670      1.1  joerg     }
   2671      1.1  joerg     if (!Method)
   2672      1.1  joerg       Method = Class->lookupClassMethod(Sel);
   2673      1.1  joerg 
   2674      1.1  joerg     // If we have an implementation in scope, check "private" methods.
   2675      1.1  joerg     if (!Method)
   2676      1.1  joerg       Method = Class->lookupPrivateClassMethod(Sel);
   2677      1.1  joerg 
   2678      1.1  joerg     if (Method && DiagnoseUseOfDecl(Method, SelectorSlotLocs,
   2679      1.1  joerg                                     nullptr, false, false, Class))
   2680      1.1  joerg       return ExprError();
   2681      1.1  joerg   }
   2682      1.1  joerg 
   2683      1.1  joerg   // Check the argument types and determine the result type.
   2684      1.1  joerg   QualType ReturnType;
   2685      1.1  joerg   ExprValueKind VK = VK_RValue;
   2686      1.1  joerg 
   2687      1.1  joerg   unsigned NumArgs = ArgsIn.size();
   2688      1.1  joerg   Expr **Args = ArgsIn.data();
   2689      1.1  joerg   if (CheckMessageArgumentTypes(/*Receiver=*/nullptr, ReceiverType,
   2690      1.1  joerg                                 MultiExprArg(Args, NumArgs), Sel, SelectorLocs,
   2691      1.1  joerg                                 Method, true, SuperLoc.isValid(), LBracLoc,
   2692      1.1  joerg                                 RBracLoc, SourceRange(), ReturnType, VK))
   2693      1.1  joerg     return ExprError();
   2694      1.1  joerg 
   2695      1.1  joerg   if (Method && !Method->getReturnType()->isVoidType() &&
   2696      1.1  joerg       RequireCompleteType(LBracLoc, Method->getReturnType(),
   2697      1.1  joerg                           diag::err_illegal_message_expr_incomplete_type))
   2698      1.1  joerg     return ExprError();
   2699      1.1  joerg 
   2700  1.1.1.2  joerg   if (Method && Method->isDirectMethod() && SuperLoc.isValid()) {
   2701  1.1.1.2  joerg     Diag(SuperLoc, diag::err_messaging_super_with_direct_method)
   2702  1.1.1.2  joerg         << FixItHint::CreateReplacement(
   2703  1.1.1.2  joerg                SuperLoc, getLangOpts().ObjCAutoRefCount
   2704  1.1.1.2  joerg                              ? "self"
   2705  1.1.1.2  joerg                              : Method->getClassInterface()->getName());
   2706  1.1.1.2  joerg     Diag(Method->getLocation(), diag::note_direct_method_declared_at)
   2707  1.1.1.2  joerg         << Method->getDeclName();
   2708  1.1.1.2  joerg   }
   2709  1.1.1.2  joerg 
   2710      1.1  joerg   // Warn about explicit call of +initialize on its own class. But not on 'super'.
   2711      1.1  joerg   if (Method && Method->getMethodFamily() == OMF_initialize) {
   2712      1.1  joerg     if (!SuperLoc.isValid()) {
   2713      1.1  joerg       const ObjCInterfaceDecl *ID =
   2714      1.1  joerg         dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext());
   2715      1.1  joerg       if (ID == Class) {
   2716      1.1  joerg         Diag(Loc, diag::warn_direct_initialize_call);
   2717      1.1  joerg         Diag(Method->getLocation(), diag::note_method_declared_at)
   2718      1.1  joerg           << Method->getDeclName();
   2719      1.1  joerg       }
   2720      1.1  joerg     }
   2721      1.1  joerg     else if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
   2722      1.1  joerg       // [super initialize] is allowed only within an +initialize implementation
   2723      1.1  joerg       if (CurMeth->getMethodFamily() != OMF_initialize) {
   2724      1.1  joerg         Diag(Loc, diag::warn_direct_super_initialize_call);
   2725      1.1  joerg         Diag(Method->getLocation(), diag::note_method_declared_at)
   2726      1.1  joerg           << Method->getDeclName();
   2727      1.1  joerg         Diag(CurMeth->getLocation(), diag::note_method_declared_at)
   2728      1.1  joerg         << CurMeth->getDeclName();
   2729      1.1  joerg       }
   2730      1.1  joerg     }
   2731      1.1  joerg   }
   2732      1.1  joerg 
   2733      1.1  joerg   DiagnoseCStringFormatDirectiveInObjCAPI(*this, Method, Sel, Args, NumArgs);
   2734      1.1  joerg 
   2735      1.1  joerg   // Construct the appropriate ObjCMessageExpr.
   2736      1.1  joerg   ObjCMessageExpr *Result;
   2737      1.1  joerg   if (SuperLoc.isValid())
   2738      1.1  joerg     Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
   2739      1.1  joerg                                      SuperLoc, /*IsInstanceSuper=*/false,
   2740      1.1  joerg                                      ReceiverType, Sel, SelectorLocs,
   2741      1.1  joerg                                      Method, makeArrayRef(Args, NumArgs),
   2742      1.1  joerg                                      RBracLoc, isImplicit);
   2743      1.1  joerg   else {
   2744      1.1  joerg     Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
   2745      1.1  joerg                                      ReceiverTypeInfo, Sel, SelectorLocs,
   2746      1.1  joerg                                      Method, makeArrayRef(Args, NumArgs),
   2747      1.1  joerg                                      RBracLoc, isImplicit);
   2748      1.1  joerg     if (!isImplicit)
   2749      1.1  joerg       checkCocoaAPI(*this, Result);
   2750      1.1  joerg   }
   2751      1.1  joerg   if (Method)
   2752      1.1  joerg     checkFoundationAPI(*this, SelLoc, Method, makeArrayRef(Args, NumArgs),
   2753      1.1  joerg                        ReceiverType, /*IsClassObjectCall=*/true);
   2754      1.1  joerg   return MaybeBindToTemporary(Result);
   2755      1.1  joerg }
   2756      1.1  joerg 
   2757      1.1  joerg // ActOnClassMessage - used for both unary and keyword messages.
   2758      1.1  joerg // ArgExprs is optional - if it is present, the number of expressions
   2759      1.1  joerg // is obtained from Sel.getNumArgs().
   2760      1.1  joerg ExprResult Sema::ActOnClassMessage(Scope *S,
   2761      1.1  joerg                                    ParsedType Receiver,
   2762      1.1  joerg                                    Selector Sel,
   2763      1.1  joerg                                    SourceLocation LBracLoc,
   2764      1.1  joerg                                    ArrayRef<SourceLocation> SelectorLocs,
   2765      1.1  joerg                                    SourceLocation RBracLoc,
   2766      1.1  joerg                                    MultiExprArg Args) {
   2767      1.1  joerg   TypeSourceInfo *ReceiverTypeInfo;
   2768      1.1  joerg   QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo);
   2769      1.1  joerg   if (ReceiverType.isNull())
   2770      1.1  joerg     return ExprError();
   2771      1.1  joerg 
   2772      1.1  joerg   if (!ReceiverTypeInfo)
   2773      1.1  joerg     ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc);
   2774      1.1  joerg 
   2775      1.1  joerg   return BuildClassMessage(ReceiverTypeInfo, ReceiverType,
   2776      1.1  joerg                            /*SuperLoc=*/SourceLocation(), Sel,
   2777      1.1  joerg                            /*Method=*/nullptr, LBracLoc, SelectorLocs, RBracLoc,
   2778      1.1  joerg                            Args);
   2779      1.1  joerg }
   2780      1.1  joerg 
   2781      1.1  joerg ExprResult Sema::BuildInstanceMessageImplicit(Expr *Receiver,
   2782      1.1  joerg                                               QualType ReceiverType,
   2783      1.1  joerg                                               SourceLocation Loc,
   2784      1.1  joerg                                               Selector Sel,
   2785      1.1  joerg                                               ObjCMethodDecl *Method,
   2786      1.1  joerg                                               MultiExprArg Args) {
   2787      1.1  joerg   return BuildInstanceMessage(Receiver, ReceiverType,
   2788      1.1  joerg                               /*SuperLoc=*/!Receiver ? Loc : SourceLocation(),
   2789      1.1  joerg                               Sel, Method, Loc, Loc, Loc, Args,
   2790      1.1  joerg                               /*isImplicit=*/true);
   2791      1.1  joerg }
   2792      1.1  joerg 
   2793      1.1  joerg static bool isMethodDeclaredInRootProtocol(Sema &S, const ObjCMethodDecl *M) {
   2794      1.1  joerg   if (!S.NSAPIObj)
   2795      1.1  joerg     return false;
   2796      1.1  joerg   const auto *Protocol = dyn_cast<ObjCProtocolDecl>(M->getDeclContext());
   2797      1.1  joerg   if (!Protocol)
   2798      1.1  joerg     return false;
   2799      1.1  joerg   const IdentifierInfo *II = S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject);
   2800      1.1  joerg   if (const auto *RootClass = dyn_cast_or_null<ObjCInterfaceDecl>(
   2801      1.1  joerg           S.LookupSingleName(S.TUScope, II, Protocol->getBeginLoc(),
   2802      1.1  joerg                              Sema::LookupOrdinaryName))) {
   2803      1.1  joerg     for (const ObjCProtocolDecl *P : RootClass->all_referenced_protocols()) {
   2804      1.1  joerg       if (P->getCanonicalDecl() == Protocol->getCanonicalDecl())
   2805      1.1  joerg         return true;
   2806      1.1  joerg     }
   2807      1.1  joerg   }
   2808      1.1  joerg   return false;
   2809      1.1  joerg }
   2810      1.1  joerg 
   2811      1.1  joerg /// Build an Objective-C instance message expression.
   2812      1.1  joerg ///
   2813      1.1  joerg /// This routine takes care of both normal instance messages and
   2814      1.1  joerg /// instance messages to the superclass instance.
   2815      1.1  joerg ///
   2816      1.1  joerg /// \param Receiver The expression that computes the object that will
   2817      1.1  joerg /// receive this message. This may be empty, in which case we are
   2818      1.1  joerg /// sending to the superclass instance and \p SuperLoc must be a valid
   2819      1.1  joerg /// source location.
   2820      1.1  joerg ///
   2821      1.1  joerg /// \param ReceiverType The (static) type of the object receiving the
   2822      1.1  joerg /// message. When a \p Receiver expression is provided, this is the
   2823      1.1  joerg /// same type as that expression. For a superclass instance send, this
   2824      1.1  joerg /// is a pointer to the type of the superclass.
   2825      1.1  joerg ///
   2826      1.1  joerg /// \param SuperLoc The location of the "super" keyword in a
   2827      1.1  joerg /// superclass instance message.
   2828      1.1  joerg ///
   2829      1.1  joerg /// \param Sel The selector to which the message is being sent.
   2830      1.1  joerg ///
   2831      1.1  joerg /// \param Method The method that this instance message is invoking, if
   2832      1.1  joerg /// already known.
   2833      1.1  joerg ///
   2834      1.1  joerg /// \param LBracLoc The location of the opening square bracket ']'.
   2835      1.1  joerg ///
   2836      1.1  joerg /// \param RBracLoc The location of the closing square bracket ']'.
   2837      1.1  joerg ///
   2838      1.1  joerg /// \param ArgsIn The message arguments.
   2839      1.1  joerg ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
   2840      1.1  joerg                                       QualType ReceiverType,
   2841      1.1  joerg                                       SourceLocation SuperLoc,
   2842      1.1  joerg                                       Selector Sel,
   2843      1.1  joerg                                       ObjCMethodDecl *Method,
   2844      1.1  joerg                                       SourceLocation LBracLoc,
   2845      1.1  joerg                                       ArrayRef<SourceLocation> SelectorLocs,
   2846      1.1  joerg                                       SourceLocation RBracLoc,
   2847      1.1  joerg                                       MultiExprArg ArgsIn,
   2848      1.1  joerg                                       bool isImplicit) {
   2849      1.1  joerg   assert((Receiver || SuperLoc.isValid()) && "If the Receiver is null, the "
   2850      1.1  joerg                                              "SuperLoc must be valid so we can "
   2851      1.1  joerg                                              "use it instead.");
   2852      1.1  joerg 
   2853      1.1  joerg   // The location of the receiver.
   2854      1.1  joerg   SourceLocation Loc = SuperLoc.isValid() ? SuperLoc : Receiver->getBeginLoc();
   2855      1.1  joerg   SourceRange RecRange =
   2856      1.1  joerg       SuperLoc.isValid()? SuperLoc : Receiver->getSourceRange();
   2857      1.1  joerg   ArrayRef<SourceLocation> SelectorSlotLocs;
   2858      1.1  joerg   if (!SelectorLocs.empty() && SelectorLocs.front().isValid())
   2859      1.1  joerg     SelectorSlotLocs = SelectorLocs;
   2860      1.1  joerg   else
   2861      1.1  joerg     SelectorSlotLocs = Loc;
   2862      1.1  joerg   SourceLocation SelLoc = SelectorSlotLocs.front();
   2863      1.1  joerg 
   2864      1.1  joerg   if (LBracLoc.isInvalid()) {
   2865      1.1  joerg     Diag(Loc, diag::err_missing_open_square_message_send)
   2866      1.1  joerg       << FixItHint::CreateInsertion(Loc, "[");
   2867      1.1  joerg     LBracLoc = Loc;
   2868      1.1  joerg   }
   2869      1.1  joerg 
   2870      1.1  joerg   // If we have a receiver expression, perform appropriate promotions
   2871      1.1  joerg   // and determine receiver type.
   2872      1.1  joerg   if (Receiver) {
   2873      1.1  joerg     if (Receiver->hasPlaceholderType()) {
   2874      1.1  joerg       ExprResult Result;
   2875      1.1  joerg       if (Receiver->getType() == Context.UnknownAnyTy)
   2876      1.1  joerg         Result = forceUnknownAnyToType(Receiver, Context.getObjCIdType());
   2877      1.1  joerg       else
   2878      1.1  joerg         Result = CheckPlaceholderExpr(Receiver);
   2879      1.1  joerg       if (Result.isInvalid()) return ExprError();
   2880      1.1  joerg       Receiver = Result.get();
   2881      1.1  joerg     }
   2882      1.1  joerg 
   2883      1.1  joerg     if (Receiver->isTypeDependent()) {
   2884      1.1  joerg       // If the receiver is type-dependent, we can't type-check anything
   2885      1.1  joerg       // at this point. Build a dependent expression.
   2886      1.1  joerg       unsigned NumArgs = ArgsIn.size();
   2887      1.1  joerg       Expr **Args = ArgsIn.data();
   2888      1.1  joerg       assert(SuperLoc.isInvalid() && "Message to super with dependent type");
   2889      1.1  joerg       return ObjCMessageExpr::Create(
   2890      1.1  joerg           Context, Context.DependentTy, VK_RValue, LBracLoc, Receiver, Sel,
   2891      1.1  joerg           SelectorLocs, /*Method=*/nullptr, makeArrayRef(Args, NumArgs),
   2892      1.1  joerg           RBracLoc, isImplicit);
   2893      1.1  joerg     }
   2894      1.1  joerg 
   2895      1.1  joerg     // If necessary, apply function/array conversion to the receiver.
   2896      1.1  joerg     // C99 6.7.5.3p[7,8].
   2897      1.1  joerg     ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver);
   2898      1.1  joerg     if (Result.isInvalid())
   2899      1.1  joerg       return ExprError();
   2900      1.1  joerg     Receiver = Result.get();
   2901      1.1  joerg     ReceiverType = Receiver->getType();
   2902      1.1  joerg 
   2903      1.1  joerg     // If the receiver is an ObjC pointer, a block pointer, or an
   2904      1.1  joerg     // __attribute__((NSObject)) pointer, we don't need to do any
   2905      1.1  joerg     // special conversion in order to look up a receiver.
   2906      1.1  joerg     if (ReceiverType->isObjCRetainableType()) {
   2907      1.1  joerg       // do nothing
   2908      1.1  joerg     } else if (!getLangOpts().ObjCAutoRefCount &&
   2909      1.1  joerg                !Context.getObjCIdType().isNull() &&
   2910      1.1  joerg                (ReceiverType->isPointerType() ||
   2911      1.1  joerg                 ReceiverType->isIntegerType())) {
   2912      1.1  joerg       // Implicitly convert integers and pointers to 'id' but emit a warning.
   2913      1.1  joerg       // But not in ARC.
   2914  1.1.1.2  joerg       Diag(Loc, diag::warn_bad_receiver_type) << ReceiverType << RecRange;
   2915      1.1  joerg       if (ReceiverType->isPointerType()) {
   2916      1.1  joerg         Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
   2917      1.1  joerg                                      CK_CPointerToObjCPointerCast).get();
   2918      1.1  joerg       } else {
   2919      1.1  joerg         // TODO: specialized warning on null receivers?
   2920      1.1  joerg         bool IsNull = Receiver->isNullPointerConstant(Context,
   2921      1.1  joerg                                               Expr::NPC_ValueDependentIsNull);
   2922      1.1  joerg         CastKind Kind = IsNull ? CK_NullToPointer : CK_IntegralToPointer;
   2923      1.1  joerg         Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(),
   2924      1.1  joerg                                      Kind).get();
   2925      1.1  joerg       }
   2926      1.1  joerg       ReceiverType = Receiver->getType();
   2927      1.1  joerg     } else if (getLangOpts().CPlusPlus) {
   2928      1.1  joerg       // The receiver must be a complete type.
   2929      1.1  joerg       if (RequireCompleteType(Loc, Receiver->getType(),
   2930      1.1  joerg                               diag::err_incomplete_receiver_type))
   2931      1.1  joerg         return ExprError();
   2932      1.1  joerg 
   2933      1.1  joerg       ExprResult result = PerformContextuallyConvertToObjCPointer(Receiver);
   2934      1.1  joerg       if (result.isUsable()) {
   2935      1.1  joerg         Receiver = result.get();
   2936      1.1  joerg         ReceiverType = Receiver->getType();
   2937      1.1  joerg       }
   2938      1.1  joerg     }
   2939      1.1  joerg   }
   2940      1.1  joerg 
   2941      1.1  joerg   // There's a somewhat weird interaction here where we assume that we
   2942      1.1  joerg   // won't actually have a method unless we also don't need to do some
   2943      1.1  joerg   // of the more detailed type-checking on the receiver.
   2944      1.1  joerg 
   2945      1.1  joerg   if (!Method) {
   2946      1.1  joerg     // Handle messages to id and __kindof types (where we use the
   2947      1.1  joerg     // global method pool).
   2948      1.1  joerg     const ObjCObjectType *typeBound = nullptr;
   2949      1.1  joerg     bool receiverIsIdLike = ReceiverType->isObjCIdOrObjectKindOfType(Context,
   2950      1.1  joerg                                                                      typeBound);
   2951      1.1  joerg     if (receiverIsIdLike || ReceiverType->isBlockPointerType() ||
   2952      1.1  joerg         (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) {
   2953      1.1  joerg       SmallVector<ObjCMethodDecl*, 4> Methods;
   2954      1.1  joerg       // If we have a type bound, further filter the methods.
   2955      1.1  joerg       CollectMultipleMethodsInGlobalPool(Sel, Methods, true/*InstanceFirst*/,
   2956      1.1  joerg                                          true/*CheckTheOther*/, typeBound);
   2957      1.1  joerg       if (!Methods.empty()) {
   2958      1.1  joerg         // We choose the first method as the initial candidate, then try to
   2959      1.1  joerg         // select a better one.
   2960      1.1  joerg         Method = Methods[0];
   2961      1.1  joerg 
   2962      1.1  joerg         if (ObjCMethodDecl *BestMethod =
   2963      1.1  joerg             SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(), Methods))
   2964      1.1  joerg           Method = BestMethod;
   2965      1.1  joerg 
   2966      1.1  joerg         if (!AreMultipleMethodsInGlobalPool(Sel, Method,
   2967      1.1  joerg                                             SourceRange(LBracLoc, RBracLoc),
   2968      1.1  joerg                                             receiverIsIdLike, Methods))
   2969      1.1  joerg           DiagnoseUseOfDecl(Method, SelectorSlotLocs);
   2970      1.1  joerg       }
   2971      1.1  joerg     } else if (ReceiverType->isObjCClassOrClassKindOfType() ||
   2972      1.1  joerg                ReceiverType->isObjCQualifiedClassType()) {
   2973      1.1  joerg       // Handle messages to Class.
   2974      1.1  joerg       // We allow sending a message to a qualified Class ("Class<foo>"), which
   2975      1.1  joerg       // is ok as long as one of the protocols implements the selector (if not,
   2976      1.1  joerg       // warn).
   2977      1.1  joerg       if (!ReceiverType->isObjCClassOrClassKindOfType()) {
   2978      1.1  joerg         const ObjCObjectPointerType *QClassTy
   2979      1.1  joerg           = ReceiverType->getAsObjCQualifiedClassType();
   2980      1.1  joerg         // Search protocols for class methods.
   2981      1.1  joerg         Method = LookupMethodInQualifiedType(Sel, QClassTy, false);
   2982      1.1  joerg         if (!Method) {
   2983      1.1  joerg           Method = LookupMethodInQualifiedType(Sel, QClassTy, true);
   2984      1.1  joerg           // warn if instance method found for a Class message.
   2985      1.1  joerg           if (Method && !isMethodDeclaredInRootProtocol(*this, Method)) {
   2986      1.1  joerg             Diag(SelLoc, diag::warn_instance_method_on_class_found)
   2987      1.1  joerg               << Method->getSelector() << Sel;
   2988      1.1  joerg             Diag(Method->getLocation(), diag::note_method_declared_at)
   2989      1.1  joerg               << Method->getDeclName();
   2990      1.1  joerg           }
   2991      1.1  joerg         }
   2992      1.1  joerg       } else {
   2993      1.1  joerg         if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) {
   2994      1.1  joerg           if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) {
   2995      1.1  joerg             // As a guess, try looking for the method in the current interface.
   2996      1.1  joerg             // This very well may not produce the "right" method.
   2997      1.1  joerg 
   2998      1.1  joerg             // First check the public methods in the class interface.
   2999      1.1  joerg             Method = ClassDecl->lookupClassMethod(Sel);
   3000      1.1  joerg 
   3001      1.1  joerg             if (!Method)
   3002      1.1  joerg               Method = ClassDecl->lookupPrivateClassMethod(Sel);
   3003      1.1  joerg 
   3004      1.1  joerg             if (Method && DiagnoseUseOfDecl(Method, SelectorSlotLocs))
   3005      1.1  joerg               return ExprError();
   3006      1.1  joerg           }
   3007      1.1  joerg         }
   3008      1.1  joerg         if (!Method) {
   3009      1.1  joerg           // If not messaging 'self', look for any factory method named 'Sel'.
   3010      1.1  joerg           if (!Receiver || !isSelfExpr(Receiver)) {
   3011      1.1  joerg             // If no class (factory) method was found, check if an _instance_
   3012      1.1  joerg             // method of the same name exists in the root class only.
   3013      1.1  joerg             SmallVector<ObjCMethodDecl*, 4> Methods;
   3014      1.1  joerg             CollectMultipleMethodsInGlobalPool(Sel, Methods,
   3015      1.1  joerg                                                false/*InstanceFirst*/,
   3016      1.1  joerg                                                true/*CheckTheOther*/);
   3017      1.1  joerg             if (!Methods.empty()) {
   3018      1.1  joerg               // We choose the first method as the initial candidate, then try
   3019      1.1  joerg               // to select a better one.
   3020      1.1  joerg               Method = Methods[0];
   3021      1.1  joerg 
   3022      1.1  joerg               // If we find an instance method, emit warning.
   3023      1.1  joerg               if (Method->isInstanceMethod()) {
   3024      1.1  joerg                 if (const ObjCInterfaceDecl *ID =
   3025      1.1  joerg                     dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) {
   3026      1.1  joerg                   if (ID->getSuperClass())
   3027      1.1  joerg                     Diag(SelLoc, diag::warn_root_inst_method_not_found)
   3028      1.1  joerg                         << Sel << SourceRange(LBracLoc, RBracLoc);
   3029      1.1  joerg                 }
   3030      1.1  joerg               }
   3031      1.1  joerg 
   3032      1.1  joerg              if (ObjCMethodDecl *BestMethod =
   3033      1.1  joerg                  SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(),
   3034      1.1  joerg                                   Methods))
   3035      1.1  joerg                Method = BestMethod;
   3036      1.1  joerg             }
   3037      1.1  joerg           }
   3038      1.1  joerg         }
   3039      1.1  joerg       }
   3040      1.1  joerg     } else {
   3041      1.1  joerg       ObjCInterfaceDecl *ClassDecl = nullptr;
   3042      1.1  joerg 
   3043      1.1  joerg       // We allow sending a message to a qualified ID ("id<foo>"), which is ok as
   3044      1.1  joerg       // long as one of the protocols implements the selector (if not, warn).
   3045      1.1  joerg       // And as long as message is not deprecated/unavailable (warn if it is).
   3046      1.1  joerg       if (const ObjCObjectPointerType *QIdTy
   3047      1.1  joerg                                    = ReceiverType->getAsObjCQualifiedIdType()) {
   3048      1.1  joerg         // Search protocols for instance methods.
   3049      1.1  joerg         Method = LookupMethodInQualifiedType(Sel, QIdTy, true);
   3050      1.1  joerg         if (!Method)
   3051      1.1  joerg           Method = LookupMethodInQualifiedType(Sel, QIdTy, false);
   3052      1.1  joerg         if (Method && DiagnoseUseOfDecl(Method, SelectorSlotLocs))
   3053      1.1  joerg           return ExprError();
   3054      1.1  joerg       } else if (const ObjCObjectPointerType *OCIType
   3055      1.1  joerg                    = ReceiverType->getAsObjCInterfacePointerType()) {
   3056      1.1  joerg         // We allow sending a message to a pointer to an interface (an object).
   3057      1.1  joerg         ClassDecl = OCIType->getInterfaceDecl();
   3058      1.1  joerg 
   3059      1.1  joerg         // Try to complete the type. Under ARC, this is a hard error from which
   3060      1.1  joerg         // we don't try to recover.
   3061      1.1  joerg         // FIXME: In the non-ARC case, this will still be a hard error if the
   3062      1.1  joerg         // definition is found in a module that's not visible.
   3063      1.1  joerg         const ObjCInterfaceDecl *forwardClass = nullptr;
   3064      1.1  joerg         if (RequireCompleteType(Loc, OCIType->getPointeeType(),
   3065  1.1.1.2  joerg                                 getLangOpts().ObjCAutoRefCount
   3066  1.1.1.2  joerg                                     ? diag::err_arc_receiver_forward_instance
   3067  1.1.1.2  joerg                                     : diag::warn_receiver_forward_instance,
   3068  1.1.1.2  joerg                                 RecRange)) {
   3069      1.1  joerg           if (getLangOpts().ObjCAutoRefCount)
   3070      1.1  joerg             return ExprError();
   3071      1.1  joerg 
   3072      1.1  joerg           forwardClass = OCIType->getInterfaceDecl();
   3073      1.1  joerg           Diag(Receiver ? Receiver->getBeginLoc() : SuperLoc,
   3074      1.1  joerg                diag::note_receiver_is_id);
   3075      1.1  joerg           Method = nullptr;
   3076      1.1  joerg         } else {
   3077      1.1  joerg           Method = ClassDecl->lookupInstanceMethod(Sel);
   3078      1.1  joerg         }
   3079      1.1  joerg 
   3080      1.1  joerg         if (!Method)
   3081      1.1  joerg           // Search protocol qualifiers.
   3082      1.1  joerg           Method = LookupMethodInQualifiedType(Sel, OCIType, true);
   3083      1.1  joerg 
   3084      1.1  joerg         if (!Method) {
   3085      1.1  joerg           // If we have implementations in scope, check "private" methods.
   3086      1.1  joerg           Method = ClassDecl->lookupPrivateMethod(Sel);
   3087      1.1  joerg 
   3088      1.1  joerg           if (!Method && getLangOpts().ObjCAutoRefCount) {
   3089      1.1  joerg             Diag(SelLoc, diag::err_arc_may_not_respond)
   3090      1.1  joerg               << OCIType->getPointeeType() << Sel << RecRange
   3091      1.1  joerg               << SourceRange(SelectorLocs.front(), SelectorLocs.back());
   3092      1.1  joerg             return ExprError();
   3093      1.1  joerg           }
   3094      1.1  joerg 
   3095      1.1  joerg           if (!Method && (!Receiver || !isSelfExpr(Receiver))) {
   3096      1.1  joerg             // If we still haven't found a method, look in the global pool. This
   3097      1.1  joerg             // behavior isn't very desirable, however we need it for GCC
   3098      1.1  joerg             // compatibility. FIXME: should we deviate??
   3099      1.1  joerg             if (OCIType->qual_empty()) {
   3100      1.1  joerg               SmallVector<ObjCMethodDecl*, 4> Methods;
   3101      1.1  joerg               CollectMultipleMethodsInGlobalPool(Sel, Methods,
   3102      1.1  joerg                                                  true/*InstanceFirst*/,
   3103      1.1  joerg                                                  false/*CheckTheOther*/);
   3104      1.1  joerg               if (!Methods.empty()) {
   3105      1.1  joerg                 // We choose the first method as the initial candidate, then try
   3106      1.1  joerg                 // to select a better one.
   3107      1.1  joerg                 Method = Methods[0];
   3108      1.1  joerg 
   3109      1.1  joerg                 if (ObjCMethodDecl *BestMethod =
   3110      1.1  joerg                     SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(),
   3111      1.1  joerg                                      Methods))
   3112      1.1  joerg                   Method = BestMethod;
   3113      1.1  joerg 
   3114      1.1  joerg                 AreMultipleMethodsInGlobalPool(Sel, Method,
   3115      1.1  joerg                                                SourceRange(LBracLoc, RBracLoc),
   3116      1.1  joerg                                                true/*receiverIdOrClass*/,
   3117      1.1  joerg                                                Methods);
   3118      1.1  joerg               }
   3119      1.1  joerg               if (Method && !forwardClass)
   3120      1.1  joerg                 Diag(SelLoc, diag::warn_maynot_respond)
   3121      1.1  joerg                   << OCIType->getInterfaceDecl()->getIdentifier()
   3122      1.1  joerg                   << Sel << RecRange;
   3123      1.1  joerg             }
   3124      1.1  joerg           }
   3125      1.1  joerg         }
   3126      1.1  joerg         if (Method && DiagnoseUseOfDecl(Method, SelectorSlotLocs, forwardClass))
   3127      1.1  joerg           return ExprError();
   3128      1.1  joerg       } else {
   3129      1.1  joerg         // Reject other random receiver types (e.g. structs).
   3130  1.1.1.2  joerg         Diag(Loc, diag::err_bad_receiver_type) << ReceiverType << RecRange;
   3131      1.1  joerg         return ExprError();
   3132      1.1  joerg       }
   3133      1.1  joerg     }
   3134      1.1  joerg   }
   3135      1.1  joerg 
   3136      1.1  joerg   FunctionScopeInfo *DIFunctionScopeInfo =
   3137      1.1  joerg     (Method && Method->getMethodFamily() == OMF_init)
   3138      1.1  joerg       ? getEnclosingFunction() : nullptr;
   3139      1.1  joerg 
   3140  1.1.1.2  joerg   if (Method && Method->isDirectMethod()) {
   3141  1.1.1.2  joerg     if (ReceiverType->isObjCIdType() && !isImplicit) {
   3142  1.1.1.2  joerg       Diag(Receiver->getExprLoc(),
   3143  1.1.1.2  joerg            diag::err_messaging_unqualified_id_with_direct_method);
   3144  1.1.1.2  joerg       Diag(Method->getLocation(), diag::note_direct_method_declared_at)
   3145  1.1.1.2  joerg           << Method->getDeclName();
   3146  1.1.1.2  joerg     }
   3147  1.1.1.2  joerg 
   3148  1.1.1.2  joerg     // Under ARC, self can't be assigned, and doing a direct call to `self`
   3149  1.1.1.2  joerg     // when it's a Class is hence safe.  For other cases, we can't trust `self`
   3150  1.1.1.2  joerg     // is what we think it is, so we reject it.
   3151  1.1.1.2  joerg     if (ReceiverType->isObjCClassType() && !isImplicit &&
   3152  1.1.1.2  joerg         !(Receiver->isObjCSelfExpr() && getLangOpts().ObjCAutoRefCount)) {
   3153  1.1.1.2  joerg       {
   3154  1.1.1.2  joerg         auto Builder = Diag(Receiver->getExprLoc(),
   3155  1.1.1.2  joerg                             diag::err_messaging_class_with_direct_method);
   3156  1.1.1.2  joerg         if (Receiver->isObjCSelfExpr()) {
   3157  1.1.1.2  joerg           Builder.AddFixItHint(FixItHint::CreateReplacement(
   3158  1.1.1.2  joerg               RecRange, Method->getClassInterface()->getName()));
   3159  1.1.1.2  joerg         }
   3160  1.1.1.2  joerg       }
   3161  1.1.1.2  joerg       Diag(Method->getLocation(), diag::note_direct_method_declared_at)
   3162  1.1.1.2  joerg           << Method->getDeclName();
   3163  1.1.1.2  joerg     }
   3164  1.1.1.2  joerg 
   3165  1.1.1.2  joerg     if (SuperLoc.isValid()) {
   3166  1.1.1.2  joerg       {
   3167  1.1.1.2  joerg         auto Builder =
   3168  1.1.1.2  joerg             Diag(SuperLoc, diag::err_messaging_super_with_direct_method);
   3169  1.1.1.2  joerg         if (ReceiverType->isObjCClassType()) {
   3170  1.1.1.2  joerg           Builder.AddFixItHint(FixItHint::CreateReplacement(
   3171  1.1.1.2  joerg               SuperLoc, Method->getClassInterface()->getName()));
   3172  1.1.1.2  joerg         } else {
   3173  1.1.1.2  joerg           Builder.AddFixItHint(FixItHint::CreateReplacement(SuperLoc, "self"));
   3174  1.1.1.2  joerg         }
   3175  1.1.1.2  joerg       }
   3176  1.1.1.2  joerg       Diag(Method->getLocation(), diag::note_direct_method_declared_at)
   3177  1.1.1.2  joerg           << Method->getDeclName();
   3178  1.1.1.2  joerg     }
   3179  1.1.1.2  joerg   } else if (ReceiverType->isObjCIdType() && !isImplicit) {
   3180  1.1.1.2  joerg     Diag(Receiver->getExprLoc(), diag::warn_messaging_unqualified_id);
   3181  1.1.1.2  joerg   }
   3182  1.1.1.2  joerg 
   3183      1.1  joerg   if (DIFunctionScopeInfo &&
   3184      1.1  joerg       DIFunctionScopeInfo->ObjCIsDesignatedInit &&
   3185      1.1  joerg       (SuperLoc.isValid() || isSelfExpr(Receiver))) {
   3186      1.1  joerg     bool isDesignatedInitChain = false;
   3187      1.1  joerg     if (SuperLoc.isValid()) {
   3188      1.1  joerg       if (const ObjCObjectPointerType *
   3189      1.1  joerg             OCIType = ReceiverType->getAsObjCInterfacePointerType()) {
   3190      1.1  joerg         if (const ObjCInterfaceDecl *ID = OCIType->getInterfaceDecl()) {
   3191      1.1  joerg           // Either we know this is a designated initializer or we
   3192      1.1  joerg           // conservatively assume it because we don't know for sure.
   3193      1.1  joerg           if (!ID->declaresOrInheritsDesignatedInitializers() ||
   3194      1.1  joerg               ID->isDesignatedInitializer(Sel)) {
   3195      1.1  joerg             isDesignatedInitChain = true;
   3196      1.1  joerg             DIFunctionScopeInfo->ObjCWarnForNoDesignatedInitChain = false;
   3197      1.1  joerg           }
   3198      1.1  joerg         }
   3199      1.1  joerg       }
   3200      1.1  joerg     }
   3201      1.1  joerg     if (!isDesignatedInitChain) {
   3202      1.1  joerg       const ObjCMethodDecl *InitMethod = nullptr;
   3203      1.1  joerg       bool isDesignated =
   3204      1.1  joerg         getCurMethodDecl()->isDesignatedInitializerForTheInterface(&InitMethod);
   3205      1.1  joerg       assert(isDesignated && InitMethod);
   3206      1.1  joerg       (void)isDesignated;
   3207      1.1  joerg       Diag(SelLoc, SuperLoc.isValid() ?
   3208      1.1  joerg              diag::warn_objc_designated_init_non_designated_init_call :
   3209      1.1  joerg              diag::warn_objc_designated_init_non_super_designated_init_call);
   3210      1.1  joerg       Diag(InitMethod->getLocation(),
   3211      1.1  joerg            diag::note_objc_designated_init_marked_here);
   3212      1.1  joerg     }
   3213      1.1  joerg   }
   3214      1.1  joerg 
   3215      1.1  joerg   if (DIFunctionScopeInfo &&
   3216      1.1  joerg       DIFunctionScopeInfo->ObjCIsSecondaryInit &&
   3217      1.1  joerg       (SuperLoc.isValid() || isSelfExpr(Receiver))) {
   3218      1.1  joerg     if (SuperLoc.isValid()) {
   3219      1.1  joerg       Diag(SelLoc, diag::warn_objc_secondary_init_super_init_call);
   3220      1.1  joerg     } else {
   3221      1.1  joerg       DIFunctionScopeInfo->ObjCWarnForNoInitDelegation = false;
   3222      1.1  joerg     }
   3223      1.1  joerg   }
   3224      1.1  joerg 
   3225      1.1  joerg   // Check the message arguments.
   3226      1.1  joerg   unsigned NumArgs = ArgsIn.size();
   3227      1.1  joerg   Expr **Args = ArgsIn.data();
   3228      1.1  joerg   QualType ReturnType;
   3229      1.1  joerg   ExprValueKind VK = VK_RValue;
   3230      1.1  joerg   bool ClassMessage = (ReceiverType->isObjCClassType() ||
   3231      1.1  joerg                        ReceiverType->isObjCQualifiedClassType());
   3232      1.1  joerg   if (CheckMessageArgumentTypes(Receiver, ReceiverType,
   3233      1.1  joerg                                 MultiExprArg(Args, NumArgs), Sel, SelectorLocs,
   3234      1.1  joerg                                 Method, ClassMessage, SuperLoc.isValid(),
   3235      1.1  joerg                                 LBracLoc, RBracLoc, RecRange, ReturnType, VK))
   3236      1.1  joerg     return ExprError();
   3237      1.1  joerg 
   3238      1.1  joerg   if (Method && !Method->getReturnType()->isVoidType() &&
   3239      1.1  joerg       RequireCompleteType(LBracLoc, Method->getReturnType(),
   3240      1.1  joerg                           diag::err_illegal_message_expr_incomplete_type))
   3241      1.1  joerg     return ExprError();
   3242      1.1  joerg 
   3243      1.1  joerg   // In ARC, forbid the user from sending messages to
   3244      1.1  joerg   // retain/release/autorelease/dealloc/retainCount explicitly.
   3245      1.1  joerg   if (getLangOpts().ObjCAutoRefCount) {
   3246      1.1  joerg     ObjCMethodFamily family =
   3247      1.1  joerg       (Method ? Method->getMethodFamily() : Sel.getMethodFamily());
   3248      1.1  joerg     switch (family) {
   3249      1.1  joerg     case OMF_init:
   3250      1.1  joerg       if (Method)
   3251      1.1  joerg         checkInitMethod(Method, ReceiverType);
   3252      1.1  joerg       break;
   3253      1.1  joerg 
   3254      1.1  joerg     case OMF_None:
   3255      1.1  joerg     case OMF_alloc:
   3256      1.1  joerg     case OMF_copy:
   3257      1.1  joerg     case OMF_finalize:
   3258      1.1  joerg     case OMF_mutableCopy:
   3259      1.1  joerg     case OMF_new:
   3260      1.1  joerg     case OMF_self:
   3261      1.1  joerg     case OMF_initialize:
   3262      1.1  joerg       break;
   3263      1.1  joerg 
   3264      1.1  joerg     case OMF_dealloc:
   3265      1.1  joerg     case OMF_retain:
   3266      1.1  joerg     case OMF_release:
   3267      1.1  joerg     case OMF_autorelease:
   3268      1.1  joerg     case OMF_retainCount:
   3269      1.1  joerg       Diag(SelLoc, diag::err_arc_illegal_explicit_message)
   3270      1.1  joerg         << Sel << RecRange;
   3271      1.1  joerg       break;
   3272      1.1  joerg 
   3273      1.1  joerg     case OMF_performSelector:
   3274      1.1  joerg       if (Method && NumArgs >= 1) {
   3275      1.1  joerg         if (const auto *SelExp =
   3276      1.1  joerg                 dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens())) {
   3277      1.1  joerg           Selector ArgSel = SelExp->getSelector();
   3278      1.1  joerg           ObjCMethodDecl *SelMethod =
   3279      1.1  joerg             LookupInstanceMethodInGlobalPool(ArgSel,
   3280      1.1  joerg                                              SelExp->getSourceRange());
   3281      1.1  joerg           if (!SelMethod)
   3282      1.1  joerg             SelMethod =
   3283      1.1  joerg               LookupFactoryMethodInGlobalPool(ArgSel,
   3284      1.1  joerg                                               SelExp->getSourceRange());
   3285      1.1  joerg           if (SelMethod) {
   3286      1.1  joerg             ObjCMethodFamily SelFamily = SelMethod->getMethodFamily();
   3287      1.1  joerg             switch (SelFamily) {
   3288      1.1  joerg               case OMF_alloc:
   3289      1.1  joerg               case OMF_copy:
   3290      1.1  joerg               case OMF_mutableCopy:
   3291      1.1  joerg               case OMF_new:
   3292      1.1  joerg               case OMF_init:
   3293      1.1  joerg                 // Issue error, unless ns_returns_not_retained.
   3294      1.1  joerg                 if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) {
   3295      1.1  joerg                   // selector names a +1 method
   3296      1.1  joerg                   Diag(SelLoc,
   3297      1.1  joerg                        diag::err_arc_perform_selector_retains);
   3298      1.1  joerg                   Diag(SelMethod->getLocation(), diag::note_method_declared_at)
   3299      1.1  joerg                     << SelMethod->getDeclName();
   3300      1.1  joerg                 }
   3301      1.1  joerg                 break;
   3302      1.1  joerg               default:
   3303      1.1  joerg                 // +0 call. OK. unless ns_returns_retained.
   3304      1.1  joerg                 if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) {
   3305      1.1  joerg                   // selector names a +1 method
   3306      1.1  joerg                   Diag(SelLoc,
   3307      1.1  joerg                        diag::err_arc_perform_selector_retains);
   3308      1.1  joerg                   Diag(SelMethod->getLocation(), diag::note_method_declared_at)
   3309      1.1  joerg                     << SelMethod->getDeclName();
   3310      1.1  joerg                 }
   3311      1.1  joerg                 break;
   3312      1.1  joerg             }
   3313      1.1  joerg           }
   3314      1.1  joerg         } else {
   3315      1.1  joerg           // error (may leak).
   3316      1.1  joerg           Diag(SelLoc, diag::warn_arc_perform_selector_leaks);
   3317      1.1  joerg           Diag(Args[0]->getExprLoc(), diag::note_used_here);
   3318      1.1  joerg         }
   3319      1.1  joerg       }
   3320      1.1  joerg       break;
   3321      1.1  joerg     }
   3322      1.1  joerg   }
   3323      1.1  joerg 
   3324      1.1  joerg   DiagnoseCStringFormatDirectiveInObjCAPI(*this, Method, Sel, Args, NumArgs);
   3325      1.1  joerg 
   3326      1.1  joerg   // Construct the appropriate ObjCMessageExpr instance.
   3327      1.1  joerg   ObjCMessageExpr *Result;
   3328      1.1  joerg   if (SuperLoc.isValid())
   3329      1.1  joerg     Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
   3330      1.1  joerg                                      SuperLoc,  /*IsInstanceSuper=*/true,
   3331      1.1  joerg                                      ReceiverType, Sel, SelectorLocs, Method,
   3332      1.1  joerg                                      makeArrayRef(Args, NumArgs), RBracLoc,
   3333      1.1  joerg                                      isImplicit);
   3334      1.1  joerg   else {
   3335      1.1  joerg     Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc,
   3336      1.1  joerg                                      Receiver, Sel, SelectorLocs, Method,
   3337      1.1  joerg                                      makeArrayRef(Args, NumArgs), RBracLoc,
   3338      1.1  joerg                                      isImplicit);
   3339      1.1  joerg     if (!isImplicit)
   3340      1.1  joerg       checkCocoaAPI(*this, Result);
   3341      1.1  joerg   }
   3342      1.1  joerg   if (Method) {
   3343      1.1  joerg     bool IsClassObjectCall = ClassMessage;
   3344      1.1  joerg     // 'self' message receivers in class methods should be treated as message
   3345      1.1  joerg     // sends to the class object in order for the semantic checks to be
   3346      1.1  joerg     // performed correctly. Messages to 'super' already count as class messages,
   3347      1.1  joerg     // so they don't need to be handled here.
   3348      1.1  joerg     if (Receiver && isSelfExpr(Receiver)) {
   3349      1.1  joerg       if (const auto *OPT = ReceiverType->getAs<ObjCObjectPointerType>()) {
   3350      1.1  joerg         if (OPT->getObjectType()->isObjCClass()) {
   3351      1.1  joerg           if (const auto *CurMeth = getCurMethodDecl()) {
   3352      1.1  joerg             IsClassObjectCall = true;
   3353      1.1  joerg             ReceiverType =
   3354      1.1  joerg                 Context.getObjCInterfaceType(CurMeth->getClassInterface());
   3355      1.1  joerg           }
   3356      1.1  joerg         }
   3357      1.1  joerg       }
   3358      1.1  joerg     }
   3359      1.1  joerg     checkFoundationAPI(*this, SelLoc, Method, makeArrayRef(Args, NumArgs),
   3360      1.1  joerg                        ReceiverType, IsClassObjectCall);
   3361      1.1  joerg   }
   3362      1.1  joerg 
   3363      1.1  joerg   if (getLangOpts().ObjCAutoRefCount) {
   3364      1.1  joerg     // In ARC, annotate delegate init calls.
   3365      1.1  joerg     if (Result->getMethodFamily() == OMF_init &&
   3366      1.1  joerg         (SuperLoc.isValid() || isSelfExpr(Receiver))) {
   3367      1.1  joerg       // Only consider init calls *directly* in init implementations,
   3368      1.1  joerg       // not within blocks.
   3369      1.1  joerg       ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext);
   3370      1.1  joerg       if (method && method->getMethodFamily() == OMF_init) {
   3371      1.1  joerg         // The implicit assignment to self means we also don't want to
   3372      1.1  joerg         // consume the result.
   3373      1.1  joerg         Result->setDelegateInitCall(true);
   3374      1.1  joerg         return Result;
   3375      1.1  joerg       }
   3376      1.1  joerg     }
   3377      1.1  joerg 
   3378      1.1  joerg     // In ARC, check for message sends which are likely to introduce
   3379      1.1  joerg     // retain cycles.
   3380      1.1  joerg     checkRetainCycles(Result);
   3381      1.1  joerg   }
   3382      1.1  joerg 
   3383      1.1  joerg   if (getLangOpts().ObjCWeak) {
   3384      1.1  joerg     if (!isImplicit && Method) {
   3385      1.1  joerg       if (const ObjCPropertyDecl *Prop = Method->findPropertyDecl()) {
   3386      1.1  joerg         bool IsWeak =
   3387  1.1.1.2  joerg             Prop->getPropertyAttributes() & ObjCPropertyAttribute::kind_weak;
   3388      1.1  joerg         if (!IsWeak && Sel.isUnarySelector())
   3389      1.1  joerg           IsWeak = ReturnType.getObjCLifetime() & Qualifiers::OCL_Weak;
   3390      1.1  joerg         if (IsWeak && !isUnevaluatedContext() &&
   3391      1.1  joerg             !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, LBracLoc))
   3392      1.1  joerg           getCurFunction()->recordUseOfWeak(Result, Prop);
   3393      1.1  joerg       }
   3394      1.1  joerg     }
   3395      1.1  joerg   }
   3396      1.1  joerg 
   3397      1.1  joerg   CheckObjCCircularContainer(Result);
   3398      1.1  joerg 
   3399      1.1  joerg   return MaybeBindToTemporary(Result);
   3400      1.1  joerg }
   3401      1.1  joerg 
   3402      1.1  joerg static void RemoveSelectorFromWarningCache(Sema &S, Expr* Arg) {
   3403      1.1  joerg   if (ObjCSelectorExpr *OSE =
   3404      1.1  joerg       dyn_cast<ObjCSelectorExpr>(Arg->IgnoreParenCasts())) {
   3405      1.1  joerg     Selector Sel = OSE->getSelector();
   3406      1.1  joerg     SourceLocation Loc = OSE->getAtLoc();
   3407      1.1  joerg     auto Pos = S.ReferencedSelectors.find(Sel);
   3408      1.1  joerg     if (Pos != S.ReferencedSelectors.end() && Pos->second == Loc)
   3409      1.1  joerg       S.ReferencedSelectors.erase(Pos);
   3410      1.1  joerg   }
   3411      1.1  joerg }
   3412      1.1  joerg 
   3413      1.1  joerg // ActOnInstanceMessage - used for both unary and keyword messages.
   3414      1.1  joerg // ArgExprs is optional - if it is present, the number of expressions
   3415      1.1  joerg // is obtained from Sel.getNumArgs().
   3416      1.1  joerg ExprResult Sema::ActOnInstanceMessage(Scope *S,
   3417      1.1  joerg                                       Expr *Receiver,
   3418      1.1  joerg                                       Selector Sel,
   3419      1.1  joerg                                       SourceLocation LBracLoc,
   3420      1.1  joerg                                       ArrayRef<SourceLocation> SelectorLocs,
   3421      1.1  joerg                                       SourceLocation RBracLoc,
   3422      1.1  joerg                                       MultiExprArg Args) {
   3423      1.1  joerg   if (!Receiver)
   3424      1.1  joerg     return ExprError();
   3425      1.1  joerg 
   3426      1.1  joerg   // A ParenListExpr can show up while doing error recovery with invalid code.
   3427      1.1  joerg   if (isa<ParenListExpr>(Receiver)) {
   3428      1.1  joerg     ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Receiver);
   3429      1.1  joerg     if (Result.isInvalid()) return ExprError();
   3430      1.1  joerg     Receiver = Result.get();
   3431      1.1  joerg   }
   3432      1.1  joerg 
   3433      1.1  joerg   if (RespondsToSelectorSel.isNull()) {
   3434      1.1  joerg     IdentifierInfo *SelectorId = &Context.Idents.get("respondsToSelector");
   3435      1.1  joerg     RespondsToSelectorSel = Context.Selectors.getUnarySelector(SelectorId);
   3436      1.1  joerg   }
   3437      1.1  joerg   if (Sel == RespondsToSelectorSel)
   3438      1.1  joerg     RemoveSelectorFromWarningCache(*this, Args[0]);
   3439      1.1  joerg 
   3440      1.1  joerg   return BuildInstanceMessage(Receiver, Receiver->getType(),
   3441      1.1  joerg                               /*SuperLoc=*/SourceLocation(), Sel,
   3442      1.1  joerg                               /*Method=*/nullptr, LBracLoc, SelectorLocs,
   3443      1.1  joerg                               RBracLoc, Args);
   3444      1.1  joerg }
   3445      1.1  joerg 
   3446      1.1  joerg enum ARCConversionTypeClass {
   3447      1.1  joerg   /// int, void, struct A
   3448      1.1  joerg   ACTC_none,
   3449      1.1  joerg 
   3450      1.1  joerg   /// id, void (^)()
   3451      1.1  joerg   ACTC_retainable,
   3452      1.1  joerg 
   3453      1.1  joerg   /// id*, id***, void (^*)(),
   3454      1.1  joerg   ACTC_indirectRetainable,
   3455      1.1  joerg 
   3456      1.1  joerg   /// void* might be a normal C type, or it might a CF type.
   3457      1.1  joerg   ACTC_voidPtr,
   3458      1.1  joerg 
   3459      1.1  joerg   /// struct A*
   3460      1.1  joerg   ACTC_coreFoundation
   3461      1.1  joerg };
   3462      1.1  joerg 
   3463      1.1  joerg static bool isAnyRetainable(ARCConversionTypeClass ACTC) {
   3464      1.1  joerg   return (ACTC == ACTC_retainable ||
   3465      1.1  joerg           ACTC == ACTC_coreFoundation ||
   3466      1.1  joerg           ACTC == ACTC_voidPtr);
   3467      1.1  joerg }
   3468      1.1  joerg 
   3469      1.1  joerg static bool isAnyCLike(ARCConversionTypeClass ACTC) {
   3470      1.1  joerg   return ACTC == ACTC_none ||
   3471      1.1  joerg          ACTC == ACTC_voidPtr ||
   3472      1.1  joerg          ACTC == ACTC_coreFoundation;
   3473      1.1  joerg }
   3474      1.1  joerg 
   3475      1.1  joerg static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) {
   3476      1.1  joerg   bool isIndirect = false;
   3477      1.1  joerg 
   3478      1.1  joerg   // Ignore an outermost reference type.
   3479      1.1  joerg   if (const ReferenceType *ref = type->getAs<ReferenceType>()) {
   3480      1.1  joerg     type = ref->getPointeeType();
   3481      1.1  joerg     isIndirect = true;
   3482      1.1  joerg   }
   3483      1.1  joerg 
   3484      1.1  joerg   // Drill through pointers and arrays recursively.
   3485      1.1  joerg   while (true) {
   3486      1.1  joerg     if (const PointerType *ptr = type->getAs<PointerType>()) {
   3487      1.1  joerg       type = ptr->getPointeeType();
   3488      1.1  joerg 
   3489      1.1  joerg       // The first level of pointer may be the innermost pointer on a CF type.
   3490      1.1  joerg       if (!isIndirect) {
   3491      1.1  joerg         if (type->isVoidType()) return ACTC_voidPtr;
   3492      1.1  joerg         if (type->isRecordType()) return ACTC_coreFoundation;
   3493      1.1  joerg       }
   3494      1.1  joerg     } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) {
   3495      1.1  joerg       type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0);
   3496      1.1  joerg     } else {
   3497      1.1  joerg       break;
   3498      1.1  joerg     }
   3499      1.1  joerg     isIndirect = true;
   3500      1.1  joerg   }
   3501      1.1  joerg 
   3502      1.1  joerg   if (isIndirect) {
   3503      1.1  joerg     if (type->isObjCARCBridgableType())
   3504      1.1  joerg       return ACTC_indirectRetainable;
   3505      1.1  joerg     return ACTC_none;
   3506      1.1  joerg   }
   3507      1.1  joerg 
   3508      1.1  joerg   if (type->isObjCARCBridgableType())
   3509      1.1  joerg     return ACTC_retainable;
   3510      1.1  joerg 
   3511      1.1  joerg   return ACTC_none;
   3512      1.1  joerg }
   3513      1.1  joerg 
   3514      1.1  joerg namespace {
   3515      1.1  joerg   /// A result from the cast checker.
   3516      1.1  joerg   enum ACCResult {
   3517      1.1  joerg     /// Cannot be casted.
   3518      1.1  joerg     ACC_invalid,
   3519      1.1  joerg 
   3520      1.1  joerg     /// Can be safely retained or not retained.
   3521      1.1  joerg     ACC_bottom,
   3522      1.1  joerg 
   3523      1.1  joerg     /// Can be casted at +0.
   3524      1.1  joerg     ACC_plusZero,
   3525      1.1  joerg 
   3526      1.1  joerg     /// Can be casted at +1.
   3527      1.1  joerg     ACC_plusOne
   3528      1.1  joerg   };
   3529      1.1  joerg   ACCResult merge(ACCResult left, ACCResult right) {
   3530      1.1  joerg     if (left == right) return left;
   3531      1.1  joerg     if (left == ACC_bottom) return right;
   3532      1.1  joerg     if (right == ACC_bottom) return left;
   3533      1.1  joerg     return ACC_invalid;
   3534      1.1  joerg   }
   3535      1.1  joerg 
   3536      1.1  joerg   /// A checker which white-lists certain expressions whose conversion
   3537      1.1  joerg   /// to or from retainable type would otherwise be forbidden in ARC.
   3538      1.1  joerg   class ARCCastChecker : public StmtVisitor<ARCCastChecker, ACCResult> {
   3539      1.1  joerg     typedef StmtVisitor<ARCCastChecker, ACCResult> super;
   3540      1.1  joerg 
   3541      1.1  joerg     ASTContext &Context;
   3542      1.1  joerg     ARCConversionTypeClass SourceClass;
   3543      1.1  joerg     ARCConversionTypeClass TargetClass;
   3544      1.1  joerg     bool Diagnose;
   3545      1.1  joerg 
   3546      1.1  joerg     static bool isCFType(QualType type) {
   3547      1.1  joerg       // Someday this can use ns_bridged.  For now, it has to do this.
   3548      1.1  joerg       return type->isCARCBridgableType();
   3549      1.1  joerg     }
   3550      1.1  joerg 
   3551      1.1  joerg   public:
   3552      1.1  joerg     ARCCastChecker(ASTContext &Context, ARCConversionTypeClass source,
   3553      1.1  joerg                    ARCConversionTypeClass target, bool diagnose)
   3554      1.1  joerg       : Context(Context), SourceClass(source), TargetClass(target),
   3555      1.1  joerg         Diagnose(diagnose) {}
   3556      1.1  joerg 
   3557      1.1  joerg     using super::Visit;
   3558      1.1  joerg     ACCResult Visit(Expr *e) {
   3559      1.1  joerg       return super::Visit(e->IgnoreParens());
   3560      1.1  joerg     }
   3561      1.1  joerg 
   3562      1.1  joerg     ACCResult VisitStmt(Stmt *s) {
   3563      1.1  joerg       return ACC_invalid;
   3564      1.1  joerg     }
   3565      1.1  joerg 
   3566      1.1  joerg     /// Null pointer constants can be casted however you please.
   3567      1.1  joerg     ACCResult VisitExpr(Expr *e) {
   3568      1.1  joerg       if (e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull))
   3569      1.1  joerg         return ACC_bottom;
   3570      1.1  joerg       return ACC_invalid;
   3571      1.1  joerg     }
   3572      1.1  joerg 
   3573      1.1  joerg     /// Objective-C string literals can be safely casted.
   3574      1.1  joerg     ACCResult VisitObjCStringLiteral(ObjCStringLiteral *e) {
   3575      1.1  joerg       // If we're casting to any retainable type, go ahead.  Global
   3576      1.1  joerg       // strings are immune to retains, so this is bottom.
   3577      1.1  joerg       if (isAnyRetainable(TargetClass)) return ACC_bottom;
   3578      1.1  joerg 
   3579      1.1  joerg       return ACC_invalid;
   3580      1.1  joerg     }
   3581      1.1  joerg 
   3582      1.1  joerg     /// Look through certain implicit and explicit casts.
   3583      1.1  joerg     ACCResult VisitCastExpr(CastExpr *e) {
   3584      1.1  joerg       switch (e->getCastKind()) {
   3585      1.1  joerg         case CK_NullToPointer:
   3586      1.1  joerg           return ACC_bottom;
   3587      1.1  joerg 
   3588      1.1  joerg         case CK_NoOp:
   3589      1.1  joerg         case CK_LValueToRValue:
   3590      1.1  joerg         case CK_BitCast:
   3591      1.1  joerg         case CK_CPointerToObjCPointerCast:
   3592      1.1  joerg         case CK_BlockPointerToObjCPointerCast:
   3593      1.1  joerg         case CK_AnyPointerToBlockPointerCast:
   3594      1.1  joerg           return Visit(e->getSubExpr());
   3595      1.1  joerg 
   3596      1.1  joerg         default:
   3597      1.1  joerg           return ACC_invalid;
   3598      1.1  joerg       }
   3599      1.1  joerg     }
   3600      1.1  joerg 
   3601      1.1  joerg     /// Look through unary extension.
   3602      1.1  joerg     ACCResult VisitUnaryExtension(UnaryOperator *e) {
   3603      1.1  joerg       return Visit(e->getSubExpr());
   3604      1.1  joerg     }
   3605      1.1  joerg 
   3606      1.1  joerg     /// Ignore the LHS of a comma operator.
   3607      1.1  joerg     ACCResult VisitBinComma(BinaryOperator *e) {
   3608      1.1  joerg       return Visit(e->getRHS());
   3609      1.1  joerg     }
   3610      1.1  joerg 
   3611      1.1  joerg     /// Conditional operators are okay if both sides are okay.
   3612      1.1  joerg     ACCResult VisitConditionalOperator(ConditionalOperator *e) {
   3613      1.1  joerg       ACCResult left = Visit(e->getTrueExpr());
   3614      1.1  joerg       if (left == ACC_invalid) return ACC_invalid;
   3615      1.1  joerg       return merge(left, Visit(e->getFalseExpr()));
   3616      1.1  joerg     }
   3617      1.1  joerg 
   3618      1.1  joerg     /// Look through pseudo-objects.
   3619      1.1  joerg     ACCResult VisitPseudoObjectExpr(PseudoObjectExpr *e) {
   3620      1.1  joerg       // If we're getting here, we should always have a result.
   3621      1.1  joerg       return Visit(e->getResultExpr());
   3622      1.1  joerg     }
   3623      1.1  joerg 
   3624      1.1  joerg     /// Statement expressions are okay if their result expression is okay.
   3625      1.1  joerg     ACCResult VisitStmtExpr(StmtExpr *e) {
   3626      1.1  joerg       return Visit(e->getSubStmt()->body_back());
   3627      1.1  joerg     }
   3628      1.1  joerg 
   3629      1.1  joerg     /// Some declaration references are okay.
   3630      1.1  joerg     ACCResult VisitDeclRefExpr(DeclRefExpr *e) {
   3631      1.1  joerg       VarDecl *var = dyn_cast<VarDecl>(e->getDecl());
   3632      1.1  joerg       // References to global constants are okay.
   3633      1.1  joerg       if (isAnyRetainable(TargetClass) &&
   3634      1.1  joerg           isAnyRetainable(SourceClass) &&
   3635      1.1  joerg           var &&
   3636      1.1  joerg           !var->hasDefinition(Context) &&
   3637      1.1  joerg           var->getType().isConstQualified()) {
   3638      1.1  joerg 
   3639      1.1  joerg         // In system headers, they can also be assumed to be immune to retains.
   3640      1.1  joerg         // These are things like 'kCFStringTransformToLatin'.
   3641      1.1  joerg         if (Context.getSourceManager().isInSystemHeader(var->getLocation()))
   3642      1.1  joerg           return ACC_bottom;
   3643      1.1  joerg 
   3644      1.1  joerg         return ACC_plusZero;
   3645      1.1  joerg       }
   3646      1.1  joerg 
   3647      1.1  joerg       // Nothing else.
   3648      1.1  joerg       return ACC_invalid;
   3649      1.1  joerg     }
   3650      1.1  joerg 
   3651      1.1  joerg     /// Some calls are okay.
   3652      1.1  joerg     ACCResult VisitCallExpr(CallExpr *e) {
   3653      1.1  joerg       if (FunctionDecl *fn = e->getDirectCallee())
   3654      1.1  joerg         if (ACCResult result = checkCallToFunction(fn))
   3655      1.1  joerg           return result;
   3656      1.1  joerg 
   3657      1.1  joerg       return super::VisitCallExpr(e);
   3658      1.1  joerg     }
   3659      1.1  joerg 
   3660      1.1  joerg     ACCResult checkCallToFunction(FunctionDecl *fn) {
   3661      1.1  joerg       // Require a CF*Ref return type.
   3662      1.1  joerg       if (!isCFType(fn->getReturnType()))
   3663      1.1  joerg         return ACC_invalid;
   3664      1.1  joerg 
   3665      1.1  joerg       if (!isAnyRetainable(TargetClass))
   3666      1.1  joerg         return ACC_invalid;
   3667      1.1  joerg 
   3668      1.1  joerg       // Honor an explicit 'not retained' attribute.
   3669      1.1  joerg       if (fn->hasAttr<CFReturnsNotRetainedAttr>())
   3670      1.1  joerg         return ACC_plusZero;
   3671      1.1  joerg 
   3672      1.1  joerg       // Honor an explicit 'retained' attribute, except that for
   3673      1.1  joerg       // now we're not going to permit implicit handling of +1 results,
   3674      1.1  joerg       // because it's a bit frightening.
   3675      1.1  joerg       if (fn->hasAttr<CFReturnsRetainedAttr>())
   3676      1.1  joerg         return Diagnose ? ACC_plusOne
   3677      1.1  joerg                         : ACC_invalid; // ACC_plusOne if we start accepting this
   3678      1.1  joerg 
   3679      1.1  joerg       // Recognize this specific builtin function, which is used by CFSTR.
   3680      1.1  joerg       unsigned builtinID = fn->getBuiltinID();
   3681      1.1  joerg       if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString)
   3682      1.1  joerg         return ACC_bottom;
   3683      1.1  joerg 
   3684      1.1  joerg       // Otherwise, don't do anything implicit with an unaudited function.
   3685      1.1  joerg       if (!fn->hasAttr<CFAuditedTransferAttr>())
   3686      1.1  joerg         return ACC_invalid;
   3687      1.1  joerg 
   3688      1.1  joerg       // Otherwise, it's +0 unless it follows the create convention.
   3689      1.1  joerg       if (ento::coreFoundation::followsCreateRule(fn))
   3690      1.1  joerg         return Diagnose ? ACC_plusOne
   3691      1.1  joerg                         : ACC_invalid; // ACC_plusOne if we start accepting this
   3692      1.1  joerg 
   3693      1.1  joerg       return ACC_plusZero;
   3694      1.1  joerg     }
   3695      1.1  joerg 
   3696      1.1  joerg     ACCResult VisitObjCMessageExpr(ObjCMessageExpr *e) {
   3697      1.1  joerg       return checkCallToMethod(e->getMethodDecl());
   3698      1.1  joerg     }
   3699      1.1  joerg 
   3700      1.1  joerg     ACCResult VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *e) {
   3701      1.1  joerg       ObjCMethodDecl *method;
   3702      1.1  joerg       if (e->isExplicitProperty())
   3703      1.1  joerg         method = e->getExplicitProperty()->getGetterMethodDecl();
   3704      1.1  joerg       else
   3705      1.1  joerg         method = e->getImplicitPropertyGetter();
   3706      1.1  joerg       return checkCallToMethod(method);
   3707      1.1  joerg     }
   3708      1.1  joerg 
   3709      1.1  joerg     ACCResult checkCallToMethod(ObjCMethodDecl *method) {
   3710      1.1  joerg       if (!method) return ACC_invalid;
   3711      1.1  joerg 
   3712      1.1  joerg       // Check for message sends to functions returning CF types.  We
   3713      1.1  joerg       // just obey the Cocoa conventions with these, even though the
   3714      1.1  joerg       // return type is CF.
   3715      1.1  joerg       if (!isAnyRetainable(TargetClass) || !isCFType(method->getReturnType()))
   3716      1.1  joerg         return ACC_invalid;
   3717      1.1  joerg 
   3718      1.1  joerg       // If the method is explicitly marked not-retained, it's +0.
   3719      1.1  joerg       if (method->hasAttr<CFReturnsNotRetainedAttr>())
   3720      1.1  joerg         return ACC_plusZero;
   3721      1.1  joerg 
   3722      1.1  joerg       // If the method is explicitly marked as returning retained, or its
   3723      1.1  joerg       // selector follows a +1 Cocoa convention, treat it as +1.
   3724      1.1  joerg       if (method->hasAttr<CFReturnsRetainedAttr>())
   3725      1.1  joerg         return ACC_plusOne;
   3726      1.1  joerg 
   3727      1.1  joerg       switch (method->getSelector().getMethodFamily()) {
   3728      1.1  joerg       case OMF_alloc:
   3729      1.1  joerg       case OMF_copy:
   3730      1.1  joerg       case OMF_mutableCopy:
   3731      1.1  joerg       case OMF_new:
   3732      1.1  joerg         return ACC_plusOne;
   3733      1.1  joerg 
   3734      1.1  joerg       default:
   3735      1.1  joerg         // Otherwise, treat it as +0.
   3736      1.1  joerg         return ACC_plusZero;
   3737      1.1  joerg       }
   3738      1.1  joerg     }
   3739      1.1  joerg   };
   3740      1.1  joerg } // end anonymous namespace
   3741      1.1  joerg 
   3742      1.1  joerg bool Sema::isKnownName(StringRef name) {
   3743      1.1  joerg   if (name.empty())
   3744      1.1  joerg     return false;
   3745      1.1  joerg   LookupResult R(*this, &Context.Idents.get(name), SourceLocation(),
   3746      1.1  joerg                  Sema::LookupOrdinaryName);
   3747      1.1  joerg   return LookupName(R, TUScope, false);
   3748      1.1  joerg }
   3749      1.1  joerg 
   3750  1.1.1.2  joerg template <typename DiagBuilderT>
   3751  1.1.1.2  joerg static void addFixitForObjCARCConversion(
   3752  1.1.1.2  joerg     Sema &S, DiagBuilderT &DiagB, Sema::CheckedConversionKind CCK,
   3753  1.1.1.2  joerg     SourceLocation afterLParen, QualType castType, Expr *castExpr,
   3754  1.1.1.2  joerg     Expr *realCast, const char *bridgeKeyword, const char *CFBridgeName) {
   3755      1.1  joerg   // We handle C-style and implicit casts here.
   3756      1.1  joerg   switch (CCK) {
   3757      1.1  joerg   case Sema::CCK_ImplicitConversion:
   3758      1.1  joerg   case Sema::CCK_ForBuiltinOverloadedOp:
   3759      1.1  joerg   case Sema::CCK_CStyleCast:
   3760      1.1  joerg   case Sema::CCK_OtherCast:
   3761      1.1  joerg     break;
   3762      1.1  joerg   case Sema::CCK_FunctionalCast:
   3763      1.1  joerg     return;
   3764      1.1  joerg   }
   3765      1.1  joerg 
   3766      1.1  joerg   if (CFBridgeName) {
   3767      1.1  joerg     if (CCK == Sema::CCK_OtherCast) {
   3768      1.1  joerg       if (const CXXNamedCastExpr *NCE = dyn_cast<CXXNamedCastExpr>(realCast)) {
   3769      1.1  joerg         SourceRange range(NCE->getOperatorLoc(),
   3770      1.1  joerg                           NCE->getAngleBrackets().getEnd());
   3771      1.1  joerg         SmallString<32> BridgeCall;
   3772      1.1  joerg 
   3773      1.1  joerg         SourceManager &SM = S.getSourceManager();
   3774      1.1  joerg         char PrevChar = *SM.getCharacterData(range.getBegin().getLocWithOffset(-1));
   3775      1.1  joerg         if (Lexer::isIdentifierBodyChar(PrevChar, S.getLangOpts()))
   3776      1.1  joerg           BridgeCall += ' ';
   3777      1.1  joerg 
   3778      1.1  joerg         BridgeCall += CFBridgeName;
   3779      1.1  joerg         DiagB.AddFixItHint(FixItHint::CreateReplacement(range, BridgeCall));
   3780      1.1  joerg       }
   3781      1.1  joerg       return;
   3782      1.1  joerg     }
   3783      1.1  joerg     Expr *castedE = castExpr;
   3784      1.1  joerg     if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(castedE))
   3785      1.1  joerg       castedE = CCE->getSubExpr();
   3786      1.1  joerg     castedE = castedE->IgnoreImpCasts();
   3787      1.1  joerg     SourceRange range = castedE->getSourceRange();
   3788      1.1  joerg 
   3789      1.1  joerg     SmallString<32> BridgeCall;
   3790      1.1  joerg 
   3791      1.1  joerg     SourceManager &SM = S.getSourceManager();
   3792      1.1  joerg     char PrevChar = *SM.getCharacterData(range.getBegin().getLocWithOffset(-1));
   3793      1.1  joerg     if (Lexer::isIdentifierBodyChar(PrevChar, S.getLangOpts()))
   3794      1.1  joerg       BridgeCall += ' ';
   3795      1.1  joerg 
   3796      1.1  joerg     BridgeCall += CFBridgeName;
   3797      1.1  joerg 
   3798      1.1  joerg     if (isa<ParenExpr>(castedE)) {
   3799      1.1  joerg       DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
   3800      1.1  joerg                          BridgeCall));
   3801      1.1  joerg     } else {
   3802      1.1  joerg       BridgeCall += '(';
   3803      1.1  joerg       DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
   3804      1.1  joerg                                                     BridgeCall));
   3805      1.1  joerg       DiagB.AddFixItHint(FixItHint::CreateInsertion(
   3806      1.1  joerg                                        S.getLocForEndOfToken(range.getEnd()),
   3807      1.1  joerg                                        ")"));
   3808      1.1  joerg     }
   3809      1.1  joerg     return;
   3810      1.1  joerg   }
   3811      1.1  joerg 
   3812      1.1  joerg   if (CCK == Sema::CCK_CStyleCast) {
   3813      1.1  joerg     DiagB.AddFixItHint(FixItHint::CreateInsertion(afterLParen, bridgeKeyword));
   3814      1.1  joerg   } else if (CCK == Sema::CCK_OtherCast) {
   3815      1.1  joerg     if (const CXXNamedCastExpr *NCE = dyn_cast<CXXNamedCastExpr>(realCast)) {
   3816      1.1  joerg       std::string castCode = "(";
   3817      1.1  joerg       castCode += bridgeKeyword;
   3818      1.1  joerg       castCode += castType.getAsString();
   3819      1.1  joerg       castCode += ")";
   3820      1.1  joerg       SourceRange Range(NCE->getOperatorLoc(),
   3821      1.1  joerg                         NCE->getAngleBrackets().getEnd());
   3822      1.1  joerg       DiagB.AddFixItHint(FixItHint::CreateReplacement(Range, castCode));
   3823      1.1  joerg     }
   3824      1.1  joerg   } else {
   3825      1.1  joerg     std::string castCode = "(";
   3826      1.1  joerg     castCode += bridgeKeyword;
   3827      1.1  joerg     castCode += castType.getAsString();
   3828      1.1  joerg     castCode += ")";
   3829      1.1  joerg     Expr *castedE = castExpr->IgnoreImpCasts();
   3830      1.1  joerg     SourceRange range = castedE->getSourceRange();
   3831      1.1  joerg     if (isa<ParenExpr>(castedE)) {
   3832      1.1  joerg       DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
   3833      1.1  joerg                          castCode));
   3834      1.1  joerg     } else {
   3835      1.1  joerg       castCode += "(";
   3836      1.1  joerg       DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(),
   3837      1.1  joerg                                                     castCode));
   3838      1.1  joerg       DiagB.AddFixItHint(FixItHint::CreateInsertion(
   3839      1.1  joerg                                        S.getLocForEndOfToken(range.getEnd()),
   3840      1.1  joerg                                        ")"));
   3841      1.1  joerg     }
   3842      1.1  joerg   }
   3843      1.1  joerg }
   3844      1.1  joerg 
   3845      1.1  joerg template <typename T>
   3846      1.1  joerg static inline T *getObjCBridgeAttr(const TypedefType *TD) {
   3847      1.1  joerg   TypedefNameDecl *TDNDecl = TD->getDecl();
   3848      1.1  joerg   QualType QT = TDNDecl->getUnderlyingType();
   3849      1.1  joerg   if (QT->isPointerType()) {
   3850      1.1  joerg     QT = QT->getPointeeType();
   3851  1.1.1.2  joerg     if (const RecordType *RT = QT->getAs<RecordType>()) {
   3852  1.1.1.2  joerg       for (auto *Redecl : RT->getDecl()->getMostRecentDecl()->redecls()) {
   3853  1.1.1.2  joerg         if (auto *attr = Redecl->getAttr<T>())
   3854  1.1.1.2  joerg           return attr;
   3855  1.1.1.2  joerg       }
   3856  1.1.1.2  joerg     }
   3857      1.1  joerg   }
   3858      1.1  joerg   return nullptr;
   3859      1.1  joerg }
   3860      1.1  joerg 
   3861      1.1  joerg static ObjCBridgeRelatedAttr *ObjCBridgeRelatedAttrFromType(QualType T,
   3862      1.1  joerg                                                             TypedefNameDecl *&TDNDecl) {
   3863      1.1  joerg   while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) {
   3864      1.1  joerg     TDNDecl = TD->getDecl();
   3865      1.1  joerg     if (ObjCBridgeRelatedAttr *ObjCBAttr =
   3866      1.1  joerg         getObjCBridgeAttr<ObjCBridgeRelatedAttr>(TD))
   3867      1.1  joerg       return ObjCBAttr;
   3868      1.1  joerg     T = TDNDecl->getUnderlyingType();
   3869      1.1  joerg   }
   3870      1.1  joerg   return nullptr;
   3871      1.1  joerg }
   3872      1.1  joerg 
   3873      1.1  joerg static void
   3874      1.1  joerg diagnoseObjCARCConversion(Sema &S, SourceRange castRange,
   3875      1.1  joerg                           QualType castType, ARCConversionTypeClass castACTC,
   3876      1.1  joerg                           Expr *castExpr, Expr *realCast,
   3877      1.1  joerg                           ARCConversionTypeClass exprACTC,
   3878      1.1  joerg                           Sema::CheckedConversionKind CCK) {
   3879      1.1  joerg   SourceLocation loc =
   3880      1.1  joerg     (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc());
   3881      1.1  joerg 
   3882      1.1  joerg   if (S.makeUnavailableInSystemHeader(loc,
   3883      1.1  joerg                                  UnavailableAttr::IR_ARCForbiddenConversion))
   3884      1.1  joerg     return;
   3885      1.1  joerg 
   3886      1.1  joerg   QualType castExprType = castExpr->getType();
   3887      1.1  joerg   // Defer emitting a diagnostic for bridge-related casts; that will be
   3888      1.1  joerg   // handled by CheckObjCBridgeRelatedConversions.
   3889      1.1  joerg   TypedefNameDecl *TDNDecl = nullptr;
   3890      1.1  joerg   if ((castACTC == ACTC_coreFoundation &&  exprACTC == ACTC_retainable &&
   3891      1.1  joerg        ObjCBridgeRelatedAttrFromType(castType, TDNDecl)) ||
   3892      1.1  joerg       (exprACTC == ACTC_coreFoundation && castACTC == ACTC_retainable &&
   3893      1.1  joerg        ObjCBridgeRelatedAttrFromType(castExprType, TDNDecl)))
   3894      1.1  joerg     return;
   3895      1.1  joerg 
   3896      1.1  joerg   unsigned srcKind = 0;
   3897      1.1  joerg   switch (exprACTC) {
   3898      1.1  joerg   case ACTC_none:
   3899      1.1  joerg   case ACTC_coreFoundation:
   3900      1.1  joerg   case ACTC_voidPtr:
   3901      1.1  joerg     srcKind = (castExprType->isPointerType() ? 1 : 0);
   3902      1.1  joerg     break;
   3903      1.1  joerg   case ACTC_retainable:
   3904      1.1  joerg     srcKind = (castExprType->isBlockPointerType() ? 2 : 3);
   3905      1.1  joerg     break;
   3906      1.1  joerg   case ACTC_indirectRetainable:
   3907      1.1  joerg     srcKind = 4;
   3908      1.1  joerg     break;
   3909      1.1  joerg   }
   3910      1.1  joerg 
   3911      1.1  joerg   // Check whether this could be fixed with a bridge cast.
   3912      1.1  joerg   SourceLocation afterLParen = S.getLocForEndOfToken(castRange.getBegin());
   3913      1.1  joerg   SourceLocation noteLoc = afterLParen.isValid() ? afterLParen : loc;
   3914      1.1  joerg 
   3915      1.1  joerg   unsigned convKindForDiag = Sema::isCast(CCK) ? 0 : 1;
   3916      1.1  joerg 
   3917      1.1  joerg   // Bridge from an ARC type to a CF type.
   3918      1.1  joerg   if (castACTC == ACTC_retainable && isAnyRetainable(exprACTC)) {
   3919      1.1  joerg 
   3920      1.1  joerg     S.Diag(loc, diag::err_arc_cast_requires_bridge)
   3921      1.1  joerg       << convKindForDiag
   3922      1.1  joerg       << 2 // of C pointer type
   3923      1.1  joerg       << castExprType
   3924      1.1  joerg       << unsigned(castType->isBlockPointerType()) // to ObjC|block type
   3925      1.1  joerg       << castType
   3926      1.1  joerg       << castRange
   3927      1.1  joerg       << castExpr->getSourceRange();
   3928      1.1  joerg     bool br = S.isKnownName("CFBridgingRelease");
   3929      1.1  joerg     ACCResult CreateRule =
   3930      1.1  joerg       ARCCastChecker(S.Context, exprACTC, castACTC, true).Visit(castExpr);
   3931      1.1  joerg     assert(CreateRule != ACC_bottom && "This cast should already be accepted.");
   3932      1.1  joerg     if (CreateRule != ACC_plusOne)
   3933      1.1  joerg     {
   3934  1.1.1.2  joerg       auto DiagB = (CCK != Sema::CCK_OtherCast)
   3935  1.1.1.2  joerg                        ? S.Diag(noteLoc, diag::note_arc_bridge)
   3936  1.1.1.2  joerg                        : S.Diag(noteLoc, diag::note_arc_cstyle_bridge);
   3937      1.1  joerg 
   3938      1.1  joerg       addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
   3939      1.1  joerg                                    castType, castExpr, realCast, "__bridge ",
   3940      1.1  joerg                                    nullptr);
   3941      1.1  joerg     }
   3942      1.1  joerg     if (CreateRule != ACC_plusZero)
   3943      1.1  joerg     {
   3944  1.1.1.2  joerg       auto DiagB = (CCK == Sema::CCK_OtherCast && !br)
   3945  1.1.1.2  joerg                        ? S.Diag(noteLoc, diag::note_arc_cstyle_bridge_transfer)
   3946  1.1.1.2  joerg                              << castExprType
   3947  1.1.1.2  joerg                        : S.Diag(br ? castExpr->getExprLoc() : noteLoc,
   3948  1.1.1.2  joerg                                 diag::note_arc_bridge_transfer)
   3949  1.1.1.2  joerg                              << castExprType << br;
   3950      1.1  joerg 
   3951      1.1  joerg       addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
   3952      1.1  joerg                                    castType, castExpr, realCast, "__bridge_transfer ",
   3953      1.1  joerg                                    br ? "CFBridgingRelease" : nullptr);
   3954      1.1  joerg     }
   3955      1.1  joerg 
   3956      1.1  joerg     return;
   3957      1.1  joerg   }
   3958      1.1  joerg 
   3959      1.1  joerg   // Bridge from a CF type to an ARC type.
   3960      1.1  joerg   if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC)) {
   3961      1.1  joerg     bool br = S.isKnownName("CFBridgingRetain");
   3962      1.1  joerg     S.Diag(loc, diag::err_arc_cast_requires_bridge)
   3963      1.1  joerg       << convKindForDiag
   3964      1.1  joerg       << unsigned(castExprType->isBlockPointerType()) // of ObjC|block type
   3965      1.1  joerg       << castExprType
   3966      1.1  joerg       << 2 // to C pointer type
   3967      1.1  joerg       << castType
   3968      1.1  joerg       << castRange
   3969      1.1  joerg       << castExpr->getSourceRange();
   3970      1.1  joerg     ACCResult CreateRule =
   3971      1.1  joerg       ARCCastChecker(S.Context, exprACTC, castACTC, true).Visit(castExpr);
   3972      1.1  joerg     assert(CreateRule != ACC_bottom && "This cast should already be accepted.");
   3973      1.1  joerg     if (CreateRule != ACC_plusOne)
   3974      1.1  joerg     {
   3975  1.1.1.2  joerg       auto DiagB = (CCK != Sema::CCK_OtherCast)
   3976  1.1.1.2  joerg                        ? S.Diag(noteLoc, diag::note_arc_bridge)
   3977  1.1.1.2  joerg                        : S.Diag(noteLoc, diag::note_arc_cstyle_bridge);
   3978      1.1  joerg       addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
   3979      1.1  joerg                                    castType, castExpr, realCast, "__bridge ",
   3980      1.1  joerg                                    nullptr);
   3981      1.1  joerg     }
   3982      1.1  joerg     if (CreateRule != ACC_plusZero)
   3983      1.1  joerg     {
   3984  1.1.1.2  joerg       auto DiagB = (CCK == Sema::CCK_OtherCast && !br)
   3985  1.1.1.2  joerg                        ? S.Diag(noteLoc, diag::note_arc_cstyle_bridge_retained)
   3986  1.1.1.2  joerg                              << castType
   3987  1.1.1.2  joerg                        : S.Diag(br ? castExpr->getExprLoc() : noteLoc,
   3988  1.1.1.2  joerg                                 diag::note_arc_bridge_retained)
   3989  1.1.1.2  joerg                              << castType << br;
   3990      1.1  joerg 
   3991      1.1  joerg       addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen,
   3992      1.1  joerg                                    castType, castExpr, realCast, "__bridge_retained ",
   3993      1.1  joerg                                    br ? "CFBridgingRetain" : nullptr);
   3994      1.1  joerg     }
   3995      1.1  joerg 
   3996      1.1  joerg     return;
   3997      1.1  joerg   }
   3998      1.1  joerg 
   3999      1.1  joerg   S.Diag(loc, diag::err_arc_mismatched_cast)
   4000      1.1  joerg     << !convKindForDiag
   4001      1.1  joerg     << srcKind << castExprType << castType
   4002      1.1  joerg     << castRange << castExpr->getSourceRange();
   4003      1.1  joerg }
   4004      1.1  joerg 
   4005      1.1  joerg template <typename TB>
   4006      1.1  joerg static bool CheckObjCBridgeNSCast(Sema &S, QualType castType, Expr *castExpr,
   4007      1.1  joerg                                   bool &HadTheAttribute, bool warn) {
   4008      1.1  joerg   QualType T = castExpr->getType();
   4009      1.1  joerg   HadTheAttribute = false;
   4010      1.1  joerg   while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) {
   4011      1.1  joerg     TypedefNameDecl *TDNDecl = TD->getDecl();
   4012      1.1  joerg     if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
   4013      1.1  joerg       if (IdentifierInfo *Parm = ObjCBAttr->getBridgedType()) {
   4014      1.1  joerg         HadTheAttribute = true;
   4015      1.1  joerg         if (Parm->isStr("id"))
   4016      1.1  joerg           return true;
   4017      1.1  joerg 
   4018      1.1  joerg         NamedDecl *Target = nullptr;
   4019      1.1  joerg         // Check for an existing type with this name.
   4020      1.1  joerg         LookupResult R(S, DeclarationName(Parm), SourceLocation(),
   4021      1.1  joerg                        Sema::LookupOrdinaryName);
   4022      1.1  joerg         if (S.LookupName(R, S.TUScope)) {
   4023      1.1  joerg           Target = R.getFoundDecl();
   4024      1.1  joerg           if (Target && isa<ObjCInterfaceDecl>(Target)) {
   4025      1.1  joerg             ObjCInterfaceDecl *ExprClass = cast<ObjCInterfaceDecl>(Target);
   4026      1.1  joerg             if (const ObjCObjectPointerType *InterfacePointerType =
   4027      1.1  joerg                   castType->getAsObjCInterfacePointerType()) {
   4028      1.1  joerg               ObjCInterfaceDecl *CastClass
   4029      1.1  joerg                 = InterfacePointerType->getObjectType()->getInterface();
   4030      1.1  joerg               if ((CastClass == ExprClass) ||
   4031      1.1  joerg                   (CastClass && CastClass->isSuperClassOf(ExprClass)))
   4032      1.1  joerg                 return true;
   4033      1.1  joerg               if (warn)
   4034      1.1  joerg                 S.Diag(castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge)
   4035      1.1  joerg                     << T << Target->getName() << castType->getPointeeType();
   4036      1.1  joerg               return false;
   4037      1.1  joerg             } else if (castType->isObjCIdType() ||
   4038      1.1  joerg                        (S.Context.ObjCObjectAdoptsQTypeProtocols(
   4039      1.1  joerg                           castType, ExprClass)))
   4040      1.1  joerg               // ok to cast to 'id'.
   4041      1.1  joerg               // casting to id<p-list> is ok if bridge type adopts all of
   4042      1.1  joerg               // p-list protocols.
   4043      1.1  joerg               return true;
   4044      1.1  joerg             else {
   4045      1.1  joerg               if (warn) {
   4046      1.1  joerg                 S.Diag(castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge)
   4047      1.1  joerg                     << T << Target->getName() << castType;
   4048      1.1  joerg                 S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4049      1.1  joerg                 S.Diag(Target->getBeginLoc(), diag::note_declared_at);
   4050      1.1  joerg               }
   4051      1.1  joerg               return false;
   4052      1.1  joerg            }
   4053      1.1  joerg           }
   4054      1.1  joerg         } else if (!castType->isObjCIdType()) {
   4055      1.1  joerg           S.Diag(castExpr->getBeginLoc(),
   4056      1.1  joerg                  diag::err_objc_cf_bridged_not_interface)
   4057      1.1  joerg               << castExpr->getType() << Parm;
   4058      1.1  joerg           S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4059      1.1  joerg           if (Target)
   4060      1.1  joerg             S.Diag(Target->getBeginLoc(), diag::note_declared_at);
   4061      1.1  joerg         }
   4062      1.1  joerg         return true;
   4063      1.1  joerg       }
   4064      1.1  joerg       return false;
   4065      1.1  joerg     }
   4066      1.1  joerg     T = TDNDecl->getUnderlyingType();
   4067      1.1  joerg   }
   4068      1.1  joerg   return true;
   4069      1.1  joerg }
   4070      1.1  joerg 
   4071      1.1  joerg template <typename TB>
   4072      1.1  joerg static bool CheckObjCBridgeCFCast(Sema &S, QualType castType, Expr *castExpr,
   4073      1.1  joerg                                   bool &HadTheAttribute, bool warn) {
   4074      1.1  joerg   QualType T = castType;
   4075      1.1  joerg   HadTheAttribute = false;
   4076      1.1  joerg   while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) {
   4077      1.1  joerg     TypedefNameDecl *TDNDecl = TD->getDecl();
   4078      1.1  joerg     if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) {
   4079      1.1  joerg       if (IdentifierInfo *Parm = ObjCBAttr->getBridgedType()) {
   4080      1.1  joerg         HadTheAttribute = true;
   4081      1.1  joerg         if (Parm->isStr("id"))
   4082      1.1  joerg           return true;
   4083      1.1  joerg 
   4084      1.1  joerg         NamedDecl *Target = nullptr;
   4085      1.1  joerg         // Check for an existing type with this name.
   4086      1.1  joerg         LookupResult R(S, DeclarationName(Parm), SourceLocation(),
   4087      1.1  joerg                        Sema::LookupOrdinaryName);
   4088      1.1  joerg         if (S.LookupName(R, S.TUScope)) {
   4089      1.1  joerg           Target = R.getFoundDecl();
   4090      1.1  joerg           if (Target && isa<ObjCInterfaceDecl>(Target)) {
   4091      1.1  joerg             ObjCInterfaceDecl *CastClass = cast<ObjCInterfaceDecl>(Target);
   4092      1.1  joerg             if (const ObjCObjectPointerType *InterfacePointerType =
   4093      1.1  joerg                   castExpr->getType()->getAsObjCInterfacePointerType()) {
   4094      1.1  joerg               ObjCInterfaceDecl *ExprClass
   4095      1.1  joerg                 = InterfacePointerType->getObjectType()->getInterface();
   4096      1.1  joerg               if ((CastClass == ExprClass) ||
   4097      1.1  joerg                   (ExprClass && CastClass->isSuperClassOf(ExprClass)))
   4098      1.1  joerg                 return true;
   4099      1.1  joerg               if (warn) {
   4100      1.1  joerg                 S.Diag(castExpr->getBeginLoc(),
   4101      1.1  joerg                        diag::warn_objc_invalid_bridge_to_cf)
   4102      1.1  joerg                     << castExpr->getType()->getPointeeType() << T;
   4103      1.1  joerg                 S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4104      1.1  joerg               }
   4105      1.1  joerg               return false;
   4106      1.1  joerg             } else if (castExpr->getType()->isObjCIdType() ||
   4107      1.1  joerg                        (S.Context.QIdProtocolsAdoptObjCObjectProtocols(
   4108      1.1  joerg                           castExpr->getType(), CastClass)))
   4109      1.1  joerg               // ok to cast an 'id' expression to a CFtype.
   4110      1.1  joerg               // ok to cast an 'id<plist>' expression to CFtype provided plist
   4111      1.1  joerg               // adopts all of CFtype's ObjetiveC's class plist.
   4112      1.1  joerg               return true;
   4113      1.1  joerg             else {
   4114      1.1  joerg               if (warn) {
   4115      1.1  joerg                 S.Diag(castExpr->getBeginLoc(),
   4116      1.1  joerg                        diag::warn_objc_invalid_bridge_to_cf)
   4117      1.1  joerg                     << castExpr->getType() << castType;
   4118      1.1  joerg                 S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4119      1.1  joerg                 S.Diag(Target->getBeginLoc(), diag::note_declared_at);
   4120      1.1  joerg               }
   4121      1.1  joerg               return false;
   4122      1.1  joerg             }
   4123      1.1  joerg           }
   4124      1.1  joerg         }
   4125      1.1  joerg         S.Diag(castExpr->getBeginLoc(),
   4126      1.1  joerg                diag::err_objc_ns_bridged_invalid_cfobject)
   4127      1.1  joerg             << castExpr->getType() << castType;
   4128      1.1  joerg         S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4129      1.1  joerg         if (Target)
   4130      1.1  joerg           S.Diag(Target->getBeginLoc(), diag::note_declared_at);
   4131      1.1  joerg         return true;
   4132      1.1  joerg       }
   4133      1.1  joerg       return false;
   4134      1.1  joerg     }
   4135      1.1  joerg     T = TDNDecl->getUnderlyingType();
   4136      1.1  joerg   }
   4137      1.1  joerg   return true;
   4138      1.1  joerg }
   4139      1.1  joerg 
   4140      1.1  joerg void Sema::CheckTollFreeBridgeCast(QualType castType, Expr *castExpr) {
   4141      1.1  joerg   if (!getLangOpts().ObjC)
   4142      1.1  joerg     return;
   4143      1.1  joerg   // warn in presence of __bridge casting to or from a toll free bridge cast.
   4144      1.1  joerg   ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExpr->getType());
   4145      1.1  joerg   ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType);
   4146      1.1  joerg   if (castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) {
   4147      1.1  joerg     bool HasObjCBridgeAttr;
   4148      1.1  joerg     bool ObjCBridgeAttrWillNotWarn =
   4149      1.1  joerg       CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr,
   4150      1.1  joerg                                             false);
   4151      1.1  joerg     if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
   4152      1.1  joerg       return;
   4153      1.1  joerg     bool HasObjCBridgeMutableAttr;
   4154      1.1  joerg     bool ObjCBridgeMutableAttrWillNotWarn =
   4155      1.1  joerg       CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr,
   4156      1.1  joerg                                                    HasObjCBridgeMutableAttr, false);
   4157      1.1  joerg     if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
   4158      1.1  joerg       return;
   4159      1.1  joerg 
   4160      1.1  joerg     if (HasObjCBridgeAttr)
   4161      1.1  joerg       CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr,
   4162      1.1  joerg                                             true);
   4163      1.1  joerg     else if (HasObjCBridgeMutableAttr)
   4164      1.1  joerg       CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr,
   4165      1.1  joerg                                                    HasObjCBridgeMutableAttr, true);
   4166      1.1  joerg   }
   4167      1.1  joerg   else if (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable) {
   4168      1.1  joerg     bool HasObjCBridgeAttr;
   4169      1.1  joerg     bool ObjCBridgeAttrWillNotWarn =
   4170      1.1  joerg       CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr,
   4171      1.1  joerg                                             false);
   4172      1.1  joerg     if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr)
   4173      1.1  joerg       return;
   4174      1.1  joerg     bool HasObjCBridgeMutableAttr;
   4175      1.1  joerg     bool ObjCBridgeMutableAttrWillNotWarn =
   4176      1.1  joerg       CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*this, castType, castExpr,
   4177      1.1  joerg                                                    HasObjCBridgeMutableAttr, false);
   4178      1.1  joerg     if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr)
   4179      1.1  joerg       return;
   4180      1.1  joerg 
   4181      1.1  joerg     if (HasObjCBridgeAttr)
   4182      1.1  joerg       CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr,
   4183      1.1  joerg                                             true);
   4184      1.1  joerg     else if (HasObjCBridgeMutableAttr)
   4185      1.1  joerg       CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*this, castType, castExpr,
   4186      1.1  joerg                                                    HasObjCBridgeMutableAttr, true);
   4187      1.1  joerg   }
   4188      1.1  joerg }
   4189      1.1  joerg 
   4190      1.1  joerg void Sema::CheckObjCBridgeRelatedCast(QualType castType, Expr *castExpr) {
   4191      1.1  joerg   QualType SrcType = castExpr->getType();
   4192      1.1  joerg   if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(castExpr)) {
   4193      1.1  joerg     if (PRE->isExplicitProperty()) {
   4194      1.1  joerg       if (ObjCPropertyDecl *PDecl = PRE->getExplicitProperty())
   4195      1.1  joerg         SrcType = PDecl->getType();
   4196      1.1  joerg     }
   4197      1.1  joerg     else if (PRE->isImplicitProperty()) {
   4198      1.1  joerg       if (ObjCMethodDecl *Getter = PRE->getImplicitPropertyGetter())
   4199      1.1  joerg         SrcType = Getter->getReturnType();
   4200      1.1  joerg     }
   4201      1.1  joerg   }
   4202      1.1  joerg 
   4203      1.1  joerg   ARCConversionTypeClass srcExprACTC = classifyTypeForARCConversion(SrcType);
   4204      1.1  joerg   ARCConversionTypeClass castExprACTC = classifyTypeForARCConversion(castType);
   4205      1.1  joerg   if (srcExprACTC != ACTC_retainable || castExprACTC != ACTC_coreFoundation)
   4206      1.1  joerg     return;
   4207      1.1  joerg   CheckObjCBridgeRelatedConversions(castExpr->getBeginLoc(), castType, SrcType,
   4208      1.1  joerg                                     castExpr);
   4209      1.1  joerg }
   4210      1.1  joerg 
   4211      1.1  joerg bool Sema::CheckTollFreeBridgeStaticCast(QualType castType, Expr *castExpr,
   4212      1.1  joerg                                          CastKind &Kind) {
   4213      1.1  joerg   if (!getLangOpts().ObjC)
   4214      1.1  joerg     return false;
   4215      1.1  joerg   ARCConversionTypeClass exprACTC =
   4216      1.1  joerg     classifyTypeForARCConversion(castExpr->getType());
   4217      1.1  joerg   ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType);
   4218      1.1  joerg   if ((castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) ||
   4219      1.1  joerg       (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable)) {
   4220      1.1  joerg     CheckTollFreeBridgeCast(castType, castExpr);
   4221      1.1  joerg     Kind = (castACTC == ACTC_coreFoundation) ? CK_BitCast
   4222      1.1  joerg                                              : CK_CPointerToObjCPointerCast;
   4223      1.1  joerg     return true;
   4224      1.1  joerg   }
   4225      1.1  joerg   return false;
   4226      1.1  joerg }
   4227      1.1  joerg 
   4228      1.1  joerg bool Sema::checkObjCBridgeRelatedComponents(SourceLocation Loc,
   4229      1.1  joerg                                             QualType DestType, QualType SrcType,
   4230      1.1  joerg                                             ObjCInterfaceDecl *&RelatedClass,
   4231      1.1  joerg                                             ObjCMethodDecl *&ClassMethod,
   4232      1.1  joerg                                             ObjCMethodDecl *&InstanceMethod,
   4233      1.1  joerg                                             TypedefNameDecl *&TDNDecl,
   4234      1.1  joerg                                             bool CfToNs, bool Diagnose) {
   4235      1.1  joerg   QualType T = CfToNs ? SrcType : DestType;
   4236      1.1  joerg   ObjCBridgeRelatedAttr *ObjCBAttr = ObjCBridgeRelatedAttrFromType(T, TDNDecl);
   4237      1.1  joerg   if (!ObjCBAttr)
   4238      1.1  joerg     return false;
   4239      1.1  joerg 
   4240      1.1  joerg   IdentifierInfo *RCId = ObjCBAttr->getRelatedClass();
   4241      1.1  joerg   IdentifierInfo *CMId = ObjCBAttr->getClassMethod();
   4242      1.1  joerg   IdentifierInfo *IMId = ObjCBAttr->getInstanceMethod();
   4243      1.1  joerg   if (!RCId)
   4244      1.1  joerg     return false;
   4245      1.1  joerg   NamedDecl *Target = nullptr;
   4246      1.1  joerg   // Check for an existing type with this name.
   4247      1.1  joerg   LookupResult R(*this, DeclarationName(RCId), SourceLocation(),
   4248      1.1  joerg                  Sema::LookupOrdinaryName);
   4249      1.1  joerg   if (!LookupName(R, TUScope)) {
   4250      1.1  joerg     if (Diagnose) {
   4251      1.1  joerg       Diag(Loc, diag::err_objc_bridged_related_invalid_class) << RCId
   4252      1.1  joerg             << SrcType << DestType;
   4253      1.1  joerg       Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4254      1.1  joerg     }
   4255      1.1  joerg     return false;
   4256      1.1  joerg   }
   4257      1.1  joerg   Target = R.getFoundDecl();
   4258      1.1  joerg   if (Target && isa<ObjCInterfaceDecl>(Target))
   4259      1.1  joerg     RelatedClass = cast<ObjCInterfaceDecl>(Target);
   4260      1.1  joerg   else {
   4261      1.1  joerg     if (Diagnose) {
   4262      1.1  joerg       Diag(Loc, diag::err_objc_bridged_related_invalid_class_name) << RCId
   4263      1.1  joerg             << SrcType << DestType;
   4264      1.1  joerg       Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4265      1.1  joerg       if (Target)
   4266      1.1  joerg         Diag(Target->getBeginLoc(), diag::note_declared_at);
   4267      1.1  joerg     }
   4268      1.1  joerg     return false;
   4269      1.1  joerg   }
   4270      1.1  joerg 
   4271      1.1  joerg   // Check for an existing class method with the given selector name.
   4272      1.1  joerg   if (CfToNs && CMId) {
   4273      1.1  joerg     Selector Sel = Context.Selectors.getUnarySelector(CMId);
   4274      1.1  joerg     ClassMethod = RelatedClass->lookupMethod(Sel, false);
   4275      1.1  joerg     if (!ClassMethod) {
   4276      1.1  joerg       if (Diagnose) {
   4277      1.1  joerg         Diag(Loc, diag::err_objc_bridged_related_known_method)
   4278      1.1  joerg               << SrcType << DestType << Sel << false;
   4279      1.1  joerg         Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4280      1.1  joerg       }
   4281      1.1  joerg       return false;
   4282      1.1  joerg     }
   4283      1.1  joerg   }
   4284      1.1  joerg 
   4285      1.1  joerg   // Check for an existing instance method with the given selector name.
   4286      1.1  joerg   if (!CfToNs && IMId) {
   4287      1.1  joerg     Selector Sel = Context.Selectors.getNullarySelector(IMId);
   4288      1.1  joerg     InstanceMethod = RelatedClass->lookupMethod(Sel, true);
   4289      1.1  joerg     if (!InstanceMethod) {
   4290      1.1  joerg       if (Diagnose) {
   4291      1.1  joerg         Diag(Loc, diag::err_objc_bridged_related_known_method)
   4292      1.1  joerg               << SrcType << DestType << Sel << true;
   4293      1.1  joerg         Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4294      1.1  joerg       }
   4295      1.1  joerg       return false;
   4296      1.1  joerg     }
   4297      1.1  joerg   }
   4298      1.1  joerg   return true;
   4299      1.1  joerg }
   4300      1.1  joerg 
   4301      1.1  joerg bool
   4302      1.1  joerg Sema::CheckObjCBridgeRelatedConversions(SourceLocation Loc,
   4303      1.1  joerg                                         QualType DestType, QualType SrcType,
   4304      1.1  joerg                                         Expr *&SrcExpr, bool Diagnose) {
   4305      1.1  joerg   ARCConversionTypeClass rhsExprACTC = classifyTypeForARCConversion(SrcType);
   4306      1.1  joerg   ARCConversionTypeClass lhsExprACTC = classifyTypeForARCConversion(DestType);
   4307      1.1  joerg   bool CfToNs = (rhsExprACTC == ACTC_coreFoundation && lhsExprACTC == ACTC_retainable);
   4308      1.1  joerg   bool NsToCf = (rhsExprACTC == ACTC_retainable && lhsExprACTC == ACTC_coreFoundation);
   4309      1.1  joerg   if (!CfToNs && !NsToCf)
   4310      1.1  joerg     return false;
   4311      1.1  joerg 
   4312      1.1  joerg   ObjCInterfaceDecl *RelatedClass;
   4313      1.1  joerg   ObjCMethodDecl *ClassMethod = nullptr;
   4314      1.1  joerg   ObjCMethodDecl *InstanceMethod = nullptr;
   4315      1.1  joerg   TypedefNameDecl *TDNDecl = nullptr;
   4316      1.1  joerg   if (!checkObjCBridgeRelatedComponents(Loc, DestType, SrcType, RelatedClass,
   4317      1.1  joerg                                         ClassMethod, InstanceMethod, TDNDecl,
   4318      1.1  joerg                                         CfToNs, Diagnose))
   4319      1.1  joerg     return false;
   4320      1.1  joerg 
   4321      1.1  joerg   if (CfToNs) {
   4322      1.1  joerg     // Implicit conversion from CF to ObjC object is needed.
   4323      1.1  joerg     if (ClassMethod) {
   4324      1.1  joerg       if (Diagnose) {
   4325      1.1  joerg         std::string ExpressionString = "[";
   4326      1.1  joerg         ExpressionString += RelatedClass->getNameAsString();
   4327      1.1  joerg         ExpressionString += " ";
   4328      1.1  joerg         ExpressionString += ClassMethod->getSelector().getAsString();
   4329      1.1  joerg         SourceLocation SrcExprEndLoc =
   4330      1.1  joerg             getLocForEndOfToken(SrcExpr->getEndLoc());
   4331      1.1  joerg         // Provide a fixit: [RelatedClass ClassMethod SrcExpr]
   4332      1.1  joerg         Diag(Loc, diag::err_objc_bridged_related_known_method)
   4333      1.1  joerg             << SrcType << DestType << ClassMethod->getSelector() << false
   4334      1.1  joerg             << FixItHint::CreateInsertion(SrcExpr->getBeginLoc(),
   4335      1.1  joerg                                           ExpressionString)
   4336      1.1  joerg             << FixItHint::CreateInsertion(SrcExprEndLoc, "]");
   4337      1.1  joerg         Diag(RelatedClass->getBeginLoc(), diag::note_declared_at);
   4338      1.1  joerg         Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4339      1.1  joerg 
   4340      1.1  joerg         QualType receiverType = Context.getObjCInterfaceType(RelatedClass);
   4341      1.1  joerg         // Argument.
   4342      1.1  joerg         Expr *args[] = { SrcExpr };
   4343      1.1  joerg         ExprResult msg = BuildClassMessageImplicit(receiverType, false,
   4344      1.1  joerg                                       ClassMethod->getLocation(),
   4345      1.1  joerg                                       ClassMethod->getSelector(), ClassMethod,
   4346      1.1  joerg                                       MultiExprArg(args, 1));
   4347      1.1  joerg         SrcExpr = msg.get();
   4348      1.1  joerg       }
   4349      1.1  joerg       return true;
   4350      1.1  joerg     }
   4351      1.1  joerg   }
   4352      1.1  joerg   else {
   4353      1.1  joerg     // Implicit conversion from ObjC type to CF object is needed.
   4354      1.1  joerg     if (InstanceMethod) {
   4355      1.1  joerg       if (Diagnose) {
   4356      1.1  joerg         std::string ExpressionString;
   4357      1.1  joerg         SourceLocation SrcExprEndLoc =
   4358      1.1  joerg             getLocForEndOfToken(SrcExpr->getEndLoc());
   4359      1.1  joerg         if (InstanceMethod->isPropertyAccessor())
   4360      1.1  joerg           if (const ObjCPropertyDecl *PDecl =
   4361      1.1  joerg                   InstanceMethod->findPropertyDecl()) {
   4362      1.1  joerg             // fixit: ObjectExpr.propertyname when it is  aproperty accessor.
   4363      1.1  joerg             ExpressionString = ".";
   4364      1.1  joerg             ExpressionString += PDecl->getNameAsString();
   4365      1.1  joerg             Diag(Loc, diag::err_objc_bridged_related_known_method)
   4366      1.1  joerg                 << SrcType << DestType << InstanceMethod->getSelector() << true
   4367      1.1  joerg                 << FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString);
   4368      1.1  joerg           }
   4369      1.1  joerg         if (ExpressionString.empty()) {
   4370      1.1  joerg           // Provide a fixit: [ObjectExpr InstanceMethod]
   4371      1.1  joerg           ExpressionString = " ";
   4372      1.1  joerg           ExpressionString += InstanceMethod->getSelector().getAsString();
   4373      1.1  joerg           ExpressionString += "]";
   4374      1.1  joerg 
   4375      1.1  joerg           Diag(Loc, diag::err_objc_bridged_related_known_method)
   4376      1.1  joerg               << SrcType << DestType << InstanceMethod->getSelector() << true
   4377      1.1  joerg               << FixItHint::CreateInsertion(SrcExpr->getBeginLoc(), "[")
   4378      1.1  joerg               << FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString);
   4379      1.1  joerg         }
   4380      1.1  joerg         Diag(RelatedClass->getBeginLoc(), diag::note_declared_at);
   4381      1.1  joerg         Diag(TDNDecl->getBeginLoc(), diag::note_declared_at);
   4382      1.1  joerg 
   4383      1.1  joerg         ExprResult msg =
   4384      1.1  joerg           BuildInstanceMessageImplicit(SrcExpr, SrcType,
   4385      1.1  joerg                                        InstanceMethod->getLocation(),
   4386      1.1  joerg                                        InstanceMethod->getSelector(),
   4387      1.1  joerg                                        InstanceMethod, None);
   4388      1.1  joerg         SrcExpr = msg.get();
   4389      1.1  joerg       }
   4390      1.1  joerg       return true;
   4391      1.1  joerg     }
   4392      1.1  joerg   }
   4393      1.1  joerg   return false;
   4394      1.1  joerg }
   4395      1.1  joerg 
   4396      1.1  joerg Sema::ARCConversionResult
   4397      1.1  joerg Sema::CheckObjCConversion(SourceRange castRange, QualType castType,
   4398      1.1  joerg                           Expr *&castExpr, CheckedConversionKind CCK,
   4399      1.1  joerg                           bool Diagnose, bool DiagnoseCFAudited,
   4400      1.1  joerg                           BinaryOperatorKind Opc) {
   4401      1.1  joerg   QualType castExprType = castExpr->getType();
   4402      1.1  joerg 
   4403      1.1  joerg   // For the purposes of the classification, we assume reference types
   4404      1.1  joerg   // will bind to temporaries.
   4405      1.1  joerg   QualType effCastType = castType;
   4406      1.1  joerg   if (const ReferenceType *ref = castType->getAs<ReferenceType>())
   4407      1.1  joerg     effCastType = ref->getPointeeType();
   4408      1.1  joerg 
   4409      1.1  joerg   ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType);
   4410      1.1  joerg   ARCConversionTypeClass castACTC = classifyTypeForARCConversion(effCastType);
   4411      1.1  joerg   if (exprACTC == castACTC) {
   4412      1.1  joerg     // Check for viability and report error if casting an rvalue to a
   4413      1.1  joerg     // life-time qualifier.
   4414      1.1  joerg     if (castACTC == ACTC_retainable &&
   4415      1.1  joerg         (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) &&
   4416      1.1  joerg         castType != castExprType) {
   4417      1.1  joerg       const Type *DT = castType.getTypePtr();
   4418      1.1  joerg       QualType QDT = castType;
   4419      1.1  joerg       // We desugar some types but not others. We ignore those
   4420      1.1  joerg       // that cannot happen in a cast; i.e. auto, and those which
   4421      1.1  joerg       // should not be de-sugared; i.e typedef.
   4422      1.1  joerg       if (const ParenType *PT = dyn_cast<ParenType>(DT))
   4423      1.1  joerg         QDT = PT->desugar();
   4424      1.1  joerg       else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT))
   4425      1.1  joerg         QDT = TP->desugar();
   4426      1.1  joerg       else if (const AttributedType *AT = dyn_cast<AttributedType>(DT))
   4427      1.1  joerg         QDT = AT->desugar();
   4428      1.1  joerg       if (QDT != castType &&
   4429      1.1  joerg           QDT.getObjCLifetime() !=  Qualifiers::OCL_None) {
   4430      1.1  joerg         if (Diagnose) {
   4431      1.1  joerg           SourceLocation loc = (castRange.isValid() ? castRange.getBegin()
   4432      1.1  joerg                                                     : castExpr->getExprLoc());
   4433      1.1  joerg           Diag(loc, diag::err_arc_nolifetime_behavior);
   4434      1.1  joerg         }
   4435      1.1  joerg         return ACR_error;
   4436      1.1  joerg       }
   4437      1.1  joerg     }
   4438      1.1  joerg     return ACR_okay;
   4439      1.1  joerg   }
   4440      1.1  joerg 
   4441      1.1  joerg   // The life-time qualifier cast check above is all we need for ObjCWeak.
   4442      1.1  joerg   // ObjCAutoRefCount has more restrictions on what is legal.
   4443      1.1  joerg   if (!getLangOpts().ObjCAutoRefCount)
   4444      1.1  joerg     return ACR_okay;
   4445      1.1  joerg 
   4446      1.1  joerg   if (isAnyCLike(exprACTC) && isAnyCLike(castACTC)) return ACR_okay;
   4447      1.1  joerg 
   4448      1.1  joerg   // Allow all of these types to be cast to integer types (but not
   4449      1.1  joerg   // vice-versa).
   4450      1.1  joerg   if (castACTC == ACTC_none && castType->isIntegralType(Context))
   4451      1.1  joerg     return ACR_okay;
   4452      1.1  joerg 
   4453      1.1  joerg   // Allow casts between pointers to lifetime types (e.g., __strong id*)
   4454      1.1  joerg   // and pointers to void (e.g., cv void *). Casting from void* to lifetime*
   4455      1.1  joerg   // must be explicit.
   4456      1.1  joerg   if (exprACTC == ACTC_indirectRetainable && castACTC == ACTC_voidPtr)
   4457      1.1  joerg     return ACR_okay;
   4458      1.1  joerg   if (castACTC == ACTC_indirectRetainable && exprACTC == ACTC_voidPtr &&
   4459      1.1  joerg       isCast(CCK))
   4460      1.1  joerg     return ACR_okay;
   4461      1.1  joerg 
   4462      1.1  joerg   switch (ARCCastChecker(Context, exprACTC, castACTC, false).Visit(castExpr)) {
   4463      1.1  joerg   // For invalid casts, fall through.
   4464      1.1  joerg   case ACC_invalid:
   4465      1.1  joerg     break;
   4466      1.1  joerg 
   4467      1.1  joerg   // Do nothing for both bottom and +0.
   4468      1.1  joerg   case ACC_bottom:
   4469      1.1  joerg   case ACC_plusZero:
   4470      1.1  joerg     return ACR_okay;
   4471      1.1  joerg 
   4472      1.1  joerg   // If the result is +1, consume it here.
   4473      1.1  joerg   case ACC_plusOne:
   4474      1.1  joerg     castExpr = ImplicitCastExpr::Create(Context, castExpr->getType(),
   4475  1.1.1.2  joerg                                         CK_ARCConsumeObject, castExpr, nullptr,
   4476  1.1.1.2  joerg                                         VK_RValue, FPOptionsOverride());
   4477      1.1  joerg     Cleanup.setExprNeedsCleanups(true);
   4478      1.1  joerg     return ACR_okay;
   4479      1.1  joerg   }
   4480      1.1  joerg 
   4481      1.1  joerg   // If this is a non-implicit cast from id or block type to a
   4482      1.1  joerg   // CoreFoundation type, delay complaining in case the cast is used
   4483      1.1  joerg   // in an acceptable context.
   4484      1.1  joerg   if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC) && isCast(CCK))
   4485      1.1  joerg     return ACR_unbridged;
   4486      1.1  joerg 
   4487      1.1  joerg   // Issue a diagnostic about a missing @-sign when implicit casting a cstring
   4488      1.1  joerg   // to 'NSString *', instead of falling through to report a "bridge cast"
   4489      1.1  joerg   // diagnostic.
   4490      1.1  joerg   if (castACTC == ACTC_retainable && exprACTC == ACTC_none &&
   4491  1.1.1.2  joerg       CheckConversionToObjCLiteral(castType, castExpr, Diagnose))
   4492      1.1  joerg     return ACR_error;
   4493      1.1  joerg 
   4494      1.1  joerg   // Do not issue "bridge cast" diagnostic when implicit casting
   4495      1.1  joerg   // a retainable object to a CF type parameter belonging to an audited
   4496      1.1  joerg   // CF API function. Let caller issue a normal type mismatched diagnostic
   4497      1.1  joerg   // instead.
   4498      1.1  joerg   if ((!DiagnoseCFAudited || exprACTC != ACTC_retainable ||
   4499      1.1  joerg        castACTC != ACTC_coreFoundation) &&
   4500      1.1  joerg       !(exprACTC == ACTC_voidPtr && castACTC == ACTC_retainable &&
   4501      1.1  joerg         (Opc == BO_NE || Opc == BO_EQ))) {
   4502      1.1  joerg     if (Diagnose)
   4503      1.1  joerg       diagnoseObjCARCConversion(*this, castRange, castType, castACTC, castExpr,
   4504      1.1  joerg                                 castExpr, exprACTC, CCK);
   4505      1.1  joerg     return ACR_error;
   4506      1.1  joerg   }
   4507      1.1  joerg   return ACR_okay;
   4508      1.1  joerg }
   4509      1.1  joerg 
   4510      1.1  joerg /// Given that we saw an expression with the ARCUnbridgedCastTy
   4511      1.1  joerg /// placeholder type, complain bitterly.
   4512      1.1  joerg void Sema::diagnoseARCUnbridgedCast(Expr *e) {
   4513      1.1  joerg   // We expect the spurious ImplicitCastExpr to already have been stripped.
   4514      1.1  joerg   assert(!e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast));
   4515      1.1  joerg   CastExpr *realCast = cast<CastExpr>(e->IgnoreParens());
   4516      1.1  joerg 
   4517      1.1  joerg   SourceRange castRange;
   4518      1.1  joerg   QualType castType;
   4519      1.1  joerg   CheckedConversionKind CCK;
   4520      1.1  joerg 
   4521      1.1  joerg   if (CStyleCastExpr *cast = dyn_cast<CStyleCastExpr>(realCast)) {
   4522      1.1  joerg     castRange = SourceRange(cast->getLParenLoc(), cast->getRParenLoc());
   4523      1.1  joerg     castType = cast->getTypeAsWritten();
   4524      1.1  joerg     CCK = CCK_CStyleCast;
   4525      1.1  joerg   } else if (ExplicitCastExpr *cast = dyn_cast<ExplicitCastExpr>(realCast)) {
   4526      1.1  joerg     castRange = cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange();
   4527      1.1  joerg     castType = cast->getTypeAsWritten();
   4528      1.1  joerg     CCK = CCK_OtherCast;
   4529      1.1  joerg   } else {
   4530      1.1  joerg     llvm_unreachable("Unexpected ImplicitCastExpr");
   4531      1.1  joerg   }
   4532      1.1  joerg 
   4533      1.1  joerg   ARCConversionTypeClass castACTC =
   4534      1.1  joerg     classifyTypeForARCConversion(castType.getNonReferenceType());
   4535      1.1  joerg 
   4536      1.1  joerg   Expr *castExpr = realCast->getSubExpr();
   4537      1.1  joerg   assert(classifyTypeForARCConversion(castExpr->getType()) == ACTC_retainable);
   4538      1.1  joerg 
   4539      1.1  joerg   diagnoseObjCARCConversion(*this, castRange, castType, castACTC,
   4540      1.1  joerg                             castExpr, realCast, ACTC_retainable, CCK);
   4541      1.1  joerg }
   4542      1.1  joerg 
   4543      1.1  joerg /// stripARCUnbridgedCast - Given an expression of ARCUnbridgedCast
   4544      1.1  joerg /// type, remove the placeholder cast.
   4545      1.1  joerg Expr *Sema::stripARCUnbridgedCast(Expr *e) {
   4546      1.1  joerg   assert(e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast));
   4547      1.1  joerg 
   4548      1.1  joerg   if (ParenExpr *pe = dyn_cast<ParenExpr>(e)) {
   4549      1.1  joerg     Expr *sub = stripARCUnbridgedCast(pe->getSubExpr());
   4550      1.1  joerg     return new (Context) ParenExpr(pe->getLParen(), pe->getRParen(), sub);
   4551      1.1  joerg   } else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) {
   4552      1.1  joerg     assert(uo->getOpcode() == UO_Extension);
   4553      1.1  joerg     Expr *sub = stripARCUnbridgedCast(uo->getSubExpr());
   4554  1.1.1.2  joerg     return UnaryOperator::Create(Context, sub, UO_Extension, sub->getType(),
   4555  1.1.1.2  joerg                                  sub->getValueKind(), sub->getObjectKind(),
   4556  1.1.1.2  joerg                                  uo->getOperatorLoc(), false,
   4557  1.1.1.2  joerg                                  CurFPFeatureOverrides());
   4558      1.1  joerg   } else if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) {
   4559      1.1  joerg     assert(!gse->isResultDependent());
   4560      1.1  joerg 
   4561      1.1  joerg     unsigned n = gse->getNumAssocs();
   4562      1.1  joerg     SmallVector<Expr *, 4> subExprs;
   4563      1.1  joerg     SmallVector<TypeSourceInfo *, 4> subTypes;
   4564      1.1  joerg     subExprs.reserve(n);
   4565      1.1  joerg     subTypes.reserve(n);
   4566  1.1.1.2  joerg     for (const GenericSelectionExpr::Association assoc : gse->associations()) {
   4567      1.1  joerg       subTypes.push_back(assoc.getTypeSourceInfo());
   4568      1.1  joerg       Expr *sub = assoc.getAssociationExpr();
   4569      1.1  joerg       if (assoc.isSelected())
   4570      1.1  joerg         sub = stripARCUnbridgedCast(sub);
   4571      1.1  joerg       subExprs.push_back(sub);
   4572      1.1  joerg     }
   4573      1.1  joerg 
   4574      1.1  joerg     return GenericSelectionExpr::Create(
   4575      1.1  joerg         Context, gse->getGenericLoc(), gse->getControllingExpr(), subTypes,
   4576      1.1  joerg         subExprs, gse->getDefaultLoc(), gse->getRParenLoc(),
   4577      1.1  joerg         gse->containsUnexpandedParameterPack(), gse->getResultIndex());
   4578      1.1  joerg   } else {
   4579      1.1  joerg     assert(isa<ImplicitCastExpr>(e) && "bad form of unbridged cast!");
   4580      1.1  joerg     return cast<ImplicitCastExpr>(e)->getSubExpr();
   4581      1.1  joerg   }
   4582      1.1  joerg }
   4583      1.1  joerg 
   4584      1.1  joerg bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType,
   4585      1.1  joerg                                                  QualType exprType) {
   4586      1.1  joerg   QualType canCastType =
   4587      1.1  joerg     Context.getCanonicalType(castType).getUnqualifiedType();
   4588      1.1  joerg   QualType canExprType =
   4589      1.1  joerg     Context.getCanonicalType(exprType).getUnqualifiedType();
   4590      1.1  joerg   if (isa<ObjCObjectPointerType>(canCastType) &&
   4591      1.1  joerg       castType.getObjCLifetime() == Qualifiers::OCL_Weak &&
   4592      1.1  joerg       canExprType->isObjCObjectPointerType()) {
   4593      1.1  joerg     if (const ObjCObjectPointerType *ObjT =
   4594      1.1  joerg         canExprType->getAs<ObjCObjectPointerType>())
   4595      1.1  joerg       if (const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl())
   4596      1.1  joerg         return !ObjI->isArcWeakrefUnavailable();
   4597      1.1  joerg   }
   4598      1.1  joerg   return true;
   4599      1.1  joerg }
   4600      1.1  joerg 
   4601      1.1  joerg /// Look for an ObjCReclaimReturnedObject cast and destroy it.
   4602      1.1  joerg static Expr *maybeUndoReclaimObject(Expr *e) {
   4603      1.1  joerg   Expr *curExpr = e, *prevExpr = nullptr;
   4604      1.1  joerg 
   4605      1.1  joerg   // Walk down the expression until we hit an implicit cast of kind
   4606      1.1  joerg   // ARCReclaimReturnedObject or an Expr that is neither a Paren nor a Cast.
   4607      1.1  joerg   while (true) {
   4608      1.1  joerg     if (auto *pe = dyn_cast<ParenExpr>(curExpr)) {
   4609      1.1  joerg       prevExpr = curExpr;
   4610      1.1  joerg       curExpr = pe->getSubExpr();
   4611      1.1  joerg       continue;
   4612      1.1  joerg     }
   4613      1.1  joerg 
   4614      1.1  joerg     if (auto *ce = dyn_cast<CastExpr>(curExpr)) {
   4615      1.1  joerg       if (auto *ice = dyn_cast<ImplicitCastExpr>(ce))
   4616      1.1  joerg         if (ice->getCastKind() == CK_ARCReclaimReturnedObject) {
   4617      1.1  joerg           if (!prevExpr)
   4618      1.1  joerg             return ice->getSubExpr();
   4619      1.1  joerg           if (auto *pe = dyn_cast<ParenExpr>(prevExpr))
   4620      1.1  joerg             pe->setSubExpr(ice->getSubExpr());
   4621      1.1  joerg           else
   4622      1.1  joerg             cast<CastExpr>(prevExpr)->setSubExpr(ice->getSubExpr());
   4623      1.1  joerg           return e;
   4624      1.1  joerg         }
   4625      1.1  joerg 
   4626      1.1  joerg       prevExpr = curExpr;
   4627      1.1  joerg       curExpr = ce->getSubExpr();
   4628      1.1  joerg       continue;
   4629      1.1  joerg     }
   4630      1.1  joerg 
   4631      1.1  joerg     // Break out of the loop if curExpr is neither a Paren nor a Cast.
   4632      1.1  joerg     break;
   4633      1.1  joerg   }
   4634      1.1  joerg 
   4635      1.1  joerg   return e;
   4636      1.1  joerg }
   4637      1.1  joerg 
   4638      1.1  joerg ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc,
   4639      1.1  joerg                                       ObjCBridgeCastKind Kind,
   4640      1.1  joerg                                       SourceLocation BridgeKeywordLoc,
   4641      1.1  joerg                                       TypeSourceInfo *TSInfo,
   4642      1.1  joerg                                       Expr *SubExpr) {
   4643      1.1  joerg   ExprResult SubResult = UsualUnaryConversions(SubExpr);
   4644      1.1  joerg   if (SubResult.isInvalid()) return ExprError();
   4645      1.1  joerg   SubExpr = SubResult.get();
   4646      1.1  joerg 
   4647      1.1  joerg   QualType T = TSInfo->getType();
   4648      1.1  joerg   QualType FromType = SubExpr->getType();
   4649      1.1  joerg 
   4650      1.1  joerg   CastKind CK;
   4651      1.1  joerg 
   4652      1.1  joerg   bool MustConsume = false;
   4653      1.1  joerg   if (T->isDependentType() || SubExpr->isTypeDependent()) {
   4654      1.1  joerg     // Okay: we'll build a dependent expression type.
   4655      1.1  joerg     CK = CK_Dependent;
   4656      1.1  joerg   } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) {
   4657      1.1  joerg     // Casting CF -> id
   4658      1.1  joerg     CK = (T->isBlockPointerType() ? CK_AnyPointerToBlockPointerCast
   4659      1.1  joerg                                   : CK_CPointerToObjCPointerCast);
   4660      1.1  joerg     switch (Kind) {
   4661      1.1  joerg     case OBC_Bridge:
   4662      1.1  joerg       break;
   4663      1.1  joerg 
   4664      1.1  joerg     case OBC_BridgeRetained: {
   4665      1.1  joerg       bool br = isKnownName("CFBridgingRelease");
   4666      1.1  joerg       Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
   4667      1.1  joerg         << 2
   4668      1.1  joerg         << FromType
   4669      1.1  joerg         << (T->isBlockPointerType()? 1 : 0)
   4670      1.1  joerg         << T
   4671      1.1  joerg         << SubExpr->getSourceRange()
   4672      1.1  joerg         << Kind;
   4673      1.1  joerg       Diag(BridgeKeywordLoc, diag::note_arc_bridge)
   4674      1.1  joerg         << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge");
   4675      1.1  joerg       Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer)
   4676      1.1  joerg         << FromType << br
   4677      1.1  joerg         << FixItHint::CreateReplacement(BridgeKeywordLoc,
   4678      1.1  joerg                                         br ? "CFBridgingRelease "
   4679      1.1  joerg                                            : "__bridge_transfer ");
   4680      1.1  joerg 
   4681      1.1  joerg       Kind = OBC_Bridge;
   4682      1.1  joerg       break;
   4683      1.1  joerg     }
   4684      1.1  joerg 
   4685      1.1  joerg     case OBC_BridgeTransfer:
   4686      1.1  joerg       // We must consume the Objective-C object produced by the cast.
   4687      1.1  joerg       MustConsume = true;
   4688      1.1  joerg       break;
   4689      1.1  joerg     }
   4690      1.1  joerg   } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) {
   4691      1.1  joerg     // Okay: id -> CF
   4692      1.1  joerg     CK = CK_BitCast;
   4693      1.1  joerg     switch (Kind) {
   4694      1.1  joerg     case OBC_Bridge:
   4695      1.1  joerg       // Reclaiming a value that's going to be __bridge-casted to CF
   4696      1.1  joerg       // is very dangerous, so we don't do it.
   4697      1.1  joerg       SubExpr = maybeUndoReclaimObject(SubExpr);
   4698      1.1  joerg       break;
   4699      1.1  joerg 
   4700      1.1  joerg     case OBC_BridgeRetained:
   4701      1.1  joerg       // Produce the object before casting it.
   4702  1.1.1.2  joerg       SubExpr = ImplicitCastExpr::Create(Context, FromType, CK_ARCProduceObject,
   4703  1.1.1.2  joerg                                          SubExpr, nullptr, VK_RValue,
   4704  1.1.1.2  joerg                                          FPOptionsOverride());
   4705      1.1  joerg       break;
   4706      1.1  joerg 
   4707      1.1  joerg     case OBC_BridgeTransfer: {
   4708      1.1  joerg       bool br = isKnownName("CFBridgingRetain");
   4709      1.1  joerg       Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind)
   4710      1.1  joerg         << (FromType->isBlockPointerType()? 1 : 0)
   4711      1.1  joerg         << FromType
   4712      1.1  joerg         << 2
   4713      1.1  joerg         << T
   4714      1.1  joerg         << SubExpr->getSourceRange()
   4715      1.1  joerg         << Kind;
   4716      1.1  joerg 
   4717      1.1  joerg       Diag(BridgeKeywordLoc, diag::note_arc_bridge)
   4718      1.1  joerg         << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge ");
   4719      1.1  joerg       Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained)
   4720      1.1  joerg         << T << br
   4721      1.1  joerg         << FixItHint::CreateReplacement(BridgeKeywordLoc,
   4722      1.1  joerg                           br ? "CFBridgingRetain " : "__bridge_retained");
   4723      1.1  joerg 
   4724      1.1  joerg       Kind = OBC_Bridge;
   4725      1.1  joerg       break;
   4726      1.1  joerg     }
   4727      1.1  joerg     }
   4728      1.1  joerg   } else {
   4729      1.1  joerg     Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible)
   4730      1.1  joerg       << FromType << T << Kind
   4731      1.1  joerg       << SubExpr->getSourceRange()
   4732      1.1  joerg       << TSInfo->getTypeLoc().getSourceRange();
   4733      1.1  joerg     return ExprError();
   4734      1.1  joerg   }
   4735      1.1  joerg 
   4736      1.1  joerg   Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, CK,
   4737      1.1  joerg                                                    BridgeKeywordLoc,
   4738      1.1  joerg                                                    TSInfo, SubExpr);
   4739      1.1  joerg 
   4740      1.1  joerg   if (MustConsume) {
   4741      1.1  joerg     Cleanup.setExprNeedsCleanups(true);
   4742      1.1  joerg     Result = ImplicitCastExpr::Create(Context, T, CK_ARCConsumeObject, Result,
   4743  1.1.1.2  joerg                                       nullptr, VK_RValue, FPOptionsOverride());
   4744      1.1  joerg   }
   4745      1.1  joerg 
   4746      1.1  joerg   return Result;
   4747      1.1  joerg }
   4748      1.1  joerg 
   4749      1.1  joerg ExprResult Sema::ActOnObjCBridgedCast(Scope *S,
   4750      1.1  joerg                                       SourceLocation LParenLoc,
   4751      1.1  joerg                                       ObjCBridgeCastKind Kind,
   4752      1.1  joerg                                       SourceLocation BridgeKeywordLoc,
   4753      1.1  joerg                                       ParsedType Type,
   4754      1.1  joerg                                       SourceLocation RParenLoc,
   4755      1.1  joerg                                       Expr *SubExpr) {
   4756      1.1  joerg   TypeSourceInfo *TSInfo = nullptr;
   4757      1.1  joerg   QualType T = GetTypeFromParser(Type, &TSInfo);
   4758      1.1  joerg   if (Kind == OBC_Bridge)
   4759      1.1  joerg     CheckTollFreeBridgeCast(T, SubExpr);
   4760      1.1  joerg   if (!TSInfo)
   4761      1.1  joerg     TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc);
   4762      1.1  joerg   return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo,
   4763      1.1  joerg                               SubExpr);
   4764      1.1  joerg }
   4765