| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1 | //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements semantic analysis for Objective-C expressions. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 13 | #include "clang/Sema/SemaInternal.h" |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/DeclObjC.h" |
| Steve Naroff | 021ca18 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 16 | #include "clang/AST/ExprObjC.h" |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 17 | #include "clang/AST/StmtVisitor.h" |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 18 | #include "clang/AST/TypeLoc.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
| 20 | #include "clang/Edit/Commit.h" |
| 21 | #include "clang/Edit/Rewriters.h" |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 22 | #include "clang/Lex/Preprocessor.h" |
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Initialization.h" |
| 24 | #include "clang/Sema/Lookup.h" |
| 25 | #include "clang/Sema/Scope.h" |
| 26 | #include "clang/Sema/ScopeInfo.h" |
| 27 | #include "llvm/ADT/SmallString.h" |
| Akira Hatanaka | 1488ee4 | 2019-03-08 04:45:37 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ConvertUTF.h" |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 29 | |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 30 | using namespace clang; |
| John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 31 | using namespace sema; |
| Argyrios Kyrtzidis | 59ad1e3 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 32 | using llvm::makeArrayRef; |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 33 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 34 | ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
| Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 35 | ArrayRef<Expr *> Strings) { |
| Chris Lattner | d7670d9 | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 36 | // Most ObjC strings are formed out of a single piece. However, we *can* |
| 37 | // have strings formed out of multiple @ strings with multiple pptokens in |
| 38 | // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one |
| 39 | // StringLiteral for ObjCStringLiteral to hold onto. |
| Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 40 | StringLiteral *S = cast<StringLiteral>(Strings[0]); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 41 | |
| Chris Lattner | d7670d9 | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 42 | // If we have a multi-part string, merge it all together. |
| Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 43 | if (Strings.size() != 1) { |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 44 | // Concatenate objc strings. |
| Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 45 | SmallString<128> StrBuf; |
| Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 46 | SmallVector<SourceLocation, 8> StrLocs; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
| Craig Topper | 883dd33 | 2015-12-24 23:58:11 +0000 | [diff] [blame] | 48 | for (Expr *E : Strings) { |
| 49 | S = cast<StringLiteral>(E); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
| Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 51 | // ObjC strings can't be wide or UTF. |
| 52 | if (!S->isAscii()) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 53 | Diag(S->getBeginLoc(), diag::err_cfstring_literal_not_string_constant) |
| 54 | << S->getSourceRange(); |
| Chris Lattner | d7670d9 | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 55 | return true; |
| 56 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
| Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 58 | // Append the string. |
| 59 | StrBuf += S->getString(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
| Chris Lattner | 163ffd2 | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 61 | // Get the locations of the string tokens. |
| 62 | StrLocs.append(S->tokloc_begin(), S->tokloc_end()); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 63 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
| Chris Lattner | 163ffd2 | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 65 | // Create the aggregate string with the appropriate content and location |
| 66 | // information. |
| Benjamin Kramer | cdac761 | 2014-02-25 12:26:20 +0000 | [diff] [blame] | 67 | const ConstantArrayType *CAT = Context.getAsConstantArrayType(S->getType()); |
| 68 | assert(CAT && "String literal not of constant array type!"); |
| 69 | QualType StrTy = Context.getConstantArrayType( |
| 70 | CAT->getElementType(), llvm::APInt(32, StrBuf.size() + 1), |
| 71 | CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers()); |
| 72 | S = StringLiteral::Create(Context, StrBuf, StringLiteral::Ascii, |
| 73 | /*Pascal=*/false, StrTy, &StrLocs[0], |
| 74 | StrLocs.size()); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 75 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 76 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 77 | return BuildObjCStringLiteral(AtLocs[0], S); |
| 78 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 80 | ExprResult Sema::BuildObjCStringLiteral(SourceLocation AtLoc, StringLiteral *S){ |
| Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 81 | // Verify that this composite string is acceptable for ObjC strings. |
| 82 | if (CheckObjCString(S)) |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 83 | return true; |
| Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 84 | |
| 85 | // Initialize the constant string interface lazily. This assumes |
| Steve Naroff | 54e5945 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 86 | // the NSString interface is seen in this translation unit. Note: We |
| 87 | // don't use NSConstantString, since the runtime team considers this |
| 88 | // interface private (even though it appears in the header files). |
| Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 89 | QualType Ty = Context.getObjCConstantStringInterface(); |
| 90 | if (!Ty.isNull()) { |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 91 | Ty = Context.getObjCObjectPointerType(Ty); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 92 | } else if (getLangOpts().NoConstantCFStrings) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 93 | IdentifierInfo *NSIdent=nullptr; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 94 | std::string StringClass(getLangOpts().ObjCConstantStringClass); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 95 | |
| Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 96 | if (StringClass.empty()) |
| 97 | NSIdent = &Context.Idents.get("NSConstantString"); |
| 98 | else |
| 99 | NSIdent = &Context.Idents.get(StringClass); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 100 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 101 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc, |
| Fariborz Jahanian | 0731763 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 102 | LookupOrdinaryName); |
| 103 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 104 | Context.setObjCConstantStringInterface(StrIF); |
| 105 | Ty = Context.getObjCConstantStringInterface(); |
| 106 | Ty = Context.getObjCObjectPointerType(Ty); |
| 107 | } else { |
| 108 | // If there is no NSConstantString interface defined then treat this |
| 109 | // as error and recover from it. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 110 | Diag(S->getBeginLoc(), diag::err_no_nsconstant_string_class) |
| 111 | << NSIdent << S->getSourceRange(); |
| Fariborz Jahanian | 0731763 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 112 | Ty = Context.getObjCIdType(); |
| 113 | } |
| Chris Lattner | 091f698 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 114 | } else { |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 115 | IdentifierInfo *NSIdent = NSAPIObj->getNSClassId(NSAPI::ClassId_NSString); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 116 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc, |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 117 | LookupOrdinaryName); |
| Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 118 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 119 | Context.setObjCConstantStringInterface(StrIF); |
| 120 | Ty = Context.getObjCConstantStringInterface(); |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 121 | Ty = Context.getObjCObjectPointerType(Ty); |
| Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 122 | } else { |
| Fariborz Jahanian | 86f8266 | 2012-02-23 22:51:36 +0000 | [diff] [blame] | 123 | // If there is no NSString interface defined, implicitly declare |
| 124 | // a @class NSString; and use that instead. This is to make sure |
| 125 | // type of an NSString literal is represented correctly, instead of |
| 126 | // being an 'id' type. |
| 127 | Ty = Context.getObjCNSStringType(); |
| 128 | if (Ty.isNull()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 129 | ObjCInterfaceDecl *NSStringIDecl = |
| 130 | ObjCInterfaceDecl::Create (Context, |
| 131 | Context.getTranslationUnitDecl(), |
| 132 | SourceLocation(), NSIdent, |
| Douglas Gregor | 85f3f95 | 2015-07-07 03:57:15 +0000 | [diff] [blame] | 133 | nullptr, nullptr, SourceLocation()); |
| Fariborz Jahanian | 86f8266 | 2012-02-23 22:51:36 +0000 | [diff] [blame] | 134 | Ty = Context.getObjCInterfaceType(NSStringIDecl); |
| 135 | Context.setObjCNSStringType(Ty); |
| 136 | } |
| 137 | Ty = Context.getObjCObjectPointerType(Ty); |
| Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 138 | } |
| Chris Lattner | 091f698 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 139 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 141 | return new (Context) ObjCStringLiteral(S, Ty, AtLoc); |
| 142 | } |
| 143 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 144 | /// Emits an error if the given method does not exist, or if the return |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 145 | /// type is not an Objective-C object. |
| 146 | static bool validateBoxingMethod(Sema &S, SourceLocation Loc, |
| 147 | const ObjCInterfaceDecl *Class, |
| 148 | Selector Sel, const ObjCMethodDecl *Method) { |
| 149 | if (!Method) { |
| 150 | // FIXME: Is there a better way to avoid quotes than using getName()? |
| 151 | S.Diag(Loc, diag::err_undeclared_boxing_method) << Sel << Class->getName(); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | // Make sure the return type is reasonable. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 156 | QualType ReturnType = Method->getReturnType(); |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 157 | if (!ReturnType->isObjCObjectPointerType()) { |
| 158 | S.Diag(Loc, diag::err_objc_literal_method_sig) |
| 159 | << Sel; |
| 160 | S.Diag(Method->getLocation(), diag::note_objc_literal_method_return) |
| 161 | << ReturnType; |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | return true; |
| 166 | } |
| 167 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 168 | /// Maps ObjCLiteralKind to NSClassIdKindKind |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 169 | static NSAPI::NSClassIdKindKind ClassKindFromLiteralKind( |
| 170 | Sema::ObjCLiteralKind LiteralKind) { |
| 171 | switch (LiteralKind) { |
| 172 | case Sema::LK_Array: |
| 173 | return NSAPI::ClassId_NSArray; |
| 174 | case Sema::LK_Dictionary: |
| 175 | return NSAPI::ClassId_NSDictionary; |
| 176 | case Sema::LK_Numeric: |
| 177 | return NSAPI::ClassId_NSNumber; |
| 178 | case Sema::LK_String: |
| 179 | return NSAPI::ClassId_NSString; |
| 180 | case Sema::LK_Boxed: |
| 181 | return NSAPI::ClassId_NSValue; |
| 182 | |
| 183 | // there is no corresponding matching |
| 184 | // between LK_None/LK_Block and NSClassIdKindKind |
| 185 | case Sema::LK_Block: |
| 186 | case Sema::LK_None: |
| Aaron Ballman | 3e839de | 2015-07-24 12:47:27 +0000 | [diff] [blame] | 187 | break; |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 188 | } |
| Aaron Ballman | 3e839de | 2015-07-24 12:47:27 +0000 | [diff] [blame] | 189 | llvm_unreachable("LiteralKind can't be converted into a ClassKind"); |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 192 | /// Validates ObjCInterfaceDecl availability. |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 193 | /// ObjCInterfaceDecl, used to create ObjC literals, should be defined |
| 194 | /// if clang not in a debugger mode. |
| 195 | static bool ValidateObjCLiteralInterfaceDecl(Sema &S, ObjCInterfaceDecl *Decl, |
| 196 | SourceLocation Loc, |
| 197 | Sema::ObjCLiteralKind LiteralKind) { |
| 198 | if (!Decl) { |
| 199 | NSAPI::NSClassIdKindKind Kind = ClassKindFromLiteralKind(LiteralKind); |
| 200 | IdentifierInfo *II = S.NSAPIObj->getNSClassId(Kind); |
| 201 | S.Diag(Loc, diag::err_undeclared_objc_literal_class) |
| 202 | << II->getName() << LiteralKind; |
| 203 | return false; |
| 204 | } else if (!Decl->hasDefinition() && !S.getLangOpts().DebuggerObjCLiteral) { |
| 205 | S.Diag(Loc, diag::err_undeclared_objc_literal_class) |
| 206 | << Decl->getName() << LiteralKind; |
| 207 | S.Diag(Decl->getLocation(), diag::note_forward_class); |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | return true; |
| 212 | } |
| 213 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 214 | /// Looks up ObjCInterfaceDecl of a given NSClassIdKindKind. |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 215 | /// Used to create ObjC literals, such as NSDictionary (@{}), |
| 216 | /// NSArray (@[]) and Boxed Expressions (@()) |
| 217 | static ObjCInterfaceDecl *LookupObjCInterfaceDeclForLiteral(Sema &S, |
| 218 | SourceLocation Loc, |
| 219 | Sema::ObjCLiteralKind LiteralKind) { |
| 220 | NSAPI::NSClassIdKindKind ClassKind = ClassKindFromLiteralKind(LiteralKind); |
| 221 | IdentifierInfo *II = S.NSAPIObj->getNSClassId(ClassKind); |
| 222 | NamedDecl *IF = S.LookupSingleName(S.TUScope, II, Loc, |
| 223 | Sema::LookupOrdinaryName); |
| 224 | ObjCInterfaceDecl *ID = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 225 | if (!ID && S.getLangOpts().DebuggerObjCLiteral) { |
| 226 | ASTContext &Context = S.Context; |
| 227 | TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); |
| 228 | ID = ObjCInterfaceDecl::Create (Context, TU, SourceLocation(), II, |
| 229 | nullptr, nullptr, SourceLocation()); |
| 230 | } |
| 231 | |
| 232 | if (!ValidateObjCLiteralInterfaceDecl(S, ID, Loc, LiteralKind)) { |
| 233 | ID = nullptr; |
| 234 | } |
| 235 | |
| 236 | return ID; |
| 237 | } |
| 238 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 239 | /// Retrieve the NSNumber factory method that should be used to create |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 240 | /// an Objective-C literal for the given type. |
| 241 | static ObjCMethodDecl *getNSNumberFactoryMethod(Sema &S, SourceLocation Loc, |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 242 | QualType NumberType, |
| 243 | bool isLiteral = false, |
| 244 | SourceRange R = SourceRange()) { |
| David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 245 | Optional<NSAPI::NSNumberLiteralMethodKind> Kind = |
| 246 | S.NSAPIObj->getNSNumberFactoryMethodKind(NumberType); |
| 247 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 248 | if (!Kind) { |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 249 | if (isLiteral) { |
| 250 | S.Diag(Loc, diag::err_invalid_nsnumber_type) |
| 251 | << NumberType << R; |
| 252 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 253 | return nullptr; |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 254 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 255 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 256 | // If we already looked up this method, we're done. |
| 257 | if (S.NSNumberLiteralMethods[*Kind]) |
| 258 | return S.NSNumberLiteralMethods[*Kind]; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 259 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 260 | Selector Sel = S.NSAPIObj->getNSNumberLiteralSelector(*Kind, |
| 261 | /*Instance=*/false); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 262 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 263 | ASTContext &CX = S.Context; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 264 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 265 | // Look up the NSNumber class, if we haven't done so already. It's cached |
| 266 | // in the Sema instance. |
| 267 | if (!S.NSNumberDecl) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 268 | S.NSNumberDecl = LookupObjCInterfaceDeclForLiteral(S, Loc, |
| 269 | Sema::LK_Numeric); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 270 | if (!S.NSNumberDecl) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 271 | return nullptr; |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 272 | } |
| Alex Denisov | e36748a | 2015-02-16 16:17:05 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | if (S.NSNumberPointer.isNull()) { |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 276 | // generate the pointer to NSNumber type. |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 277 | QualType NSNumberObject = CX.getObjCInterfaceType(S.NSNumberDecl); |
| 278 | S.NSNumberPointer = CX.getObjCObjectPointerType(NSNumberObject); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 279 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 280 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 281 | // Look for the appropriate method within NSNumber. |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 282 | ObjCMethodDecl *Method = S.NSNumberDecl->lookupClassMethod(Sel); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 283 | if (!Method && S.getLangOpts().DebuggerObjCLiteral) { |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 284 | // create a stub definition this NSNumber factory method. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 285 | TypeSourceInfo *ReturnTInfo = nullptr; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 286 | Method = |
| 287 | ObjCMethodDecl::Create(CX, SourceLocation(), SourceLocation(), Sel, |
| 288 | S.NSNumberPointer, ReturnTInfo, S.NSNumberDecl, |
| 289 | /*isInstance=*/false, /*isVariadic=*/false, |
| 290 | /*isPropertyAccessor=*/false, |
| 291 | /*isImplicitlyDeclared=*/true, |
| 292 | /*isDefined=*/false, ObjCMethodDecl::Required, |
| 293 | /*HasRelatedResultType=*/false); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 294 | ParmVarDecl *value = ParmVarDecl::Create(S.Context, Method, |
| 295 | SourceLocation(), SourceLocation(), |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 296 | &CX.Idents.get("value"), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 297 | NumberType, /*TInfo=*/nullptr, |
| 298 | SC_None, nullptr); |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 299 | Method->setMethodParams(S.Context, value, None); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 302 | if (!validateBoxingMethod(S, Loc, S.NSNumberDecl, Sel, Method)) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 303 | return nullptr; |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 304 | |
| 305 | // Note: if the parameter type is out-of-line, we'll catch it later in the |
| 306 | // implicit conversion. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 307 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 308 | S.NSNumberLiteralMethods[*Kind] = Method; |
| 309 | return Method; |
| 310 | } |
| 311 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 312 | /// BuildObjCNumericLiteral - builds an ObjCBoxedExpr AST node for the |
| 313 | /// numeric literal expression. Type of the expression will be "NSNumber *". |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 314 | ExprResult Sema::BuildObjCNumericLiteral(SourceLocation AtLoc, Expr *Number) { |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 315 | // Determine the type of the literal. |
| 316 | QualType NumberType = Number->getType(); |
| 317 | if (CharacterLiteral *Char = dyn_cast<CharacterLiteral>(Number)) { |
| 318 | // In C, character literals have type 'int'. That's not the type we want |
| 319 | // to use to determine the Objective-c literal kind. |
| 320 | switch (Char->getKind()) { |
| 321 | case CharacterLiteral::Ascii: |
| Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 322 | case CharacterLiteral::UTF8: |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 323 | NumberType = Context.CharTy; |
| 324 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 325 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 326 | case CharacterLiteral::Wide: |
| Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 327 | NumberType = Context.getWideCharType(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 328 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 329 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 330 | case CharacterLiteral::UTF16: |
| 331 | NumberType = Context.Char16Ty; |
| 332 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 333 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 334 | case CharacterLiteral::UTF32: |
| 335 | NumberType = Context.Char32Ty; |
| 336 | break; |
| 337 | } |
| 338 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 339 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 340 | // Look for the appropriate method within NSNumber. |
| 341 | // Construct the literal. |
| Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 342 | SourceRange NR(Number->getSourceRange()); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 343 | ObjCMethodDecl *Method = getNSNumberFactoryMethod(*this, AtLoc, NumberType, |
| Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 344 | true, NR); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 345 | if (!Method) |
| 346 | return ExprError(); |
| 347 | |
| 348 | // Convert the number to the type that the parameter expects. |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 349 | ParmVarDecl *ParamDecl = Method->parameters()[0]; |
| Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 350 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 351 | ParamDecl); |
| 352 | ExprResult ConvertedNumber = PerformCopyInitialization(Entity, |
| 353 | SourceLocation(), |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 354 | Number); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 355 | if (ConvertedNumber.isInvalid()) |
| 356 | return ExprError(); |
| 357 | Number = ConvertedNumber.get(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 358 | |
| Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 359 | // Use the effective source range of the literal, including the leading '@'. |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 360 | return MaybeBindToTemporary( |
| Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 361 | new (Context) ObjCBoxedExpr(Number, NSNumberPointer, Method, |
| 362 | SourceRange(AtLoc, NR.getEnd()))); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 365 | ExprResult Sema::ActOnObjCBoolLiteral(SourceLocation AtLoc, |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 366 | SourceLocation ValueLoc, |
| 367 | bool Value) { |
| 368 | ExprResult Inner; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 369 | if (getLangOpts().CPlusPlus) { |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 370 | Inner = ActOnCXXBoolLiteral(ValueLoc, Value? tok::kw_true : tok::kw_false); |
| 371 | } else { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 372 | // C doesn't actually have a way to represent literal values of type |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 373 | // _Bool. So, we'll use 0/1 and implicit cast to _Bool. |
| 374 | Inner = ActOnIntegerConstant(ValueLoc, Value? 1 : 0); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 375 | Inner = ImpCastExprToType(Inner.get(), Context.BoolTy, |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 376 | CK_IntegralToBoolean); |
| 377 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 378 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 379 | return BuildObjCNumericLiteral(AtLoc, Inner.get()); |
| 380 | } |
| 381 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 382 | /// Check that the given expression is a valid element of an Objective-C |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 383 | /// collection literal. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 384 | static ExprResult CheckObjCCollectionLiteralElement(Sema &S, Expr *Element, |
| Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 385 | QualType T, |
| 386 | bool ArrayLiteral = false) { |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 387 | // If the expression is type-dependent, there's nothing for us to do. |
| 388 | if (Element->isTypeDependent()) |
| 389 | return Element; |
| 390 | |
| 391 | ExprResult Result = S.CheckPlaceholderExpr(Element); |
| 392 | if (Result.isInvalid()) |
| 393 | return ExprError(); |
| 394 | Element = Result.get(); |
| 395 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 396 | // In C++, check for an implicit conversion to an Objective-C object pointer |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 397 | // type. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 398 | if (S.getLangOpts().CPlusPlus && Element->getType()->isRecordType()) { |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 399 | InitializedEntity Entity |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 400 | = InitializedEntity::InitializeParameter(S.Context, T, |
| 401 | /*Consumed=*/false); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 402 | InitializationKind Kind = InitializationKind::CreateCopy( |
| 403 | Element->getBeginLoc(), SourceLocation()); |
| Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 404 | InitializationSequence Seq(S, Entity, Kind, Element); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 405 | if (!Seq.Failed()) |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 406 | return Seq.Perform(S, Entity, Kind, Element); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | Expr *OrigElement = Element; |
| 410 | |
| 411 | // Perform lvalue-to-rvalue conversion. |
| 412 | Result = S.DefaultLvalueConversion(Element); |
| 413 | if (Result.isInvalid()) |
| 414 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 415 | Element = Result.get(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 416 | |
| 417 | // Make sure that we have an Objective-C pointer type or block. |
| 418 | if (!Element->getType()->isObjCObjectPointerType() && |
| 419 | !Element->getType()->isBlockPointerType()) { |
| 420 | bool Recovered = false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 421 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 422 | // If this is potentially an Objective-C numeric literal, add the '@'. |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 423 | if (isa<IntegerLiteral>(OrigElement) || |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 424 | isa<CharacterLiteral>(OrigElement) || |
| 425 | isa<FloatingLiteral>(OrigElement) || |
| 426 | isa<ObjCBoolLiteralExpr>(OrigElement) || |
| 427 | isa<CXXBoolLiteralExpr>(OrigElement)) { |
| 428 | if (S.NSAPIObj->getNSNumberFactoryMethodKind(OrigElement->getType())) { |
| 429 | int Which = isa<CharacterLiteral>(OrigElement) ? 1 |
| 430 | : (isa<CXXBoolLiteralExpr>(OrigElement) || |
| 431 | isa<ObjCBoolLiteralExpr>(OrigElement)) ? 2 |
| 432 | : 3; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 433 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 434 | S.Diag(OrigElement->getBeginLoc(), diag::err_box_literal_collection) |
| 435 | << Which << OrigElement->getSourceRange() |
| 436 | << FixItHint::CreateInsertion(OrigElement->getBeginLoc(), "@"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 437 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 438 | Result = |
| 439 | S.BuildObjCNumericLiteral(OrigElement->getBeginLoc(), OrigElement); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 440 | if (Result.isInvalid()) |
| 441 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 442 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 443 | Element = Result.get(); |
| 444 | Recovered = true; |
| 445 | } |
| 446 | } |
| 447 | // If this is potentially an Objective-C string literal, add the '@'. |
| 448 | else if (StringLiteral *String = dyn_cast<StringLiteral>(OrigElement)) { |
| 449 | if (String->isAscii()) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 450 | S.Diag(OrigElement->getBeginLoc(), diag::err_box_literal_collection) |
| 451 | << 0 << OrigElement->getSourceRange() |
| 452 | << FixItHint::CreateInsertion(OrigElement->getBeginLoc(), "@"); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 453 | |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 454 | Result = S.BuildObjCStringLiteral(OrigElement->getBeginLoc(), String); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 455 | if (Result.isInvalid()) |
| 456 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 457 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 458 | Element = Result.get(); |
| 459 | Recovered = true; |
| 460 | } |
| 461 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 462 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 463 | if (!Recovered) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 464 | S.Diag(Element->getBeginLoc(), diag::err_invalid_collection_element) |
| 465 | << Element->getType(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 466 | return ExprError(); |
| 467 | } |
| 468 | } |
| Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 469 | if (ArrayLiteral) |
| Ted Kremenek | 197fee4 | 2013-10-09 22:34:33 +0000 | [diff] [blame] | 470 | if (ObjCStringLiteral *getString = |
| 471 | dyn_cast<ObjCStringLiteral>(OrigElement)) { |
| 472 | if (StringLiteral *SL = getString->getString()) { |
| 473 | unsigned numConcat = SL->getNumConcatenated(); |
| 474 | if (numConcat > 1) { |
| 475 | // Only warn if the concatenated string doesn't come from a macro. |
| 476 | bool hasMacro = false; |
| 477 | for (unsigned i = 0; i < numConcat ; ++i) |
| 478 | if (SL->getStrTokenLoc(i).isMacroID()) { |
| 479 | hasMacro = true; |
| 480 | break; |
| 481 | } |
| 482 | if (!hasMacro) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 483 | S.Diag(Element->getBeginLoc(), |
| Ted Kremenek | 197fee4 | 2013-10-09 22:34:33 +0000 | [diff] [blame] | 484 | diag::warn_concatenated_nsarray_literal) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 485 | << Element->getType(); |
| Ted Kremenek | 197fee4 | 2013-10-09 22:34:33 +0000 | [diff] [blame] | 486 | } |
| 487 | } |
| Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 490 | // Make sure that the element has the type that the container factory |
| 491 | // function expects. |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 492 | return S.PerformCopyInitialization( |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 493 | InitializedEntity::InitializeParameter(S.Context, T, |
| 494 | /*Consumed=*/false), |
| 495 | Element->getBeginLoc(), Element); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 498 | ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) { |
| 499 | if (ValueExpr->isTypeDependent()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 500 | ObjCBoxedExpr *BoxedExpr = |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 501 | new (Context) ObjCBoxedExpr(ValueExpr, Context.DependentTy, nullptr, SR); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 502 | return BoxedExpr; |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 503 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 504 | ObjCMethodDecl *BoxingMethod = nullptr; |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 505 | QualType BoxedType; |
| 506 | // Convert the expression to an RValue, so we can check for pointer types... |
| 507 | ExprResult RValue = DefaultFunctionArrayLvalueConversion(ValueExpr); |
| 508 | if (RValue.isInvalid()) { |
| 509 | return ExprError(); |
| 510 | } |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 511 | SourceLocation Loc = SR.getBegin(); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 512 | ValueExpr = RValue.get(); |
| Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 513 | QualType ValueType(ValueExpr->getType()); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 514 | if (const PointerType *PT = ValueType->getAs<PointerType>()) { |
| 515 | QualType PointeeType = PT->getPointeeType(); |
| 516 | if (Context.hasSameUnqualifiedType(PointeeType, Context.CharTy)) { |
| 517 | |
| 518 | if (!NSStringDecl) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 519 | NSStringDecl = LookupObjCInterfaceDeclForLiteral(*this, Loc, |
| 520 | Sema::LK_String); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 521 | if (!NSStringDecl) { |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 522 | return ExprError(); |
| 523 | } |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 524 | QualType NSStringObject = Context.getObjCInterfaceType(NSStringDecl); |
| 525 | NSStringPointer = Context.getObjCObjectPointerType(NSStringObject); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 526 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 527 | |
| Akira Hatanaka | 1488ee4 | 2019-03-08 04:45:37 +0000 | [diff] [blame] | 528 | // The boxed expression can be emitted as a compile time constant if it is |
| 529 | // a string literal whose character encoding is compatible with UTF-8. |
| 530 | if (auto *CE = dyn_cast<ImplicitCastExpr>(ValueExpr)) |
| 531 | if (CE->getCastKind() == CK_ArrayToPointerDecay) |
| 532 | if (auto *SL = |
| 533 | dyn_cast<StringLiteral>(CE->getSubExpr()->IgnoreParens())) { |
| 534 | assert((SL->isAscii() || SL->isUTF8()) && |
| 535 | "unexpected character encoding"); |
| 536 | StringRef Str = SL->getString(); |
| 537 | const llvm::UTF8 *StrBegin = Str.bytes_begin(); |
| 538 | const llvm::UTF8 *StrEnd = Str.bytes_end(); |
| 539 | // Check that this is a valid UTF-8 string. |
| 540 | if (llvm::isLegalUTF8String(&StrBegin, StrEnd)) { |
| 541 | BoxedType = Context.getAttributedType( |
| 542 | AttributedType::getNullabilityAttrKind( |
| 543 | NullabilityKind::NonNull), |
| 544 | NSStringPointer, NSStringPointer); |
| 545 | return new (Context) ObjCBoxedExpr(CE, BoxedType, nullptr, SR); |
| 546 | } |
| 547 | |
| 548 | Diag(SL->getBeginLoc(), diag::warn_objc_boxing_invalid_utf8_string) |
| 549 | << NSStringPointer << SL->getSourceRange(); |
| 550 | } |
| 551 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 552 | if (!StringWithUTF8StringMethod) { |
| 553 | IdentifierInfo *II = &Context.Idents.get("stringWithUTF8String"); |
| 554 | Selector stringWithUTF8String = Context.Selectors.getUnarySelector(II); |
| 555 | |
| 556 | // Look for the appropriate method within NSString. |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 557 | BoxingMethod = NSStringDecl->lookupClassMethod(stringWithUTF8String); |
| 558 | if (!BoxingMethod && getLangOpts().DebuggerObjCLiteral) { |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 559 | // Debugger needs to work even if NSString hasn't been defined. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 560 | TypeSourceInfo *ReturnTInfo = nullptr; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 561 | ObjCMethodDecl *M = ObjCMethodDecl::Create( |
| 562 | Context, SourceLocation(), SourceLocation(), stringWithUTF8String, |
| 563 | NSStringPointer, ReturnTInfo, NSStringDecl, |
| 564 | /*isInstance=*/false, /*isVariadic=*/false, |
| 565 | /*isPropertyAccessor=*/false, |
| 566 | /*isImplicitlyDeclared=*/true, |
| 567 | /*isDefined=*/false, ObjCMethodDecl::Required, |
| 568 | /*HasRelatedResultType=*/false); |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 569 | QualType ConstCharType = Context.CharTy.withConst(); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 570 | ParmVarDecl *value = |
| 571 | ParmVarDecl::Create(Context, M, |
| 572 | SourceLocation(), SourceLocation(), |
| 573 | &Context.Idents.get("value"), |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 574 | Context.getPointerType(ConstCharType), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 575 | /*TInfo=*/nullptr, |
| 576 | SC_None, nullptr); |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 577 | M->setMethodParams(Context, value, None); |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 578 | BoxingMethod = M; |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 579 | } |
| Jordy Rose | 890f457 | 2012-05-12 15:53:41 +0000 | [diff] [blame] | 580 | |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 581 | if (!validateBoxingMethod(*this, Loc, NSStringDecl, |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 582 | stringWithUTF8String, BoxingMethod)) |
| 583 | return ExprError(); |
| 584 | |
| 585 | StringWithUTF8StringMethod = BoxingMethod; |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 586 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 587 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 588 | BoxingMethod = StringWithUTF8StringMethod; |
| 589 | BoxedType = NSStringPointer; |
| Alex Lorenz | 49370ac | 2017-11-08 21:33:15 +0000 | [diff] [blame] | 590 | // Transfer the nullability from method's return type. |
| 591 | Optional<NullabilityKind> Nullability = |
| 592 | BoxingMethod->getReturnType()->getNullability(Context); |
| 593 | if (Nullability) |
| 594 | BoxedType = Context.getAttributedType( |
| 595 | AttributedType::getNullabilityAttrKind(*Nullability), BoxedType, |
| 596 | BoxedType); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 597 | } |
| Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 598 | } else if (ValueType->isBuiltinType()) { |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 599 | // The other types we support are numeric, char and BOOL/bool. We could also |
| 600 | // provide limited support for structure types, such as NSRange, NSRect, and |
| 601 | // NSSize. See NSValue (NSValueGeometryExtensions) in <Foundation/NSGeometry.h> |
| 602 | // for more details. |
| 603 | |
| 604 | // Check for a top-level character literal. |
| 605 | if (const CharacterLiteral *Char = |
| 606 | dyn_cast<CharacterLiteral>(ValueExpr->IgnoreParens())) { |
| 607 | // In C, character literals have type 'int'. That's not the type we want |
| 608 | // to use to determine the Objective-c literal kind. |
| 609 | switch (Char->getKind()) { |
| 610 | case CharacterLiteral::Ascii: |
| Aaron Ballman | 9a17c85 | 2016-01-07 20:59:26 +0000 | [diff] [blame] | 611 | case CharacterLiteral::UTF8: |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 612 | ValueType = Context.CharTy; |
| 613 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 614 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 615 | case CharacterLiteral::Wide: |
| Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 616 | ValueType = Context.getWideCharType(); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 617 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 618 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 619 | case CharacterLiteral::UTF16: |
| 620 | ValueType = Context.Char16Ty; |
| 621 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 622 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 623 | case CharacterLiteral::UTF32: |
| 624 | ValueType = Context.Char32Ty; |
| 625 | break; |
| 626 | } |
| 627 | } |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 628 | // FIXME: Do I need to do anything special with BoolTy expressions? |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 629 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 630 | // Look for the appropriate method within NSNumber. |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 631 | BoxingMethod = getNSNumberFactoryMethod(*this, Loc, ValueType); |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 632 | BoxedType = NSNumberPointer; |
| Argyrios Kyrtzidis | 8e6951d | 2012-05-15 19:17:44 +0000 | [diff] [blame] | 633 | } else if (const EnumType *ET = ValueType->getAs<EnumType>()) { |
| 634 | if (!ET->getDecl()->isComplete()) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 635 | Diag(Loc, diag::err_objc_incomplete_boxed_expression_type) |
| Argyrios Kyrtzidis | 8e6951d | 2012-05-15 19:17:44 +0000 | [diff] [blame] | 636 | << ValueType << ValueExpr->getSourceRange(); |
| 637 | return ExprError(); |
| 638 | } |
| 639 | |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 640 | BoxingMethod = getNSNumberFactoryMethod(*this, Loc, |
| Argyrios Kyrtzidis | 8e6951d | 2012-05-15 19:17:44 +0000 | [diff] [blame] | 641 | ET->getDecl()->getIntegerType()); |
| 642 | BoxedType = NSNumberPointer; |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 643 | } else if (ValueType->isObjCBoxableRecordType()) { |
| 644 | // Support for structure types, that marked as objc_boxable |
| 645 | // struct __attribute__((objc_boxable)) s { ... }; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 646 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 647 | // Look up the NSValue class, if we haven't done so already. It's cached |
| 648 | // in the Sema instance. |
| 649 | if (!NSValueDecl) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 650 | NSValueDecl = LookupObjCInterfaceDeclForLiteral(*this, Loc, |
| 651 | Sema::LK_Boxed); |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 652 | if (!NSValueDecl) { |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 653 | return ExprError(); |
| 654 | } |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 655 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 656 | // generate the pointer to NSValue type. |
| 657 | QualType NSValueObject = Context.getObjCInterfaceType(NSValueDecl); |
| 658 | NSValuePointer = Context.getObjCObjectPointerType(NSValueObject); |
| 659 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 660 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 661 | if (!ValueWithBytesObjCTypeMethod) { |
| 662 | IdentifierInfo *II[] = { |
| 663 | &Context.Idents.get("valueWithBytes"), |
| 664 | &Context.Idents.get("objCType") |
| 665 | }; |
| 666 | Selector ValueWithBytesObjCType = Context.Selectors.getSelector(2, II); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 667 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 668 | // Look for the appropriate method within NSValue. |
| 669 | BoxingMethod = NSValueDecl->lookupClassMethod(ValueWithBytesObjCType); |
| 670 | if (!BoxingMethod && getLangOpts().DebuggerObjCLiteral) { |
| 671 | // Debugger needs to work even if NSValue hasn't been defined. |
| 672 | TypeSourceInfo *ReturnTInfo = nullptr; |
| 673 | ObjCMethodDecl *M = ObjCMethodDecl::Create( |
| 674 | Context, |
| 675 | SourceLocation(), |
| 676 | SourceLocation(), |
| 677 | ValueWithBytesObjCType, |
| 678 | NSValuePointer, |
| 679 | ReturnTInfo, |
| 680 | NSValueDecl, |
| 681 | /*isInstance=*/false, |
| 682 | /*isVariadic=*/false, |
| 683 | /*isPropertyAccessor=*/false, |
| 684 | /*isImplicitlyDeclared=*/true, |
| 685 | /*isDefined=*/false, |
| 686 | ObjCMethodDecl::Required, |
| 687 | /*HasRelatedResultType=*/false); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 688 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 689 | SmallVector<ParmVarDecl *, 2> Params; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 690 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 691 | ParmVarDecl *bytes = |
| 692 | ParmVarDecl::Create(Context, M, |
| 693 | SourceLocation(), SourceLocation(), |
| 694 | &Context.Idents.get("bytes"), |
| 695 | Context.VoidPtrTy.withConst(), |
| 696 | /*TInfo=*/nullptr, |
| 697 | SC_None, nullptr); |
| 698 | Params.push_back(bytes); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 699 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 700 | QualType ConstCharType = Context.CharTy.withConst(); |
| 701 | ParmVarDecl *type = |
| 702 | ParmVarDecl::Create(Context, M, |
| 703 | SourceLocation(), SourceLocation(), |
| 704 | &Context.Idents.get("type"), |
| 705 | Context.getPointerType(ConstCharType), |
| 706 | /*TInfo=*/nullptr, |
| 707 | SC_None, nullptr); |
| 708 | Params.push_back(type); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 709 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 710 | M->setMethodParams(Context, Params, None); |
| 711 | BoxingMethod = M; |
| 712 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 713 | |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 714 | if (!validateBoxingMethod(*this, Loc, NSValueDecl, |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 715 | ValueWithBytesObjCType, BoxingMethod)) |
| 716 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 717 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 718 | ValueWithBytesObjCTypeMethod = BoxingMethod; |
| 719 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 720 | |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 721 | if (!ValueType.isTriviallyCopyableType(Context)) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 722 | Diag(Loc, diag::err_objc_non_trivially_copyable_boxed_expression_type) |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 723 | << ValueType << ValueExpr->getSourceRange(); |
| 724 | return ExprError(); |
| 725 | } |
| 726 | |
| 727 | BoxingMethod = ValueWithBytesObjCTypeMethod; |
| 728 | BoxedType = NSValuePointer; |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | if (!BoxingMethod) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 732 | Diag(Loc, diag::err_objc_illegal_boxed_expression_type) |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 733 | << ValueType << ValueExpr->getSourceRange(); |
| 734 | return ExprError(); |
| 735 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 736 | |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 737 | DiagnoseUseOfDecl(BoxingMethod, Loc); |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 738 | |
| 739 | ExprResult ConvertedValueExpr; |
| 740 | if (ValueType->isObjCBoxableRecordType()) { |
| 741 | InitializedEntity IE = InitializedEntity::InitializeTemporary(ValueType); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 742 | ConvertedValueExpr = PerformCopyInitialization(IE, ValueExpr->getExprLoc(), |
| Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 743 | ValueExpr); |
| 744 | } else { |
| 745 | // Convert the expression to the type that the parameter requires. |
| 746 | ParmVarDecl *ParamDecl = BoxingMethod->parameters()[0]; |
| 747 | InitializedEntity IE = InitializedEntity::InitializeParameter(Context, |
| 748 | ParamDecl); |
| 749 | ConvertedValueExpr = PerformCopyInitialization(IE, SourceLocation(), |
| 750 | ValueExpr); |
| 751 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 752 | |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 753 | if (ConvertedValueExpr.isInvalid()) |
| 754 | return ExprError(); |
| 755 | ValueExpr = ConvertedValueExpr.get(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 756 | |
| 757 | ObjCBoxedExpr *BoxedExpr = |
| Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 758 | new (Context) ObjCBoxedExpr(ValueExpr, BoxedType, |
| 759 | BoxingMethod, SR); |
| 760 | return MaybeBindToTemporary(BoxedExpr); |
| 761 | } |
| 762 | |
| John McCall | f253834 | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 763 | /// Build an ObjC subscript pseudo-object expression, given that |
| 764 | /// that's supported by the runtime. |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 765 | ExprResult Sema::BuildObjCSubscriptExpression(SourceLocation RB, Expr *BaseExpr, |
| 766 | Expr *IndexExpr, |
| 767 | ObjCMethodDecl *getterMethod, |
| 768 | ObjCMethodDecl *setterMethod) { |
| Fariborz Jahanian | e1e33f8 | 2013-11-01 21:58:17 +0000 | [diff] [blame] | 769 | assert(!LangOpts.isSubscriptPointerArithmetic()); |
| John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 770 | |
| John McCall | f253834 | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 771 | // We can't get dependent types here; our callers should have |
| 772 | // filtered them out. |
| 773 | assert((!BaseExpr->isTypeDependent() && !IndexExpr->isTypeDependent()) && |
| 774 | "base or index cannot have dependent type here"); |
| 775 | |
| 776 | // Filter out placeholders in the index. In theory, overloads could |
| 777 | // be preserved here, although that might not actually work correctly. |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 778 | ExprResult Result = CheckPlaceholderExpr(IndexExpr); |
| 779 | if (Result.isInvalid()) |
| 780 | return ExprError(); |
| 781 | IndexExpr = Result.get(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 782 | |
| John McCall | f253834 | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 783 | // Perform lvalue-to-rvalue conversion on the base. |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 784 | Result = DefaultLvalueConversion(BaseExpr); |
| 785 | if (Result.isInvalid()) |
| 786 | return ExprError(); |
| 787 | BaseExpr = Result.get(); |
| John McCall | f253834 | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 788 | |
| 789 | // Build the pseudo-object expression. |
| James Y Knight | 6c2f06b | 2015-12-31 04:43:19 +0000 | [diff] [blame] | 790 | return new (Context) ObjCSubscriptRefExpr( |
| 791 | BaseExpr, IndexExpr, Context.PseudoObjectTy, VK_LValue, OK_ObjCSubscript, |
| 792 | getterMethod, setterMethod, RB); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 796 | SourceLocation Loc = SR.getBegin(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 797 | |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 798 | if (!NSArrayDecl) { |
| 799 | NSArrayDecl = LookupObjCInterfaceDeclForLiteral(*this, Loc, |
| 800 | Sema::LK_Array); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 801 | if (!NSArrayDecl) { |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 802 | return ExprError(); |
| 803 | } |
| 804 | } |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 805 | |
| Fariborz Jahanian | 495bc3f | 2014-08-08 17:31:14 +0000 | [diff] [blame] | 806 | // Find the arrayWithObjects:count: method, if we haven't done so already. |
| Fariborz Jahanian | bf09db4 | 2014-08-08 18:29:52 +0000 | [diff] [blame] | 807 | QualType IdT = Context.getObjCIdType(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 808 | if (!ArrayWithObjectsMethod) { |
| 809 | Selector |
| Fariborz Jahanian | 9ad94aa | 2014-10-28 18:28:16 +0000 | [diff] [blame] | 810 | Sel = NSAPIObj->getNSArraySelector(NSAPI::NSArr_arrayWithObjectsCount); |
| 811 | ObjCMethodDecl *Method = NSArrayDecl->lookupClassMethod(Sel); |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 812 | if (!Method && getLangOpts().DebuggerObjCLiteral) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 813 | TypeSourceInfo *ReturnTInfo = nullptr; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 814 | Method = ObjCMethodDecl::Create( |
| 815 | Context, SourceLocation(), SourceLocation(), Sel, IdT, ReturnTInfo, |
| Fariborz Jahanian | 9ad94aa | 2014-10-28 18:28:16 +0000 | [diff] [blame] | 816 | Context.getTranslationUnitDecl(), false /*Instance*/, |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 817 | false /*isVariadic*/, |
| 818 | /*isPropertyAccessor=*/false, |
| 819 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
| 820 | ObjCMethodDecl::Required, false); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 821 | SmallVector<ParmVarDecl *, 2> Params; |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 822 | ParmVarDecl *objects = ParmVarDecl::Create(Context, Method, |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 823 | SourceLocation(), |
| 824 | SourceLocation(), |
| 825 | &Context.Idents.get("objects"), |
| 826 | Context.getPointerType(IdT), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 827 | /*TInfo=*/nullptr, |
| 828 | SC_None, nullptr); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 829 | Params.push_back(objects); |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 830 | ParmVarDecl *cnt = ParmVarDecl::Create(Context, Method, |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 831 | SourceLocation(), |
| 832 | SourceLocation(), |
| 833 | &Context.Idents.get("cnt"), |
| 834 | Context.UnsignedLongTy, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 835 | /*TInfo=*/nullptr, SC_None, |
| 836 | nullptr); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 837 | Params.push_back(cnt); |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 838 | Method->setMethodParams(Context, Params, None); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 841 | if (!validateBoxingMethod(*this, Loc, NSArrayDecl, Sel, Method)) |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 842 | return ExprError(); |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 843 | |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 844 | // Dig out the type that all elements should be converted to. |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 845 | QualType T = Method->parameters()[0]->getType(); |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 846 | const PointerType *PtrT = T->getAs<PointerType>(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 847 | if (!PtrT || |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 848 | !Context.hasSameUnqualifiedType(PtrT->getPointeeType(), IdT)) { |
| 849 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 850 | << Sel; |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 851 | Diag(Method->parameters()[0]->getLocation(), |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 852 | diag::note_objc_literal_method_param) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 853 | << 0 << T |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 854 | << Context.getPointerType(IdT.withConst()); |
| 855 | return ExprError(); |
| 856 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 857 | |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 858 | // Check that the 'count' parameter is integral. |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 859 | if (!Method->parameters()[1]->getType()->isIntegerType()) { |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 860 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 861 | << Sel; |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 862 | Diag(Method->parameters()[1]->getLocation(), |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 863 | diag::note_objc_literal_method_param) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 864 | << 1 |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 865 | << Method->parameters()[1]->getType() |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 866 | << "integral"; |
| 867 | return ExprError(); |
| 868 | } |
| 869 | |
| 870 | // We've found a good +arrayWithObjects:count: method. Save it! |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 871 | ArrayWithObjectsMethod = Method; |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 874 | QualType ObjectsType = ArrayWithObjectsMethod->parameters()[0]->getType(); |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 875 | QualType RequiredType = ObjectsType->castAs<PointerType>()->getPointeeType(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 876 | |
| 877 | // Check that each of the elements provided is valid in a collection literal, |
| 878 | // performing conversions as necessary. |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 879 | Expr **ElementsBuffer = Elements.data(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 880 | for (unsigned I = 0, N = Elements.size(); I != N; ++I) { |
| 881 | ExprResult Converted = CheckObjCCollectionLiteralElement(*this, |
| 882 | ElementsBuffer[I], |
| Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 883 | RequiredType, true); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 884 | if (Converted.isInvalid()) |
| 885 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 886 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 887 | ElementsBuffer[I] = Converted.get(); |
| 888 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 889 | |
| 890 | QualType Ty |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 891 | = Context.getObjCObjectPointerType( |
| 892 | Context.getObjCInterfaceType(NSArrayDecl)); |
| 893 | |
| 894 | return MaybeBindToTemporary( |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 895 | ObjCArrayLiteral::Create(Context, Elements, Ty, |
| Fariborz Jahanian | 9ad94aa | 2014-10-28 18:28:16 +0000 | [diff] [blame] | 896 | ArrayWithObjectsMethod, SR)); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 897 | } |
| 898 | |
| Craig Topper | d4336e0 | 2015-12-24 23:58:15 +0000 | [diff] [blame] | 899 | ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR, |
| 900 | MutableArrayRef<ObjCDictionaryElement> Elements) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 901 | SourceLocation Loc = SR.getBegin(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 902 | |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 903 | if (!NSDictionaryDecl) { |
| 904 | NSDictionaryDecl = LookupObjCInterfaceDeclForLiteral(*this, Loc, |
| 905 | Sema::LK_Dictionary); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 906 | if (!NSDictionaryDecl) { |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 907 | return ExprError(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
| Alex Denisov | b7d8563 | 2015-07-24 05:09:40 +0000 | [diff] [blame] | 910 | |
| Fariborz Jahanian | 9ad94aa | 2014-10-28 18:28:16 +0000 | [diff] [blame] | 911 | // Find the dictionaryWithObjects:forKeys:count: method, if we haven't done |
| 912 | // so already. |
| Fariborz Jahanian | bf09db4 | 2014-08-08 18:29:52 +0000 | [diff] [blame] | 913 | QualType IdT = Context.getObjCIdType(); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 914 | if (!DictionaryWithObjectsMethod) { |
| Fariborz Jahanian | 9ad94aa | 2014-10-28 18:28:16 +0000 | [diff] [blame] | 915 | Selector Sel = NSAPIObj->getNSDictionarySelector( |
| 916 | NSAPI::NSDict_dictionaryWithObjectsForKeysCount); |
| 917 | ObjCMethodDecl *Method = NSDictionaryDecl->lookupClassMethod(Sel); |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 918 | if (!Method && getLangOpts().DebuggerObjCLiteral) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 919 | Method = ObjCMethodDecl::Create(Context, |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 920 | SourceLocation(), SourceLocation(), Sel, |
| 921 | IdT, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 922 | nullptr /*TypeSourceInfo */, |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 923 | Context.getTranslationUnitDecl(), |
| Fariborz Jahanian | 9ad94aa | 2014-10-28 18:28:16 +0000 | [diff] [blame] | 924 | false /*Instance*/, false/*isVariadic*/, |
| Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 925 | /*isPropertyAccessor=*/false, |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 926 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
| 927 | ObjCMethodDecl::Required, |
| 928 | false); |
| 929 | SmallVector<ParmVarDecl *, 3> Params; |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 930 | ParmVarDecl *objects = ParmVarDecl::Create(Context, Method, |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 931 | SourceLocation(), |
| 932 | SourceLocation(), |
| 933 | &Context.Idents.get("objects"), |
| 934 | Context.getPointerType(IdT), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 935 | /*TInfo=*/nullptr, SC_None, |
| 936 | nullptr); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 937 | Params.push_back(objects); |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 938 | ParmVarDecl *keys = ParmVarDecl::Create(Context, Method, |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 939 | SourceLocation(), |
| 940 | SourceLocation(), |
| 941 | &Context.Idents.get("keys"), |
| 942 | Context.getPointerType(IdT), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 943 | /*TInfo=*/nullptr, SC_None, |
| 944 | nullptr); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 945 | Params.push_back(keys); |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 946 | ParmVarDecl *cnt = ParmVarDecl::Create(Context, Method, |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 947 | SourceLocation(), |
| 948 | SourceLocation(), |
| 949 | &Context.Idents.get("cnt"), |
| 950 | Context.UnsignedLongTy, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 951 | /*TInfo=*/nullptr, SC_None, |
| 952 | nullptr); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 953 | Params.push_back(cnt); |
| Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 954 | Method->setMethodParams(Context, Params, None); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 957 | if (!validateBoxingMethod(*this, SR.getBegin(), NSDictionaryDecl, Sel, |
| 958 | Method)) |
| 959 | return ExprError(); |
| 960 | |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 961 | // Dig out the type that all values should be converted to. |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 962 | QualType ValueT = Method->parameters()[0]->getType(); |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 963 | const PointerType *PtrValue = ValueT->getAs<PointerType>(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 964 | if (!PtrValue || |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 965 | !Context.hasSameUnqualifiedType(PtrValue->getPointeeType(), IdT)) { |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 966 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 967 | << Sel; |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 968 | Diag(Method->parameters()[0]->getLocation(), |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 969 | diag::note_objc_literal_method_param) |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 970 | << 0 << ValueT |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 971 | << Context.getPointerType(IdT.withConst()); |
| 972 | return ExprError(); |
| 973 | } |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 974 | |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 975 | // Dig out the type that all keys should be converted to. |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 976 | QualType KeyT = Method->parameters()[1]->getType(); |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 977 | const PointerType *PtrKey = KeyT->getAs<PointerType>(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 978 | if (!PtrKey || |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 979 | !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), |
| 980 | IdT)) { |
| 981 | bool err = true; |
| 982 | if (PtrKey) { |
| 983 | if (QIDNSCopying.isNull()) { |
| 984 | // key argument of selector is id<NSCopying>? |
| 985 | if (ObjCProtocolDecl *NSCopyingPDecl = |
| 986 | LookupProtocol(&Context.Idents.get("NSCopying"), SR.getBegin())) { |
| 987 | ObjCProtocolDecl *PQ[] = {NSCopyingPDecl}; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 988 | QIDNSCopying = |
| Douglas Gregor | e9d95f1 | 2015-07-07 03:57:35 +0000 | [diff] [blame] | 989 | Context.getObjCObjectType(Context.ObjCBuiltinIdTy, { }, |
| 990 | llvm::makeArrayRef( |
| 991 | (ObjCProtocolDecl**) PQ, |
| Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 992 | 1), |
| 993 | false); |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 994 | QIDNSCopying = Context.getObjCObjectPointerType(QIDNSCopying); |
| 995 | } |
| 996 | } |
| 997 | if (!QIDNSCopying.isNull()) |
| 998 | err = !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), |
| 999 | QIDNSCopying); |
| 1000 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1001 | |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 1002 | if (err) { |
| 1003 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 1004 | << Sel; |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 1005 | Diag(Method->parameters()[1]->getLocation(), |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 1006 | diag::note_objc_literal_method_param) |
| 1007 | << 1 << KeyT |
| 1008 | << Context.getPointerType(IdT.withConst()); |
| 1009 | return ExprError(); |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | // Check that the 'count' parameter is integral. |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 1014 | QualType CountType = Method->parameters()[2]->getType(); |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 1015 | if (!CountType->isIntegerType()) { |
| 1016 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 1017 | << Sel; |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 1018 | Diag(Method->parameters()[2]->getLocation(), |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 1019 | diag::note_objc_literal_method_param) |
| 1020 | << 2 << CountType |
| 1021 | << "integral"; |
| 1022 | return ExprError(); |
| 1023 | } |
| 1024 | |
| 1025 | // We've found a good +dictionaryWithObjects:keys:count: method; save it! |
| 1026 | DictionaryWithObjectsMethod = Method; |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 1029 | QualType ValuesT = DictionaryWithObjectsMethod->parameters()[0]->getType(); |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 1030 | QualType ValueT = ValuesT->castAs<PointerType>()->getPointeeType(); |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 1031 | QualType KeysT = DictionaryWithObjectsMethod->parameters()[1]->getType(); |
| Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 1032 | QualType KeyT = KeysT->castAs<PointerType>()->getPointeeType(); |
| 1033 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1034 | // Check that each of the keys and values provided is valid in a collection |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1035 | // literal, performing conversions as necessary. |
| 1036 | bool HasPackExpansions = false; |
| Craig Topper | d4336e0 | 2015-12-24 23:58:15 +0000 | [diff] [blame] | 1037 | for (ObjCDictionaryElement &Element : Elements) { |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1038 | // Check the key. |
| Craig Topper | d4336e0 | 2015-12-24 23:58:15 +0000 | [diff] [blame] | 1039 | ExprResult Key = CheckObjCCollectionLiteralElement(*this, Element.Key, |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1040 | KeyT); |
| 1041 | if (Key.isInvalid()) |
| 1042 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1043 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1044 | // Check the value. |
| 1045 | ExprResult Value |
| Craig Topper | d4336e0 | 2015-12-24 23:58:15 +0000 | [diff] [blame] | 1046 | = CheckObjCCollectionLiteralElement(*this, Element.Value, ValueT); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1047 | if (Value.isInvalid()) |
| 1048 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1049 | |
| Craig Topper | d4336e0 | 2015-12-24 23:58:15 +0000 | [diff] [blame] | 1050 | Element.Key = Key.get(); |
| 1051 | Element.Value = Value.get(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1052 | |
| Craig Topper | d4336e0 | 2015-12-24 23:58:15 +0000 | [diff] [blame] | 1053 | if (Element.EllipsisLoc.isInvalid()) |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1054 | continue; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1055 | |
| Craig Topper | d4336e0 | 2015-12-24 23:58:15 +0000 | [diff] [blame] | 1056 | if (!Element.Key->containsUnexpandedParameterPack() && |
| 1057 | !Element.Value->containsUnexpandedParameterPack()) { |
| 1058 | Diag(Element.EllipsisLoc, |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1059 | diag::err_pack_expansion_without_parameter_packs) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1060 | << SourceRange(Element.Key->getBeginLoc(), |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1061 | Element.Value->getEndLoc()); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1062 | return ExprError(); |
| 1063 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1064 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1065 | HasPackExpansions = true; |
| 1066 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1067 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1068 | QualType Ty |
| 1069 | = Context.getObjCObjectPointerType( |
| Robert Wilhelm | 88e0249 | 2013-08-19 07:57:02 +0000 | [diff] [blame] | 1070 | Context.getObjCInterfaceType(NSDictionaryDecl)); |
| 1071 | return MaybeBindToTemporary(ObjCDictionaryLiteral::Create( |
| Craig Topper | d4336e0 | 2015-12-24 23:58:15 +0000 | [diff] [blame] | 1072 | Context, Elements, HasPackExpansions, Ty, |
| Fariborz Jahanian | 9ad94aa | 2014-10-28 18:28:16 +0000 | [diff] [blame] | 1073 | DictionaryWithObjectsMethod, SR)); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| Argyrios Kyrtzidis | 7da04c6 | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 1076 | ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, |
| Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 1077 | TypeSourceInfo *EncodedTypeInfo, |
| Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 1078 | SourceLocation RParenLoc) { |
| Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 1079 | QualType EncodedType = EncodedTypeInfo->getType(); |
| Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 1080 | QualType StrTy; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1081 | if (EncodedType->isDependentType()) |
| Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 1082 | StrTy = Context.DependentTy; |
| 1083 | else { |
| Fariborz Jahanian | d9bc6c3 | 2011-06-16 22:34:44 +0000 | [diff] [blame] | 1084 | if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled. |
| 1085 | !EncodedType->isVoidType()) // void is handled too. |
| Argyrios Kyrtzidis | 7da04c6 | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 1086 | if (RequireCompleteType(AtLoc, EncodedType, |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1087 | diag::err_incomplete_type_objc_at_encode, |
| 1088 | EncodedTypeInfo->getTypeLoc())) |
| Argyrios Kyrtzidis | 7da04c6 | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 1089 | return ExprError(); |
| 1090 | |
| Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 1091 | std::string Str; |
| Fariborz Jahanian | 4bf437e | 2014-08-22 23:17:52 +0000 | [diff] [blame] | 1092 | QualType NotEncodedT; |
| 1093 | Context.getObjCEncodingForType(EncodedType, Str, nullptr, &NotEncodedT); |
| 1094 | if (!NotEncodedT.isNull()) |
| 1095 | Diag(AtLoc, diag::warn_incomplete_encoded_type) |
| 1096 | << EncodedType << NotEncodedT; |
| Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 1097 | |
| 1098 | // The type of @encode is the same as the type of the corresponding string, |
| 1099 | // which is an array type. |
| 1100 | StrTy = Context.CharTy; |
| 1101 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1102 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
| Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 1103 | StrTy.addConst(); |
| 1104 | StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), |
| 1105 | ArrayType::Normal, 0); |
| 1106 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | |
| Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 1108 | return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc); |
| Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1111 | ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 1112 | SourceLocation EncodeLoc, |
| 1113 | SourceLocation LParenLoc, |
| 1114 | ParsedType ty, |
| 1115 | SourceLocation RParenLoc) { |
| Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 1116 | // FIXME: Preserve type source info ? |
| Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 1117 | TypeSourceInfo *TInfo; |
| 1118 | QualType EncodedType = GetTypeFromParser(ty, &TInfo); |
| 1119 | if (!TInfo) |
| 1120 | TInfo = Context.getTrivialTypeSourceInfo(EncodedType, |
| Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 1121 | getLocForEndOfToken(LParenLoc)); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1122 | |
| Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 1123 | return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1126 | static bool HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S, |
| 1127 | SourceLocation AtLoc, |
| Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1128 | SourceLocation LParenLoc, |
| 1129 | SourceLocation RParenLoc, |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1130 | ObjCMethodDecl *Method, |
| 1131 | ObjCMethodList &MethList) { |
| 1132 | ObjCMethodList *M = &MethList; |
| 1133 | bool Warned = false; |
| 1134 | for (M = M->getNext(); M; M=M->getNext()) { |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 1135 | ObjCMethodDecl *MatchingMethodDecl = M->getMethod(); |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1136 | if (MatchingMethodDecl == Method || |
| 1137 | isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()) || |
| 1138 | MatchingMethodDecl->getSelector() != Method->getSelector()) |
| 1139 | continue; |
| 1140 | if (!S.MatchTwoMethodDeclarations(Method, |
| 1141 | MatchingMethodDecl, Sema::MMS_loose)) { |
| 1142 | if (!Warned) { |
| 1143 | Warned = true; |
| Richard Smith | 01d9698 | 2016-12-02 23:00:28 +0000 | [diff] [blame] | 1144 | S.Diag(AtLoc, diag::warn_multiple_selectors) |
| Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1145 | << Method->getSelector() << FixItHint::CreateInsertion(LParenLoc, "(") |
| 1146 | << FixItHint::CreateInsertion(RParenLoc, ")"); |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1147 | S.Diag(Method->getLocation(), diag::note_method_declared_at) |
| 1148 | << Method->getDeclName(); |
| 1149 | } |
| 1150 | S.Diag(MatchingMethodDecl->getLocation(), diag::note_method_declared_at) |
| 1151 | << MatchingMethodDecl->getDeclName(); |
| 1152 | } |
| 1153 | } |
| 1154 | return Warned; |
| 1155 | } |
| 1156 | |
| 1157 | static void DiagnoseMismatchedSelectors(Sema &S, SourceLocation AtLoc, |
| Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1158 | ObjCMethodDecl *Method, |
| 1159 | SourceLocation LParenLoc, |
| 1160 | SourceLocation RParenLoc, |
| 1161 | bool WarnMultipleSelectors) { |
| 1162 | if (!WarnMultipleSelectors || |
| Richard Smith | 01d9698 | 2016-12-02 23:00:28 +0000 | [diff] [blame] | 1163 | S.Diags.isIgnored(diag::warn_multiple_selectors, SourceLocation())) |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1164 | return; |
| 1165 | bool Warned = false; |
| 1166 | for (Sema::GlobalMethodPool::iterator b = S.MethodPool.begin(), |
| 1167 | e = S.MethodPool.end(); b != e; b++) { |
| 1168 | // first, instance methods |
| 1169 | ObjCMethodList &InstMethList = b->second.first; |
| Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1170 | if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc, |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1171 | Method, InstMethList)) |
| 1172 | Warned = true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1173 | |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1174 | // second, class methods |
| 1175 | ObjCMethodList &ClsMethList = b->second.second; |
| Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1176 | if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc, |
| 1177 | Method, ClsMethList) || Warned) |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1178 | return; |
| 1179 | } |
| 1180 | } |
| 1181 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1182 | ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 1183 | SourceLocation AtLoc, |
| 1184 | SourceLocation SelLoc, |
| 1185 | SourceLocation LParenLoc, |
| Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1186 | SourceLocation RParenLoc, |
| 1187 | bool WarnMultipleSelectors) { |
| Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 1188 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel, |
| Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 1189 | SourceRange(LParenLoc, RParenLoc)); |
| Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 1190 | if (!Method) |
| 1191 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| Fariborz Jahanian | 0571d9b | 2009-06-16 16:25:00 +0000 | [diff] [blame] | 1192 | SourceRange(LParenLoc, RParenLoc)); |
| Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 1193 | if (!Method) { |
| 1194 | if (const ObjCMethodDecl *OM = SelectorsForTypoCorrection(Sel)) { |
| 1195 | Selector MatchedSel = OM->getSelector(); |
| 1196 | SourceRange SelectorRange(LParenLoc.getLocWithOffset(1), |
| 1197 | RParenLoc.getLocWithOffset(-1)); |
| 1198 | Diag(SelLoc, diag::warn_undeclared_selector_with_typo) |
| 1199 | << Sel << MatchedSel |
| 1200 | << FixItHint::CreateReplacement(SelectorRange, MatchedSel.getAsString()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1201 | |
| Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 1202 | } else |
| 1203 | Diag(SelLoc, diag::warn_undeclared_selector) << Sel; |
| Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1204 | } else |
| Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1205 | DiagnoseMismatchedSelectors(*this, AtLoc, Method, LParenLoc, RParenLoc, |
| 1206 | WarnMultipleSelectors); |
| Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 1207 | |
| Fariborz Jahanian | 65a78b5 | 2014-05-09 19:51:39 +0000 | [diff] [blame] | 1208 | if (Method && |
| 1209 | Method->getImplementationControl() != ObjCMethodDecl::Optional && |
| Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 1210 | !getSourceManager().isInSystemHeader(Method->getLocation())) |
| 1211 | ReferencedSelectors.insert(std::make_pair(Sel, AtLoc)); |
| Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1212 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1213 | // In ARC, forbid the user from using @selector for |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1214 | // retain/release/autorelease/dealloc/retainCount. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1215 | if (getLangOpts().ObjCAutoRefCount) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1216 | switch (Sel.getMethodFamily()) { |
| 1217 | case OMF_retain: |
| 1218 | case OMF_release: |
| 1219 | case OMF_autorelease: |
| 1220 | case OMF_retainCount: |
| 1221 | case OMF_dealloc: |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1222 | Diag(AtLoc, diag::err_arc_illegal_selector) << |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1223 | Sel << SourceRange(LParenLoc, RParenLoc); |
| 1224 | break; |
| 1225 | |
| 1226 | case OMF_None: |
| 1227 | case OMF_alloc: |
| 1228 | case OMF_copy: |
| Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 1229 | case OMF_finalize: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1230 | case OMF_init: |
| 1231 | case OMF_mutableCopy: |
| 1232 | case OMF_new: |
| 1233 | case OMF_self: |
| Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 1234 | case OMF_initialize: |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1235 | case OMF_performSelector: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1236 | break; |
| 1237 | } |
| 1238 | } |
| Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 1239 | QualType Ty = Context.getObjCSelType(); |
| Daniel Dunbar | 45858d2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 1240 | return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1243 | ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 1244 | SourceLocation AtLoc, |
| 1245 | SourceLocation ProtoLoc, |
| 1246 | SourceLocation LParenLoc, |
| Argyrios Kyrtzidis | b7e4367 | 2012-05-16 00:50:02 +0000 | [diff] [blame] | 1247 | SourceLocation ProtoIdLoc, |
| John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1248 | SourceLocation RParenLoc) { |
| Argyrios Kyrtzidis | b7e4367 | 2012-05-16 00:50:02 +0000 | [diff] [blame] | 1249 | ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoIdLoc); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1250 | if (!PDecl) { |
| Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1251 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1252 | return true; |
| 1253 | } |
| Alex Lorenz | b111da1 | 2018-08-17 22:18:08 +0000 | [diff] [blame] | 1254 | if (!PDecl->hasDefinition()) { |
| 1255 | Diag(ProtoLoc, diag::err_atprotocol_protocol) << PDecl; |
| 1256 | Diag(PDecl->getLocation(), diag::note_entity_declared_at) << PDecl; |
| 1257 | } else { |
| Fariborz Jahanian | a57d91c2 | 2014-07-25 19:45:01 +0000 | [diff] [blame] | 1258 | PDecl = PDecl->getDefinition(); |
| Alex Lorenz | b111da1 | 2018-08-17 22:18:08 +0000 | [diff] [blame] | 1259 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1260 | |
| Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 1261 | QualType Ty = Context.getObjCProtoType(); |
| 1262 | if (Ty.isNull()) |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1263 | return true; |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1264 | Ty = Context.getObjCObjectPointerType(Ty); |
| Argyrios Kyrtzidis | b7e4367 | 2012-05-16 00:50:02 +0000 | [diff] [blame] | 1265 | return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, ProtoIdLoc, RParenLoc); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
| John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1268 | /// Try to capture an implicit reference to 'self'. |
| Eli Friedman | 24af850 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 1269 | ObjCMethodDecl *Sema::tryCaptureObjCSelf(SourceLocation Loc) { |
| 1270 | DeclContext *DC = getFunctionLevelDeclContext(); |
| John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1271 | |
| 1272 | // If we're not in an ObjC method, error out. Note that, unlike the |
| 1273 | // C++ case, we don't require an instance method --- class methods |
| 1274 | // still have a 'self', and we really do still need to capture it! |
| 1275 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC); |
| 1276 | if (!method) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1277 | return nullptr; |
| John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1278 | |
| Douglas Gregor | fdf598e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 1279 | tryCaptureVariable(method->getSelfDecl(), Loc); |
| John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1280 | |
| 1281 | return method; |
| 1282 | } |
| 1283 | |
| Douglas Gregor | 64910ca | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1284 | static QualType stripObjCInstanceType(ASTContext &Context, QualType T) { |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1285 | QualType origType = T; |
| 1286 | if (auto nullability = AttributedType::stripOuterNullability(T)) { |
| 1287 | if (T == Context.getObjCInstanceType()) { |
| 1288 | return Context.getAttributedType( |
| 1289 | AttributedType::getNullabilityAttrKind(*nullability), |
| 1290 | Context.getObjCIdType(), |
| 1291 | Context.getObjCIdType()); |
| 1292 | } |
| 1293 | |
| 1294 | return origType; |
| 1295 | } |
| 1296 | |
| Douglas Gregor | 64910ca | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1297 | if (T == Context.getObjCInstanceType()) |
| 1298 | return Context.getObjCIdType(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1299 | |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1300 | return origType; |
| Douglas Gregor | 64910ca | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1303 | /// Determine the result type of a message send based on the receiver type, |
| 1304 | /// method, and the kind of message send. |
| 1305 | /// |
| 1306 | /// This is the "base" result type, which will still need to be adjusted |
| 1307 | /// to account for nullability. |
| 1308 | static QualType getBaseMessageSendResultType(Sema &S, |
| 1309 | QualType ReceiverType, |
| 1310 | ObjCMethodDecl *Method, |
| 1311 | bool isClassMessage, |
| 1312 | bool isSuperMessage) { |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1313 | assert(Method && "Must have a method"); |
| 1314 | if (!Method->hasRelatedResultType()) |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1315 | return Method->getSendResultType(ReceiverType); |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1316 | |
| 1317 | ASTContext &Context = S.Context; |
| 1318 | |
| 1319 | // Local function that transfers the nullability of the method's |
| 1320 | // result type to the returned result. |
| 1321 | auto transferNullability = [&](QualType type) -> QualType { |
| 1322 | // If the method's result type has nullability, extract it. |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1323 | if (auto nullability = Method->getSendResultType(ReceiverType) |
| 1324 | ->getNullability(Context)){ |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1325 | // Strip off any outer nullability sugar from the provided type. |
| 1326 | (void)AttributedType::stripOuterNullability(type); |
| 1327 | |
| 1328 | // Form a new attributed type using the method result type's nullability. |
| 1329 | return Context.getAttributedType( |
| 1330 | AttributedType::getNullabilityAttrKind(*nullability), |
| 1331 | type, |
| 1332 | type); |
| 1333 | } |
| 1334 | |
| 1335 | return type; |
| 1336 | }; |
| 1337 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1338 | // If a method has a related return type: |
| 1339 | // - if the method found is an instance method, but the message send |
| 1340 | // was a class message send, T is the declared return type of the method |
| 1341 | // found |
| 1342 | if (Method->isInstanceMethod() && isClassMessage) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1343 | return stripObjCInstanceType(Context, |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1344 | Method->getSendResultType(ReceiverType)); |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1345 | |
| 1346 | // - if the receiver is super, T is a pointer to the class of the |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1347 | // enclosing method definition |
| 1348 | if (isSuperMessage) { |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1349 | if (ObjCMethodDecl *CurMethod = S.getCurMethodDecl()) |
| 1350 | if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface()) { |
| 1351 | return transferNullability( |
| 1352 | Context.getObjCObjectPointerType( |
| 1353 | Context.getObjCInterfaceType(Class))); |
| 1354 | } |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1355 | } |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1356 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1357 | // - if the receiver is the name of a class U, T is a pointer to U |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1358 | if (ReceiverType->getAsObjCInterfaceType()) |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1359 | return transferNullability(Context.getObjCObjectPointerType(ReceiverType)); |
| 1360 | // - if the receiver is of type Class or qualified Class type, |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1361 | // T is the declared return type of the method. |
| 1362 | if (ReceiverType->isObjCClassType() || |
| 1363 | ReceiverType->isObjCQualifiedClassType()) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1364 | return stripObjCInstanceType(Context, |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1365 | Method->getSendResultType(ReceiverType)); |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1366 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1367 | // - if the receiver is id, qualified id, Class, or qualified Class, T |
| 1368 | // is the receiver type, otherwise |
| 1369 | // - T is the type of the receiver expression. |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1370 | return transferNullability(ReceiverType); |
| 1371 | } |
| 1372 | |
| Alex Lorenz | f50d1ac | 2018-12-20 22:11:11 +0000 | [diff] [blame] | 1373 | QualType Sema::getMessageSendResultType(const Expr *Receiver, |
| 1374 | QualType ReceiverType, |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1375 | ObjCMethodDecl *Method, |
| 1376 | bool isClassMessage, |
| 1377 | bool isSuperMessage) { |
| 1378 | // Produce the result type. |
| 1379 | QualType resultType = getBaseMessageSendResultType(*this, ReceiverType, |
| 1380 | Method, |
| 1381 | isClassMessage, |
| 1382 | isSuperMessage); |
| 1383 | |
| Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1384 | // If this is a class message, ignore the nullability of the receiver. |
| Alex Lorenz | f50d1ac | 2018-12-20 22:11:11 +0000 | [diff] [blame] | 1385 | if (isClassMessage) { |
| 1386 | // In a class method, class messages to 'self' that return instancetype can |
| 1387 | // be typed as the current class. We can safely do this in ARC because self |
| 1388 | // can't be reassigned, and we do it unsafely outside of ARC because in |
| 1389 | // practice people never reassign self in class methods and there's some |
| 1390 | // virtue in not being aggressively pedantic. |
| 1391 | if (Receiver && Receiver->isObjCSelfExpr()) { |
| 1392 | assert(ReceiverType->isObjCClassType() && "expected a Class self"); |
| 1393 | QualType T = Method->getSendResultType(ReceiverType); |
| 1394 | AttributedType::stripOuterNullability(T); |
| 1395 | if (T == Context.getObjCInstanceType()) { |
| 1396 | const ObjCMethodDecl *MD = cast<ObjCMethodDecl>( |
| 1397 | cast<ImplicitParamDecl>( |
| 1398 | cast<DeclRefExpr>(Receiver->IgnoreParenImpCasts())->getDecl()) |
| 1399 | ->getDeclContext()); |
| 1400 | assert(MD->isClassMethod() && "expected a class method"); |
| 1401 | QualType NewResultType = Context.getObjCObjectPointerType( |
| 1402 | Context.getObjCInterfaceType(MD->getClassInterface())); |
| 1403 | if (auto Nullability = resultType->getNullability(Context)) |
| 1404 | NewResultType = Context.getAttributedType( |
| 1405 | AttributedType::getNullabilityAttrKind(*Nullability), |
| 1406 | NewResultType, NewResultType); |
| 1407 | return NewResultType; |
| 1408 | } |
| 1409 | } |
| Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1410 | return resultType; |
| Alex Lorenz | f50d1ac | 2018-12-20 22:11:11 +0000 | [diff] [blame] | 1411 | } |
| Douglas Gregor | 2a20bd1 | 2015-06-19 18:25:57 +0000 | [diff] [blame] | 1412 | |
| Akira Hatanaka | 66d405d | 2018-07-26 17:51:13 +0000 | [diff] [blame] | 1413 | // There is nothing left to do if the result type cannot have a nullability |
| 1414 | // specifier. |
| 1415 | if (!resultType->canHaveNullability()) |
| 1416 | return resultType; |
| 1417 | |
| Douglas Gregor | 813a066 | 2015-06-19 18:14:38 +0000 | [diff] [blame] | 1418 | // Map the nullability of the result into a table index. |
| 1419 | unsigned receiverNullabilityIdx = 0; |
| 1420 | if (auto nullability = ReceiverType->getNullability(Context)) |
| 1421 | receiverNullabilityIdx = 1 + static_cast<unsigned>(*nullability); |
| 1422 | |
| 1423 | unsigned resultNullabilityIdx = 0; |
| 1424 | if (auto nullability = resultType->getNullability(Context)) |
| 1425 | resultNullabilityIdx = 1 + static_cast<unsigned>(*nullability); |
| 1426 | |
| 1427 | // The table of nullability mappings, indexed by the receiver's nullability |
| 1428 | // and then the result type's nullability. |
| 1429 | static const uint8_t None = 0; |
| 1430 | static const uint8_t NonNull = 1; |
| 1431 | static const uint8_t Nullable = 2; |
| 1432 | static const uint8_t Unspecified = 3; |
| 1433 | static const uint8_t nullabilityMap[4][4] = { |
| 1434 | // None NonNull Nullable Unspecified |
| 1435 | /* None */ { None, None, Nullable, None }, |
| 1436 | /* NonNull */ { None, NonNull, Nullable, Unspecified }, |
| 1437 | /* Nullable */ { Nullable, Nullable, Nullable, Nullable }, |
| 1438 | /* Unspecified */ { None, Unspecified, Nullable, Unspecified } |
| 1439 | }; |
| 1440 | |
| 1441 | unsigned newResultNullabilityIdx |
| 1442 | = nullabilityMap[receiverNullabilityIdx][resultNullabilityIdx]; |
| 1443 | if (newResultNullabilityIdx == resultNullabilityIdx) |
| 1444 | return resultType; |
| 1445 | |
| 1446 | // Strip off the existing nullability. This removes as little type sugar as |
| 1447 | // possible. |
| 1448 | do { |
| 1449 | if (auto attributed = dyn_cast<AttributedType>(resultType.getTypePtr())) { |
| 1450 | resultType = attributed->getModifiedType(); |
| 1451 | } else { |
| 1452 | resultType = resultType.getDesugaredType(Context); |
| 1453 | } |
| 1454 | } while (resultType->getNullability(Context)); |
| 1455 | |
| 1456 | // Add nullability back if needed. |
| 1457 | if (newResultNullabilityIdx > 0) { |
| 1458 | auto newNullability |
| 1459 | = static_cast<NullabilityKind>(newResultNullabilityIdx-1); |
| 1460 | return Context.getAttributedType( |
| 1461 | AttributedType::getNullabilityAttrKind(newNullability), |
| 1462 | resultType, resultType); |
| 1463 | } |
| 1464 | |
| 1465 | return resultType; |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1466 | } |
| John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1467 | |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1468 | /// Look for an ObjC method whose result type exactly matches the given type. |
| 1469 | static const ObjCMethodDecl * |
| 1470 | findExplicitInstancetypeDeclarer(const ObjCMethodDecl *MD, |
| 1471 | QualType instancetype) { |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1472 | if (MD->getReturnType() == instancetype) |
| 1473 | return MD; |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1474 | |
| 1475 | // For these purposes, a method in an @implementation overrides a |
| 1476 | // declaration in the @interface. |
| 1477 | if (const ObjCImplDecl *impl = |
| 1478 | dyn_cast<ObjCImplDecl>(MD->getDeclContext())) { |
| 1479 | const ObjCContainerDecl *iface; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1480 | if (const ObjCCategoryImplDecl *catImpl = |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1481 | dyn_cast<ObjCCategoryImplDecl>(impl)) { |
| 1482 | iface = catImpl->getCategoryDecl(); |
| 1483 | } else { |
| 1484 | iface = impl->getClassInterface(); |
| 1485 | } |
| 1486 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1487 | const ObjCMethodDecl *ifaceMD = |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1488 | iface->getMethod(MD->getSelector(), MD->isInstanceMethod()); |
| 1489 | if (ifaceMD) return findExplicitInstancetypeDeclarer(ifaceMD, instancetype); |
| 1490 | } |
| 1491 | |
| 1492 | SmallVector<const ObjCMethodDecl *, 4> overrides; |
| 1493 | MD->getOverriddenMethods(overrides); |
| 1494 | for (unsigned i = 0, e = overrides.size(); i != e; ++i) { |
| 1495 | if (const ObjCMethodDecl *result = |
| 1496 | findExplicitInstancetypeDeclarer(overrides[i], instancetype)) |
| 1497 | return result; |
| 1498 | } |
| 1499 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1500 | return nullptr; |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | void Sema::EmitRelatedResultTypeNoteForReturn(QualType destType) { |
| 1504 | // Only complain if we're in an ObjC method and the required return |
| 1505 | // type doesn't match the method's declared return type. |
| 1506 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurContext); |
| 1507 | if (!MD || !MD->hasRelatedResultType() || |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1508 | Context.hasSameUnqualifiedType(destType, MD->getReturnType())) |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1509 | return; |
| 1510 | |
| 1511 | // Look for a method overridden by this method which explicitly uses |
| 1512 | // 'instancetype'. |
| 1513 | if (const ObjCMethodDecl *overridden = |
| 1514 | findExplicitInstancetypeDeclarer(MD, Context.getObjCInstanceType())) { |
| Aaron Ballman | 41b10ac | 2014-08-01 13:20:09 +0000 | [diff] [blame] | 1515 | SourceRange range = overridden->getReturnTypeSourceRange(); |
| 1516 | SourceLocation loc = range.getBegin(); |
| John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1517 | if (loc.isInvalid()) |
| 1518 | loc = overridden->getLocation(); |
| 1519 | Diag(loc, diag::note_related_result_type_explicit) |
| 1520 | << /*current method*/ 1 << range; |
| 1521 | return; |
| 1522 | } |
| 1523 | |
| 1524 | // Otherwise, if we have an interesting method family, note that. |
| 1525 | // This should always trigger if the above didn't. |
| 1526 | if (ObjCMethodFamily family = MD->getMethodFamily()) |
| 1527 | Diag(MD->getLocation(), diag::note_related_result_type_family) |
| 1528 | << /*current method*/ 1 |
| 1529 | << family; |
| 1530 | } |
| 1531 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1532 | void Sema::EmitRelatedResultTypeNote(const Expr *E) { |
| 1533 | E = E->IgnoreParenImpCasts(); |
| 1534 | const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E); |
| 1535 | if (!MsgSend) |
| 1536 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1537 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1538 | const ObjCMethodDecl *Method = MsgSend->getMethodDecl(); |
| 1539 | if (!Method) |
| 1540 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1541 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1542 | if (!Method->hasRelatedResultType()) |
| 1543 | return; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1544 | |
| 1545 | if (Context.hasSameUnqualifiedType( |
| 1546 | Method->getReturnType().getNonReferenceType(), MsgSend->getType())) |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1547 | return; |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1548 | |
| 1549 | if (!Context.hasSameUnqualifiedType(Method->getReturnType(), |
| Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 1550 | Context.getObjCInstanceType())) |
| 1551 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1552 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1553 | Diag(Method->getLocation(), diag::note_related_result_type_inferred) |
| 1554 | << Method->isInstanceMethod() << Method->getSelector() |
| 1555 | << MsgSend->getType(); |
| 1556 | } |
| 1557 | |
| Alex Lorenz | f50d1ac | 2018-12-20 22:11:11 +0000 | [diff] [blame] | 1558 | bool Sema::CheckMessageArgumentTypes( |
| 1559 | const Expr *Receiver, QualType ReceiverType, MultiExprArg Args, |
| 1560 | Selector Sel, ArrayRef<SourceLocation> SelectorLocs, ObjCMethodDecl *Method, |
| 1561 | bool isClassMessage, bool isSuperMessage, SourceLocation lbrac, |
| 1562 | SourceLocation rbrac, SourceRange RecRange, QualType &ReturnType, |
| 1563 | ExprValueKind &VK) { |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 1564 | SourceLocation SelLoc; |
| 1565 | if (!SelectorLocs.empty() && SelectorLocs.front().isValid()) |
| 1566 | SelLoc = SelectorLocs.front(); |
| 1567 | else |
| 1568 | SelLoc = lbrac; |
| 1569 | |
| Daniel Dunbar | aa9326c | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1570 | if (!Method) { |
| Daniel Dunbar | 83876b4 | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 1571 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
| Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1572 | for (unsigned i = 0, e = Args.size(); i != e; i++) { |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1573 | if (Args[i]->isTypeDependent()) |
| 1574 | continue; |
| 1575 | |
| John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1576 | ExprResult result; |
| 1577 | if (getLangOpts().DebuggerSupport) { |
| 1578 | QualType paramTy; // ignored |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 1579 | result = checkUnknownAnyArg(SelLoc, Args[i], paramTy); |
| John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1580 | } else { |
| 1581 | result = DefaultArgumentPromotion(Args[i]); |
| 1582 | } |
| 1583 | if (result.isInvalid()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1584 | return true; |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1585 | Args[i] = result.get(); |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1586 | } |
| Daniel Dunbar | 83876b4 | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 1587 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1588 | unsigned DiagID; |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1589 | if (getLangOpts().ObjCAutoRefCount) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1590 | DiagID = diag::err_arc_method_not_found; |
| 1591 | else |
| 1592 | DiagID = isClassMessage ? diag::warn_class_method_not_found |
| 1593 | : diag::warn_inst_method_not_found; |
| Fariborz Jahanian | 773df4a | 2013-05-14 23:24:17 +0000 | [diff] [blame] | 1594 | if (!getLangOpts().DebuggerSupport) { |
| Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 1595 | const ObjCMethodDecl *OMD = SelectorsForTypoCorrection(Sel, ReceiverType); |
| Fariborz Jahanian | 0649923 | 2013-06-18 17:10:58 +0000 | [diff] [blame] | 1596 | if (OMD && !OMD->isInvalidDecl()) { |
| Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 1597 | if (getLangOpts().ObjCAutoRefCount) |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 1598 | DiagID = diag::err_method_not_found_with_typo; |
| Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 1599 | else |
| 1600 | DiagID = isClassMessage ? diag::warn_class_method_not_found_with_typo |
| 1601 | : diag::warn_instance_method_not_found_with_typo; |
| Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 1602 | Selector MatchedSel = OMD->getSelector(); |
| 1603 | SourceRange SelectorRange(SelectorLocs.front(), SelectorLocs.back()); |
| Fariborz Jahanian | 5ab8750 | 2014-08-12 22:16:41 +0000 | [diff] [blame] | 1604 | if (MatchedSel.isUnarySelector()) |
| 1605 | Diag(SelLoc, DiagID) |
| 1606 | << Sel<< isClassMessage << MatchedSel |
| 1607 | << FixItHint::CreateReplacement(SelectorRange, MatchedSel.getAsString()); |
| 1608 | else |
| 1609 | Diag(SelLoc, DiagID) << Sel<< isClassMessage << MatchedSel; |
| Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 1610 | } |
| 1611 | else |
| 1612 | Diag(SelLoc, DiagID) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1613 | << Sel << isClassMessage << SourceRange(SelectorLocs.front(), |
| Fariborz Jahanian | 6ce25c0 | 2012-08-31 17:03:18 +0000 | [diff] [blame] | 1614 | SelectorLocs.back()); |
| Fariborz Jahanian | 773df4a | 2013-05-14 23:24:17 +0000 | [diff] [blame] | 1615 | // Find the class to which we are sending this message. |
| 1616 | if (ReceiverType->isObjCObjectPointerType()) { |
| Fariborz Jahanian | 19c2e2f | 2014-08-19 23:39:17 +0000 | [diff] [blame] | 1617 | if (ObjCInterfaceDecl *ThisClass = |
| 1618 | ReceiverType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()) { |
| 1619 | Diag(ThisClass->getLocation(), diag::note_receiver_class_declared); |
| 1620 | if (!RecRange.isInvalid()) |
| 1621 | if (ThisClass->lookupClassMethod(Sel)) |
| 1622 | Diag(RecRange.getBegin(),diag::note_receiver_expr_here) |
| 1623 | << FixItHint::CreateReplacement(RecRange, |
| 1624 | ThisClass->getNameAsString()); |
| 1625 | } |
| Fariborz Jahanian | 773df4a | 2013-05-14 23:24:17 +0000 | [diff] [blame] | 1626 | } |
| 1627 | } |
| John McCall | 3f4138c | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 1628 | |
| 1629 | // In debuggers, we want to use __unknown_anytype for these |
| 1630 | // results so that clients can cast them. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1631 | if (getLangOpts().DebuggerSupport) { |
| John McCall | 3f4138c | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 1632 | ReturnType = Context.UnknownAnyTy; |
| 1633 | } else { |
| 1634 | ReturnType = Context.getObjCIdType(); |
| 1635 | } |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1636 | VK = VK_RValue; |
| Daniel Dunbar | aa9326c | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1637 | return false; |
| Daniel Dunbar | aa9326c | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1638 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
| Alex Lorenz | f50d1ac | 2018-12-20 22:11:11 +0000 | [diff] [blame] | 1640 | ReturnType = getMessageSendResultType(Receiver, ReceiverType, Method, |
| 1641 | isClassMessage, isSuperMessage); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1642 | VK = Expr::getValueKindForType(Method->getReturnType()); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1643 | |
| Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1644 | unsigned NumNamedArgs = Sel.getNumArgs(); |
| Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1645 | // Method might have more arguments than selector indicates. This is due |
| 1646 | // to addition of c-style arguments in method. |
| 1647 | if (Method->param_size() > Sel.getNumArgs()) |
| 1648 | NumNamedArgs = Method->param_size(); |
| 1649 | // FIXME. This need be cleaned up. |
| Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1650 | if (Args.size() < NumNamedArgs) { |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 1651 | Diag(SelLoc, diag::err_typecheck_call_too_few_args) |
| Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1652 | << 2 << NumNamedArgs << static_cast<unsigned>(Args.size()); |
| Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1653 | return false; |
| 1654 | } |
| Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1655 | |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1656 | // Compute the set of type arguments to be substituted into each parameter |
| 1657 | // type. |
| 1658 | Optional<ArrayRef<QualType>> typeArgs |
| 1659 | = ReceiverType->getObjCSubstitutions(Method->getDeclContext()); |
| Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 1660 | bool IsError = false; |
| Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1661 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1662 | // We can't do any type-checking on a type-dependent argument. |
| 1663 | if (Args[i]->isTypeDependent()) |
| 1664 | continue; |
| 1665 | |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1666 | Expr *argExpr = Args[i]; |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1667 | |
| Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 1668 | ParmVarDecl *param = Method->parameters()[i]; |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1669 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1670 | |
| Akira Hatanaka | 627586b | 2018-03-02 01:53:15 +0000 | [diff] [blame] | 1671 | if (param->hasAttr<NoEscapeAttr>()) |
| 1672 | if (auto *BE = dyn_cast<BlockExpr>( |
| 1673 | argExpr->IgnoreParenNoopCasts(Context))) |
| 1674 | BE->getBlockDecl()->setDoesNotEscape(); |
| 1675 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1676 | // Strip the unbridged-cast placeholder expression off unless it's |
| 1677 | // a consumed argument. |
| 1678 | if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) && |
| 1679 | !param->hasAttr<CFConsumedAttr>()) |
| 1680 | argExpr = stripARCUnbridgedCast(argExpr); |
| 1681 | |
| John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 1682 | // If the parameter is __unknown_anytype, infer its type |
| 1683 | // from the argument. |
| 1684 | if (param->getType() == Context.UnknownAnyTy) { |
| John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1685 | QualType paramType; |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 1686 | ExprResult argE = checkUnknownAnyArg(SelLoc, argExpr, paramType); |
| John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1687 | if (argE.isInvalid()) { |
| John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 1688 | IsError = true; |
| John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1689 | } else { |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1690 | Args[i] = argE.get(); |
| John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 1691 | |
| John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1692 | // Update the parameter type in-place. |
| 1693 | param->setType(paramType); |
| 1694 | } |
| 1695 | continue; |
| John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1698 | QualType origParamType = param->getType(); |
| 1699 | QualType paramType = param->getType(); |
| 1700 | if (typeArgs) |
| 1701 | paramType = paramType.substObjCTypeArgs( |
| 1702 | Context, |
| 1703 | *typeArgs, |
| 1704 | ObjCSubstitutionContext::Parameter); |
| 1705 | |
| Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1706 | if (RequireCompleteType(argExpr->getSourceRange().getBegin(), |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1707 | paramType, |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1708 | diag::err_call_incomplete_argument, argExpr)) |
| Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1709 | return true; |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1710 | |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1711 | InitializedEntity Entity |
| 1712 | = InitializedEntity::InitializeParameter(Context, param, paramType); |
| Fariborz Jahanian | a1db7df | 2014-07-31 17:39:50 +0000 | [diff] [blame] | 1713 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), argExpr); |
| Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1714 | if (ArgE.isInvalid()) |
| 1715 | IsError = true; |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1716 | else { |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1717 | Args[i] = ArgE.getAs<Expr>(); |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1718 | |
| 1719 | // If we are type-erasing a block to a block-compatible |
| 1720 | // Objective-C pointer type, we may need to extend the lifetime |
| 1721 | // of the block object. |
| 1722 | if (typeArgs && Args[i]->isRValue() && paramType->isBlockPointerType() && |
| Bob Wilson | 7e9fd56 | 2015-10-02 01:05:29 +0000 | [diff] [blame] | 1723 | Args[i]->getType()->isBlockPointerType() && |
| 1724 | origParamType->isObjCObjectPointerType()) { |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 1725 | ExprResult arg = Args[i]; |
| 1726 | maybeExtendBlockObject(arg); |
| 1727 | Args[i] = arg.get(); |
| 1728 | } |
| 1729 | } |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1730 | } |
| Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1731 | |
| 1732 | // Promote additional arguments to variadic methods. |
| 1733 | if (Method->isVariadic()) { |
| Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1734 | for (unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) { |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1735 | if (Args[i]->isTypeDependent()) |
| 1736 | continue; |
| 1737 | |
| Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 1738 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1739 | nullptr); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1740 | IsError |= Arg.isInvalid(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1741 | Args[i] = Arg.get(); |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1742 | } |
| Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1743 | } else { |
| 1744 | // Check for extra arguments to non-variadic methods. |
| Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1745 | if (Args.size() != NumNamedArgs) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1746 | Diag(Args[NumNamedArgs]->getBeginLoc(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1747 | diag::err_typecheck_call_too_many_args) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1748 | << 2 /*method*/ << NumNamedArgs << static_cast<unsigned>(Args.size()) |
| 1749 | << Method->getSourceRange() |
| 1750 | << SourceRange(Args[NumNamedArgs]->getBeginLoc(), |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1751 | Args.back()->getEndLoc()); |
| Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1752 | } |
| 1753 | } |
| 1754 | |
| Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1755 | DiagnoseSentinelCalls(Method, SelLoc, Args); |
| Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1756 | |
| 1757 | // Do additional checkings on method. |
| Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1758 | IsError |= CheckObjCMethodCall( |
| Craig Topper | 8c2a2a0 | 2014-08-30 16:55:39 +0000 | [diff] [blame] | 1759 | Method, SelLoc, makeArrayRef(Args.data(), Args.size())); |
| Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1760 | |
| Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 1761 | return IsError; |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 1764 | bool Sema::isSelfExpr(Expr *RExpr) { |
| Fariborz Jahanian | b3b1e17 | 2011-03-27 19:53:47 +0000 | [diff] [blame] | 1765 | // 'self' is objc 'self' in an objc method only. |
| Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 1766 | ObjCMethodDecl *Method = |
| 1767 | dyn_cast_or_null<ObjCMethodDecl>(CurContext->getNonClosureAncestor()); |
| 1768 | return isSelfExpr(RExpr, Method); |
| 1769 | } |
| 1770 | |
| 1771 | bool Sema::isSelfExpr(Expr *receiver, const ObjCMethodDecl *method) { |
| John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1772 | if (!method) return false; |
| 1773 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1774 | receiver = receiver->IgnoreParenLValueCasts(); |
| 1775 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver)) |
| John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1776 | if (DRE->getDecl() == method->getSelfDecl()) |
| Douglas Gregor | 486b74e | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 1777 | return true; |
| 1778 | return false; |
| Steve Naroff | 3f49fee | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1781 | /// LookupMethodInType - Look up a method in an ObjCObjectType. |
| 1782 | ObjCMethodDecl *Sema::LookupMethodInObjectType(Selector sel, QualType type, |
| 1783 | bool isInstance) { |
| 1784 | const ObjCObjectType *objType = type->castAs<ObjCObjectType>(); |
| 1785 | if (ObjCInterfaceDecl *iface = objType->getInterface()) { |
| 1786 | // Look it up in the main interface (and categories, etc.) |
| 1787 | if (ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance)) |
| 1788 | return method; |
| 1789 | |
| 1790 | // Okay, look for "private" methods declared in any |
| 1791 | // @implementations we've seen. |
| Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 1792 | if (ObjCMethodDecl *method = iface->lookupPrivateMethod(sel, isInstance)) |
| 1793 | return method; |
| John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | // Check qualifiers. |
| Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 1797 | for (const auto *I : objType->quals()) |
| 1798 | if (ObjCMethodDecl *method = I->lookupMethod(sel, isInstance)) |
| John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1799 | return method; |
| 1800 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1801 | return nullptr; |
| John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1804 | /// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier |
| Fariborz Jahanian | 3dc11ad | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 1805 | /// list of a qualified objective pointer type. |
| 1806 | ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel, |
| 1807 | const ObjCObjectPointerType *OPT, |
| 1808 | bool Instance) |
| 1809 | { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1810 | ObjCMethodDecl *MD = nullptr; |
| Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 1811 | for (const auto *PROTO : OPT->quals()) { |
| Fariborz Jahanian | 3dc11ad | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 1812 | if ((MD = PROTO->lookupMethod(Sel, Instance))) { |
| 1813 | return MD; |
| 1814 | } |
| 1815 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1816 | return nullptr; |
| Fariborz Jahanian | 3dc11ad | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1819 | /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an |
| 1820 | /// objective C interface. This is a property reference expression. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1821 | ExprResult Sema:: |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1822 | HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT, |
| Fariborz Jahanian | c297cd8 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1823 | Expr *BaseExpr, SourceLocation OpLoc, |
| 1824 | DeclarationName MemberName, |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1825 | SourceLocation MemberLoc, |
| 1826 | SourceLocation SuperLoc, QualType SuperType, |
| 1827 | bool Super) { |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1828 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 1829 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
| Benjamin Kramer | 1458c1c | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 1830 | |
| Benjamin Kramer | 365082d | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1831 | if (!MemberName.isIdentifier()) { |
| Douglas Gregor | d645931 | 2011-04-20 18:19:55 +0000 | [diff] [blame] | 1832 | Diag(MemberLoc, diag::err_invalid_property_name) |
| 1833 | << MemberName << QualType(OPT, 0); |
| 1834 | return ExprError(); |
| 1835 | } |
| Benjamin Kramer | 365082d | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1836 | |
| 1837 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1838 | |
| Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1839 | SourceRange BaseRange = Super? SourceRange(SuperLoc) |
| 1840 | : BaseExpr->getSourceRange(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1841 | if (RequireCompleteType(MemberLoc, OPT->getPointeeType(), |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1842 | diag::err_property_not_found_forward_class, |
| 1843 | MemberName, BaseRange)) |
| Fariborz Jahanian | 7cabbe0 | 2010-12-16 00:56:28 +0000 | [diff] [blame] | 1844 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1845 | |
| Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1846 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration( |
| 1847 | Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1848 | // Check whether we can reference this property. |
| 1849 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1850 | return ExprError(); |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1851 | if (Super) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1852 | return new (Context) |
| 1853 | ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue, |
| 1854 | OK_ObjCProperty, MemberLoc, SuperLoc, SuperType); |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1855 | else |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1856 | return new (Context) |
| 1857 | ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue, |
| 1858 | OK_ObjCProperty, MemberLoc, BaseExpr); |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1859 | } |
| 1860 | // Check protocols on qualified interfaces. |
| Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 1861 | for (const auto *I : OPT->quals()) |
| Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1862 | if (ObjCPropertyDecl *PD = I->FindPropertyDeclaration( |
| 1863 | Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1864 | // Check whether we can reference this property. |
| 1865 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1866 | return ExprError(); |
| Fariborz Jahanian | fce89c6 | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1867 | |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1868 | if (Super) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1869 | return new (Context) ObjCPropertyRefExpr( |
| 1870 | PD, Context.PseudoObjectTy, VK_LValue, OK_ObjCProperty, MemberLoc, |
| 1871 | SuperLoc, SuperType); |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1872 | else |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1873 | return new (Context) |
| 1874 | ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue, |
| 1875 | OK_ObjCProperty, MemberLoc, BaseExpr); |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1876 | } |
| 1877 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1878 | // selector is implemented. |
| 1879 | |
| 1880 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1881 | // shared with the code in ActOnInstanceMessage. |
| 1882 | |
| 1883 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 1884 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1885 | |
| Manman Ren | 2b2b1a9 | 2016-06-28 23:01:49 +0000 | [diff] [blame] | 1886 | // May be found in property's qualified list. |
| Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1887 | if (!Getter) |
| 1888 | Getter = LookupMethodInQualifiedType(Sel, OPT, true); |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1889 | |
| 1890 | // If this reference is in an @implementation, check for 'private' methods. |
| 1891 | if (!Getter) |
| Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 1892 | Getter = IFace->lookupPrivateMethod(Sel); |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1893 | |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1894 | if (Getter) { |
| 1895 | // Check if we can reference this property. |
| 1896 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1897 | return ExprError(); |
| 1898 | } |
| 1899 | // If we found a getter then this may be a valid dot-reference, we |
| 1900 | // will look for the matching setter, in case it is needed. |
| 1901 | Selector SetterSel = |
| Adrian Prantl | a4ce906 | 2013-06-07 22:29:12 +0000 | [diff] [blame] | 1902 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1903 | PP.getSelectorTable(), Member); |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1904 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1905 | |
| Manman Ren | 2b2b1a9 | 2016-06-28 23:01:49 +0000 | [diff] [blame] | 1906 | // May be found in property's qualified list. |
| Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1907 | if (!Setter) |
| 1908 | Setter = LookupMethodInQualifiedType(SetterSel, OPT, true); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1909 | |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1910 | if (!Setter) { |
| 1911 | // If this reference is in an @implementation, also check for 'private' |
| 1912 | // methods. |
| Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 1913 | Setter = IFace->lookupPrivateMethod(SetterSel); |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1914 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1915 | |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1916 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1917 | return ExprError(); |
| 1918 | |
| Fariborz Jahanian | 0b1d288 | 2014-08-08 22:33:24 +0000 | [diff] [blame] | 1919 | // Special warning if member name used in a property-dot for a setter accessor |
| 1920 | // does not use a property with same name; e.g. obj.X = ... for a property with |
| 1921 | // name 'x'. |
| Manman Ren | 5b78640 | 2016-01-28 18:49:28 +0000 | [diff] [blame] | 1922 | if (Setter && Setter->isImplicit() && Setter->isPropertyAccessor() && |
| 1923 | !IFace->FindPropertyDeclaration( |
| 1924 | Member, ObjCPropertyQueryKind::OBJC_PR_query_instance)) { |
| Fariborz Jahanian | 4eda2c0 | 2014-08-15 17:39:00 +0000 | [diff] [blame] | 1925 | if (const ObjCPropertyDecl *PDecl = Setter->findPropertyDecl()) { |
| 1926 | // Do not warn if user is using property-dot syntax to make call to |
| 1927 | // user named setter. |
| 1928 | if (!(PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter)) |
| Fariborz Jahanian | 0b1d288 | 2014-08-08 22:33:24 +0000 | [diff] [blame] | 1929 | Diag(MemberLoc, |
| 1930 | diag::warn_property_access_suggest) |
| 1931 | << MemberName << QualType(OPT, 0) << PDecl->getName() |
| 1932 | << FixItHint::CreateReplacement(MemberLoc, PDecl->getName()); |
| Fariborz Jahanian | 4eda2c0 | 2014-08-15 17:39:00 +0000 | [diff] [blame] | 1933 | } |
| Fariborz Jahanian | 0b1d288 | 2014-08-08 22:33:24 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
| Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1936 | if (Getter || Setter) { |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1937 | if (Super) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1938 | return new (Context) |
| 1939 | ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue, |
| 1940 | OK_ObjCProperty, MemberLoc, SuperLoc, SuperType); |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1941 | else |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1942 | return new (Context) |
| 1943 | ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue, |
| 1944 | OK_ObjCProperty, MemberLoc, BaseExpr); |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1945 | |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | // Attempt to correct for typos in property names. |
| Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 1949 | DeclFilterCCC<ObjCPropertyDecl> CCC{}; |
| 1950 | if (TypoCorrection Corrected = CorrectTypo( |
| 1951 | DeclarationNameInfo(MemberName, MemberLoc), LookupOrdinaryName, |
| 1952 | nullptr, nullptr, CCC, CTK_ErrorRecovery, IFace, false, OPT)) { |
| Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1953 | DeclarationName TypoResult = Corrected.getCorrection(); |
| Manman Ren | 2b2b1a9 | 2016-06-28 23:01:49 +0000 | [diff] [blame] | 1954 | if (TypoResult.isIdentifier() && |
| 1955 | TypoResult.getAsIdentifierInfo() == Member) { |
| 1956 | // There is no need to try the correction if it is the same. |
| 1957 | NamedDecl *ChosenDecl = |
| 1958 | Corrected.isKeyword() ? nullptr : Corrected.getFoundDecl(); |
| 1959 | if (ChosenDecl && isa<ObjCPropertyDecl>(ChosenDecl)) |
| 1960 | if (cast<ObjCPropertyDecl>(ChosenDecl)->isClassProperty()) { |
| 1961 | // This is a class property, we should not use the instance to |
| 1962 | // access it. |
| 1963 | Diag(MemberLoc, diag::err_class_property_found) << MemberName |
| 1964 | << OPT->getInterfaceDecl()->getName() |
| 1965 | << FixItHint::CreateReplacement(BaseExpr->getSourceRange(), |
| 1966 | OPT->getInterfaceDecl()->getName()); |
| 1967 | return ExprError(); |
| 1968 | } |
| 1969 | } else { |
| 1970 | diagnoseTypo(Corrected, PDiag(diag::err_property_not_found_suggest) |
| 1971 | << MemberName << QualType(OPT, 0)); |
| 1972 | return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc, |
| 1973 | TypoResult, MemberLoc, |
| 1974 | SuperLoc, SuperType, Super); |
| 1975 | } |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1976 | } |
| Fariborz Jahanian | 05d389f | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1977 | ObjCInterfaceDecl *ClassDeclared; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1978 | if (ObjCIvarDecl *Ivar = |
| Fariborz Jahanian | 05d389f | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1979 | IFace->lookupInstanceVariable(Member, ClassDeclared)) { |
| 1980 | QualType T = Ivar->getType(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1981 | if (const ObjCObjectPointerType * OBJPT = |
| Fariborz Jahanian | 05d389f | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1982 | T->getAsObjCInterfacePointerType()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1983 | if (RequireCompleteType(MemberLoc, OBJPT->getPointeeType(), |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1984 | diag::err_property_not_as_forward_class, |
| 1985 | MemberName, BaseExpr)) |
| Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1986 | return ExprError(); |
| Fariborz Jahanian | 05d389f | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1987 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1988 | Diag(MemberLoc, |
| Fariborz Jahanian | c297cd8 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1989 | diag::err_ivar_access_using_property_syntax_suggest) |
| 1990 | << MemberName << QualType(OPT, 0) << Ivar->getDeclName() |
| 1991 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| 1992 | return ExprError(); |
| Fariborz Jahanian | 05d389f | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1993 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1994 | |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1995 | Diag(MemberLoc, diag::err_property_not_found) |
| 1996 | << MemberName << QualType(OPT, 0); |
| Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1997 | if (Setter) |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1998 | Diag(Setter->getLocation(), diag::note_getter_unavailable) |
| Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1999 | << MemberName << BaseExpr->getSourceRange(); |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 2000 | return ExprError(); |
| Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2003 | ExprResult Sema:: |
| Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2004 | ActOnClassPropertyRefExpr(IdentifierInfo &receiverName, |
| 2005 | IdentifierInfo &propertyName, |
| 2006 | SourceLocation receiverNameLoc, |
| 2007 | SourceLocation propertyNameLoc) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2008 | |
| Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 2009 | IdentifierInfo *receiverNamePtr = &receiverName; |
| Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2010 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr, |
| 2011 | receiverNameLoc); |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2012 | |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2013 | QualType SuperType; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2014 | if (!IFace) { |
| Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2015 | // If the "receiver" is 'super' in a method, handle it as an expression-like |
| 2016 | // property reference. |
| John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 2017 | if (receiverNamePtr->isStr("super")) { |
| Eli Friedman | 24af850 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 2018 | if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf(receiverNameLoc)) { |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2019 | if (auto classDecl = CurMethod->getClassInterface()) { |
| 2020 | SuperType = QualType(classDecl->getSuperClassType(), 0); |
| Fariborz Jahanian | 86dd456 | 2015-01-20 16:53:34 +0000 | [diff] [blame] | 2021 | if (CurMethod->isInstanceMethod()) { |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2022 | if (SuperType.isNull()) { |
| Fariborz Jahanian | 86dd456 | 2015-01-20 16:53:34 +0000 | [diff] [blame] | 2023 | // The current class does not have a superclass. |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 2024 | Diag(receiverNameLoc, diag::err_root_class_cannot_use_super) |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2025 | << CurMethod->getClassInterface()->getIdentifier(); |
| Fariborz Jahanian | 86dd456 | 2015-01-20 16:53:34 +0000 | [diff] [blame] | 2026 | return ExprError(); |
| 2027 | } |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2028 | QualType T = Context.getObjCObjectPointerType(SuperType); |
| Fariborz Jahanian | 86dd456 | 2015-01-20 16:53:34 +0000 | [diff] [blame] | 2029 | |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2030 | return HandleExprPropertyRefExpr(T->castAs<ObjCObjectPointerType>(), |
| Fariborz Jahanian | 86dd456 | 2015-01-20 16:53:34 +0000 | [diff] [blame] | 2031 | /*BaseExpr*/nullptr, |
| 2032 | SourceLocation()/*OpLoc*/, |
| 2033 | &propertyName, |
| 2034 | propertyNameLoc, |
| 2035 | receiverNameLoc, T, true); |
| Fariborz Jahanian | 05e2aaa | 2013-03-11 22:26:33 +0000 | [diff] [blame] | 2036 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2037 | |
| Fariborz Jahanian | 86dd456 | 2015-01-20 16:53:34 +0000 | [diff] [blame] | 2038 | // Otherwise, if this is a class method, try dispatching to our |
| 2039 | // superclass. |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2040 | IFace = CurMethod->getClassInterface()->getSuperClass(); |
| Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2041 | } |
| Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2042 | } |
| John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 2043 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2044 | |
| 2045 | if (!IFace) { |
| Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 2046 | Diag(receiverNameLoc, diag::err_expected_either) << tok::identifier |
| 2047 | << tok::l_paren; |
| Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2048 | return ExprError(); |
| 2049 | } |
| 2050 | } |
| 2051 | |
| Saleem Abdulrasool | b3a2d04 | 2017-02-20 23:45:49 +0000 | [diff] [blame] | 2052 | Selector GetterSel; |
| 2053 | Selector SetterSel; |
| 2054 | if (auto PD = IFace->FindPropertyDeclaration( |
| 2055 | &propertyName, ObjCPropertyQueryKind::OBJC_PR_query_class)) { |
| 2056 | GetterSel = PD->getGetterName(); |
| 2057 | SetterSel = PD->getSetterName(); |
| 2058 | } else { |
| 2059 | GetterSel = PP.getSelectorTable().getNullarySelector(&propertyName); |
| 2060 | SetterSel = SelectorTable::constructSetterSelector( |
| 2061 | PP.getIdentifierTable(), PP.getSelectorTable(), &propertyName); |
| 2062 | } |
| 2063 | |
| Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2064 | // Search for a declared property first. |
| Saleem Abdulrasool | b3a2d04 | 2017-02-20 23:45:49 +0000 | [diff] [blame] | 2065 | ObjCMethodDecl *Getter = IFace->lookupClassMethod(GetterSel); |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2066 | |
| 2067 | // If this reference is in an @implementation, check for 'private' methods. |
| 2068 | if (!Getter) |
| Saleem Abdulrasool | b3a2d04 | 2017-02-20 23:45:49 +0000 | [diff] [blame] | 2069 | Getter = IFace->lookupPrivateClassMethod(GetterSel); |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2070 | |
| 2071 | if (Getter) { |
| 2072 | // FIXME: refactor/share with ActOnMemberReference(). |
| 2073 | // Check if we can reference this property. |
| 2074 | if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) |
| 2075 | return ExprError(); |
| 2076 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2077 | |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2078 | // Look for the matching setter, in case it is needed. |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2079 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2080 | if (!Setter) { |
| 2081 | // If this reference is in an @implementation, also check for 'private' |
| 2082 | // methods. |
| Fariborz Jahanian | 29cdbc6 | 2014-04-21 20:22:17 +0000 | [diff] [blame] | 2083 | Setter = IFace->lookupPrivateClassMethod(SetterSel); |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2084 | } |
| 2085 | // Look through local category implementations associated with the class. |
| Argyrios Kyrtzidis | 1559d67b | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 2086 | if (!Setter) |
| 2087 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2088 | |
| 2089 | if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) |
| 2090 | return ExprError(); |
| 2091 | |
| 2092 | if (Getter || Setter) { |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2093 | if (!SuperType.isNull()) |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2094 | return new (Context) |
| 2095 | ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue, |
| 2096 | OK_ObjCProperty, propertyNameLoc, receiverNameLoc, |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2097 | SuperType); |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2098 | |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2099 | return new (Context) ObjCPropertyRefExpr( |
| 2100 | Getter, Setter, Context.PseudoObjectTy, VK_LValue, OK_ObjCProperty, |
| 2101 | propertyNameLoc, receiverNameLoc, IFace); |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2102 | } |
| 2103 | return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) |
| 2104 | << &propertyName << Context.getObjCInterfaceType(IFace)); |
| 2105 | } |
| 2106 | |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2107 | namespace { |
| 2108 | |
| Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2109 | class ObjCInterfaceOrSuperCCC final : public CorrectionCandidateCallback { |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2110 | public: |
| 2111 | ObjCInterfaceOrSuperCCC(ObjCMethodDecl *Method) { |
| 2112 | // Determine whether "super" is acceptable in the current context. |
| 2113 | if (Method && Method->getClassInterface()) |
| 2114 | WantObjCSuper = Method->getClassInterface()->getSuperClass(); |
| 2115 | } |
| 2116 | |
| Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 2117 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2118 | return candidate.getCorrectionDeclAs<ObjCInterfaceDecl>() || |
| 2119 | candidate.isKeyword("super"); |
| 2120 | } |
| Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2121 | |
| 2122 | std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| 2123 | return llvm::make_unique<ObjCInterfaceOrSuperCCC>(*this); |
| 2124 | } |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2125 | }; |
| 2126 | |
| Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 2127 | } // end anonymous namespace |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2128 | |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2129 | Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S, |
| Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2130 | IdentifierInfo *Name, |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2131 | SourceLocation NameLoc, |
| 2132 | bool IsSuper, |
| Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2133 | bool HasTrailingDot, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2134 | ParsedType &ReceiverType) { |
| David Blaikie | efdccaa | 2016-01-15 23:43:34 +0000 | [diff] [blame] | 2135 | ReceiverType = nullptr; |
| Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2136 | |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2137 | // If the identifier is "super" and there is no trailing dot, we're |
| Douglas Gregor | 57756ea | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 2138 | // messaging super. If the identifier is "super" and there is a |
| 2139 | // trailing dot, it's an instance message. |
| 2140 | if (IsSuper && S->isInObjcMethodScope()) |
| 2141 | return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2142 | |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2143 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| 2144 | LookupName(Result, S); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2145 | |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2146 | switch (Result.getResultKind()) { |
| 2147 | case LookupResult::NotFound: |
| Douglas Gregor | ca7136b | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 2148 | // Normal name lookup didn't find anything. If we're in an |
| 2149 | // Objective-C method, look for ivars. If we find one, we're done! |
| Douglas Gregor | 57756ea | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 2150 | // FIXME: This is a hack. Ivar lookup should be part of normal |
| 2151 | // lookup. |
| Douglas Gregor | ca7136b | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 2152 | if (ObjCMethodDecl *Method = getCurMethodDecl()) { |
| Argyrios Kyrtzidis | 3a8de5b | 2011-11-09 00:22:48 +0000 | [diff] [blame] | 2153 | if (!Method->getClassInterface()) { |
| 2154 | // Fall back: let the parser try to parse it as an instance message. |
| 2155 | return ObjCInstanceMessage; |
| 2156 | } |
| 2157 | |
| Douglas Gregor | ca7136b | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 2158 | ObjCInterfaceDecl *ClassDeclared; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2159 | if (Method->getClassInterface()->lookupInstanceVariable(Name, |
| Douglas Gregor | ca7136b | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 2160 | ClassDeclared)) |
| 2161 | return ObjCInstanceMessage; |
| 2162 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2163 | |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2164 | // Break out; we'll perform typo correction below. |
| 2165 | break; |
| 2166 | |
| 2167 | case LookupResult::NotFoundInCurrentInstantiation: |
| 2168 | case LookupResult::FoundOverloaded: |
| 2169 | case LookupResult::FoundUnresolvedValue: |
| 2170 | case LookupResult::Ambiguous: |
| 2171 | Result.suppressDiagnostics(); |
| 2172 | return ObjCInstanceMessage; |
| 2173 | |
| 2174 | case LookupResult::Found: { |
| Fariborz Jahanian | 14889fc | 2011-02-08 00:23:07 +0000 | [diff] [blame] | 2175 | // If the identifier is a class or not, and there is a trailing dot, |
| 2176 | // it's an instance message. |
| 2177 | if (HasTrailingDot) |
| 2178 | return ObjCInstanceMessage; |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2179 | // We found something. If it's a type, then we have a class |
| 2180 | // message. Otherwise, it's an instance message. |
| 2181 | NamedDecl *ND = Result.getFoundDecl(); |
| Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2182 | QualType T; |
| 2183 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 2184 | T = Context.getObjCInterfaceType(Class); |
| Fariborz Jahanian | 83f1be1 | 2013-04-04 18:45:52 +0000 | [diff] [blame] | 2185 | else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) { |
| Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2186 | T = Context.getTypeDeclType(Type); |
| Fariborz Jahanian | 83f1be1 | 2013-04-04 18:45:52 +0000 | [diff] [blame] | 2187 | DiagnoseUseOfDecl(Type, NameLoc); |
| 2188 | } |
| 2189 | else |
| Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2190 | return ObjCInstanceMessage; |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2191 | |
| Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2192 | // We have a class message, and T is the type we're |
| 2193 | // messaging. Build source-location information for it. |
| 2194 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2195 | ReceiverType = CreateParsedType(T, TSInfo); |
| Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 2196 | return ObjCClassMessage; |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2197 | } |
| 2198 | } |
| 2199 | |
| Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2200 | ObjCInterfaceOrSuperCCC CCC(getCurMethodDecl()); |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2201 | if (TypoCorrection Corrected = CorrectTypo( |
| Bruno Ricci | 70ad396 | 2019-03-25 17:08:51 +0000 | [diff] [blame] | 2202 | Result.getLookupNameInfo(), Result.getLookupKind(), S, nullptr, CCC, |
| Kaelyn Takata | 89c881b | 2014-10-27 18:07:29 +0000 | [diff] [blame] | 2203 | CTK_ErrorRecovery, nullptr, false, nullptr, false)) { |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2204 | if (Corrected.isKeyword()) { |
| 2205 | // If we've found the keyword "super" (the only keyword that would be |
| 2206 | // returned by CorrectTypo), this is a send to super. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2207 | diagnoseTypo(Corrected, |
| 2208 | PDiag(diag::err_unknown_receiver_suggest) << Name); |
| Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 2209 | return ObjCSuperMessage; |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2210 | } else if (ObjCInterfaceDecl *Class = |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2211 | Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2212 | // If we found a declaration, correct when it refers to an Objective-C |
| 2213 | // class. |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2214 | diagnoseTypo(Corrected, |
| 2215 | PDiag(diag::err_unknown_receiver_suggest) << Name); |
| Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 2216 | QualType T = Context.getObjCInterfaceType(Class); |
| 2217 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
| 2218 | ReceiverType = CreateParsedType(T, TSInfo); |
| 2219 | return ObjCClassMessage; |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2220 | } |
| 2221 | } |
| Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 2222 | |
| Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 2223 | // Fall back: let the parser try to parse it as an instance message. |
| 2224 | return ObjCInstanceMessage; |
| 2225 | } |
| Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 2226 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2227 | ExprResult Sema::ActOnSuperMessage(Scope *S, |
| Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2228 | SourceLocation SuperLoc, |
| 2229 | Selector Sel, |
| 2230 | SourceLocation LBracLoc, |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2231 | ArrayRef<SourceLocation> SelectorLocs, |
| Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2232 | SourceLocation RBracLoc, |
| 2233 | MultiExprArg Args) { |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2234 | // Determine whether we are inside a method or not. |
| Eli Friedman | 24af850 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 2235 | ObjCMethodDecl *Method = tryCaptureObjCSelf(SuperLoc); |
| Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 2236 | if (!Method) { |
| 2237 | Diag(SuperLoc, diag::err_invalid_receiver_to_message_super); |
| 2238 | return ExprError(); |
| 2239 | } |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2240 | |
| Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 2241 | ObjCInterfaceDecl *Class = Method->getClassInterface(); |
| 2242 | if (!Class) { |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 2243 | Diag(SuperLoc, diag::err_no_super_class_message) |
| Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 2244 | << Method->getDeclName(); |
| 2245 | return ExprError(); |
| 2246 | } |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2247 | |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2248 | QualType SuperTy(Class->getSuperClassType(), 0); |
| 2249 | if (SuperTy.isNull()) { |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2250 | // The current class does not have a superclass. |
| Richard Smith | f881267 | 2016-12-02 22:38:31 +0000 | [diff] [blame] | 2251 | Diag(SuperLoc, diag::err_root_class_cannot_use_super) |
| Ted Kremenek | 499897b | 2011-01-23 17:21:34 +0000 | [diff] [blame] | 2252 | << Class->getIdentifier(); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2253 | return ExprError(); |
| Chris Lattner | c2ebb03 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 2254 | } |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2255 | |
| Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 2256 | // We are in a method whose class has a superclass, so 'super' |
| 2257 | // is acting as a keyword. |
| Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 2258 | if (Method->getSelector() == Sel) |
| 2259 | getCurFunction()->ObjCShouldCallSuper = false; |
| Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 2260 | |
| Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 2261 | if (Method->isInstanceMethod()) { |
| Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 2262 | // Since we are in an instance method, this is an instance |
| 2263 | // message to the superclass instance. |
| Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 2264 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2265 | return BuildInstanceMessage(nullptr, SuperTy, SuperLoc, |
| 2266 | Sel, /*Method=*/nullptr, |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2267 | LBracLoc, SelectorLocs, RBracLoc, Args); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2268 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2269 | |
| Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 2270 | // Since we are in a class method, this is a class message to |
| 2271 | // the superclass. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2272 | return BuildClassMessage(/*ReceiverTypeInfo=*/nullptr, |
| Douglas Gregor | e83b956 | 2015-07-07 03:57:53 +0000 | [diff] [blame] | 2273 | SuperTy, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2274 | SuperLoc, Sel, /*Method=*/nullptr, |
| Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2275 | LBracLoc, SelectorLocs, RBracLoc, Args); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2278 | ExprResult Sema::BuildClassMessageImplicit(QualType ReceiverType, |
| 2279 | bool isSuperReceiver, |
| 2280 | SourceLocation Loc, |
| 2281 | Selector Sel, |
| 2282 | ObjCMethodDecl *Method, |
| 2283 | MultiExprArg Args) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2284 | TypeSourceInfo *receiverTypeInfo = nullptr; |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2285 | if (!ReceiverType.isNull()) |
| 2286 | receiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType); |
| 2287 | |
| 2288 | return BuildClassMessage(receiverTypeInfo, ReceiverType, |
| 2289 | /*SuperLoc=*/isSuperReceiver ? Loc : SourceLocation(), |
| 2290 | Sel, Method, Loc, Loc, Loc, Args, |
| 2291 | /*isImplicit=*/true); |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2294 | static void applyCocoaAPICheck(Sema &S, const ObjCMessageExpr *Msg, |
| 2295 | unsigned DiagID, |
| 2296 | bool (*refactor)(const ObjCMessageExpr *, |
| 2297 | const NSAPI &, edit::Commit &)) { |
| 2298 | SourceLocation MsgLoc = Msg->getExprLoc(); |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2299 | if (S.Diags.isIgnored(DiagID, MsgLoc)) |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2300 | return; |
| 2301 | |
| 2302 | SourceManager &SM = S.SourceMgr; |
| 2303 | edit::Commit ECommit(SM, S.LangOpts); |
| 2304 | if (refactor(Msg,*S.NSAPIObj, ECommit)) { |
| 2305 | DiagnosticBuilder Builder = S.Diag(MsgLoc, DiagID) |
| 2306 | << Msg->getSelector() << Msg->getSourceRange(); |
| 2307 | // FIXME: Don't emit diagnostic at all if fixits are non-commitable. |
| 2308 | if (!ECommit.isCommitable()) |
| 2309 | return; |
| 2310 | for (edit::Commit::edit_iterator |
| 2311 | I = ECommit.edit_begin(), E = ECommit.edit_end(); I != E; ++I) { |
| 2312 | const edit::Commit::Edit &Edit = *I; |
| 2313 | switch (Edit.Kind) { |
| 2314 | case edit::Commit::Act_Insert: |
| 2315 | Builder.AddFixItHint(FixItHint::CreateInsertion(Edit.OrigLoc, |
| 2316 | Edit.Text, |
| 2317 | Edit.BeforePrev)); |
| 2318 | break; |
| 2319 | case edit::Commit::Act_InsertFromRange: |
| 2320 | Builder.AddFixItHint( |
| 2321 | FixItHint::CreateInsertionFromRange(Edit.OrigLoc, |
| 2322 | Edit.getInsertFromRange(SM), |
| 2323 | Edit.BeforePrev)); |
| 2324 | break; |
| 2325 | case edit::Commit::Act_Remove: |
| 2326 | Builder.AddFixItHint(FixItHint::CreateRemoval(Edit.getFileRange(SM))); |
| 2327 | break; |
| 2328 | } |
| 2329 | } |
| 2330 | } |
| 2331 | } |
| 2332 | |
| 2333 | static void checkCocoaAPI(Sema &S, const ObjCMessageExpr *Msg) { |
| 2334 | applyCocoaAPICheck(S, Msg, diag::warn_objc_redundant_literal_use, |
| 2335 | edit::rewriteObjCRedundantCallWithLiteral); |
| 2336 | } |
| 2337 | |
| Alex Lorenz | 0e23c61 | 2017-03-06 15:58:34 +0000 | [diff] [blame] | 2338 | static void checkFoundationAPI(Sema &S, SourceLocation Loc, |
| 2339 | const ObjCMethodDecl *Method, |
| 2340 | ArrayRef<Expr *> Args, QualType ReceiverType, |
| 2341 | bool IsClassObjectCall) { |
| 2342 | // Check if this is a performSelector method that uses a selector that returns |
| 2343 | // a record or a vector type. |
| Alex Lorenz | 5ffe4e1 | 2017-03-23 10:46:05 +0000 | [diff] [blame] | 2344 | if (Method->getSelector().getMethodFamily() != OMF_performSelector || |
| 2345 | Args.empty()) |
| Alex Lorenz | 0e23c61 | 2017-03-06 15:58:34 +0000 | [diff] [blame] | 2346 | return; |
| 2347 | const auto *SE = dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens()); |
| 2348 | if (!SE) |
| 2349 | return; |
| 2350 | ObjCMethodDecl *ImpliedMethod; |
| 2351 | if (!IsClassObjectCall) { |
| 2352 | const auto *OPT = ReceiverType->getAs<ObjCObjectPointerType>(); |
| 2353 | if (!OPT || !OPT->getInterfaceDecl()) |
| 2354 | return; |
| 2355 | ImpliedMethod = |
| 2356 | OPT->getInterfaceDecl()->lookupInstanceMethod(SE->getSelector()); |
| 2357 | if (!ImpliedMethod) |
| 2358 | ImpliedMethod = |
| 2359 | OPT->getInterfaceDecl()->lookupPrivateMethod(SE->getSelector()); |
| 2360 | } else { |
| 2361 | const auto *IT = ReceiverType->getAs<ObjCInterfaceType>(); |
| 2362 | if (!IT) |
| 2363 | return; |
| 2364 | ImpliedMethod = IT->getDecl()->lookupClassMethod(SE->getSelector()); |
| 2365 | if (!ImpliedMethod) |
| 2366 | ImpliedMethod = |
| 2367 | IT->getDecl()->lookupPrivateClassMethod(SE->getSelector()); |
| 2368 | } |
| 2369 | if (!ImpliedMethod) |
| 2370 | return; |
| 2371 | QualType Ret = ImpliedMethod->getReturnType(); |
| 2372 | if (Ret->isRecordType() || Ret->isVectorType() || Ret->isExtVectorType()) { |
| 2373 | QualType Ret = ImpliedMethod->getReturnType(); |
| 2374 | S.Diag(Loc, diag::warn_objc_unsafe_perform_selector) |
| 2375 | << Method->getSelector() |
| 2376 | << (!Ret->isRecordType() |
| 2377 | ? /*Vector*/ 2 |
| 2378 | : Ret->isUnionType() ? /*Union*/ 1 : /*Struct*/ 0); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2379 | S.Diag(ImpliedMethod->getBeginLoc(), |
| Alex Lorenz | 0e23c61 | 2017-03-06 15:58:34 +0000 | [diff] [blame] | 2380 | diag::note_objc_unsafe_perform_selector_method_declared_here) |
| 2381 | << ImpliedMethod->getSelector() << Ret; |
| 2382 | } |
| 2383 | } |
| 2384 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2385 | /// Diagnose use of %s directive in an NSString which is being passed |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 2386 | /// as formatting string to formatting method. |
| 2387 | static void |
| 2388 | DiagnoseCStringFormatDirectiveInObjCAPI(Sema &S, |
| 2389 | ObjCMethodDecl *Method, |
| 2390 | Selector Sel, |
| 2391 | Expr **Args, unsigned NumArgs) { |
| Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 2392 | unsigned Idx = 0; |
| 2393 | bool Format = false; |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 2394 | ObjCStringFormatFamily SFFamily = Sel.getStringFormatFamily(); |
| 2395 | if (SFFamily == ObjCStringFormatFamily::SFF_NSString) { |
| Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 2396 | Idx = 0; |
| 2397 | Format = true; |
| 2398 | } |
| 2399 | else if (Method) { |
| 2400 | for (const auto *I : Method->specific_attrs<FormatAttr>()) { |
| 2401 | if (S.GetFormatNSStringIdx(I, Idx)) { |
| 2402 | Format = true; |
| 2403 | break; |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 2404 | } |
| 2405 | } |
| 2406 | } |
| Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 2407 | if (!Format || NumArgs <= Idx) |
| 2408 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2409 | |
| Fariborz Jahanian | fba4fe6 | 2014-09-11 19:13:23 +0000 | [diff] [blame] | 2410 | Expr *FormatExpr = Args[Idx]; |
| 2411 | if (ObjCStringLiteral *OSL = |
| 2412 | dyn_cast<ObjCStringLiteral>(FormatExpr->IgnoreParenImpCasts())) { |
| 2413 | StringLiteral *FormatString = OSL->getString(); |
| 2414 | if (S.FormatStringHasSArg(FormatString)) { |
| 2415 | S.Diag(FormatExpr->getExprLoc(), diag::warn_objc_cdirective_format_string) |
| 2416 | << "%s" << 0 << 0; |
| 2417 | if (Method) |
| 2418 | S.Diag(Method->getLocation(), diag::note_method_declared_at) |
| 2419 | << Method->getDeclName(); |
| 2420 | } |
| 2421 | } |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2424 | /// Build an Objective-C class message expression. |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2425 | /// |
| 2426 | /// This routine takes care of both normal class messages and |
| 2427 | /// class messages to the superclass. |
| 2428 | /// |
| 2429 | /// \param ReceiverTypeInfo Type source information that describes the |
| 2430 | /// receiver of this message. This may be NULL, in which case we are |
| 2431 | /// sending to the superclass and \p SuperLoc must be a valid source |
| 2432 | /// location. |
| 2433 | |
| 2434 | /// \param ReceiverType The type of the object receiving the |
| 2435 | /// message. When \p ReceiverTypeInfo is non-NULL, this is the same |
| 2436 | /// type as that refers to. For a superclass send, this is the type of |
| 2437 | /// the superclass. |
| 2438 | /// |
| 2439 | /// \param SuperLoc The location of the "super" keyword in a |
| 2440 | /// superclass message. |
| 2441 | /// |
| 2442 | /// \param Sel The selector to which the message is being sent. |
| 2443 | /// |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2444 | /// \param Method The method that this class message is invoking, if |
| 2445 | /// already known. |
| 2446 | /// |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2447 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 2448 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 2449 | /// \param RBracLoc The location of the closing square bracket ']'. |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2450 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 2451 | /// \param ArgsIn The message arguments. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2452 | ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo, |
| Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2453 | QualType ReceiverType, |
| 2454 | SourceLocation SuperLoc, |
| 2455 | Selector Sel, |
| 2456 | ObjCMethodDecl *Method, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2457 | SourceLocation LBracLoc, |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2458 | ArrayRef<SourceLocation> SelectorLocs, |
| Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2459 | SourceLocation RBracLoc, |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2460 | MultiExprArg ArgsIn, |
| 2461 | bool isImplicit) { |
| Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2462 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc |
| Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2463 | : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin(); |
| Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2464 | if (LBracLoc.isInvalid()) { |
| 2465 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 2466 | << FixItHint::CreateInsertion(Loc, "["); |
| 2467 | LBracLoc = Loc; |
| 2468 | } |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2469 | ArrayRef<SourceLocation> SelectorSlotLocs; |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2470 | if (!SelectorLocs.empty() && SelectorLocs.front().isValid()) |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2471 | SelectorSlotLocs = SelectorLocs; |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2472 | else |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2473 | SelectorSlotLocs = Loc; |
| 2474 | SourceLocation SelLoc = SelectorSlotLocs.front(); |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2475 | |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2476 | if (ReceiverType->isDependentType()) { |
| 2477 | // If the receiver type is dependent, we can't type-check anything |
| 2478 | // at this point. Build a dependent expression. |
| 2479 | unsigned NumArgs = ArgsIn.size(); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2480 | Expr **Args = ArgsIn.data(); |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2481 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2482 | return ObjCMessageExpr::Create( |
| 2483 | Context, ReceiverType, VK_RValue, LBracLoc, ReceiverTypeInfo, Sel, |
| 2484 | SelectorLocs, /*Method=*/nullptr, makeArrayRef(Args, NumArgs), RBracLoc, |
| 2485 | isImplicit); |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2486 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2487 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2488 | // Find the class to which we are sending this message. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2489 | ObjCInterfaceDecl *Class = nullptr; |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2490 | const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>(); |
| 2491 | if (!ClassType || !(Class = ClassType->getInterface())) { |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2492 | Diag(Loc, diag::err_invalid_receiver_class_message) |
| 2493 | << ReceiverType; |
| 2494 | return ExprError(); |
| Steve Naroff | e2177fb | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 2495 | } |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2496 | assert(Class && "We don't know which class we're messaging?"); |
| Fariborz Jahanian | c27cd1b | 2011-10-15 19:18:36 +0000 | [diff] [blame] | 2497 | // objc++ diagnoses during typename annotation. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2498 | if (!getLangOpts().CPlusPlus) |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2499 | (void)DiagnoseUseOfDecl(Class, SelectorSlotLocs); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2500 | // Find the method we are messaging. |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2501 | if (!Method) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2502 | SourceRange TypeRange |
| Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2503 | = SuperLoc.isValid()? SourceRange(SuperLoc) |
| 2504 | : ReceiverTypeInfo->getTypeLoc().getSourceRange(); |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2505 | if (RequireCompleteType(Loc, Context.getObjCInterfaceType(Class), |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2506 | (getLangOpts().ObjCAutoRefCount |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2507 | ? diag::err_arc_receiver_forward_class |
| 2508 | : diag::warn_receiver_forward_class), |
| 2509 | TypeRange)) { |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2510 | // A forward class used in messaging is treated as a 'Class' |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2511 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2512 | SourceRange(LBracLoc, RBracLoc)); |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2513 | if (Method && !getLangOpts().ObjCAutoRefCount) |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2514 | Diag(Method->getLocation(), diag::note_method_sent_forward_class) |
| 2515 | << Method->getDeclName(); |
| 2516 | } |
| 2517 | if (!Method) |
| 2518 | Method = Class->lookupClassMethod(Sel); |
| 2519 | |
| 2520 | // If we have an implementation in scope, check "private" methods. |
| 2521 | if (!Method) |
| Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 2522 | Method = Class->lookupPrivateClassMethod(Sel); |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2523 | |
| Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 2524 | if (Method && DiagnoseUseOfDecl(Method, SelectorSlotLocs, |
| 2525 | nullptr, false, false, Class)) |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2526 | return ExprError(); |
| Fariborz Jahanian | 1bd844d | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 2527 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2528 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2529 | // Check the argument types and determine the result type. |
| 2530 | QualType ReturnType; |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2531 | ExprValueKind VK = VK_RValue; |
| 2532 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2533 | unsigned NumArgs = ArgsIn.size(); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2534 | Expr **Args = ArgsIn.data(); |
| Alex Lorenz | f50d1ac | 2018-12-20 22:11:11 +0000 | [diff] [blame] | 2535 | if (CheckMessageArgumentTypes(/*Receiver=*/nullptr, ReceiverType, |
| 2536 | MultiExprArg(Args, NumArgs), Sel, SelectorLocs, |
| 2537 | Method, true, SuperLoc.isValid(), LBracLoc, |
| 2538 | RBracLoc, SourceRange(), ReturnType, VK)) |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2539 | return ExprError(); |
| Ted Kremenek | a3a37ae | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 2540 | |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2541 | if (Method && !Method->getReturnType()->isVoidType() && |
| 2542 | RequireCompleteType(LBracLoc, Method->getReturnType(), |
| Douglas Gregor | aec93c6 | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 2543 | diag::err_illegal_message_expr_incomplete_type)) |
| 2544 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2545 | |
| Fariborz Jahanian | e8b4550 | 2014-08-22 19:52:49 +0000 | [diff] [blame] | 2546 | // Warn about explicit call of +initialize on its own class. But not on 'super'. |
| Fariborz Jahanian | 4229228 | 2014-08-25 21:27:38 +0000 | [diff] [blame] | 2547 | if (Method && Method->getMethodFamily() == OMF_initialize) { |
| 2548 | if (!SuperLoc.isValid()) { |
| 2549 | const ObjCInterfaceDecl *ID = |
| 2550 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext()); |
| 2551 | if (ID == Class) { |
| 2552 | Diag(Loc, diag::warn_direct_initialize_call); |
| 2553 | Diag(Method->getLocation(), diag::note_method_declared_at) |
| 2554 | << Method->getDeclName(); |
| 2555 | } |
| 2556 | } |
| 2557 | else if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 2558 | // [super initialize] is allowed only within an +initialize implementation |
| 2559 | if (CurMeth->getMethodFamily() != OMF_initialize) { |
| 2560 | Diag(Loc, diag::warn_direct_super_initialize_call); |
| 2561 | Diag(Method->getLocation(), diag::note_method_declared_at) |
| 2562 | << Method->getDeclName(); |
| 2563 | Diag(CurMeth->getLocation(), diag::note_method_declared_at) |
| 2564 | << CurMeth->getDeclName(); |
| 2565 | } |
| Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2568 | |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 2569 | DiagnoseCStringFormatDirectiveInObjCAPI(*this, Method, Sel, Args, NumArgs); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2570 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2571 | // Construct the appropriate ObjCMessageExpr. |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2572 | ObjCMessageExpr *Result; |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2573 | if (SuperLoc.isValid()) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2574 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
| 2575 | SuperLoc, /*IsInstanceSuper=*/false, |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2576 | ReceiverType, Sel, SelectorLocs, |
| Argyrios Kyrtzidis | 59ad1e3 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2577 | Method, makeArrayRef(Args, NumArgs), |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2578 | RBracLoc, isImplicit); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2579 | else { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2580 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2581 | ReceiverTypeInfo, Sel, SelectorLocs, |
| Argyrios Kyrtzidis | 59ad1e3 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2582 | Method, makeArrayRef(Args, NumArgs), |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2583 | RBracLoc, isImplicit); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2584 | if (!isImplicit) |
| 2585 | checkCocoaAPI(*this, Result); |
| 2586 | } |
| Alex Lorenz | 0e23c61 | 2017-03-06 15:58:34 +0000 | [diff] [blame] | 2587 | if (Method) |
| 2588 | checkFoundationAPI(*this, SelLoc, Method, makeArrayRef(Args, NumArgs), |
| 2589 | ReceiverType, /*IsClassObjectCall=*/true); |
| Douglas Gregor | aae38d6 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2590 | return MaybeBindToTemporary(Result); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2593 | // ActOnClassMessage - used for both unary and keyword messages. |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2594 | // ArgExprs is optional - if it is present, the number of expressions |
| 2595 | // is obtained from Sel.getNumArgs(). |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2596 | ExprResult Sema::ActOnClassMessage(Scope *S, |
| Douglas Gregor | 3e97200 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 2597 | ParsedType Receiver, |
| 2598 | Selector Sel, |
| 2599 | SourceLocation LBracLoc, |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2600 | ArrayRef<SourceLocation> SelectorLocs, |
| Douglas Gregor | 3e97200 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 2601 | SourceLocation RBracLoc, |
| 2602 | MultiExprArg Args) { |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2603 | TypeSourceInfo *ReceiverTypeInfo; |
| 2604 | QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo); |
| 2605 | if (ReceiverType.isNull()) |
| 2606 | return ExprError(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2607 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2608 | if (!ReceiverTypeInfo) |
| 2609 | ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc); |
| 2610 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2611 | return BuildClassMessage(ReceiverTypeInfo, ReceiverType, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2612 | /*SuperLoc=*/SourceLocation(), Sel, |
| 2613 | /*Method=*/nullptr, LBracLoc, SelectorLocs, RBracLoc, |
| 2614 | Args); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2617 | ExprResult Sema::BuildInstanceMessageImplicit(Expr *Receiver, |
| 2618 | QualType ReceiverType, |
| 2619 | SourceLocation Loc, |
| 2620 | Selector Sel, |
| 2621 | ObjCMethodDecl *Method, |
| 2622 | MultiExprArg Args) { |
| 2623 | return BuildInstanceMessage(Receiver, ReceiverType, |
| 2624 | /*SuperLoc=*/!Receiver ? Loc : SourceLocation(), |
| 2625 | Sel, Method, Loc, Loc, Loc, Args, |
| 2626 | /*isImplicit=*/true); |
| 2627 | } |
| 2628 | |
| Alex Lorenz | 5e895cf | 2017-03-15 17:16:41 +0000 | [diff] [blame] | 2629 | static bool isMethodDeclaredInRootProtocol(Sema &S, const ObjCMethodDecl *M) { |
| 2630 | if (!S.NSAPIObj) |
| 2631 | return false; |
| 2632 | const auto *Protocol = dyn_cast<ObjCProtocolDecl>(M->getDeclContext()); |
| 2633 | if (!Protocol) |
| 2634 | return false; |
| 2635 | const IdentifierInfo *II = S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject); |
| 2636 | if (const auto *RootClass = dyn_cast_or_null<ObjCInterfaceDecl>( |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2637 | S.LookupSingleName(S.TUScope, II, Protocol->getBeginLoc(), |
| Alex Lorenz | 5e895cf | 2017-03-15 17:16:41 +0000 | [diff] [blame] | 2638 | Sema::LookupOrdinaryName))) { |
| 2639 | for (const ObjCProtocolDecl *P : RootClass->all_referenced_protocols()) { |
| 2640 | if (P->getCanonicalDecl() == Protocol->getCanonicalDecl()) |
| 2641 | return true; |
| 2642 | } |
| 2643 | } |
| 2644 | return false; |
| 2645 | } |
| 2646 | |
| Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 2647 | /// Build an Objective-C instance message expression. |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2648 | /// |
| 2649 | /// This routine takes care of both normal instance messages and |
| 2650 | /// instance messages to the superclass instance. |
| 2651 | /// |
| 2652 | /// \param Receiver The expression that computes the object that will |
| 2653 | /// receive this message. This may be empty, in which case we are |
| 2654 | /// sending to the superclass instance and \p SuperLoc must be a valid |
| 2655 | /// source location. |
| 2656 | /// |
| 2657 | /// \param ReceiverType The (static) type of the object receiving the |
| 2658 | /// message. When a \p Receiver expression is provided, this is the |
| 2659 | /// same type as that expression. For a superclass instance send, this |
| 2660 | /// is a pointer to the type of the superclass. |
| 2661 | /// |
| 2662 | /// \param SuperLoc The location of the "super" keyword in a |
| 2663 | /// superclass instance message. |
| 2664 | /// |
| 2665 | /// \param Sel The selector to which the message is being sent. |
| 2666 | /// |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2667 | /// \param Method The method that this instance message is invoking, if |
| 2668 | /// already known. |
| 2669 | /// |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2670 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 2671 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 2672 | /// \param RBracLoc The location of the closing square bracket ']'. |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2673 | /// |
| James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 2674 | /// \param ArgsIn The message arguments. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2675 | ExprResult Sema::BuildInstanceMessage(Expr *Receiver, |
| Argyrios Kyrtzidis | d0039e5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2676 | QualType ReceiverType, |
| 2677 | SourceLocation SuperLoc, |
| 2678 | Selector Sel, |
| 2679 | ObjCMethodDecl *Method, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2680 | SourceLocation LBracLoc, |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2681 | ArrayRef<SourceLocation> SelectorLocs, |
| Argyrios Kyrtzidis | d0039e5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2682 | SourceLocation RBracLoc, |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2683 | MultiExprArg ArgsIn, |
| 2684 | bool isImplicit) { |
| Chandler Carruth | 3d40284 | 2016-11-04 06:11:54 +0000 | [diff] [blame] | 2685 | assert((Receiver || SuperLoc.isValid()) && "If the Receiver is null, the " |
| 2686 | "SuperLoc must be valid so we can " |
| 2687 | "use it instead."); |
| 2688 | |
| Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2689 | // The location of the receiver. |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2690 | SourceLocation Loc = SuperLoc.isValid() ? SuperLoc : Receiver->getBeginLoc(); |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2691 | SourceRange RecRange = |
| 2692 | SuperLoc.isValid()? SuperLoc : Receiver->getSourceRange(); |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2693 | ArrayRef<SourceLocation> SelectorSlotLocs; |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2694 | if (!SelectorLocs.empty() && SelectorLocs.front().isValid()) |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2695 | SelectorSlotLocs = SelectorLocs; |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2696 | else |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2697 | SelectorSlotLocs = Loc; |
| 2698 | SourceLocation SelLoc = SelectorSlotLocs.front(); |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2699 | |
| Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2700 | if (LBracLoc.isInvalid()) { |
| 2701 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 2702 | << FixItHint::CreateInsertion(Loc, "["); |
| 2703 | LBracLoc = Loc; |
| 2704 | } |
| 2705 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2706 | // If we have a receiver expression, perform appropriate promotions |
| 2707 | // and determine receiver type. |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2708 | if (Receiver) { |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2709 | if (Receiver->hasPlaceholderType()) { |
| Douglas Gregor | d8fb1e3 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 2710 | ExprResult Result; |
| 2711 | if (Receiver->getType() == Context.UnknownAnyTy) |
| 2712 | Result = forceUnknownAnyToType(Receiver, Context.getObjCIdType()); |
| 2713 | else |
| 2714 | Result = CheckPlaceholderExpr(Receiver); |
| 2715 | if (Result.isInvalid()) return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2716 | Receiver = Result.get(); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2719 | if (Receiver->isTypeDependent()) { |
| 2720 | // If the receiver is type-dependent, we can't type-check anything |
| 2721 | // at this point. Build a dependent expression. |
| 2722 | unsigned NumArgs = ArgsIn.size(); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2723 | Expr **Args = ArgsIn.data(); |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2724 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2725 | return ObjCMessageExpr::Create( |
| 2726 | Context, Context.DependentTy, VK_RValue, LBracLoc, Receiver, Sel, |
| 2727 | SelectorLocs, /*Method=*/nullptr, makeArrayRef(Args, NumArgs), |
| 2728 | RBracLoc, isImplicit); |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2729 | } |
| 2730 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2731 | // If necessary, apply function/array conversion to the receiver. |
| 2732 | // C99 6.7.5.3p[7,8]. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2733 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver); |
| 2734 | if (Result.isInvalid()) |
| 2735 | return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2736 | Receiver = Result.get(); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2737 | ReceiverType = Receiver->getType(); |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2738 | |
| 2739 | // If the receiver is an ObjC pointer, a block pointer, or an |
| 2740 | // __attribute__((NSObject)) pointer, we don't need to do any |
| 2741 | // special conversion in order to look up a receiver. |
| 2742 | if (ReceiverType->isObjCRetainableType()) { |
| 2743 | // do nothing |
| 2744 | } else if (!getLangOpts().ObjCAutoRefCount && |
| 2745 | !Context.getObjCIdType().isNull() && |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2746 | (ReceiverType->isPointerType() || |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2747 | ReceiverType->isIntegerType())) { |
| 2748 | // Implicitly convert integers and pointers to 'id' but emit a warning. |
| 2749 | // But not in ARC. |
| 2750 | Diag(Loc, diag::warn_bad_receiver_type) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2751 | << ReceiverType |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2752 | << Receiver->getSourceRange(); |
| 2753 | if (ReceiverType->isPointerType()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2754 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2755 | CK_CPointerToObjCPointerCast).get(); |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2756 | } else { |
| 2757 | // TODO: specialized warning on null receivers? |
| 2758 | bool IsNull = Receiver->isNullPointerConstant(Context, |
| 2759 | Expr::NPC_ValueDependentIsNull); |
| 2760 | CastKind Kind = IsNull ? CK_NullToPointer : CK_IntegralToPointer; |
| 2761 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2762 | Kind).get(); |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2763 | } |
| 2764 | ReceiverType = Receiver->getType(); |
| 2765 | } else if (getLangOpts().CPlusPlus) { |
| Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 2766 | // The receiver must be a complete type. |
| 2767 | if (RequireCompleteType(Loc, Receiver->getType(), |
| 2768 | diag::err_incomplete_receiver_type)) |
| 2769 | return ExprError(); |
| 2770 | |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2771 | ExprResult result = PerformContextuallyConvertToObjCPointer(Receiver); |
| 2772 | if (result.isUsable()) { |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2773 | Receiver = result.get(); |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2774 | ReceiverType = Receiver->getType(); |
| 2775 | } |
| 2776 | } |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
| Alex Lorenz | d9f1284 | 2017-08-25 16:12:17 +0000 | [diff] [blame] | 2779 | if (ReceiverType->isObjCIdType() && !isImplicit) |
| 2780 | Diag(Receiver->getExprLoc(), diag::warn_messaging_unqualified_id); |
| 2781 | |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2782 | // There's a somewhat weird interaction here where we assume that we |
| 2783 | // won't actually have a method unless we also don't need to do some |
| 2784 | // of the more detailed type-checking on the receiver. |
| 2785 | |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2786 | if (!Method) { |
| Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2787 | // Handle messages to id and __kindof types (where we use the |
| 2788 | // global method pool). |
| Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2789 | const ObjCObjectType *typeBound = nullptr; |
| 2790 | bool receiverIsIdLike = ReceiverType->isObjCIdOrObjectKindOfType(Context, |
| 2791 | typeBound); |
| 2792 | if (receiverIsIdLike || ReceiverType->isBlockPointerType() || |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2793 | (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) { |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2794 | SmallVector<ObjCMethodDecl*, 4> Methods; |
| Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 2795 | // If we have a type bound, further filter the methods. |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2796 | CollectMultipleMethodsInGlobalPool(Sel, Methods, true/*InstanceFirst*/, |
| Manman Ren | 7ed4f98 | 2016-04-07 19:32:24 +0000 | [diff] [blame] | 2797 | true/*CheckTheOther*/, typeBound); |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2798 | if (!Methods.empty()) { |
| George Burgess IV | 52d07de | 2016-09-01 01:26:58 +0000 | [diff] [blame] | 2799 | // We choose the first method as the initial candidate, then try to |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2800 | // select a better one. |
| 2801 | Method = Methods[0]; |
| 2802 | |
| Fariborz Jahanian | 0ded424 | 2014-08-13 21:24:14 +0000 | [diff] [blame] | 2803 | if (ObjCMethodDecl *BestMethod = |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2804 | SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(), Methods)) |
| Fariborz Jahanian | 0ded424 | 2014-08-13 21:24:14 +0000 | [diff] [blame] | 2805 | Method = BestMethod; |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2806 | |
| Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 2807 | if (!AreMultipleMethodsInGlobalPool(Sel, Method, |
| 2808 | SourceRange(LBracLoc, RBracLoc), |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2809 | receiverIsIdLike, Methods)) |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2810 | DiagnoseUseOfDecl(Method, SelectorSlotLocs); |
| Fariborz Jahanian | 05e77f8 | 2014-11-07 23:51:15 +0000 | [diff] [blame] | 2811 | } |
| Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2812 | } else if (ReceiverType->isObjCClassOrClassKindOfType() || |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2813 | ReceiverType->isObjCQualifiedClassType()) { |
| 2814 | // Handle messages to Class. |
| Nico Weber | 2e0c8f7 | 2014-12-27 03:58:08 +0000 | [diff] [blame] | 2815 | // We allow sending a message to a qualified Class ("Class<foo>"), which |
| 2816 | // is ok as long as one of the protocols implements the selector (if not, |
| 2817 | // warn). |
| Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 2818 | if (!ReceiverType->isObjCClassOrClassKindOfType()) { |
| 2819 | const ObjCObjectPointerType *QClassTy |
| 2820 | = ReceiverType->getAsObjCQualifiedClassType(); |
| Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2821 | // Search protocols for class methods. |
| 2822 | Method = LookupMethodInQualifiedType(Sel, QClassTy, false); |
| 2823 | if (!Method) { |
| 2824 | Method = LookupMethodInQualifiedType(Sel, QClassTy, true); |
| 2825 | // warn if instance method found for a Class message. |
| Alex Lorenz | 5e895cf | 2017-03-15 17:16:41 +0000 | [diff] [blame] | 2826 | if (Method && !isMethodDeclaredInRootProtocol(*this, Method)) { |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2827 | Diag(SelLoc, diag::warn_instance_method_on_class_found) |
| Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2828 | << Method->getSelector() << Sel; |
| Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2829 | Diag(Method->getLocation(), diag::note_method_declared_at) |
| 2830 | << Method->getDeclName(); |
| Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2831 | } |
| Steve Naroff | 3f49fee | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 2832 | } |
| Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2833 | } else { |
| 2834 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 2835 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { |
| Erik Pilkington | 3062887 | 2019-02-04 23:30:57 +0000 | [diff] [blame] | 2836 | // As a guess, try looking for the method in the current interface. |
| 2837 | // This very well may not produce the "right" method. |
| Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 2838 | |
| Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2839 | // First check the public methods in the class interface. |
| 2840 | Method = ClassDecl->lookupClassMethod(Sel); |
| 2841 | |
| 2842 | if (!Method) |
| Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 2843 | Method = ClassDecl->lookupPrivateClassMethod(Sel); |
| Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 2844 | |
| Erik Pilkington | 3062887 | 2019-02-04 23:30:57 +0000 | [diff] [blame] | 2845 | if (Method && DiagnoseUseOfDecl(Method, SelectorSlotLocs)) |
| Erik Pilkington | 4257857 | 2018-09-10 22:20:09 +0000 | [diff] [blame] | 2846 | return ExprError(); |
| Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2847 | } |
| Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2848 | } |
| 2849 | if (!Method) { |
| 2850 | // If not messaging 'self', look for any factory method named 'Sel'. |
| Douglas Gregor | 486b74e | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 2851 | if (!Receiver || !isSelfExpr(Receiver)) { |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2852 | // If no class (factory) method was found, check if an _instance_ |
| 2853 | // method of the same name exists in the root class only. |
| 2854 | SmallVector<ObjCMethodDecl*, 4> Methods; |
| 2855 | CollectMultipleMethodsInGlobalPool(Sel, Methods, |
| 2856 | false/*InstanceFirst*/, |
| 2857 | true/*CheckTheOther*/); |
| 2858 | if (!Methods.empty()) { |
| George Burgess IV | 52d07de | 2016-09-01 01:26:58 +0000 | [diff] [blame] | 2859 | // We choose the first method as the initial candidate, then try |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2860 | // to select a better one. |
| 2861 | Method = Methods[0]; |
| 2862 | |
| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 2863 | // If we find an instance method, emit warning. |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2864 | if (Method->isInstanceMethod()) { |
| 2865 | if (const ObjCInterfaceDecl *ID = |
| 2866 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { |
| 2867 | if (ID->getSuperClass()) |
| 2868 | Diag(SelLoc, diag::warn_root_inst_method_not_found) |
| 2869 | << Sel << SourceRange(LBracLoc, RBracLoc); |
| 2870 | } |
| 2871 | } |
| 2872 | |
| 2873 | if (ObjCMethodDecl *BestMethod = |
| 2874 | SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(), |
| 2875 | Methods)) |
| 2876 | Method = BestMethod; |
| Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2877 | } |
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2878 | } |
| 2879 | } |
| 2880 | } |
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2881 | } else { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2882 | ObjCInterfaceDecl *ClassDecl = nullptr; |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2883 | |
| 2884 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 2885 | // long as one of the protocols implements the selector (if not, warn). |
| Fariborz Jahanian | a245f19 | 2012-06-23 18:39:57 +0000 | [diff] [blame] | 2886 | // And as long as message is not deprecated/unavailable (warn if it is). |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2887 | if (const ObjCObjectPointerType *QIdTy |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2888 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 2889 | // Search protocols for instance methods. |
| Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2890 | Method = LookupMethodInQualifiedType(Sel, QIdTy, true); |
| 2891 | if (!Method) |
| 2892 | Method = LookupMethodInQualifiedType(Sel, QIdTy, false); |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2893 | if (Method && DiagnoseUseOfDecl(Method, SelectorSlotLocs)) |
| Fariborz Jahanian | a245f19 | 2012-06-23 18:39:57 +0000 | [diff] [blame] | 2894 | return ExprError(); |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2895 | } else if (const ObjCObjectPointerType *OCIType |
| 2896 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 2897 | // We allow sending a message to a pointer to an interface (an object). |
| 2898 | ClassDecl = OCIType->getInterfaceDecl(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2899 | |
| Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2900 | // Try to complete the type. Under ARC, this is a hard error from which |
| 2901 | // we don't try to recover. |
| Richard Smith | db0ac55 | 2015-12-18 22:40:25 +0000 | [diff] [blame] | 2902 | // FIXME: In the non-ARC case, this will still be a hard error if the |
| 2903 | // definition is found in a module that's not visible. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2904 | const ObjCInterfaceDecl *forwardClass = nullptr; |
| Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2905 | if (RequireCompleteType(Loc, OCIType->getPointeeType(), |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2906 | getLangOpts().ObjCAutoRefCount |
| Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2907 | ? diag::err_arc_receiver_forward_instance |
| 2908 | : diag::warn_receiver_forward_instance, |
| 2909 | Receiver? Receiver->getSourceRange() |
| 2910 | : SourceRange(SuperLoc))) { |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2911 | if (getLangOpts().ObjCAutoRefCount) |
| Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2912 | return ExprError(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2913 | |
| Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2914 | forwardClass = OCIType->getInterfaceDecl(); |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2915 | Diag(Receiver ? Receiver->getBeginLoc() : SuperLoc, |
| 2916 | diag::note_receiver_is_id); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2917 | Method = nullptr; |
| Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2918 | } else { |
| 2919 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2920 | } |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2921 | |
| Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2922 | if (!Method) |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2923 | // Search protocol qualifiers. |
| Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2924 | Method = LookupMethodInQualifiedType(Sel, OCIType, true); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 2925 | |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2926 | if (!Method) { |
| 2927 | // If we have implementations in scope, check "private" methods. |
| Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 2928 | Method = ClassDecl->lookupPrivateMethod(Sel); |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2929 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2930 | if (!Method && getLangOpts().ObjCAutoRefCount) { |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2931 | Diag(SelLoc, diag::err_arc_may_not_respond) |
| 2932 | << OCIType->getPointeeType() << Sel << RecRange |
| Fariborz Jahanian | 32c1350 | 2012-11-28 01:27:44 +0000 | [diff] [blame] | 2933 | << SourceRange(SelectorLocs.front(), SelectorLocs.back()); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2934 | return ExprError(); |
| 2935 | } |
| 2936 | |
| Douglas Gregor | 486b74e | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 2937 | if (!Method && (!Receiver || !isSelfExpr(Receiver))) { |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2938 | // If we still haven't found a method, look in the global pool. This |
| 2939 | // behavior isn't very desirable, however we need it for GCC |
| 2940 | // compatibility. FIXME: should we deviate?? |
| 2941 | if (OCIType->qual_empty()) { |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2942 | SmallVector<ObjCMethodDecl*, 4> Methods; |
| 2943 | CollectMultipleMethodsInGlobalPool(Sel, Methods, |
| 2944 | true/*InstanceFirst*/, |
| 2945 | false/*CheckTheOther*/); |
| 2946 | if (!Methods.empty()) { |
| George Burgess IV | 52d07de | 2016-09-01 01:26:58 +0000 | [diff] [blame] | 2947 | // We choose the first method as the initial candidate, then try |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2948 | // to select a better one. |
| 2949 | Method = Methods[0]; |
| 2950 | |
| 2951 | if (ObjCMethodDecl *BestMethod = |
| 2952 | SelectBestMethod(Sel, ArgsIn, Method->isInstanceMethod(), |
| 2953 | Methods)) |
| Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 2954 | Method = BestMethod; |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2955 | |
| Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 2956 | AreMultipleMethodsInGlobalPool(Sel, Method, |
| 2957 | SourceRange(LBracLoc, RBracLoc), |
| Manman Ren | d2a3cd7 | 2016-04-07 19:30:20 +0000 | [diff] [blame] | 2958 | true/*receiverIdOrClass*/, |
| 2959 | Methods); |
| Fariborz Jahanian | 890803f | 2015-04-15 17:26:21 +0000 | [diff] [blame] | 2960 | } |
| Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 2961 | if (Method && !forwardClass) |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2962 | Diag(SelLoc, diag::warn_maynot_respond) |
| 2963 | << OCIType->getInterfaceDecl()->getIdentifier() |
| 2964 | << Sel << RecRange; |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2965 | } |
| 2966 | } |
| 2967 | } |
| Volodymyr Sapsai | 7d89ce9 | 2018-03-29 17:34:09 +0000 | [diff] [blame] | 2968 | if (Method && DiagnoseUseOfDecl(Method, SelectorSlotLocs, forwardClass)) |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2969 | return ExprError(); |
| John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 2970 | } else { |
| John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2971 | // Reject other random receiver types (e.g. structs). |
| 2972 | Diag(Loc, diag::err_bad_receiver_type) |
| 2973 | << ReceiverType << Receiver->getSourceRange(); |
| 2974 | return ExprError(); |
| Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2975 | } |
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2976 | } |
| Chris Lattner | 6b946cc | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 2977 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2978 | |
| Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 2979 | FunctionScopeInfo *DIFunctionScopeInfo = |
| 2980 | (Method && Method->getMethodFamily() == OMF_init) |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2981 | ? getEnclosingFunction() : nullptr; |
| 2982 | |
| Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 2983 | if (DIFunctionScopeInfo && |
| 2984 | DIFunctionScopeInfo->ObjCIsDesignatedInit && |
| Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 2985 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
| 2986 | bool isDesignatedInitChain = false; |
| 2987 | if (SuperLoc.isValid()) { |
| 2988 | if (const ObjCObjectPointerType * |
| 2989 | OCIType = ReceiverType->getAsObjCInterfacePointerType()) { |
| 2990 | if (const ObjCInterfaceDecl *ID = OCIType->getInterfaceDecl()) { |
| Argyrios Kyrtzidis | d664a34 | 2013-12-13 03:48:17 +0000 | [diff] [blame] | 2991 | // Either we know this is a designated initializer or we |
| 2992 | // conservatively assume it because we don't know for sure. |
| 2993 | if (!ID->declaresOrInheritsDesignatedInitializers() || |
| 2994 | ID->isDesignatedInitializer(Sel)) { |
| Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 2995 | isDesignatedInitChain = true; |
| Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 2996 | DIFunctionScopeInfo->ObjCWarnForNoDesignatedInitChain = false; |
| Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 2997 | } |
| 2998 | } |
| Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 2999 | } |
| 3000 | } |
| Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 3001 | if (!isDesignatedInitChain) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3002 | const ObjCMethodDecl *InitMethod = nullptr; |
| Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 3003 | bool isDesignated = |
| 3004 | getCurMethodDecl()->isDesignatedInitializerForTheInterface(&InitMethod); |
| 3005 | assert(isDesignated && InitMethod); |
| 3006 | (void)isDesignated; |
| 3007 | Diag(SelLoc, SuperLoc.isValid() ? |
| 3008 | diag::warn_objc_designated_init_non_designated_init_call : |
| 3009 | diag::warn_objc_designated_init_non_super_designated_init_call); |
| 3010 | Diag(InitMethod->getLocation(), |
| 3011 | diag::note_objc_designated_init_marked_here); |
| 3012 | } |
| Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
| Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 3015 | if (DIFunctionScopeInfo && |
| 3016 | DIFunctionScopeInfo->ObjCIsSecondaryInit && |
| Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 3017 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
| 3018 | if (SuperLoc.isValid()) { |
| 3019 | Diag(SelLoc, diag::warn_objc_secondary_init_super_init_call); |
| 3020 | } else { |
| Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 3021 | DIFunctionScopeInfo->ObjCWarnForNoInitDelegation = false; |
| Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 3025 | // Check the message arguments. |
| 3026 | unsigned NumArgs = ArgsIn.size(); |
| Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3027 | Expr **Args = ArgsIn.data(); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 3028 | QualType ReturnType; |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3029 | ExprValueKind VK = VK_RValue; |
| Fariborz Jahanian | 6850091 | 2010-12-01 01:07:24 +0000 | [diff] [blame] | 3030 | bool ClassMessage = (ReceiverType->isObjCClassType() || |
| 3031 | ReceiverType->isObjCQualifiedClassType()); |
| Alex Lorenz | f50d1ac | 2018-12-20 22:11:11 +0000 | [diff] [blame] | 3032 | if (CheckMessageArgumentTypes(Receiver, ReceiverType, |
| 3033 | MultiExprArg(Args, NumArgs), Sel, SelectorLocs, |
| 3034 | Method, ClassMessage, SuperLoc.isValid(), |
| Fariborz Jahanian | 19c2e2f | 2014-08-19 23:39:17 +0000 | [diff] [blame] | 3035 | LBracLoc, RBracLoc, RecRange, ReturnType, VK)) |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 3036 | return ExprError(); |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3037 | |
| 3038 | if (Method && !Method->getReturnType()->isVoidType() && |
| 3039 | RequireCompleteType(LBracLoc, Method->getReturnType(), |
| Douglas Gregor | aec93c6 | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 3040 | diag::err_illegal_message_expr_incomplete_type)) |
| 3041 | return ExprError(); |
| Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3042 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3043 | // In ARC, forbid the user from sending messages to |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3044 | // retain/release/autorelease/dealloc/retainCount explicitly. |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3045 | if (getLangOpts().ObjCAutoRefCount) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3046 | ObjCMethodFamily family = |
| 3047 | (Method ? Method->getMethodFamily() : Sel.getMethodFamily()); |
| 3048 | switch (family) { |
| 3049 | case OMF_init: |
| 3050 | if (Method) |
| 3051 | checkInitMethod(Method, ReceiverType); |
| Adrian Prantl | f3b3ccd | 2017-12-19 22:06:11 +0000 | [diff] [blame] | 3052 | break; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3053 | |
| 3054 | case OMF_None: |
| 3055 | case OMF_alloc: |
| 3056 | case OMF_copy: |
| Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 3057 | case OMF_finalize: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3058 | case OMF_mutableCopy: |
| 3059 | case OMF_new: |
| 3060 | case OMF_self: |
| Fariborz Jahanian | 78e9deb | 2014-08-22 16:57:26 +0000 | [diff] [blame] | 3061 | case OMF_initialize: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3062 | break; |
| 3063 | |
| 3064 | case OMF_dealloc: |
| 3065 | case OMF_retain: |
| 3066 | case OMF_release: |
| 3067 | case OMF_autorelease: |
| 3068 | case OMF_retainCount: |
| Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 3069 | Diag(SelLoc, diag::err_arc_illegal_explicit_message) |
| 3070 | << Sel << RecRange; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3071 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3072 | |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3073 | case OMF_performSelector: |
| 3074 | if (Method && NumArgs >= 1) { |
| Alex Lorenz | 51c0128 | 2017-02-20 17:55:15 +0000 | [diff] [blame] | 3075 | if (const auto *SelExp = |
| 3076 | dyn_cast<ObjCSelectorExpr>(Args[0]->IgnoreParens())) { |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3077 | Selector ArgSel = SelExp->getSelector(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3078 | ObjCMethodDecl *SelMethod = |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3079 | LookupInstanceMethodInGlobalPool(ArgSel, |
| 3080 | SelExp->getSourceRange()); |
| 3081 | if (!SelMethod) |
| 3082 | SelMethod = |
| 3083 | LookupFactoryMethodInGlobalPool(ArgSel, |
| 3084 | SelExp->getSourceRange()); |
| 3085 | if (SelMethod) { |
| 3086 | ObjCMethodFamily SelFamily = SelMethod->getMethodFamily(); |
| 3087 | switch (SelFamily) { |
| 3088 | case OMF_alloc: |
| 3089 | case OMF_copy: |
| 3090 | case OMF_mutableCopy: |
| 3091 | case OMF_new: |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3092 | case OMF_init: |
| 3093 | // Issue error, unless ns_returns_not_retained. |
| 3094 | if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3095 | // selector names a +1 method |
| 3096 | Diag(SelLoc, |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3097 | diag::err_arc_perform_selector_retains); |
| Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 3098 | Diag(SelMethod->getLocation(), diag::note_method_declared_at) |
| 3099 | << SelMethod->getDeclName(); |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3100 | } |
| 3101 | break; |
| 3102 | default: |
| 3103 | // +0 call. OK. unless ns_returns_retained. |
| 3104 | if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) { |
| 3105 | // selector names a +1 method |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3106 | Diag(SelLoc, |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3107 | diag::err_arc_perform_selector_retains); |
| Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 3108 | Diag(SelMethod->getLocation(), diag::note_method_declared_at) |
| 3109 | << SelMethod->getDeclName(); |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3110 | } |
| 3111 | break; |
| 3112 | } |
| 3113 | } |
| 3114 | } else { |
| 3115 | // error (may leak). |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 3116 | Diag(SelLoc, diag::warn_arc_perform_selector_leaks); |
| Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 3117 | Diag(Args[0]->getExprLoc(), diag::note_used_here); |
| 3118 | } |
| 3119 | } |
| 3120 | break; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3121 | } |
| 3122 | } |
| 3123 | |
| Fariborz Jahanian | 6485fe4 | 2014-09-09 23:10:54 +0000 | [diff] [blame] | 3124 | DiagnoseCStringFormatDirectiveInObjCAPI(*this, Method, Sel, Args, NumArgs); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3125 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 3126 | // Construct the appropriate ObjCMessageExpr instance. |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3127 | ObjCMessageExpr *Result; |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 3128 | if (SuperLoc.isValid()) |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3129 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
| Douglas Gregor | aae38d6 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 3130 | SuperLoc, /*IsInstanceSuper=*/true, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3131 | ReceiverType, Sel, SelectorLocs, Method, |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 3132 | makeArrayRef(Args, NumArgs), RBracLoc, |
| 3133 | isImplicit); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3134 | else { |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3135 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 3136 | Receiver, Sel, SelectorLocs, Method, |
| Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 3137 | makeArrayRef(Args, NumArgs), RBracLoc, |
| 3138 | isImplicit); |
| Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3139 | if (!isImplicit) |
| 3140 | checkCocoaAPI(*this, Result); |
| 3141 | } |
| Alex Lorenz | 0e23c61 | 2017-03-06 15:58:34 +0000 | [diff] [blame] | 3142 | if (Method) { |
| 3143 | bool IsClassObjectCall = ClassMessage; |
| 3144 | // 'self' message receivers in class methods should be treated as message |
| 3145 | // sends to the class object in order for the semantic checks to be |
| 3146 | // performed correctly. Messages to 'super' already count as class messages, |
| 3147 | // so they don't need to be handled here. |
| 3148 | if (Receiver && isSelfExpr(Receiver)) { |
| 3149 | if (const auto *OPT = ReceiverType->getAs<ObjCObjectPointerType>()) { |
| 3150 | if (OPT->getObjectType()->isObjCClass()) { |
| 3151 | if (const auto *CurMeth = getCurMethodDecl()) { |
| 3152 | IsClassObjectCall = true; |
| 3153 | ReceiverType = |
| 3154 | Context.getObjCInterfaceType(CurMeth->getClassInterface()); |
| 3155 | } |
| 3156 | } |
| 3157 | } |
| 3158 | } |
| 3159 | checkFoundationAPI(*this, SelLoc, Method, makeArrayRef(Args, NumArgs), |
| 3160 | ReceiverType, IsClassObjectCall); |
| 3161 | } |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3162 | |
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3163 | if (getLangOpts().ObjCAutoRefCount) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3164 | // In ARC, annotate delegate init calls. |
| 3165 | if (Result->getMethodFamily() == OMF_init && |
| Douglas Gregor | 486b74e | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 3166 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3167 | // Only consider init calls *directly* in init implementations, |
| 3168 | // not within blocks. |
| 3169 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext); |
| 3170 | if (method && method->getMethodFamily() == OMF_init) { |
| 3171 | // The implicit assignment to self means we also don't want to |
| 3172 | // consume the result. |
| 3173 | Result->setDelegateInitCall(true); |
| Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 3174 | return Result; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3175 | } |
| 3176 | } |
| 3177 | |
| 3178 | // In ARC, check for message sends which are likely to introduce |
| 3179 | // retain cycles. |
| 3180 | checkRetainCycles(Result); |
| Brian Kelley | cafd912 | 2017-03-29 17:55:11 +0000 | [diff] [blame] | 3181 | } |
| Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 3182 | |
| Brian Kelley | cafd912 | 2017-03-29 17:55:11 +0000 | [diff] [blame] | 3183 | if (getLangOpts().ObjCWeak) { |
| Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 3184 | if (!isImplicit && Method) { |
| 3185 | if (const ObjCPropertyDecl *Prop = Method->findPropertyDecl()) { |
| 3186 | bool IsWeak = |
| 3187 | Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak; |
| 3188 | if (!IsWeak && Sel.isUnarySelector()) |
| 3189 | IsWeak = ReturnType.getObjCLifetime() & Qualifiers::OCL_Weak; |
| Akira Hatanaka | 0a84856 | 2019-01-10 20:12:16 +0000 | [diff] [blame] | 3190 | if (IsWeak && !isUnevaluatedContext() && |
| Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 3191 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, LBracLoc)) |
| 3192 | getCurFunction()->recordUseOfWeak(Result, Prop); |
| Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 3193 | } |
| 3194 | } |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3195 | } |
| Alex Denisov | e1d882c | 2015-03-04 17:55:52 +0000 | [diff] [blame] | 3196 | |
| 3197 | CheckObjCCircularContainer(Result); |
| 3198 | |
| Douglas Gregor | aae38d6 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 3199 | return MaybeBindToTemporary(Result); |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 3202 | static void RemoveSelectorFromWarningCache(Sema &S, Expr* Arg) { |
| 3203 | if (ObjCSelectorExpr *OSE = |
| 3204 | dyn_cast<ObjCSelectorExpr>(Arg->IgnoreParenCasts())) { |
| 3205 | Selector Sel = OSE->getSelector(); |
| 3206 | SourceLocation Loc = OSE->getAtLoc(); |
| Chandler Carruth | 12c8f65 | 2015-03-27 00:55:05 +0000 | [diff] [blame] | 3207 | auto Pos = S.ReferencedSelectors.find(Sel); |
| Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 3208 | if (Pos != S.ReferencedSelectors.end() && Pos->second == Loc) |
| 3209 | S.ReferencedSelectors.erase(Pos); |
| 3210 | } |
| 3211 | } |
| 3212 | |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 3213 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 3214 | // ArgExprs is optional - if it is present, the number of expressions |
| 3215 | // is obtained from Sel.getNumArgs(). |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3216 | ExprResult Sema::ActOnInstanceMessage(Scope *S, |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3217 | Expr *Receiver, |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3218 | Selector Sel, |
| 3219 | SourceLocation LBracLoc, |
| Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 3220 | ArrayRef<SourceLocation> SelectorLocs, |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3221 | SourceLocation RBracLoc, |
| 3222 | MultiExprArg Args) { |
| Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 3223 | if (!Receiver) |
| 3224 | return ExprError(); |
| Argyrios Kyrtzidis | 336cc8b | 2013-02-15 18:34:15 +0000 | [diff] [blame] | 3225 | |
| 3226 | // A ParenListExpr can show up while doing error recovery with invalid code. |
| 3227 | if (isa<ParenListExpr>(Receiver)) { |
| 3228 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Receiver); |
| 3229 | if (Result.isInvalid()) return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3230 | Receiver = Result.get(); |
| Argyrios Kyrtzidis | 336cc8b | 2013-02-15 18:34:15 +0000 | [diff] [blame] | 3231 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3232 | |
| Fariborz Jahanian | 1774806 | 2013-01-22 19:05:17 +0000 | [diff] [blame] | 3233 | if (RespondsToSelectorSel.isNull()) { |
| 3234 | IdentifierInfo *SelectorId = &Context.Idents.get("respondsToSelector"); |
| 3235 | RespondsToSelectorSel = Context.Selectors.getUnarySelector(SelectorId); |
| 3236 | } |
| 3237 | if (Sel == RespondsToSelectorSel) |
| Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 3238 | RemoveSelectorFromWarningCache(*this, Args[0]); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3239 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3240 | return BuildInstanceMessage(Receiver, Receiver->getType(), |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3241 | /*SuperLoc=*/SourceLocation(), Sel, |
| 3242 | /*Method=*/nullptr, LBracLoc, SelectorLocs, |
| 3243 | RBracLoc, Args); |
| Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 3244 | } |
| Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 3245 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3246 | enum ARCConversionTypeClass { |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3247 | /// int, void, struct A |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3248 | ACTC_none, |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3249 | |
| 3250 | /// id, void (^)() |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3251 | ACTC_retainable, |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3252 | |
| 3253 | /// id*, id***, void (^*)(), |
| 3254 | ACTC_indirectRetainable, |
| 3255 | |
| 3256 | /// void* might be a normal C type, or it might a CF type. |
| 3257 | ACTC_voidPtr, |
| 3258 | |
| 3259 | /// struct A* |
| 3260 | ACTC_coreFoundation |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3261 | }; |
| Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3262 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3263 | static bool isAnyRetainable(ARCConversionTypeClass ACTC) { |
| 3264 | return (ACTC == ACTC_retainable || |
| 3265 | ACTC == ACTC_coreFoundation || |
| 3266 | ACTC == ACTC_voidPtr); |
| 3267 | } |
| Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3268 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3269 | static bool isAnyCLike(ARCConversionTypeClass ACTC) { |
| 3270 | return ACTC == ACTC_none || |
| 3271 | ACTC == ACTC_voidPtr || |
| 3272 | ACTC == ACTC_coreFoundation; |
| 3273 | } |
| 3274 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3275 | static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) { |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3276 | bool isIndirect = false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3277 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3278 | // Ignore an outermost reference type. |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3279 | if (const ReferenceType *ref = type->getAs<ReferenceType>()) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3280 | type = ref->getPointeeType(); |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3281 | isIndirect = true; |
| 3282 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3283 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3284 | // Drill through pointers and arrays recursively. |
| 3285 | while (true) { |
| 3286 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 3287 | type = ptr->getPointeeType(); |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3288 | |
| 3289 | // The first level of pointer may be the innermost pointer on a CF type. |
| 3290 | if (!isIndirect) { |
| 3291 | if (type->isVoidType()) return ACTC_voidPtr; |
| 3292 | if (type->isRecordType()) return ACTC_coreFoundation; |
| 3293 | } |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3294 | } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) { |
| 3295 | type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0); |
| 3296 | } else { |
| 3297 | break; |
| 3298 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3299 | isIndirect = true; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3300 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3301 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3302 | if (isIndirect) { |
| 3303 | if (type->isObjCARCBridgableType()) |
| 3304 | return ACTC_indirectRetainable; |
| 3305 | return ACTC_none; |
| 3306 | } |
| 3307 | |
| 3308 | if (type->isObjCARCBridgableType()) |
| 3309 | return ACTC_retainable; |
| 3310 | |
| 3311 | return ACTC_none; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | namespace { |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3315 | /// A result from the cast checker. |
| 3316 | enum ACCResult { |
| 3317 | /// Cannot be casted. |
| 3318 | ACC_invalid, |
| 3319 | |
| 3320 | /// Can be safely retained or not retained. |
| 3321 | ACC_bottom, |
| 3322 | |
| 3323 | /// Can be casted at +0. |
| 3324 | ACC_plusZero, |
| 3325 | |
| 3326 | /// Can be casted at +1. |
| 3327 | ACC_plusOne |
| 3328 | }; |
| 3329 | ACCResult merge(ACCResult left, ACCResult right) { |
| 3330 | if (left == right) return left; |
| 3331 | if (left == ACC_bottom) return right; |
| 3332 | if (right == ACC_bottom) return left; |
| 3333 | return ACC_invalid; |
| 3334 | } |
| 3335 | |
| 3336 | /// A checker which white-lists certain expressions whose conversion |
| 3337 | /// to or from retainable type would otherwise be forbidden in ARC. |
| 3338 | class ARCCastChecker : public StmtVisitor<ARCCastChecker, ACCResult> { |
| 3339 | typedef StmtVisitor<ARCCastChecker, ACCResult> super; |
| 3340 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3341 | ASTContext &Context; |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3342 | ARCConversionTypeClass SourceClass; |
| 3343 | ARCConversionTypeClass TargetClass; |
| Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 3344 | bool Diagnose; |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3345 | |
| 3346 | static bool isCFType(QualType type) { |
| 3347 | // Someday this can use ns_bridged. For now, it has to do this. |
| 3348 | return type->isCARCBridgableType(); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3349 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3350 | |
| 3351 | public: |
| 3352 | ARCCastChecker(ASTContext &Context, ARCConversionTypeClass source, |
| Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 3353 | ARCConversionTypeClass target, bool diagnose) |
| 3354 | : Context(Context), SourceClass(source), TargetClass(target), |
| 3355 | Diagnose(diagnose) {} |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3356 | |
| 3357 | using super::Visit; |
| 3358 | ACCResult Visit(Expr *e) { |
| 3359 | return super::Visit(e->IgnoreParens()); |
| 3360 | } |
| 3361 | |
| 3362 | ACCResult VisitStmt(Stmt *s) { |
| 3363 | return ACC_invalid; |
| 3364 | } |
| 3365 | |
| 3366 | /// Null pointer constants can be casted however you please. |
| 3367 | ACCResult VisitExpr(Expr *e) { |
| 3368 | if (e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 3369 | return ACC_bottom; |
| 3370 | return ACC_invalid; |
| 3371 | } |
| 3372 | |
| 3373 | /// Objective-C string literals can be safely casted. |
| 3374 | ACCResult VisitObjCStringLiteral(ObjCStringLiteral *e) { |
| 3375 | // If we're casting to any retainable type, go ahead. Global |
| 3376 | // strings are immune to retains, so this is bottom. |
| 3377 | if (isAnyRetainable(TargetClass)) return ACC_bottom; |
| 3378 | |
| 3379 | return ACC_invalid; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3380 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3381 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3382 | /// Look through certain implicit and explicit casts. |
| 3383 | ACCResult VisitCastExpr(CastExpr *e) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3384 | switch (e->getCastKind()) { |
| 3385 | case CK_NullToPointer: |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3386 | return ACC_bottom; |
| 3387 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3388 | case CK_NoOp: |
| 3389 | case CK_LValueToRValue: |
| 3390 | case CK_BitCast: |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3391 | case CK_CPointerToObjCPointerCast: |
| 3392 | case CK_BlockPointerToObjCPointerCast: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3393 | case CK_AnyPointerToBlockPointerCast: |
| 3394 | return Visit(e->getSubExpr()); |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3395 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3396 | default: |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3397 | return ACC_invalid; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3398 | } |
| 3399 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3400 | |
| 3401 | /// Look through unary extension. |
| 3402 | ACCResult VisitUnaryExtension(UnaryOperator *e) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3403 | return Visit(e->getSubExpr()); |
| 3404 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3405 | |
| 3406 | /// Ignore the LHS of a comma operator. |
| 3407 | ACCResult VisitBinComma(BinaryOperator *e) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3408 | return Visit(e->getRHS()); |
| 3409 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3410 | |
| 3411 | /// Conditional operators are okay if both sides are okay. |
| 3412 | ACCResult VisitConditionalOperator(ConditionalOperator *e) { |
| 3413 | ACCResult left = Visit(e->getTrueExpr()); |
| 3414 | if (left == ACC_invalid) return ACC_invalid; |
| 3415 | return merge(left, Visit(e->getFalseExpr())); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3416 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3417 | |
| John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3418 | /// Look through pseudo-objects. |
| 3419 | ACCResult VisitPseudoObjectExpr(PseudoObjectExpr *e) { |
| 3420 | // If we're getting here, we should always have a result. |
| 3421 | return Visit(e->getResultExpr()); |
| 3422 | } |
| 3423 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3424 | /// Statement expressions are okay if their result expression is okay. |
| 3425 | ACCResult VisitStmtExpr(StmtExpr *e) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3426 | return Visit(e->getSubStmt()->body_back()); |
| 3427 | } |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3428 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3429 | /// Some declaration references are okay. |
| 3430 | ACCResult VisitDeclRefExpr(DeclRefExpr *e) { |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3431 | VarDecl *var = dyn_cast<VarDecl>(e->getDecl()); |
| Ben Langmuir | 443aa4b | 2015-02-25 20:09:06 +0000 | [diff] [blame] | 3432 | // References to global constants are okay. |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3433 | if (isAnyRetainable(TargetClass) && |
| 3434 | isAnyRetainable(SourceClass) && |
| 3435 | var && |
| Akira Hatanaka | ad51539 | 2017-04-11 22:01:33 +0000 | [diff] [blame] | 3436 | !var->hasDefinition(Context) && |
| Ben Langmuir | 443aa4b | 2015-02-25 20:09:06 +0000 | [diff] [blame] | 3437 | var->getType().isConstQualified()) { |
| 3438 | |
| 3439 | // In system headers, they can also be assumed to be immune to retains. |
| 3440 | // These are things like 'kCFStringTransformToLatin'. |
| 3441 | if (Context.getSourceManager().isInSystemHeader(var->getLocation())) |
| 3442 | return ACC_bottom; |
| 3443 | |
| 3444 | return ACC_plusZero; |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3445 | } |
| 3446 | |
| 3447 | // Nothing else. |
| 3448 | return ACC_invalid; |
| Fariborz Jahanian | 7887637 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 3449 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3450 | |
| 3451 | /// Some calls are okay. |
| 3452 | ACCResult VisitCallExpr(CallExpr *e) { |
| 3453 | if (FunctionDecl *fn = e->getDirectCallee()) |
| 3454 | if (ACCResult result = checkCallToFunction(fn)) |
| 3455 | return result; |
| 3456 | |
| 3457 | return super::VisitCallExpr(e); |
| 3458 | } |
| 3459 | |
| 3460 | ACCResult checkCallToFunction(FunctionDecl *fn) { |
| 3461 | // Require a CF*Ref return type. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3462 | if (!isCFType(fn->getReturnType())) |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3463 | return ACC_invalid; |
| 3464 | |
| 3465 | if (!isAnyRetainable(TargetClass)) |
| 3466 | return ACC_invalid; |
| 3467 | |
| 3468 | // Honor an explicit 'not retained' attribute. |
| 3469 | if (fn->hasAttr<CFReturnsNotRetainedAttr>()) |
| 3470 | return ACC_plusZero; |
| 3471 | |
| 3472 | // Honor an explicit 'retained' attribute, except that for |
| 3473 | // now we're not going to permit implicit handling of +1 results, |
| 3474 | // because it's a bit frightening. |
| 3475 | if (fn->hasAttr<CFReturnsRetainedAttr>()) |
| Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3476 | return Diagnose ? ACC_plusOne |
| 3477 | : ACC_invalid; // ACC_plusOne if we start accepting this |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3478 | |
| 3479 | // Recognize this specific builtin function, which is used by CFSTR. |
| 3480 | unsigned builtinID = fn->getBuiltinID(); |
| 3481 | if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString) |
| 3482 | return ACC_bottom; |
| 3483 | |
| Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3484 | // Otherwise, don't do anything implicit with an unaudited function. |
| 3485 | if (!fn->hasAttr<CFAuditedTransferAttr>()) |
| 3486 | return ACC_invalid; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3487 | |
| Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 3488 | // Otherwise, it's +0 unless it follows the create convention. |
| 3489 | if (ento::coreFoundation::followsCreateRule(fn)) |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3490 | return Diagnose ? ACC_plusOne |
| Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 3491 | : ACC_invalid; // ACC_plusOne if we start accepting this |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3492 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3493 | return ACC_plusZero; |
| 3494 | } |
| 3495 | |
| 3496 | ACCResult VisitObjCMessageExpr(ObjCMessageExpr *e) { |
| 3497 | return checkCallToMethod(e->getMethodDecl()); |
| 3498 | } |
| 3499 | |
| 3500 | ACCResult VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *e) { |
| 3501 | ObjCMethodDecl *method; |
| 3502 | if (e->isExplicitProperty()) |
| 3503 | method = e->getExplicitProperty()->getGetterMethodDecl(); |
| 3504 | else |
| 3505 | method = e->getImplicitPropertyGetter(); |
| 3506 | return checkCallToMethod(method); |
| 3507 | } |
| 3508 | |
| 3509 | ACCResult checkCallToMethod(ObjCMethodDecl *method) { |
| 3510 | if (!method) return ACC_invalid; |
| 3511 | |
| 3512 | // Check for message sends to functions returning CF types. We |
| 3513 | // just obey the Cocoa conventions with these, even though the |
| 3514 | // return type is CF. |
| Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3515 | if (!isAnyRetainable(TargetClass) || !isCFType(method->getReturnType())) |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3516 | return ACC_invalid; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3517 | |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3518 | // If the method is explicitly marked not-retained, it's +0. |
| 3519 | if (method->hasAttr<CFReturnsNotRetainedAttr>()) |
| 3520 | return ACC_plusZero; |
| 3521 | |
| 3522 | // If the method is explicitly marked as returning retained, or its |
| 3523 | // selector follows a +1 Cocoa convention, treat it as +1. |
| 3524 | if (method->hasAttr<CFReturnsRetainedAttr>()) |
| 3525 | return ACC_plusOne; |
| 3526 | |
| 3527 | switch (method->getSelector().getMethodFamily()) { |
| 3528 | case OMF_alloc: |
| 3529 | case OMF_copy: |
| 3530 | case OMF_mutableCopy: |
| 3531 | case OMF_new: |
| 3532 | return ACC_plusOne; |
| 3533 | |
| 3534 | default: |
| 3535 | // Otherwise, treat it as +0. |
| 3536 | return ACC_plusZero; |
| Fariborz Jahanian | 9b83be8 | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 3537 | } |
| 3538 | } |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3539 | }; |
| Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 3540 | } // end anonymous namespace |
| Fariborz Jahanian | 4ad5686 | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 3541 | |
| Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3542 | bool Sema::isKnownName(StringRef name) { |
| 3543 | if (name.empty()) |
| 3544 | return false; |
| 3545 | LookupResult R(*this, &Context.Idents.get(name), SourceLocation(), |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3546 | Sema::LookupOrdinaryName); |
| Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3547 | return LookupName(R, TUScope, false); |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3550 | static void addFixitForObjCARCConversion(Sema &S, |
| 3551 | DiagnosticBuilder &DiagB, |
| 3552 | Sema::CheckedConversionKind CCK, |
| 3553 | SourceLocation afterLParen, |
| 3554 | QualType castType, |
| 3555 | Expr *castExpr, |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3556 | Expr *realCast, |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3557 | const char *bridgeKeyword, |
| 3558 | const char *CFBridgeName) { |
| 3559 | // We handle C-style and implicit casts here. |
| 3560 | switch (CCK) { |
| 3561 | case Sema::CCK_ImplicitConversion: |
| Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 3562 | case Sema::CCK_ForBuiltinOverloadedOp: |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3563 | case Sema::CCK_CStyleCast: |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3564 | case Sema::CCK_OtherCast: |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3565 | break; |
| 3566 | case Sema::CCK_FunctionalCast: |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3567 | return; |
| 3568 | } |
| 3569 | |
| 3570 | if (CFBridgeName) { |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3571 | if (CCK == Sema::CCK_OtherCast) { |
| 3572 | if (const CXXNamedCastExpr *NCE = dyn_cast<CXXNamedCastExpr>(realCast)) { |
| 3573 | SourceRange range(NCE->getOperatorLoc(), |
| 3574 | NCE->getAngleBrackets().getEnd()); |
| 3575 | SmallString<32> BridgeCall; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3576 | |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3577 | SourceManager &SM = S.getSourceManager(); |
| 3578 | char PrevChar = *SM.getCharacterData(range.getBegin().getLocWithOffset(-1)); |
| 3579 | if (Lexer::isIdentifierBodyChar(PrevChar, S.getLangOpts())) |
| 3580 | BridgeCall += ' '; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3581 | |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3582 | BridgeCall += CFBridgeName; |
| 3583 | DiagB.AddFixItHint(FixItHint::CreateReplacement(range, BridgeCall)); |
| 3584 | } |
| 3585 | return; |
| 3586 | } |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3587 | Expr *castedE = castExpr; |
| 3588 | if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(castedE)) |
| 3589 | castedE = CCE->getSubExpr(); |
| 3590 | castedE = castedE->IgnoreImpCasts(); |
| 3591 | SourceRange range = castedE->getSourceRange(); |
| Jordan Rose | 288c421 | 2012-06-07 01:10:31 +0000 | [diff] [blame] | 3592 | |
| 3593 | SmallString<32> BridgeCall; |
| 3594 | |
| 3595 | SourceManager &SM = S.getSourceManager(); |
| 3596 | char PrevChar = *SM.getCharacterData(range.getBegin().getLocWithOffset(-1)); |
| 3597 | if (Lexer::isIdentifierBodyChar(PrevChar, S.getLangOpts())) |
| 3598 | BridgeCall += ' '; |
| 3599 | |
| 3600 | BridgeCall += CFBridgeName; |
| 3601 | |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3602 | if (isa<ParenExpr>(castedE)) { |
| 3603 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| Jordan Rose | 288c421 | 2012-06-07 01:10:31 +0000 | [diff] [blame] | 3604 | BridgeCall)); |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3605 | } else { |
| Jordan Rose | 288c421 | 2012-06-07 01:10:31 +0000 | [diff] [blame] | 3606 | BridgeCall += '('; |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3607 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| Jordan Rose | 288c421 | 2012-06-07 01:10:31 +0000 | [diff] [blame] | 3608 | BridgeCall)); |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3609 | DiagB.AddFixItHint(FixItHint::CreateInsertion( |
| Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 3610 | S.getLocForEndOfToken(range.getEnd()), |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3611 | ")")); |
| 3612 | } |
| 3613 | return; |
| 3614 | } |
| 3615 | |
| 3616 | if (CCK == Sema::CCK_CStyleCast) { |
| 3617 | DiagB.AddFixItHint(FixItHint::CreateInsertion(afterLParen, bridgeKeyword)); |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3618 | } else if (CCK == Sema::CCK_OtherCast) { |
| 3619 | if (const CXXNamedCastExpr *NCE = dyn_cast<CXXNamedCastExpr>(realCast)) { |
| 3620 | std::string castCode = "("; |
| 3621 | castCode += bridgeKeyword; |
| 3622 | castCode += castType.getAsString(); |
| 3623 | castCode += ")"; |
| 3624 | SourceRange Range(NCE->getOperatorLoc(), |
| 3625 | NCE->getAngleBrackets().getEnd()); |
| 3626 | DiagB.AddFixItHint(FixItHint::CreateReplacement(Range, castCode)); |
| 3627 | } |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3628 | } else { |
| 3629 | std::string castCode = "("; |
| 3630 | castCode += bridgeKeyword; |
| 3631 | castCode += castType.getAsString(); |
| 3632 | castCode += ")"; |
| 3633 | Expr *castedE = castExpr->IgnoreImpCasts(); |
| 3634 | SourceRange range = castedE->getSourceRange(); |
| 3635 | if (isa<ParenExpr>(castedE)) { |
| 3636 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 3637 | castCode)); |
| 3638 | } else { |
| 3639 | castCode += "("; |
| 3640 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 3641 | castCode)); |
| 3642 | DiagB.AddFixItHint(FixItHint::CreateInsertion( |
| Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 3643 | S.getLocForEndOfToken(range.getEnd()), |
| Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3644 | ")")); |
| 3645 | } |
| 3646 | } |
| 3647 | } |
| 3648 | |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3649 | template <typename T> |
| 3650 | static inline T *getObjCBridgeAttr(const TypedefType *TD) { |
| 3651 | TypedefNameDecl *TDNDecl = TD->getDecl(); |
| 3652 | QualType QT = TDNDecl->getUnderlyingType(); |
| 3653 | if (QT->isPointerType()) { |
| 3654 | QT = QT->getPointeeType(); |
| 3655 | if (const RecordType *RT = QT->getAs<RecordType>()) |
| Fariborz Jahanian | 9af6a78 | 2014-06-11 19:10:46 +0000 | [diff] [blame] | 3656 | if (RecordDecl *RD = RT->getDecl()->getMostRecentDecl()) |
| Aaron Ballman | 2084f8f | 2013-12-19 13:20:36 +0000 | [diff] [blame] | 3657 | return RD->getAttr<T>(); |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3658 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3659 | return nullptr; |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3660 | } |
| 3661 | |
| 3662 | static ObjCBridgeRelatedAttr *ObjCBridgeRelatedAttrFromType(QualType T, |
| 3663 | TypedefNameDecl *&TDNDecl) { |
| 3664 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) { |
| 3665 | TDNDecl = TD->getDecl(); |
| 3666 | if (ObjCBridgeRelatedAttr *ObjCBAttr = |
| 3667 | getObjCBridgeAttr<ObjCBridgeRelatedAttr>(TD)) |
| 3668 | return ObjCBAttr; |
| 3669 | T = TDNDecl->getUnderlyingType(); |
| 3670 | } |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3671 | return nullptr; |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3674 | static void |
| 3675 | diagnoseObjCARCConversion(Sema &S, SourceRange castRange, |
| 3676 | QualType castType, ARCConversionTypeClass castACTC, |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3677 | Expr *castExpr, Expr *realCast, |
| 3678 | ARCConversionTypeClass exprACTC, |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3679 | Sema::CheckedConversionKind CCK) { |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3680 | SourceLocation loc = |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3681 | (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc()); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3682 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3683 | if (S.makeUnavailableInSystemHeader(loc, |
| John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 3684 | UnavailableAttr::IR_ARCForbiddenConversion)) |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3685 | return; |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3686 | |
| 3687 | QualType castExprType = castExpr->getType(); |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 3688 | // Defer emitting a diagnostic for bridge-related casts; that will be |
| 3689 | // handled by CheckObjCBridgeRelatedConversions. |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3690 | TypedefNameDecl *TDNDecl = nullptr; |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3691 | if ((castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable && |
| 3692 | ObjCBridgeRelatedAttrFromType(castType, TDNDecl)) || |
| 3693 | (exprACTC == ACTC_coreFoundation && castACTC == ACTC_retainable && |
| Fariborz Jahanian | 1ad83a3 | 2014-06-18 23:22:38 +0000 | [diff] [blame] | 3694 | ObjCBridgeRelatedAttrFromType(castExprType, TDNDecl))) |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3695 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3696 | |
| John McCall | 640767f | 2011-06-17 06:50:50 +0000 | [diff] [blame] | 3697 | unsigned srcKind = 0; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3698 | switch (exprACTC) { |
| John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3699 | case ACTC_none: |
| 3700 | case ACTC_coreFoundation: |
| 3701 | case ACTC_voidPtr: |
| 3702 | srcKind = (castExprType->isPointerType() ? 1 : 0); |
| 3703 | break; |
| 3704 | case ACTC_retainable: |
| 3705 | srcKind = (castExprType->isBlockPointerType() ? 2 : 3); |
| 3706 | break; |
| 3707 | case ACTC_indirectRetainable: |
| 3708 | srcKind = 4; |
| 3709 | break; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3710 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3711 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3712 | // Check whether this could be fixed with a bridge cast. |
| Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 3713 | SourceLocation afterLParen = S.getLocForEndOfToken(castRange.getBegin()); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3714 | SourceLocation noteLoc = afterLParen.isValid() ? afterLParen : loc; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3715 | |
| Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 3716 | unsigned convKindForDiag = Sema::isCast(CCK) ? 0 : 1; |
| 3717 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3718 | // Bridge from an ARC type to a CF type. |
| 3719 | if (castACTC == ACTC_retainable && isAnyRetainable(exprACTC)) { |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3720 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3721 | S.Diag(loc, diag::err_arc_cast_requires_bridge) |
| Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 3722 | << convKindForDiag |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3723 | << 2 // of C pointer type |
| 3724 | << castExprType |
| 3725 | << unsigned(castType->isBlockPointerType()) // to ObjC|block type |
| 3726 | << castType |
| 3727 | << castRange |
| 3728 | << castExpr->getSourceRange(); |
| Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3729 | bool br = S.isKnownName("CFBridgingRelease"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3730 | ACCResult CreateRule = |
| Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3731 | ARCCastChecker(S.Context, exprACTC, castACTC, true).Visit(castExpr); |
| Jordan Rose | 4502b53 | 2012-07-31 01:07:43 +0000 | [diff] [blame] | 3732 | assert(CreateRule != ACC_bottom && "This cast should already be accepted."); |
| Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3733 | if (CreateRule != ACC_plusOne) |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3734 | { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3735 | DiagnosticBuilder DiagB = |
| Fariborz Jahanian | ac2d082 | 2013-02-22 01:22:48 +0000 | [diff] [blame] | 3736 | (CCK != Sema::CCK_OtherCast) ? S.Diag(noteLoc, diag::note_arc_bridge) |
| 3737 | : S.Diag(noteLoc, diag::note_arc_cstyle_bridge); |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3738 | |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3739 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3740 | castType, castExpr, realCast, "__bridge ", |
| 3741 | nullptr); |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3742 | } |
| Fariborz Jahanian | f7759e8 | 2012-07-28 18:59:49 +0000 | [diff] [blame] | 3743 | if (CreateRule != ACC_plusZero) |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3744 | { |
| Fariborz Jahanian | ac2d082 | 2013-02-22 01:22:48 +0000 | [diff] [blame] | 3745 | DiagnosticBuilder DiagB = |
| 3746 | (CCK == Sema::CCK_OtherCast && !br) ? |
| 3747 | S.Diag(noteLoc, diag::note_arc_cstyle_bridge_transfer) << castExprType : |
| 3748 | S.Diag(br ? castExpr->getExprLoc() : noteLoc, |
| 3749 | diag::note_arc_bridge_transfer) |
| 3750 | << castExprType << br; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3751 | |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3752 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3753 | castType, castExpr, realCast, "__bridge_transfer ", |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3754 | br ? "CFBridgingRelease" : nullptr); |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3755 | } |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3756 | |
| 3757 | return; |
| 3758 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3759 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3760 | // Bridge from a CF type to an ARC type. |
| 3761 | if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC)) { |
| Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3762 | bool br = S.isKnownName("CFBridgingRetain"); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3763 | S.Diag(loc, diag::err_arc_cast_requires_bridge) |
| Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 3764 | << convKindForDiag |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3765 | << unsigned(castExprType->isBlockPointerType()) // of ObjC|block type |
| 3766 | << castExprType |
| 3767 | << 2 // to C pointer type |
| 3768 | << castType |
| 3769 | << castRange |
| 3770 | << castExpr->getSourceRange(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3771 | ACCResult CreateRule = |
| Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3772 | ARCCastChecker(S.Context, exprACTC, castACTC, true).Visit(castExpr); |
| Jordan Rose | 4502b53 | 2012-07-31 01:07:43 +0000 | [diff] [blame] | 3773 | assert(CreateRule != ACC_bottom && "This cast should already be accepted."); |
| Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3774 | if (CreateRule != ACC_plusOne) |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3775 | { |
| Fariborz Jahanian | ac2d082 | 2013-02-22 01:22:48 +0000 | [diff] [blame] | 3776 | DiagnosticBuilder DiagB = |
| 3777 | (CCK != Sema::CCK_OtherCast) ? S.Diag(noteLoc, diag::note_arc_bridge) |
| 3778 | : S.Diag(noteLoc, diag::note_arc_cstyle_bridge); |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3779 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3780 | castType, castExpr, realCast, "__bridge ", |
| 3781 | nullptr); |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3782 | } |
| Fariborz Jahanian | f7759e8 | 2012-07-28 18:59:49 +0000 | [diff] [blame] | 3783 | if (CreateRule != ACC_plusZero) |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3784 | { |
| Fariborz Jahanian | ac2d082 | 2013-02-22 01:22:48 +0000 | [diff] [blame] | 3785 | DiagnosticBuilder DiagB = |
| 3786 | (CCK == Sema::CCK_OtherCast && !br) ? |
| 3787 | S.Diag(noteLoc, diag::note_arc_cstyle_bridge_retained) << castType : |
| 3788 | S.Diag(br ? castExpr->getExprLoc() : noteLoc, |
| 3789 | diag::note_arc_bridge_retained) |
| 3790 | << castType << br; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3791 | |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3792 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3793 | castType, castExpr, realCast, "__bridge_retained ", |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3794 | br ? "CFBridgingRetain" : nullptr); |
| Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3795 | } |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3796 | |
| 3797 | return; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3798 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3799 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3800 | S.Diag(loc, diag::err_arc_mismatched_cast) |
| Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 3801 | << !convKindForDiag |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3802 | << srcKind << castExprType << castType |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3803 | << castRange << castExpr->getSourceRange(); |
| 3804 | } |
| 3805 | |
| Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3806 | template <typename TB> |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3807 | static bool CheckObjCBridgeNSCast(Sema &S, QualType castType, Expr *castExpr, |
| 3808 | bool &HadTheAttribute, bool warn) { |
| Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3809 | QualType T = castExpr->getType(); |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3810 | HadTheAttribute = false; |
| Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3811 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) { |
| 3812 | TypedefNameDecl *TDNDecl = TD->getDecl(); |
| Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3813 | if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) { |
| Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 3814 | if (IdentifierInfo *Parm = ObjCBAttr->getBridgedType()) { |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3815 | HadTheAttribute = true; |
| Fariborz Jahanian | c9e266b | 2014-12-11 22:56:26 +0000 | [diff] [blame] | 3816 | if (Parm->isStr("id")) |
| 3817 | return true; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3818 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3819 | NamedDecl *Target = nullptr; |
| Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3820 | // Check for an existing type with this name. |
| 3821 | LookupResult R(S, DeclarationName(Parm), SourceLocation(), |
| 3822 | Sema::LookupOrdinaryName); |
| 3823 | if (S.LookupName(R, S.TUScope)) { |
| Fariborz Jahanian | f07183c | 2013-11-16 01:45:25 +0000 | [diff] [blame] | 3824 | Target = R.getFoundDecl(); |
| 3825 | if (Target && isa<ObjCInterfaceDecl>(Target)) { |
| 3826 | ObjCInterfaceDecl *ExprClass = cast<ObjCInterfaceDecl>(Target); |
| 3827 | if (const ObjCObjectPointerType *InterfacePointerType = |
| 3828 | castType->getAsObjCInterfacePointerType()) { |
| 3829 | ObjCInterfaceDecl *CastClass |
| 3830 | = InterfacePointerType->getObjectType()->getInterface(); |
| Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3831 | if ((CastClass == ExprClass) || |
| Fariborz Jahanian | 27aa9b4 | 2015-04-10 22:07:47 +0000 | [diff] [blame] | 3832 | (CastClass && CastClass->isSuperClassOf(ExprClass))) |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3833 | return true; |
| 3834 | if (warn) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3835 | S.Diag(castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge) |
| 3836 | << T << Target->getName() << castType->getPointeeType(); |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3837 | return false; |
| Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3838 | } else if (castType->isObjCIdType() || |
| 3839 | (S.Context.ObjCObjectAdoptsQTypeProtocols( |
| 3840 | castType, ExprClass))) |
| 3841 | // ok to cast to 'id'. |
| 3842 | // casting to id<p-list> is ok if bridge type adopts all of |
| 3843 | // p-list protocols. |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3844 | return true; |
| Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3845 | else { |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3846 | if (warn) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3847 | S.Diag(castExpr->getBeginLoc(), diag::warn_objc_invalid_bridge) |
| 3848 | << T << Target->getName() << castType; |
| 3849 | S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| 3850 | S.Diag(Target->getBeginLoc(), diag::note_declared_at); |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3851 | } |
| 3852 | return false; |
| Fariborz Jahanian | 2c31212 | 2013-11-16 23:22:37 +0000 | [diff] [blame] | 3853 | } |
| Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3854 | } |
| Fariborz Jahanian | 696c887 | 2015-04-09 23:39:53 +0000 | [diff] [blame] | 3855 | } else if (!castType->isObjCIdType()) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3856 | S.Diag(castExpr->getBeginLoc(), |
| 3857 | diag::err_objc_cf_bridged_not_interface) |
| 3858 | << castExpr->getType() << Parm; |
| 3859 | S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| Fariborz Jahanian | 696c887 | 2015-04-09 23:39:53 +0000 | [diff] [blame] | 3860 | if (Target) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3861 | S.Diag(Target->getBeginLoc(), diag::note_declared_at); |
| Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3862 | } |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3863 | return true; |
| Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3864 | } |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3865 | return false; |
| Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3866 | } |
| 3867 | T = TDNDecl->getUnderlyingType(); |
| 3868 | } |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3869 | return true; |
| Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3870 | } |
| 3871 | |
| Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3872 | template <typename TB> |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3873 | static bool CheckObjCBridgeCFCast(Sema &S, QualType castType, Expr *castExpr, |
| 3874 | bool &HadTheAttribute, bool warn) { |
| Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3875 | QualType T = castType; |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3876 | HadTheAttribute = false; |
| Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3877 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) { |
| 3878 | TypedefNameDecl *TDNDecl = TD->getDecl(); |
| Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3879 | if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) { |
| Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 3880 | if (IdentifierInfo *Parm = ObjCBAttr->getBridgedType()) { |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3881 | HadTheAttribute = true; |
| John McCall | af6b3f8 | 2015-03-10 18:41:23 +0000 | [diff] [blame] | 3882 | if (Parm->isStr("id")) |
| 3883 | return true; |
| 3884 | |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3885 | NamedDecl *Target = nullptr; |
| Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3886 | // Check for an existing type with this name. |
| 3887 | LookupResult R(S, DeclarationName(Parm), SourceLocation(), |
| 3888 | Sema::LookupOrdinaryName); |
| 3889 | if (S.LookupName(R, S.TUScope)) { |
| 3890 | Target = R.getFoundDecl(); |
| 3891 | if (Target && isa<ObjCInterfaceDecl>(Target)) { |
| 3892 | ObjCInterfaceDecl *CastClass = cast<ObjCInterfaceDecl>(Target); |
| 3893 | if (const ObjCObjectPointerType *InterfacePointerType = |
| 3894 | castExpr->getType()->getAsObjCInterfacePointerType()) { |
| 3895 | ObjCInterfaceDecl *ExprClass |
| 3896 | = InterfacePointerType->getObjectType()->getInterface(); |
| Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3897 | if ((CastClass == ExprClass) || |
| 3898 | (ExprClass && CastClass->isSuperClassOf(ExprClass))) |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3899 | return true; |
| 3900 | if (warn) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3901 | S.Diag(castExpr->getBeginLoc(), |
| 3902 | diag::warn_objc_invalid_bridge_to_cf) |
| 3903 | << castExpr->getType()->getPointeeType() << T; |
| 3904 | S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3905 | } |
| 3906 | return false; |
| Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3907 | } else if (castExpr->getType()->isObjCIdType() || |
| 3908 | (S.Context.QIdProtocolsAdoptObjCObjectProtocols( |
| 3909 | castExpr->getType(), CastClass))) |
| 3910 | // ok to cast an 'id' expression to a CFtype. |
| 3911 | // ok to cast an 'id<plist>' expression to CFtype provided plist |
| 3912 | // adopts all of CFtype's ObjetiveC's class plist. |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3913 | return true; |
| Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3914 | else { |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3915 | if (warn) { |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3916 | S.Diag(castExpr->getBeginLoc(), |
| 3917 | diag::warn_objc_invalid_bridge_to_cf) |
| 3918 | << castExpr->getType() << castType; |
| 3919 | S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| 3920 | S.Diag(Target->getBeginLoc(), diag::note_declared_at); |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3921 | } |
| 3922 | return false; |
| Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3923 | } |
| 3924 | } |
| 3925 | } |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3926 | S.Diag(castExpr->getBeginLoc(), |
| 3927 | diag::err_objc_ns_bridged_invalid_cfobject) |
| 3928 | << castExpr->getType() << castType; |
| 3929 | S.Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3930 | if (Target) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3931 | S.Diag(Target->getBeginLoc(), diag::note_declared_at); |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3932 | return true; |
| Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3933 | } |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3934 | return false; |
| Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3935 | } |
| 3936 | T = TDNDecl->getUnderlyingType(); |
| 3937 | } |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3938 | return true; |
| Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
| Fariborz Jahanian | 8c5b4be | 2013-11-21 00:39:36 +0000 | [diff] [blame] | 3941 | void Sema::CheckTollFreeBridgeCast(QualType castType, Expr *castExpr) { |
| Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 3942 | if (!getLangOpts().ObjC) |
| Fariborz Jahanian | f1a22f4 | 2014-04-29 16:12:56 +0000 | [diff] [blame] | 3943 | return; |
| Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 3944 | // warn in presence of __bridge casting to or from a toll free bridge cast. |
| Fariborz Jahanian | 8c5b4be | 2013-11-21 00:39:36 +0000 | [diff] [blame] | 3945 | ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExpr->getType()); |
| 3946 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType); |
| Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3947 | if (castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) { |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3948 | bool HasObjCBridgeAttr; |
| 3949 | bool ObjCBridgeAttrWillNotWarn = |
| 3950 | CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr, |
| 3951 | false); |
| 3952 | if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr) |
| 3953 | return; |
| 3954 | bool HasObjCBridgeMutableAttr; |
| 3955 | bool ObjCBridgeMutableAttrWillNotWarn = |
| 3956 | CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, |
| 3957 | HasObjCBridgeMutableAttr, false); |
| 3958 | if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr) |
| 3959 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3960 | |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3961 | if (HasObjCBridgeAttr) |
| 3962 | CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr, |
| 3963 | true); |
| 3964 | else if (HasObjCBridgeMutableAttr) |
| 3965 | CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, |
| 3966 | HasObjCBridgeMutableAttr, true); |
| Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3967 | } |
| 3968 | else if (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable) { |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3969 | bool HasObjCBridgeAttr; |
| 3970 | bool ObjCBridgeAttrWillNotWarn = |
| 3971 | CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr, |
| 3972 | false); |
| 3973 | if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr) |
| 3974 | return; |
| 3975 | bool HasObjCBridgeMutableAttr; |
| 3976 | bool ObjCBridgeMutableAttrWillNotWarn = |
| 3977 | CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, |
| 3978 | HasObjCBridgeMutableAttr, false); |
| 3979 | if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr) |
| 3980 | return; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 3981 | |
| Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3982 | if (HasObjCBridgeAttr) |
| 3983 | CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr, |
| 3984 | true); |
| 3985 | else if (HasObjCBridgeMutableAttr) |
| 3986 | CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, |
| 3987 | HasObjCBridgeMutableAttr, true); |
| Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3988 | } |
| Fariborz Jahanian | 8c5b4be | 2013-11-21 00:39:36 +0000 | [diff] [blame] | 3989 | } |
| 3990 | |
| Fariborz Jahanian | 53f867a | 2014-06-26 21:22:16 +0000 | [diff] [blame] | 3991 | void Sema::CheckObjCBridgeRelatedCast(QualType castType, Expr *castExpr) { |
| 3992 | QualType SrcType = castExpr->getType(); |
| 3993 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(castExpr)) { |
| 3994 | if (PRE->isExplicitProperty()) { |
| 3995 | if (ObjCPropertyDecl *PDecl = PRE->getExplicitProperty()) |
| 3996 | SrcType = PDecl->getType(); |
| 3997 | } |
| 3998 | else if (PRE->isImplicitProperty()) { |
| 3999 | if (ObjCMethodDecl *Getter = PRE->getImplicitPropertyGetter()) |
| 4000 | SrcType = Getter->getReturnType(); |
| Fariborz Jahanian | 53f867a | 2014-06-26 21:22:16 +0000 | [diff] [blame] | 4001 | } |
| 4002 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4003 | |
| Fariborz Jahanian | 53f867a | 2014-06-26 21:22:16 +0000 | [diff] [blame] | 4004 | ARCConversionTypeClass srcExprACTC = classifyTypeForARCConversion(SrcType); |
| 4005 | ARCConversionTypeClass castExprACTC = classifyTypeForARCConversion(castType); |
| 4006 | if (srcExprACTC != ACTC_retainable || castExprACTC != ACTC_coreFoundation) |
| 4007 | return; |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4008 | CheckObjCBridgeRelatedConversions(castExpr->getBeginLoc(), castType, SrcType, |
| 4009 | castExpr); |
| Fariborz Jahanian | 53f867a | 2014-06-26 21:22:16 +0000 | [diff] [blame] | 4010 | } |
| 4011 | |
| Fariborz Jahanian | c70a543 | 2014-05-10 17:40:11 +0000 | [diff] [blame] | 4012 | bool Sema::CheckTollFreeBridgeStaticCast(QualType castType, Expr *castExpr, |
| 4013 | CastKind &Kind) { |
| Erik Pilkington | fa98390 | 2018-10-30 20:31:30 +0000 | [diff] [blame] | 4014 | if (!getLangOpts().ObjC) |
| Fariborz Jahanian | c70a543 | 2014-05-10 17:40:11 +0000 | [diff] [blame] | 4015 | return false; |
| 4016 | ARCConversionTypeClass exprACTC = |
| 4017 | classifyTypeForARCConversion(castExpr->getType()); |
| 4018 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType); |
| 4019 | if ((castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) || |
| 4020 | (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable)) { |
| 4021 | CheckTollFreeBridgeCast(castType, castExpr); |
| 4022 | Kind = (castACTC == ACTC_coreFoundation) ? CK_BitCast |
| 4023 | : CK_CPointerToObjCPointerCast; |
| 4024 | return true; |
| 4025 | } |
| 4026 | return false; |
| 4027 | } |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4028 | |
| 4029 | bool Sema::checkObjCBridgeRelatedComponents(SourceLocation Loc, |
| 4030 | QualType DestType, QualType SrcType, |
| 4031 | ObjCInterfaceDecl *&RelatedClass, |
| 4032 | ObjCMethodDecl *&ClassMethod, |
| 4033 | ObjCMethodDecl *&InstanceMethod, |
| 4034 | TypedefNameDecl *&TDNDecl, |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4035 | bool CfToNs, bool Diagnose) { |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4036 | QualType T = CfToNs ? SrcType : DestType; |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4037 | ObjCBridgeRelatedAttr *ObjCBAttr = ObjCBridgeRelatedAttrFromType(T, TDNDecl); |
| 4038 | if (!ObjCBAttr) |
| 4039 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4040 | |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4041 | IdentifierInfo *RCId = ObjCBAttr->getRelatedClass(); |
| 4042 | IdentifierInfo *CMId = ObjCBAttr->getClassMethod(); |
| 4043 | IdentifierInfo *IMId = ObjCBAttr->getInstanceMethod(); |
| 4044 | if (!RCId) |
| 4045 | return false; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4046 | NamedDecl *Target = nullptr; |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4047 | // Check for an existing type with this name. |
| 4048 | LookupResult R(*this, DeclarationName(RCId), SourceLocation(), |
| 4049 | Sema::LookupOrdinaryName); |
| 4050 | if (!LookupName(R, TUScope)) { |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4051 | if (Diagnose) { |
| 4052 | Diag(Loc, diag::err_objc_bridged_related_invalid_class) << RCId |
| 4053 | << SrcType << DestType; |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4054 | Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4055 | } |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4056 | return false; |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4057 | } |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4058 | Target = R.getFoundDecl(); |
| 4059 | if (Target && isa<ObjCInterfaceDecl>(Target)) |
| 4060 | RelatedClass = cast<ObjCInterfaceDecl>(Target); |
| 4061 | else { |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4062 | if (Diagnose) { |
| 4063 | Diag(Loc, diag::err_objc_bridged_related_invalid_class_name) << RCId |
| 4064 | << SrcType << DestType; |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4065 | Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4066 | if (Target) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4067 | Diag(Target->getBeginLoc(), diag::note_declared_at); |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4068 | } |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4069 | return false; |
| 4070 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4071 | |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4072 | // Check for an existing class method with the given selector name. |
| 4073 | if (CfToNs && CMId) { |
| 4074 | Selector Sel = Context.Selectors.getUnarySelector(CMId); |
| 4075 | ClassMethod = RelatedClass->lookupMethod(Sel, false); |
| 4076 | if (!ClassMethod) { |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4077 | if (Diagnose) { |
| 4078 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
| 4079 | << SrcType << DestType << Sel << false; |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4080 | Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4081 | } |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4082 | return false; |
| 4083 | } |
| 4084 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4085 | |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4086 | // Check for an existing instance method with the given selector name. |
| 4087 | if (!CfToNs && IMId) { |
| 4088 | Selector Sel = Context.Selectors.getNullarySelector(IMId); |
| 4089 | InstanceMethod = RelatedClass->lookupMethod(Sel, true); |
| 4090 | if (!InstanceMethod) { |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4091 | if (Diagnose) { |
| 4092 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
| 4093 | << SrcType << DestType << Sel << true; |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4094 | Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4095 | } |
| Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 4096 | return false; |
| 4097 | } |
| 4098 | } |
| 4099 | return true; |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | bool |
| 4103 | Sema::CheckObjCBridgeRelatedConversions(SourceLocation Loc, |
| Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 4104 | QualType DestType, QualType SrcType, |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4105 | Expr *&SrcExpr, bool Diagnose) { |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4106 | ARCConversionTypeClass rhsExprACTC = classifyTypeForARCConversion(SrcType); |
| 4107 | ARCConversionTypeClass lhsExprACTC = classifyTypeForARCConversion(DestType); |
| 4108 | bool CfToNs = (rhsExprACTC == ACTC_coreFoundation && lhsExprACTC == ACTC_retainable); |
| 4109 | bool NsToCf = (rhsExprACTC == ACTC_retainable && lhsExprACTC == ACTC_coreFoundation); |
| 4110 | if (!CfToNs && !NsToCf) |
| 4111 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4112 | |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4113 | ObjCInterfaceDecl *RelatedClass; |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4114 | ObjCMethodDecl *ClassMethod = nullptr; |
| 4115 | ObjCMethodDecl *InstanceMethod = nullptr; |
| 4116 | TypedefNameDecl *TDNDecl = nullptr; |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4117 | if (!checkObjCBridgeRelatedComponents(Loc, DestType, SrcType, RelatedClass, |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4118 | ClassMethod, InstanceMethod, TDNDecl, |
| 4119 | CfToNs, Diagnose)) |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4120 | return false; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4121 | |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4122 | if (CfToNs) { |
| 4123 | // Implicit conversion from CF to ObjC object is needed. |
| Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 4124 | if (ClassMethod) { |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4125 | if (Diagnose) { |
| 4126 | std::string ExpressionString = "["; |
| 4127 | ExpressionString += RelatedClass->getNameAsString(); |
| 4128 | ExpressionString += " "; |
| 4129 | ExpressionString += ClassMethod->getSelector().getAsString(); |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4130 | SourceLocation SrcExprEndLoc = |
| 4131 | getLocForEndOfToken(SrcExpr->getEndLoc()); |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4132 | // Provide a fixit: [RelatedClass ClassMethod SrcExpr] |
| 4133 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4134 | << SrcType << DestType << ClassMethod->getSelector() << false |
| 4135 | << FixItHint::CreateInsertion(SrcExpr->getBeginLoc(), |
| 4136 | ExpressionString) |
| 4137 | << FixItHint::CreateInsertion(SrcExprEndLoc, "]"); |
| 4138 | Diag(RelatedClass->getBeginLoc(), diag::note_declared_at); |
| 4139 | Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4140 | |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4141 | QualType receiverType = Context.getObjCInterfaceType(RelatedClass); |
| 4142 | // Argument. |
| 4143 | Expr *args[] = { SrcExpr }; |
| 4144 | ExprResult msg = BuildClassMessageImplicit(receiverType, false, |
| Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 4145 | ClassMethod->getLocation(), |
| 4146 | ClassMethod->getSelector(), ClassMethod, |
| 4147 | MultiExprArg(args, 1)); |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4148 | SrcExpr = msg.get(); |
| 4149 | } |
| Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 4150 | return true; |
| Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 4151 | } |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4152 | } |
| 4153 | else { |
| 4154 | // Implicit conversion from ObjC type to CF object is needed. |
| Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 4155 | if (InstanceMethod) { |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4156 | if (Diagnose) { |
| 4157 | std::string ExpressionString; |
| 4158 | SourceLocation SrcExprEndLoc = |
| Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 4159 | getLocForEndOfToken(SrcExpr->getEndLoc()); |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4160 | if (InstanceMethod->isPropertyAccessor()) |
| 4161 | if (const ObjCPropertyDecl *PDecl = |
| 4162 | InstanceMethod->findPropertyDecl()) { |
| 4163 | // fixit: ObjectExpr.propertyname when it is aproperty accessor. |
| 4164 | ExpressionString = "."; |
| 4165 | ExpressionString += PDecl->getNameAsString(); |
| 4166 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
| 4167 | << SrcType << DestType << InstanceMethod->getSelector() << true |
| 4168 | << FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString); |
| 4169 | } |
| 4170 | if (ExpressionString.empty()) { |
| 4171 | // Provide a fixit: [ObjectExpr InstanceMethod] |
| 4172 | ExpressionString = " "; |
| 4173 | ExpressionString += InstanceMethod->getSelector().getAsString(); |
| 4174 | ExpressionString += "]"; |
| 4175 | |
| Fariborz Jahanian | 88b6898 | 2013-12-10 23:18:06 +0000 | [diff] [blame] | 4176 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4177 | << SrcType << DestType << InstanceMethod->getSelector() << true |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4178 | << FixItHint::CreateInsertion(SrcExpr->getBeginLoc(), "[") |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4179 | << FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString); |
| Fariborz Jahanian | 88b6898 | 2013-12-10 23:18:06 +0000 | [diff] [blame] | 4180 | } |
| Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4181 | Diag(RelatedClass->getBeginLoc(), diag::note_declared_at); |
| 4182 | Diag(TDNDecl->getBeginLoc(), diag::note_declared_at); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4183 | |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4184 | ExprResult msg = |
| 4185 | BuildInstanceMessageImplicit(SrcExpr, SrcType, |
| 4186 | InstanceMethod->getLocation(), |
| 4187 | InstanceMethod->getSelector(), |
| 4188 | InstanceMethod, None); |
| 4189 | SrcExpr = msg.get(); |
| 4190 | } |
| Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 4191 | return true; |
| Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 4192 | } |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4193 | } |
| Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 4194 | return false; |
| Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4197 | Sema::ARCConversionResult |
| Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame] | 4198 | Sema::CheckObjCConversion(SourceRange castRange, QualType castType, |
| 4199 | Expr *&castExpr, CheckedConversionKind CCK, |
| 4200 | bool Diagnose, bool DiagnoseCFAudited, |
| 4201 | BinaryOperatorKind Opc) { |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4202 | QualType castExprType = castExpr->getType(); |
| 4203 | |
| 4204 | // For the purposes of the classification, we assume reference types |
| 4205 | // will bind to temporaries. |
| 4206 | QualType effCastType = castType; |
| 4207 | if (const ReferenceType *ref = castType->getAs<ReferenceType>()) |
| 4208 | effCastType = ref->getPointeeType(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4209 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4210 | ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType); |
| 4211 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(effCastType); |
| Fariborz Jahanian | 2fa646d | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 4212 | if (exprACTC == castACTC) { |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4213 | // Check for viability and report error if casting an rvalue to a |
| Fariborz Jahanian | 2fa646d | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 4214 | // life-time qualifier. |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4215 | if (castACTC == ACTC_retainable && |
| Fariborz Jahanian | 2fa646d | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 4216 | (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) && |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4217 | castType != castExprType) { |
| Fariborz Jahanian | 244b187 | 2011-10-29 00:06:10 +0000 | [diff] [blame] | 4218 | const Type *DT = castType.getTypePtr(); |
| 4219 | QualType QDT = castType; |
| 4220 | // We desugar some types but not others. We ignore those |
| 4221 | // that cannot happen in a cast; i.e. auto, and those which |
| 4222 | // should not be de-sugared; i.e typedef. |
| 4223 | if (const ParenType *PT = dyn_cast<ParenType>(DT)) |
| 4224 | QDT = PT->desugar(); |
| 4225 | else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT)) |
| 4226 | QDT = TP->desugar(); |
| 4227 | else if (const AttributedType *AT = dyn_cast<AttributedType>(DT)) |
| 4228 | QDT = AT->desugar(); |
| 4229 | if (QDT != castType && |
| 4230 | QDT.getObjCLifetime() != Qualifiers::OCL_None) { |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4231 | if (Diagnose) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4232 | SourceLocation loc = (castRange.isValid() ? castRange.getBegin() |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4233 | : castExpr->getExprLoc()); |
| 4234 | Diag(loc, diag::err_arc_nolifetime_behavior); |
| 4235 | } |
| 4236 | return ACR_error; |
| Fariborz Jahanian | 244b187 | 2011-10-29 00:06:10 +0000 | [diff] [blame] | 4237 | } |
| Fariborz Jahanian | 2fa646d | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 4238 | } |
| 4239 | return ACR_okay; |
| 4240 | } |
| Brian Kelley | 11352a8 | 2017-03-29 18:09:02 +0000 | [diff] [blame] | 4241 | |
| 4242 | // The life-time qualifier cast check above is all we need for ObjCWeak. |
| 4243 | // ObjCAutoRefCount has more restrictions on what is legal. |
| 4244 | if (!getLangOpts().ObjCAutoRefCount) |
| 4245 | return ACR_okay; |
| 4246 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4247 | if (isAnyCLike(exprACTC) && isAnyCLike(castACTC)) return ACR_okay; |
| 4248 | |
| 4249 | // Allow all of these types to be cast to integer types (but not |
| 4250 | // vice-versa). |
| 4251 | if (castACTC == ACTC_none && castType->isIntegralType(Context)) |
| 4252 | return ACR_okay; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4253 | |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4254 | // Allow casts between pointers to lifetime types (e.g., __strong id*) |
| 4255 | // and pointers to void (e.g., cv void *). Casting from void* to lifetime* |
| 4256 | // must be explicit. |
| 4257 | if (exprACTC == ACTC_indirectRetainable && castACTC == ACTC_voidPtr) |
| 4258 | return ACR_okay; |
| 4259 | if (castACTC == ACTC_indirectRetainable && exprACTC == ACTC_voidPtr && |
| Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 4260 | isCast(CCK)) |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4261 | return ACR_okay; |
| 4262 | |
| Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 4263 | switch (ARCCastChecker(Context, exprACTC, castACTC, false).Visit(castExpr)) { |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4264 | // For invalid casts, fall through. |
| 4265 | case ACC_invalid: |
| 4266 | break; |
| 4267 | |
| 4268 | // Do nothing for both bottom and +0. |
| 4269 | case ACC_bottom: |
| 4270 | case ACC_plusZero: |
| 4271 | return ACR_okay; |
| 4272 | |
| 4273 | // If the result is +1, consume it here. |
| 4274 | case ACC_plusOne: |
| 4275 | castExpr = ImplicitCastExpr::Create(Context, castExpr->getType(), |
| 4276 | CK_ARCConsumeObject, castExpr, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4277 | nullptr, VK_RValue); |
| Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 4278 | Cleanup.setExprNeedsCleanups(true); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4279 | return ACR_okay; |
| 4280 | } |
| 4281 | |
| 4282 | // If this is a non-implicit cast from id or block type to a |
| 4283 | // CoreFoundation type, delay complaining in case the cast is used |
| 4284 | // in an acceptable context. |
| Richard Smith | 1ef7554 | 2018-06-27 20:30:34 +0000 | [diff] [blame] | 4285 | if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC) && isCast(CCK)) |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4286 | return ACR_unbridged; |
| 4287 | |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4288 | // Issue a diagnostic about a missing @-sign when implicit casting a cstring |
| 4289 | // to 'NSString *', instead of falling through to report a "bridge cast" |
| 4290 | // diagnostic. |
| Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 4291 | if (castACTC == ACTC_retainable && exprACTC == ACTC_none && |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4292 | ConversionToObjCStringLiteralCheck(castType, castExpr, Diagnose)) |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4293 | return ACR_error; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4294 | |
| Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 4295 | // Do not issue "bridge cast" diagnostic when implicit casting |
| 4296 | // a retainable object to a CF type parameter belonging to an audited |
| 4297 | // CF API function. Let caller issue a normal type mismatched diagnostic |
| 4298 | // instead. |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4299 | if ((!DiagnoseCFAudited || exprACTC != ACTC_retainable || |
| 4300 | castACTC != ACTC_coreFoundation) && |
| 4301 | !(exprACTC == ACTC_voidPtr && castACTC == ACTC_retainable && |
| 4302 | (Opc == BO_NE || Opc == BO_EQ))) { |
| 4303 | if (Diagnose) |
| George Burgess IV | 60bc972 | 2016-01-13 23:36:34 +0000 | [diff] [blame] | 4304 | diagnoseObjCARCConversion(*this, castRange, castType, castACTC, castExpr, |
| 4305 | castExpr, exprACTC, CCK); |
| Bob Wilson | f5c53b8 | 2016-02-13 01:41:41 +0000 | [diff] [blame] | 4306 | return ACR_error; |
| 4307 | } |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4308 | return ACR_okay; |
| 4309 | } |
| 4310 | |
| 4311 | /// Given that we saw an expression with the ARCUnbridgedCastTy |
| 4312 | /// placeholder type, complain bitterly. |
| 4313 | void Sema::diagnoseARCUnbridgedCast(Expr *e) { |
| 4314 | // We expect the spurious ImplicitCastExpr to already have been stripped. |
| 4315 | assert(!e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 4316 | CastExpr *realCast = cast<CastExpr>(e->IgnoreParens()); |
| 4317 | |
| 4318 | SourceRange castRange; |
| 4319 | QualType castType; |
| 4320 | CheckedConversionKind CCK; |
| 4321 | |
| 4322 | if (CStyleCastExpr *cast = dyn_cast<CStyleCastExpr>(realCast)) { |
| 4323 | castRange = SourceRange(cast->getLParenLoc(), cast->getRParenLoc()); |
| 4324 | castType = cast->getTypeAsWritten(); |
| 4325 | CCK = CCK_CStyleCast; |
| 4326 | } else if (ExplicitCastExpr *cast = dyn_cast<ExplicitCastExpr>(realCast)) { |
| 4327 | castRange = cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange(); |
| 4328 | castType = cast->getTypeAsWritten(); |
| 4329 | CCK = CCK_OtherCast; |
| 4330 | } else { |
| Akira Hatanaka | 2cd7e86 | 2017-05-09 01:54:51 +0000 | [diff] [blame] | 4331 | llvm_unreachable("Unexpected ImplicitCastExpr"); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | ARCConversionTypeClass castACTC = |
| 4335 | classifyTypeForARCConversion(castType.getNonReferenceType()); |
| 4336 | |
| 4337 | Expr *castExpr = realCast->getSubExpr(); |
| 4338 | assert(classifyTypeForARCConversion(castExpr->getType()) == ACTC_retainable); |
| 4339 | |
| 4340 | diagnoseObjCARCConversion(*this, castRange, castType, castACTC, |
| Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 4341 | castExpr, realCast, ACTC_retainable, CCK); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | /// stripARCUnbridgedCast - Given an expression of ARCUnbridgedCast |
| 4345 | /// type, remove the placeholder cast. |
| 4346 | Expr *Sema::stripARCUnbridgedCast(Expr *e) { |
| 4347 | assert(e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 4348 | |
| 4349 | if (ParenExpr *pe = dyn_cast<ParenExpr>(e)) { |
| 4350 | Expr *sub = stripARCUnbridgedCast(pe->getSubExpr()); |
| 4351 | return new (Context) ParenExpr(pe->getLParen(), pe->getRParen(), sub); |
| 4352 | } else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) { |
| 4353 | assert(uo->getOpcode() == UO_Extension); |
| 4354 | Expr *sub = stripARCUnbridgedCast(uo->getSubExpr()); |
| Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 4355 | return new (Context) |
| 4356 | UnaryOperator(sub, UO_Extension, sub->getType(), sub->getValueKind(), |
| 4357 | sub->getObjectKind(), uo->getOperatorLoc(), false); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4358 | } else if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) { |
| 4359 | assert(!gse->isResultDependent()); |
| 4360 | |
| 4361 | unsigned n = gse->getNumAssocs(); |
| Bruno Ricci | 1ec7fd3 | 2019-01-29 12:57:11 +0000 | [diff] [blame] | 4362 | SmallVector<Expr *, 4> subExprs; |
| 4363 | SmallVector<TypeSourceInfo *, 4> subTypes; |
| 4364 | subExprs.reserve(n); |
| 4365 | subTypes.reserve(n); |
| 4366 | for (const GenericSelectionExpr::Association &assoc : gse->associations()) { |
| 4367 | subTypes.push_back(assoc.getTypeSourceInfo()); |
| 4368 | Expr *sub = assoc.getAssociationExpr(); |
| 4369 | if (assoc.isSelected()) |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4370 | sub = stripARCUnbridgedCast(sub); |
| Bruno Ricci | 1ec7fd3 | 2019-01-29 12:57:11 +0000 | [diff] [blame] | 4371 | subExprs.push_back(sub); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4372 | } |
| 4373 | |
| Bruno Ricci | db07683 | 2019-01-26 14:15:10 +0000 | [diff] [blame] | 4374 | return GenericSelectionExpr::Create( |
| 4375 | Context, gse->getGenericLoc(), gse->getControllingExpr(), subTypes, |
| 4376 | subExprs, gse->getDefaultLoc(), gse->getRParenLoc(), |
| 4377 | gse->containsUnexpandedParameterPack(), gse->getResultIndex()); |
| John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 4378 | } else { |
| 4379 | assert(isa<ImplicitCastExpr>(e) && "bad form of unbridged cast!"); |
| 4380 | return cast<ImplicitCastExpr>(e)->getSubExpr(); |
| 4381 | } |
| 4382 | } |
| 4383 | |
| Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4384 | bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType, |
| 4385 | QualType exprType) { |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4386 | QualType canCastType = |
| Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4387 | Context.getCanonicalType(castType).getUnqualifiedType(); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4388 | QualType canExprType = |
| Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4389 | Context.getCanonicalType(exprType).getUnqualifiedType(); |
| 4390 | if (isa<ObjCObjectPointerType>(canCastType) && |
| 4391 | castType.getObjCLifetime() == Qualifiers::OCL_Weak && |
| 4392 | canExprType->isObjCObjectPointerType()) { |
| 4393 | if (const ObjCObjectPointerType *ObjT = |
| 4394 | canExprType->getAs<ObjCObjectPointerType>()) |
| Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 4395 | if (const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl()) |
| 4396 | return !ObjI->isArcWeakrefUnavailable(); |
| Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4397 | } |
| 4398 | return true; |
| 4399 | } |
| 4400 | |
| John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4401 | /// Look for an ObjCReclaimReturnedObject cast and destroy it. |
| 4402 | static Expr *maybeUndoReclaimObject(Expr *e) { |
| Akira Hatanaka | cc7171a | 2017-10-10 01:24:33 +0000 | [diff] [blame] | 4403 | Expr *curExpr = e, *prevExpr = nullptr; |
| 4404 | |
| 4405 | // Walk down the expression until we hit an implicit cast of kind |
| 4406 | // ARCReclaimReturnedObject or an Expr that is neither a Paren nor a Cast. |
| 4407 | while (true) { |
| 4408 | if (auto *pe = dyn_cast<ParenExpr>(curExpr)) { |
| 4409 | prevExpr = curExpr; |
| 4410 | curExpr = pe->getSubExpr(); |
| 4411 | continue; |
| 4412 | } |
| 4413 | |
| 4414 | if (auto *ce = dyn_cast<CastExpr>(curExpr)) { |
| 4415 | if (auto *ice = dyn_cast<ImplicitCastExpr>(ce)) |
| 4416 | if (ice->getCastKind() == CK_ARCReclaimReturnedObject) { |
| 4417 | if (!prevExpr) |
| 4418 | return ice->getSubExpr(); |
| 4419 | if (auto *pe = dyn_cast<ParenExpr>(prevExpr)) |
| 4420 | pe->setSubExpr(ice->getSubExpr()); |
| 4421 | else |
| 4422 | cast<CastExpr>(prevExpr)->setSubExpr(ice->getSubExpr()); |
| 4423 | return e; |
| 4424 | } |
| 4425 | |
| 4426 | prevExpr = curExpr; |
| 4427 | curExpr = ce->getSubExpr(); |
| 4428 | continue; |
| 4429 | } |
| 4430 | |
| 4431 | // Break out of the loop if curExpr is neither a Paren nor a Cast. |
| 4432 | break; |
| 4433 | } |
| John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 4434 | |
| 4435 | return e; |
| 4436 | } |
| 4437 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4438 | ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc, |
| 4439 | ObjCBridgeCastKind Kind, |
| 4440 | SourceLocation BridgeKeywordLoc, |
| 4441 | TypeSourceInfo *TSInfo, |
| 4442 | Expr *SubExpr) { |
| John McCall | eb07554 | 2011-08-26 00:48:42 +0000 | [diff] [blame] | 4443 | ExprResult SubResult = UsualUnaryConversions(SubExpr); |
| 4444 | if (SubResult.isInvalid()) return ExprError(); |
| Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 4445 | SubExpr = SubResult.get(); |
| John McCall | eb07554 | 2011-08-26 00:48:42 +0000 | [diff] [blame] | 4446 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4447 | QualType T = TSInfo->getType(); |
| 4448 | QualType FromType = SubExpr->getType(); |
| 4449 | |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4450 | CastKind CK; |
| 4451 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4452 | bool MustConsume = false; |
| 4453 | if (T->isDependentType() || SubExpr->isTypeDependent()) { |
| 4454 | // Okay: we'll build a dependent expression type. |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4455 | CK = CK_Dependent; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4456 | } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) { |
| 4457 | // Casting CF -> id |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4458 | CK = (T->isBlockPointerType() ? CK_AnyPointerToBlockPointerCast |
| 4459 | : CK_CPointerToObjCPointerCast); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4460 | switch (Kind) { |
| 4461 | case OBC_Bridge: |
| 4462 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4463 | |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4464 | case OBC_BridgeRetained: { |
| Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 4465 | bool br = isKnownName("CFBridgingRelease"); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4466 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 4467 | << 2 |
| 4468 | << FromType |
| 4469 | << (T->isBlockPointerType()? 1 : 0) |
| 4470 | << T |
| 4471 | << SubExpr->getSourceRange() |
| 4472 | << Kind; |
| 4473 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 4474 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge"); |
| 4475 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer) |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4476 | << FromType << br |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4477 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
| 4478 | br ? "CFBridgingRelease " |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4479 | : "__bridge_transfer "); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4480 | |
| 4481 | Kind = OBC_Bridge; |
| 4482 | break; |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4483 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4484 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4485 | case OBC_BridgeTransfer: |
| 4486 | // We must consume the Objective-C object produced by the cast. |
| 4487 | MustConsume = true; |
| 4488 | break; |
| 4489 | } |
| 4490 | } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) { |
| 4491 | // Okay: id -> CF |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4492 | CK = CK_BitCast; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4493 | switch (Kind) { |
| Fariborz Jahanian | 214567c | 2014-10-28 17:26:21 +0000 | [diff] [blame] | 4494 | case OBC_Bridge: |
| 4495 | // Reclaiming a value that's going to be __bridge-casted to CF |
| 4496 | // is very dangerous, so we don't do it. |
| 4497 | SubExpr = maybeUndoReclaimObject(SubExpr); |
| 4498 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4499 | |
| 4500 | case OBC_BridgeRetained: |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4501 | // Produce the object before casting it. |
| 4502 | SubExpr = ImplicitCastExpr::Create(Context, FromType, |
| John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4503 | CK_ARCProduceObject, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4504 | SubExpr, nullptr, VK_RValue); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4505 | break; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4506 | |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4507 | case OBC_BridgeTransfer: { |
| Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 4508 | bool br = isKnownName("CFBridgingRetain"); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4509 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 4510 | << (FromType->isBlockPointerType()? 1 : 0) |
| 4511 | << FromType |
| 4512 | << 2 |
| 4513 | << T |
| 4514 | << SubExpr->getSourceRange() |
| 4515 | << Kind; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4516 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4517 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 4518 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge "); |
| 4519 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained) |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4520 | << T << br |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4521 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4522 | br ? "CFBridgingRetain " : "__bridge_retained"); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4523 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4524 | Kind = OBC_Bridge; |
| 4525 | break; |
| 4526 | } |
| Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 4527 | } |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4528 | } else { |
| 4529 | Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible) |
| 4530 | << FromType << T << Kind |
| 4531 | << SubExpr->getSourceRange() |
| 4532 | << TSInfo->getTypeLoc().getSourceRange(); |
| 4533 | return ExprError(); |
| 4534 | } |
| 4535 | |
| John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4536 | Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, CK, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4537 | BridgeKeywordLoc, |
| 4538 | TSInfo, SubExpr); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4539 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4540 | if (MustConsume) { |
| Tim Shen | 4a05bb8 | 2016-06-21 20:29:17 +0000 | [diff] [blame] | 4541 | Cleanup.setExprNeedsCleanups(true); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4542 | Result = ImplicitCastExpr::Create(Context, T, CK_ARCConsumeObject, Result, |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4543 | nullptr, VK_RValue); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4544 | } |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4545 | |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4546 | return Result; |
| 4547 | } |
| 4548 | |
| 4549 | ExprResult Sema::ActOnObjCBridgedCast(Scope *S, |
| 4550 | SourceLocation LParenLoc, |
| 4551 | ObjCBridgeCastKind Kind, |
| 4552 | SourceLocation BridgeKeywordLoc, |
| 4553 | ParsedType Type, |
| 4554 | SourceLocation RParenLoc, |
| 4555 | Expr *SubExpr) { |
| Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4556 | TypeSourceInfo *TSInfo = nullptr; |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4557 | QualType T = GetTypeFromParser(Type, &TSInfo); |
| Fariborz Jahanian | 8c5b4be | 2013-11-21 00:39:36 +0000 | [diff] [blame] | 4558 | if (Kind == OBC_Bridge) |
| 4559 | CheckTollFreeBridgeCast(T, SubExpr); |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4560 | if (!TSInfo) |
| 4561 | TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 4562 | return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo, |
| John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4563 | SubExpr); |
| 4564 | } |