Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1 | //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective-C expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | 021ca18 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprObjC.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 18 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 19 | #include "clang/AST/TypeLoc.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
| 21 | #include "clang/Edit/Commit.h" |
| 22 | #include "clang/Edit/Rewriters.h" |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Initialization.h" |
| 25 | #include "clang/Sema/Lookup.h" |
| 26 | #include "clang/Sema/Scope.h" |
| 27 | #include "clang/Sema/ScopeInfo.h" |
| 28 | #include "llvm/ADT/SmallString.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, |
| 35 | Expr **strings, |
| 36 | unsigned NumStrings) { |
Chris Lattner | 163ffd2 | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 37 | StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); |
| 38 | |
Chris Lattner | d7670d9 | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 39 | // Most ObjC strings are formed out of a single piece. However, we *can* |
| 40 | // have strings formed out of multiple @ strings with multiple pptokens in |
| 41 | // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one |
| 42 | // StringLiteral for ObjCStringLiteral to hold onto. |
Chris Lattner | 163ffd2 | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 43 | StringLiteral *S = Strings[0]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Chris Lattner | d7670d9 | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 45 | // If we have a multi-part string, merge it all together. |
| 46 | if (NumStrings != 1) { |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 47 | // Concatenate objc strings. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 48 | SmallString<128> StrBuf; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 49 | SmallVector<SourceLocation, 8> StrLocs; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 630970d | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 51 | for (unsigned i = 0; i != NumStrings; ++i) { |
Chris Lattner | 163ffd2 | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 52 | S = Strings[i]; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 54 | // ObjC strings can't be wide or UTF. |
| 55 | if (!S->isAscii()) { |
Chris Lattner | d7670d9 | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 56 | Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 57 | << S->getSourceRange(); |
| 58 | return true; |
| 59 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Benjamin Kramer | 35b077e | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 61 | // Append the string. |
| 62 | StrBuf += S->getString(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 163ffd2 | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 64 | // Get the locations of the string tokens. |
| 65 | StrLocs.append(S->tokloc_begin(), S->tokloc_end()); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 66 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 163ffd2 | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 68 | // Create the aggregate string with the appropriate content and location |
| 69 | // information. |
Benjamin Kramer | cdac761 | 2014-02-25 12:26:20 +0000 | [diff] [blame] | 70 | const ConstantArrayType *CAT = Context.getAsConstantArrayType(S->getType()); |
| 71 | assert(CAT && "String literal not of constant array type!"); |
| 72 | QualType StrTy = Context.getConstantArrayType( |
| 73 | CAT->getElementType(), llvm::APInt(32, StrBuf.size() + 1), |
| 74 | CAT->getSizeModifier(), CAT->getIndexTypeCVRQualifiers()); |
| 75 | S = StringLiteral::Create(Context, StrBuf, StringLiteral::Ascii, |
| 76 | /*Pascal=*/false, StrTy, &StrLocs[0], |
| 77 | StrLocs.size()); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 78 | } |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 79 | |
| 80 | return BuildObjCStringLiteral(AtLocs[0], S); |
| 81 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 83 | ExprResult Sema::BuildObjCStringLiteral(SourceLocation AtLoc, StringLiteral *S){ |
Chris Lattner | 6436fb6 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 84 | // Verify that this composite string is acceptable for ObjC strings. |
| 85 | if (CheckObjCString(S)) |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 86 | return true; |
Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 87 | |
| 88 | // Initialize the constant string interface lazily. This assumes |
Steve Naroff | 54e5945 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 89 | // the NSString interface is seen in this translation unit. Note: We |
| 90 | // don't use NSConstantString, since the runtime team considers this |
| 91 | // interface private (even though it appears in the header files). |
Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 92 | QualType Ty = Context.getObjCConstantStringInterface(); |
| 93 | if (!Ty.isNull()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 94 | Ty = Context.getObjCObjectPointerType(Ty); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 95 | } else if (getLangOpts().NoConstantCFStrings) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 96 | IdentifierInfo *NSIdent=nullptr; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 97 | std::string StringClass(getLangOpts().ObjCConstantStringClass); |
Fariborz Jahanian | 50c925f | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 98 | |
| 99 | if (StringClass.empty()) |
| 100 | NSIdent = &Context.Idents.get("NSConstantString"); |
| 101 | else |
| 102 | NSIdent = &Context.Idents.get(StringClass); |
| 103 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 104 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc, |
Fariborz Jahanian | 0731763 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 105 | LookupOrdinaryName); |
| 106 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 107 | Context.setObjCConstantStringInterface(StrIF); |
| 108 | Ty = Context.getObjCConstantStringInterface(); |
| 109 | Ty = Context.getObjCObjectPointerType(Ty); |
| 110 | } else { |
| 111 | // If there is no NSConstantString interface defined then treat this |
| 112 | // as error and recover from it. |
| 113 | Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent |
| 114 | << S->getSourceRange(); |
| 115 | Ty = Context.getObjCIdType(); |
| 116 | } |
Chris Lattner | 091f698 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 117 | } else { |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 118 | IdentifierInfo *NSIdent = NSAPIObj->getNSClassId(NSAPI::ClassId_NSString); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 119 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc, |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 120 | LookupOrdinaryName); |
Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 121 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 122 | Context.setObjCConstantStringInterface(StrIF); |
| 123 | Ty = Context.getObjCConstantStringInterface(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 124 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 125 | } else { |
Fariborz Jahanian | 86f8266 | 2012-02-23 22:51:36 +0000 | [diff] [blame] | 126 | // If there is no NSString interface defined, implicitly declare |
| 127 | // a @class NSString; and use that instead. This is to make sure |
| 128 | // type of an NSString literal is represented correctly, instead of |
| 129 | // being an 'id' type. |
| 130 | Ty = Context.getObjCNSStringType(); |
| 131 | if (Ty.isNull()) { |
| 132 | ObjCInterfaceDecl *NSStringIDecl = |
| 133 | ObjCInterfaceDecl::Create (Context, |
| 134 | Context.getTranslationUnitDecl(), |
| 135 | SourceLocation(), NSIdent, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 136 | nullptr, SourceLocation()); |
Fariborz Jahanian | 86f8266 | 2012-02-23 22:51:36 +0000 | [diff] [blame] | 137 | Ty = Context.getObjCInterfaceType(NSStringIDecl); |
| 138 | Context.setObjCNSStringType(Ty); |
| 139 | } |
| 140 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 141 | } |
Chris Lattner | 091f698 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 142 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 144 | return new (Context) ObjCStringLiteral(S, Ty, AtLoc); |
| 145 | } |
| 146 | |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 147 | /// \brief Emits an error if the given method does not exist, or if the return |
| 148 | /// type is not an Objective-C object. |
| 149 | static bool validateBoxingMethod(Sema &S, SourceLocation Loc, |
| 150 | const ObjCInterfaceDecl *Class, |
| 151 | Selector Sel, const ObjCMethodDecl *Method) { |
| 152 | if (!Method) { |
| 153 | // FIXME: Is there a better way to avoid quotes than using getName()? |
| 154 | S.Diag(Loc, diag::err_undeclared_boxing_method) << Sel << Class->getName(); |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | // Make sure the return type is reasonable. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 159 | QualType ReturnType = Method->getReturnType(); |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 160 | if (!ReturnType->isObjCObjectPointerType()) { |
| 161 | S.Diag(Loc, diag::err_objc_literal_method_sig) |
| 162 | << Sel; |
| 163 | S.Diag(Method->getLocation(), diag::note_objc_literal_method_return) |
| 164 | << ReturnType; |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 171 | /// \brief Retrieve the NSNumber factory method that should be used to create |
| 172 | /// an Objective-C literal for the given type. |
| 173 | static ObjCMethodDecl *getNSNumberFactoryMethod(Sema &S, SourceLocation Loc, |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 174 | QualType NumberType, |
| 175 | bool isLiteral = false, |
| 176 | SourceRange R = SourceRange()) { |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 177 | Optional<NSAPI::NSNumberLiteralMethodKind> Kind = |
| 178 | S.NSAPIObj->getNSNumberFactoryMethodKind(NumberType); |
| 179 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 180 | if (!Kind) { |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 181 | if (isLiteral) { |
| 182 | S.Diag(Loc, diag::err_invalid_nsnumber_type) |
| 183 | << NumberType << R; |
| 184 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 185 | return nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 186 | } |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 187 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 188 | // If we already looked up this method, we're done. |
| 189 | if (S.NSNumberLiteralMethods[*Kind]) |
| 190 | return S.NSNumberLiteralMethods[*Kind]; |
| 191 | |
| 192 | Selector Sel = S.NSAPIObj->getNSNumberLiteralSelector(*Kind, |
| 193 | /*Instance=*/false); |
| 194 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 195 | ASTContext &CX = S.Context; |
| 196 | |
| 197 | // Look up the NSNumber class, if we haven't done so already. It's cached |
| 198 | // in the Sema instance. |
| 199 | if (!S.NSNumberDecl) { |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 200 | IdentifierInfo *NSNumberId = |
| 201 | S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSNumber); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 202 | NamedDecl *IF = S.LookupSingleName(S.TUScope, NSNumberId, |
| 203 | Loc, Sema::LookupOrdinaryName); |
| 204 | S.NSNumberDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 205 | if (!S.NSNumberDecl) { |
| 206 | if (S.getLangOpts().DebuggerObjCLiteral) { |
| 207 | // Create a stub definition of NSNumber. |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 208 | S.NSNumberDecl = ObjCInterfaceDecl::Create(CX, |
| 209 | CX.getTranslationUnitDecl(), |
| 210 | SourceLocation(), NSNumberId, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 211 | nullptr, SourceLocation()); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 212 | } else { |
| 213 | // Otherwise, require a declaration of NSNumber. |
| 214 | S.Diag(Loc, diag::err_undeclared_nsnumber); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 215 | return nullptr; |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 216 | } |
| 217 | } else if (!S.NSNumberDecl->hasDefinition()) { |
| 218 | S.Diag(Loc, diag::err_undeclared_nsnumber); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 219 | return nullptr; |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // generate the pointer to NSNumber type. |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 223 | QualType NSNumberObject = CX.getObjCInterfaceType(S.NSNumberDecl); |
| 224 | S.NSNumberPointer = CX.getObjCObjectPointerType(NSNumberObject); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 227 | // Look for the appropriate method within NSNumber. |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 228 | ObjCMethodDecl *Method = S.NSNumberDecl->lookupClassMethod(Sel); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 229 | if (!Method && S.getLangOpts().DebuggerObjCLiteral) { |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 230 | // create a stub definition this NSNumber factory method. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 231 | TypeSourceInfo *ReturnTInfo = nullptr; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 232 | Method = |
| 233 | ObjCMethodDecl::Create(CX, SourceLocation(), SourceLocation(), Sel, |
| 234 | S.NSNumberPointer, ReturnTInfo, S.NSNumberDecl, |
| 235 | /*isInstance=*/false, /*isVariadic=*/false, |
| 236 | /*isPropertyAccessor=*/false, |
| 237 | /*isImplicitlyDeclared=*/true, |
| 238 | /*isDefined=*/false, ObjCMethodDecl::Required, |
| 239 | /*HasRelatedResultType=*/false); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 240 | ParmVarDecl *value = ParmVarDecl::Create(S.Context, Method, |
| 241 | SourceLocation(), SourceLocation(), |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 242 | &CX.Idents.get("value"), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 243 | NumberType, /*TInfo=*/nullptr, |
| 244 | SC_None, nullptr); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 245 | Method->setMethodParams(S.Context, value, None); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 248 | if (!validateBoxingMethod(S, Loc, S.NSNumberDecl, Sel, Method)) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 249 | return nullptr; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 250 | |
| 251 | // Note: if the parameter type is out-of-line, we'll catch it later in the |
| 252 | // implicit conversion. |
| 253 | |
| 254 | S.NSNumberLiteralMethods[*Kind] = Method; |
| 255 | return Method; |
| 256 | } |
| 257 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 258 | /// BuildObjCNumericLiteral - builds an ObjCBoxedExpr AST node for the |
| 259 | /// numeric literal expression. Type of the expression will be "NSNumber *". |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 260 | ExprResult Sema::BuildObjCNumericLiteral(SourceLocation AtLoc, Expr *Number) { |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 261 | // Determine the type of the literal. |
| 262 | QualType NumberType = Number->getType(); |
| 263 | if (CharacterLiteral *Char = dyn_cast<CharacterLiteral>(Number)) { |
| 264 | // In C, character literals have type 'int'. That's not the type we want |
| 265 | // to use to determine the Objective-c literal kind. |
| 266 | switch (Char->getKind()) { |
| 267 | case CharacterLiteral::Ascii: |
| 268 | NumberType = Context.CharTy; |
| 269 | break; |
| 270 | |
| 271 | case CharacterLiteral::Wide: |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 272 | NumberType = Context.getWideCharType(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 273 | break; |
| 274 | |
| 275 | case CharacterLiteral::UTF16: |
| 276 | NumberType = Context.Char16Ty; |
| 277 | break; |
| 278 | |
| 279 | case CharacterLiteral::UTF32: |
| 280 | NumberType = Context.Char32Ty; |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 285 | // Look for the appropriate method within NSNumber. |
| 286 | // Construct the literal. |
Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 287 | SourceRange NR(Number->getSourceRange()); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 288 | ObjCMethodDecl *Method = getNSNumberFactoryMethod(*this, AtLoc, NumberType, |
Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 289 | true, NR); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 290 | if (!Method) |
| 291 | return ExprError(); |
| 292 | |
| 293 | // Convert the number to the type that the parameter expects. |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 294 | ParmVarDecl *ParamDecl = Method->parameters()[0]; |
Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 295 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 296 | ParamDecl); |
| 297 | ExprResult ConvertedNumber = PerformCopyInitialization(Entity, |
| 298 | SourceLocation(), |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 299 | Number); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 300 | if (ConvertedNumber.isInvalid()) |
| 301 | return ExprError(); |
| 302 | Number = ConvertedNumber.get(); |
| 303 | |
Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 304 | // Use the effective source range of the literal, including the leading '@'. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 305 | return MaybeBindToTemporary( |
Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 306 | new (Context) ObjCBoxedExpr(Number, NSNumberPointer, Method, |
| 307 | SourceRange(AtLoc, NR.getEnd()))); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | ExprResult Sema::ActOnObjCBoolLiteral(SourceLocation AtLoc, |
| 311 | SourceLocation ValueLoc, |
| 312 | bool Value) { |
| 313 | ExprResult Inner; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 314 | if (getLangOpts().CPlusPlus) { |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 315 | Inner = ActOnCXXBoolLiteral(ValueLoc, Value? tok::kw_true : tok::kw_false); |
| 316 | } else { |
| 317 | // C doesn't actually have a way to represent literal values of type |
| 318 | // _Bool. So, we'll use 0/1 and implicit cast to _Bool. |
| 319 | Inner = ActOnIntegerConstant(ValueLoc, Value? 1 : 0); |
| 320 | Inner = ImpCastExprToType(Inner.get(), Context.BoolTy, |
| 321 | CK_IntegralToBoolean); |
| 322 | } |
| 323 | |
| 324 | return BuildObjCNumericLiteral(AtLoc, Inner.get()); |
| 325 | } |
| 326 | |
| 327 | /// \brief Check that the given expression is a valid element of an Objective-C |
| 328 | /// collection literal. |
| 329 | static ExprResult CheckObjCCollectionLiteralElement(Sema &S, Expr *Element, |
Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 330 | QualType T, |
| 331 | bool ArrayLiteral = false) { |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 332 | // If the expression is type-dependent, there's nothing for us to do. |
| 333 | if (Element->isTypeDependent()) |
| 334 | return Element; |
| 335 | |
| 336 | ExprResult Result = S.CheckPlaceholderExpr(Element); |
| 337 | if (Result.isInvalid()) |
| 338 | return ExprError(); |
| 339 | Element = Result.get(); |
| 340 | |
| 341 | // In C++, check for an implicit conversion to an Objective-C object pointer |
| 342 | // type. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 343 | if (S.getLangOpts().CPlusPlus && Element->getType()->isRecordType()) { |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 344 | InitializedEntity Entity |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 345 | = InitializedEntity::InitializeParameter(S.Context, T, |
| 346 | /*Consumed=*/false); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 347 | InitializationKind Kind |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 348 | = InitializationKind::CreateCopy(Element->getLocStart(), |
| 349 | SourceLocation()); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 350 | InitializationSequence Seq(S, Entity, Kind, Element); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 351 | if (!Seq.Failed()) |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 352 | return Seq.Perform(S, Entity, Kind, Element); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | Expr *OrigElement = Element; |
| 356 | |
| 357 | // Perform lvalue-to-rvalue conversion. |
| 358 | Result = S.DefaultLvalueConversion(Element); |
| 359 | if (Result.isInvalid()) |
| 360 | return ExprError(); |
| 361 | Element = Result.get(); |
| 362 | |
| 363 | // Make sure that we have an Objective-C pointer type or block. |
| 364 | if (!Element->getType()->isObjCObjectPointerType() && |
| 365 | !Element->getType()->isBlockPointerType()) { |
| 366 | bool Recovered = false; |
| 367 | |
| 368 | // If this is potentially an Objective-C numeric literal, add the '@'. |
| 369 | if (isa<IntegerLiteral>(OrigElement) || |
| 370 | isa<CharacterLiteral>(OrigElement) || |
| 371 | isa<FloatingLiteral>(OrigElement) || |
| 372 | isa<ObjCBoolLiteralExpr>(OrigElement) || |
| 373 | isa<CXXBoolLiteralExpr>(OrigElement)) { |
| 374 | if (S.NSAPIObj->getNSNumberFactoryMethodKind(OrigElement->getType())) { |
| 375 | int Which = isa<CharacterLiteral>(OrigElement) ? 1 |
| 376 | : (isa<CXXBoolLiteralExpr>(OrigElement) || |
| 377 | isa<ObjCBoolLiteralExpr>(OrigElement)) ? 2 |
| 378 | : 3; |
| 379 | |
| 380 | S.Diag(OrigElement->getLocStart(), diag::err_box_literal_collection) |
| 381 | << Which << OrigElement->getSourceRange() |
| 382 | << FixItHint::CreateInsertion(OrigElement->getLocStart(), "@"); |
| 383 | |
| 384 | Result = S.BuildObjCNumericLiteral(OrigElement->getLocStart(), |
| 385 | OrigElement); |
| 386 | if (Result.isInvalid()) |
| 387 | return ExprError(); |
| 388 | |
| 389 | Element = Result.get(); |
| 390 | Recovered = true; |
| 391 | } |
| 392 | } |
| 393 | // If this is potentially an Objective-C string literal, add the '@'. |
| 394 | else if (StringLiteral *String = dyn_cast<StringLiteral>(OrigElement)) { |
| 395 | if (String->isAscii()) { |
| 396 | S.Diag(OrigElement->getLocStart(), diag::err_box_literal_collection) |
| 397 | << 0 << OrigElement->getSourceRange() |
| 398 | << FixItHint::CreateInsertion(OrigElement->getLocStart(), "@"); |
| 399 | |
| 400 | Result = S.BuildObjCStringLiteral(OrigElement->getLocStart(), String); |
| 401 | if (Result.isInvalid()) |
| 402 | return ExprError(); |
| 403 | |
| 404 | Element = Result.get(); |
| 405 | Recovered = true; |
| 406 | } |
| 407 | } |
Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 409 | if (!Recovered) { |
| 410 | S.Diag(Element->getLocStart(), diag::err_invalid_collection_element) |
| 411 | << Element->getType(); |
| 412 | return ExprError(); |
| 413 | } |
| 414 | } |
Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 415 | if (ArrayLiteral) |
Ted Kremenek | 197fee4 | 2013-10-09 22:34:33 +0000 | [diff] [blame] | 416 | if (ObjCStringLiteral *getString = |
| 417 | dyn_cast<ObjCStringLiteral>(OrigElement)) { |
| 418 | if (StringLiteral *SL = getString->getString()) { |
| 419 | unsigned numConcat = SL->getNumConcatenated(); |
| 420 | if (numConcat > 1) { |
| 421 | // Only warn if the concatenated string doesn't come from a macro. |
| 422 | bool hasMacro = false; |
| 423 | for (unsigned i = 0; i < numConcat ; ++i) |
| 424 | if (SL->getStrTokenLoc(i).isMacroID()) { |
| 425 | hasMacro = true; |
| 426 | break; |
| 427 | } |
| 428 | if (!hasMacro) |
| 429 | S.Diag(Element->getLocStart(), |
| 430 | diag::warn_concatenated_nsarray_literal) |
| 431 | << Element->getType(); |
| 432 | } |
| 433 | } |
Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 436 | // Make sure that the element has the type that the container factory |
| 437 | // function expects. |
| 438 | return S.PerformCopyInitialization( |
| 439 | InitializedEntity::InitializeParameter(S.Context, T, |
| 440 | /*Consumed=*/false), |
| 441 | Element->getLocStart(), Element); |
| 442 | } |
| 443 | |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 444 | ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) { |
| 445 | if (ValueExpr->isTypeDependent()) { |
| 446 | ObjCBoxedExpr *BoxedExpr = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 447 | new (Context) ObjCBoxedExpr(ValueExpr, Context.DependentTy, nullptr, SR); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 448 | return BoxedExpr; |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 449 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 450 | ObjCMethodDecl *BoxingMethod = nullptr; |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 451 | QualType BoxedType; |
| 452 | // Convert the expression to an RValue, so we can check for pointer types... |
| 453 | ExprResult RValue = DefaultFunctionArrayLvalueConversion(ValueExpr); |
| 454 | if (RValue.isInvalid()) { |
| 455 | return ExprError(); |
| 456 | } |
| 457 | ValueExpr = RValue.get(); |
Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 458 | QualType ValueType(ValueExpr->getType()); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 459 | if (const PointerType *PT = ValueType->getAs<PointerType>()) { |
| 460 | QualType PointeeType = PT->getPointeeType(); |
| 461 | if (Context.hasSameUnqualifiedType(PointeeType, Context.CharTy)) { |
| 462 | |
| 463 | if (!NSStringDecl) { |
| 464 | IdentifierInfo *NSStringId = |
| 465 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSString); |
| 466 | NamedDecl *Decl = LookupSingleName(TUScope, NSStringId, |
| 467 | SR.getBegin(), LookupOrdinaryName); |
| 468 | NSStringDecl = dyn_cast_or_null<ObjCInterfaceDecl>(Decl); |
| 469 | if (!NSStringDecl) { |
| 470 | if (getLangOpts().DebuggerObjCLiteral) { |
| 471 | // Support boxed expressions in the debugger w/o NSString declaration. |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 472 | DeclContext *TU = Context.getTranslationUnitDecl(); |
| 473 | NSStringDecl = ObjCInterfaceDecl::Create(Context, TU, |
| 474 | SourceLocation(), |
| 475 | NSStringId, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 476 | nullptr, SourceLocation()); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 477 | } else { |
| 478 | Diag(SR.getBegin(), diag::err_undeclared_nsstring); |
| 479 | return ExprError(); |
| 480 | } |
| 481 | } else if (!NSStringDecl->hasDefinition()) { |
| 482 | Diag(SR.getBegin(), diag::err_undeclared_nsstring); |
| 483 | return ExprError(); |
| 484 | } |
| 485 | assert(NSStringDecl && "NSStringDecl should not be NULL"); |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 486 | QualType NSStringObject = Context.getObjCInterfaceType(NSStringDecl); |
| 487 | NSStringPointer = Context.getObjCObjectPointerType(NSStringObject); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | if (!StringWithUTF8StringMethod) { |
| 491 | IdentifierInfo *II = &Context.Idents.get("stringWithUTF8String"); |
| 492 | Selector stringWithUTF8String = Context.Selectors.getUnarySelector(II); |
| 493 | |
| 494 | // Look for the appropriate method within NSString. |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 495 | BoxingMethod = NSStringDecl->lookupClassMethod(stringWithUTF8String); |
| 496 | if (!BoxingMethod && getLangOpts().DebuggerObjCLiteral) { |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 497 | // Debugger needs to work even if NSString hasn't been defined. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 498 | TypeSourceInfo *ReturnTInfo = nullptr; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 499 | ObjCMethodDecl *M = ObjCMethodDecl::Create( |
| 500 | Context, SourceLocation(), SourceLocation(), stringWithUTF8String, |
| 501 | NSStringPointer, ReturnTInfo, NSStringDecl, |
| 502 | /*isInstance=*/false, /*isVariadic=*/false, |
| 503 | /*isPropertyAccessor=*/false, |
| 504 | /*isImplicitlyDeclared=*/true, |
| 505 | /*isDefined=*/false, ObjCMethodDecl::Required, |
| 506 | /*HasRelatedResultType=*/false); |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 507 | QualType ConstCharType = Context.CharTy.withConst(); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 508 | ParmVarDecl *value = |
| 509 | ParmVarDecl::Create(Context, M, |
| 510 | SourceLocation(), SourceLocation(), |
| 511 | &Context.Idents.get("value"), |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 512 | Context.getPointerType(ConstCharType), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 513 | /*TInfo=*/nullptr, |
| 514 | SC_None, nullptr); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 515 | M->setMethodParams(Context, value, None); |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 516 | BoxingMethod = M; |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 517 | } |
Jordy Rose | 890f457 | 2012-05-12 15:53:41 +0000 | [diff] [blame] | 518 | |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 519 | if (!validateBoxingMethod(*this, SR.getBegin(), NSStringDecl, |
| 520 | stringWithUTF8String, BoxingMethod)) |
| 521 | return ExprError(); |
| 522 | |
| 523 | StringWithUTF8StringMethod = BoxingMethod; |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | BoxingMethod = StringWithUTF8StringMethod; |
| 527 | BoxedType = NSStringPointer; |
| 528 | } |
Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 529 | } else if (ValueType->isBuiltinType()) { |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 530 | // The other types we support are numeric, char and BOOL/bool. We could also |
| 531 | // provide limited support for structure types, such as NSRange, NSRect, and |
| 532 | // NSSize. See NSValue (NSValueGeometryExtensions) in <Foundation/NSGeometry.h> |
| 533 | // for more details. |
| 534 | |
| 535 | // Check for a top-level character literal. |
| 536 | if (const CharacterLiteral *Char = |
| 537 | dyn_cast<CharacterLiteral>(ValueExpr->IgnoreParens())) { |
| 538 | // In C, character literals have type 'int'. That's not the type we want |
| 539 | // to use to determine the Objective-c literal kind. |
| 540 | switch (Char->getKind()) { |
| 541 | case CharacterLiteral::Ascii: |
| 542 | ValueType = Context.CharTy; |
| 543 | break; |
| 544 | |
| 545 | case CharacterLiteral::Wide: |
Hans Wennborg | 0d81e01 | 2013-05-10 10:08:40 +0000 | [diff] [blame] | 546 | ValueType = Context.getWideCharType(); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 547 | break; |
| 548 | |
| 549 | case CharacterLiteral::UTF16: |
| 550 | ValueType = Context.Char16Ty; |
| 551 | break; |
| 552 | |
| 553 | case CharacterLiteral::UTF32: |
| 554 | ValueType = Context.Char32Ty; |
| 555 | break; |
| 556 | } |
| 557 | } |
Fariborz Jahanian | 5d64abb | 2014-06-18 20:49:02 +0000 | [diff] [blame] | 558 | CheckForIntOverflow(ValueExpr); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 559 | // FIXME: Do I need to do anything special with BoolTy expressions? |
| 560 | |
| 561 | // Look for the appropriate method within NSNumber. |
| 562 | BoxingMethod = getNSNumberFactoryMethod(*this, SR.getBegin(), ValueType); |
| 563 | BoxedType = NSNumberPointer; |
Argyrios Kyrtzidis | 8e6951d | 2012-05-15 19:17:44 +0000 | [diff] [blame] | 564 | |
| 565 | } else if (const EnumType *ET = ValueType->getAs<EnumType>()) { |
| 566 | if (!ET->getDecl()->isComplete()) { |
| 567 | Diag(SR.getBegin(), diag::err_objc_incomplete_boxed_expression_type) |
| 568 | << ValueType << ValueExpr->getSourceRange(); |
| 569 | return ExprError(); |
| 570 | } |
| 571 | |
| 572 | BoxingMethod = getNSNumberFactoryMethod(*this, SR.getBegin(), |
| 573 | ET->getDecl()->getIntegerType()); |
| 574 | BoxedType = NSNumberPointer; |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | if (!BoxingMethod) { |
| 578 | Diag(SR.getBegin(), diag::err_objc_illegal_boxed_expression_type) |
| 579 | << ValueType << ValueExpr->getSourceRange(); |
| 580 | return ExprError(); |
| 581 | } |
| 582 | |
| 583 | // Convert the expression to the type that the parameter requires. |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 584 | ParmVarDecl *ParamDecl = BoxingMethod->parameters()[0]; |
Patrick Beard | 2565c59 | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 585 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 586 | ParamDecl); |
| 587 | ExprResult ConvertedValueExpr = PerformCopyInitialization(Entity, |
| 588 | SourceLocation(), |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 589 | ValueExpr); |
Patrick Beard | 0caa394 | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 590 | if (ConvertedValueExpr.isInvalid()) |
| 591 | return ExprError(); |
| 592 | ValueExpr = ConvertedValueExpr.get(); |
| 593 | |
| 594 | ObjCBoxedExpr *BoxedExpr = |
| 595 | new (Context) ObjCBoxedExpr(ValueExpr, BoxedType, |
| 596 | BoxingMethod, SR); |
| 597 | return MaybeBindToTemporary(BoxedExpr); |
| 598 | } |
| 599 | |
John McCall | f253834 | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 600 | /// Build an ObjC subscript pseudo-object expression, given that |
| 601 | /// that's supported by the runtime. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 602 | ExprResult Sema::BuildObjCSubscriptExpression(SourceLocation RB, Expr *BaseExpr, |
| 603 | Expr *IndexExpr, |
| 604 | ObjCMethodDecl *getterMethod, |
| 605 | ObjCMethodDecl *setterMethod) { |
Fariborz Jahanian | e1e33f8 | 2013-11-01 21:58:17 +0000 | [diff] [blame] | 606 | assert(!LangOpts.isSubscriptPointerArithmetic()); |
John McCall | 5fb5df9 | 2012-06-20 06:18:46 +0000 | [diff] [blame] | 607 | |
John McCall | f253834 | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 608 | // We can't get dependent types here; our callers should have |
| 609 | // filtered them out. |
| 610 | assert((!BaseExpr->isTypeDependent() && !IndexExpr->isTypeDependent()) && |
| 611 | "base or index cannot have dependent type here"); |
| 612 | |
| 613 | // Filter out placeholders in the index. In theory, overloads could |
| 614 | // be preserved here, although that might not actually work correctly. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 615 | ExprResult Result = CheckPlaceholderExpr(IndexExpr); |
| 616 | if (Result.isInvalid()) |
| 617 | return ExprError(); |
| 618 | IndexExpr = Result.get(); |
| 619 | |
John McCall | f253834 | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 620 | // Perform lvalue-to-rvalue conversion on the base. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 621 | Result = DefaultLvalueConversion(BaseExpr); |
| 622 | if (Result.isInvalid()) |
| 623 | return ExprError(); |
| 624 | BaseExpr = Result.get(); |
John McCall | f253834 | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 625 | |
| 626 | // Build the pseudo-object expression. |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 627 | return ObjCSubscriptRefExpr::Create(Context, BaseExpr, IndexExpr, |
| 628 | Context.PseudoObjectTy, getterMethod, |
| 629 | setterMethod, RB); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) { |
| 633 | // Look up the NSArray class, if we haven't done so already. |
| 634 | if (!NSArrayDecl) { |
| 635 | NamedDecl *IF = LookupSingleName(TUScope, |
| 636 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSArray), |
| 637 | SR.getBegin(), |
| 638 | LookupOrdinaryName); |
| 639 | NSArrayDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 640 | if (!NSArrayDecl && getLangOpts().DebuggerObjCLiteral) |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 641 | NSArrayDecl = ObjCInterfaceDecl::Create (Context, |
| 642 | Context.getTranslationUnitDecl(), |
| 643 | SourceLocation(), |
| 644 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSArray), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 645 | nullptr, SourceLocation()); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 646 | |
| 647 | if (!NSArrayDecl) { |
| 648 | Diag(SR.getBegin(), diag::err_undeclared_nsarray); |
| 649 | return ExprError(); |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | // Find the arrayWithObjects:count: method, if we haven't done so already. |
| 654 | QualType IdT = Context.getObjCIdType(); |
| 655 | if (!ArrayWithObjectsMethod) { |
| 656 | Selector |
| 657 | Sel = NSAPIObj->getNSArraySelector(NSAPI::NSArr_arrayWithObjectsCount); |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 658 | ObjCMethodDecl *Method = NSArrayDecl->lookupClassMethod(Sel); |
| 659 | if (!Method && getLangOpts().DebuggerObjCLiteral) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 660 | TypeSourceInfo *ReturnTInfo = nullptr; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 661 | Method = ObjCMethodDecl::Create( |
| 662 | Context, SourceLocation(), SourceLocation(), Sel, IdT, ReturnTInfo, |
| 663 | Context.getTranslationUnitDecl(), false /*Instance*/, |
| 664 | false /*isVariadic*/, |
| 665 | /*isPropertyAccessor=*/false, |
| 666 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
| 667 | ObjCMethodDecl::Required, false); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 668 | SmallVector<ParmVarDecl *, 2> Params; |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 669 | ParmVarDecl *objects = ParmVarDecl::Create(Context, Method, |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 670 | SourceLocation(), |
| 671 | SourceLocation(), |
| 672 | &Context.Idents.get("objects"), |
| 673 | Context.getPointerType(IdT), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 674 | /*TInfo=*/nullptr, |
| 675 | SC_None, nullptr); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 676 | Params.push_back(objects); |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 677 | ParmVarDecl *cnt = ParmVarDecl::Create(Context, Method, |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 678 | SourceLocation(), |
| 679 | SourceLocation(), |
| 680 | &Context.Idents.get("cnt"), |
| 681 | Context.UnsignedLongTy, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 682 | /*TInfo=*/nullptr, SC_None, |
| 683 | nullptr); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 684 | Params.push_back(cnt); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 685 | Method->setMethodParams(Context, Params, None); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 688 | if (!validateBoxingMethod(*this, SR.getBegin(), NSArrayDecl, Sel, Method)) |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 689 | return ExprError(); |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 690 | |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 691 | // Dig out the type that all elements should be converted to. |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 692 | QualType T = Method->parameters()[0]->getType(); |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 693 | const PointerType *PtrT = T->getAs<PointerType>(); |
| 694 | if (!PtrT || |
| 695 | !Context.hasSameUnqualifiedType(PtrT->getPointeeType(), IdT)) { |
| 696 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 697 | << Sel; |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 698 | Diag(Method->parameters()[0]->getLocation(), |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 699 | diag::note_objc_literal_method_param) |
| 700 | << 0 << T |
| 701 | << Context.getPointerType(IdT.withConst()); |
| 702 | return ExprError(); |
| 703 | } |
| 704 | |
| 705 | // Check that the 'count' parameter is integral. |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 706 | if (!Method->parameters()[1]->getType()->isIntegerType()) { |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 707 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 708 | << Sel; |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 709 | Diag(Method->parameters()[1]->getLocation(), |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 710 | diag::note_objc_literal_method_param) |
| 711 | << 1 |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 712 | << Method->parameters()[1]->getType() |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 713 | << "integral"; |
| 714 | return ExprError(); |
| 715 | } |
| 716 | |
| 717 | // We've found a good +arrayWithObjects:count: method. Save it! |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 718 | ArrayWithObjectsMethod = Method; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 721 | QualType ObjectsType = ArrayWithObjectsMethod->parameters()[0]->getType(); |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 722 | QualType RequiredType = ObjectsType->castAs<PointerType>()->getPointeeType(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 723 | |
| 724 | // Check that each of the elements provided is valid in a collection literal, |
| 725 | // performing conversions as necessary. |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 726 | Expr **ElementsBuffer = Elements.data(); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 727 | for (unsigned I = 0, N = Elements.size(); I != N; ++I) { |
| 728 | ExprResult Converted = CheckObjCCollectionLiteralElement(*this, |
| 729 | ElementsBuffer[I], |
Fariborz Jahanian | a802c35 | 2013-08-13 23:44:55 +0000 | [diff] [blame] | 730 | RequiredType, true); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 731 | if (Converted.isInvalid()) |
| 732 | return ExprError(); |
| 733 | |
| 734 | ElementsBuffer[I] = Converted.get(); |
| 735 | } |
| 736 | |
| 737 | QualType Ty |
| 738 | = Context.getObjCObjectPointerType( |
| 739 | Context.getObjCInterfaceType(NSArrayDecl)); |
| 740 | |
| 741 | return MaybeBindToTemporary( |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 742 | ObjCArrayLiteral::Create(Context, Elements, Ty, |
| 743 | ArrayWithObjectsMethod, SR)); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR, |
| 747 | ObjCDictionaryElement *Elements, |
| 748 | unsigned NumElements) { |
| 749 | // Look up the NSDictionary class, if we haven't done so already. |
| 750 | if (!NSDictionaryDecl) { |
| 751 | NamedDecl *IF = LookupSingleName(TUScope, |
| 752 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSDictionary), |
| 753 | SR.getBegin(), LookupOrdinaryName); |
| 754 | NSDictionaryDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 755 | if (!NSDictionaryDecl && getLangOpts().DebuggerObjCLiteral) |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 756 | NSDictionaryDecl = ObjCInterfaceDecl::Create (Context, |
| 757 | Context.getTranslationUnitDecl(), |
| 758 | SourceLocation(), |
| 759 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSDictionary), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 760 | nullptr, SourceLocation()); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 761 | |
| 762 | if (!NSDictionaryDecl) { |
| 763 | Diag(SR.getBegin(), diag::err_undeclared_nsdictionary); |
| 764 | return ExprError(); |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | // Find the dictionaryWithObjects:forKeys:count: method, if we haven't done |
| 769 | // so already. |
| 770 | QualType IdT = Context.getObjCIdType(); |
| 771 | if (!DictionaryWithObjectsMethod) { |
| 772 | Selector Sel = NSAPIObj->getNSDictionarySelector( |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 773 | NSAPI::NSDict_dictionaryWithObjectsForKeysCount); |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 774 | ObjCMethodDecl *Method = NSDictionaryDecl->lookupClassMethod(Sel); |
| 775 | if (!Method && getLangOpts().DebuggerObjCLiteral) { |
| 776 | Method = ObjCMethodDecl::Create(Context, |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 777 | SourceLocation(), SourceLocation(), Sel, |
| 778 | IdT, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 779 | nullptr /*TypeSourceInfo */, |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 780 | Context.getTranslationUnitDecl(), |
| 781 | false /*Instance*/, false/*isVariadic*/, |
Jordan Rose | d01e83a | 2012-10-10 16:42:25 +0000 | [diff] [blame] | 782 | /*isPropertyAccessor=*/false, |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 783 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
| 784 | ObjCMethodDecl::Required, |
| 785 | false); |
| 786 | SmallVector<ParmVarDecl *, 3> Params; |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 787 | ParmVarDecl *objects = ParmVarDecl::Create(Context, Method, |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 788 | SourceLocation(), |
| 789 | SourceLocation(), |
| 790 | &Context.Idents.get("objects"), |
| 791 | Context.getPointerType(IdT), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 792 | /*TInfo=*/nullptr, SC_None, |
| 793 | nullptr); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 794 | Params.push_back(objects); |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 795 | ParmVarDecl *keys = ParmVarDecl::Create(Context, Method, |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 796 | SourceLocation(), |
| 797 | SourceLocation(), |
| 798 | &Context.Idents.get("keys"), |
| 799 | Context.getPointerType(IdT), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 800 | /*TInfo=*/nullptr, SC_None, |
| 801 | nullptr); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 802 | Params.push_back(keys); |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 803 | ParmVarDecl *cnt = ParmVarDecl::Create(Context, Method, |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 804 | SourceLocation(), |
| 805 | SourceLocation(), |
| 806 | &Context.Idents.get("cnt"), |
| 807 | Context.UnsignedLongTy, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 808 | /*TInfo=*/nullptr, SC_None, |
| 809 | nullptr); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 810 | Params.push_back(cnt); |
Dmitri Gribenko | 44ebbd5 | 2013-05-05 00:41:58 +0000 | [diff] [blame] | 811 | Method->setMethodParams(Context, Params, None); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Jordy Rose | 08e500c | 2012-05-12 17:32:44 +0000 | [diff] [blame] | 814 | if (!validateBoxingMethod(*this, SR.getBegin(), NSDictionaryDecl, Sel, |
| 815 | Method)) |
| 816 | return ExprError(); |
| 817 | |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 818 | // Dig out the type that all values should be converted to. |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 819 | QualType ValueT = Method->parameters()[0]->getType(); |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 820 | const PointerType *PtrValue = ValueT->getAs<PointerType>(); |
| 821 | if (!PtrValue || |
| 822 | !Context.hasSameUnqualifiedType(PtrValue->getPointeeType(), IdT)) { |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 823 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 824 | << Sel; |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 825 | Diag(Method->parameters()[0]->getLocation(), |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 826 | diag::note_objc_literal_method_param) |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 827 | << 0 << ValueT |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 828 | << Context.getPointerType(IdT.withConst()); |
| 829 | return ExprError(); |
| 830 | } |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 831 | |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 832 | // Dig out the type that all keys should be converted to. |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 833 | QualType KeyT = Method->parameters()[1]->getType(); |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 834 | const PointerType *PtrKey = KeyT->getAs<PointerType>(); |
| 835 | if (!PtrKey || |
| 836 | !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), |
| 837 | IdT)) { |
| 838 | bool err = true; |
| 839 | if (PtrKey) { |
| 840 | if (QIDNSCopying.isNull()) { |
| 841 | // key argument of selector is id<NSCopying>? |
| 842 | if (ObjCProtocolDecl *NSCopyingPDecl = |
| 843 | LookupProtocol(&Context.Idents.get("NSCopying"), SR.getBegin())) { |
| 844 | ObjCProtocolDecl *PQ[] = {NSCopyingPDecl}; |
| 845 | QIDNSCopying = |
| 846 | Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
| 847 | (ObjCProtocolDecl**) PQ,1); |
| 848 | QIDNSCopying = Context.getObjCObjectPointerType(QIDNSCopying); |
| 849 | } |
| 850 | } |
| 851 | if (!QIDNSCopying.isNull()) |
| 852 | err = !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), |
| 853 | QIDNSCopying); |
| 854 | } |
| 855 | |
| 856 | if (err) { |
| 857 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 858 | << Sel; |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 859 | Diag(Method->parameters()[1]->getLocation(), |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 860 | diag::note_objc_literal_method_param) |
| 861 | << 1 << KeyT |
| 862 | << Context.getPointerType(IdT.withConst()); |
| 863 | return ExprError(); |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | // Check that the 'count' parameter is integral. |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 868 | QualType CountType = Method->parameters()[2]->getType(); |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 869 | if (!CountType->isIntegerType()) { |
| 870 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 871 | << Sel; |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 872 | Diag(Method->parameters()[2]->getLocation(), |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 873 | diag::note_objc_literal_method_param) |
| 874 | << 2 << CountType |
| 875 | << "integral"; |
| 876 | return ExprError(); |
| 877 | } |
| 878 | |
| 879 | // We've found a good +dictionaryWithObjects:keys:count: method; save it! |
| 880 | DictionaryWithObjectsMethod = Method; |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 883 | QualType ValuesT = DictionaryWithObjectsMethod->parameters()[0]->getType(); |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 884 | QualType ValueT = ValuesT->castAs<PointerType>()->getPointeeType(); |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 885 | QualType KeysT = DictionaryWithObjectsMethod->parameters()[1]->getType(); |
Jordy Rose | 4af4487 | 2012-05-12 17:32:56 +0000 | [diff] [blame] | 886 | QualType KeyT = KeysT->castAs<PointerType>()->getPointeeType(); |
| 887 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 888 | // Check that each of the keys and values provided is valid in a collection |
| 889 | // literal, performing conversions as necessary. |
| 890 | bool HasPackExpansions = false; |
| 891 | for (unsigned I = 0, N = NumElements; I != N; ++I) { |
| 892 | // Check the key. |
| 893 | ExprResult Key = CheckObjCCollectionLiteralElement(*this, Elements[I].Key, |
| 894 | KeyT); |
| 895 | if (Key.isInvalid()) |
| 896 | return ExprError(); |
| 897 | |
| 898 | // Check the value. |
| 899 | ExprResult Value |
| 900 | = CheckObjCCollectionLiteralElement(*this, Elements[I].Value, ValueT); |
| 901 | if (Value.isInvalid()) |
| 902 | return ExprError(); |
| 903 | |
| 904 | Elements[I].Key = Key.get(); |
| 905 | Elements[I].Value = Value.get(); |
| 906 | |
| 907 | if (Elements[I].EllipsisLoc.isInvalid()) |
| 908 | continue; |
| 909 | |
| 910 | if (!Elements[I].Key->containsUnexpandedParameterPack() && |
| 911 | !Elements[I].Value->containsUnexpandedParameterPack()) { |
| 912 | Diag(Elements[I].EllipsisLoc, |
| 913 | diag::err_pack_expansion_without_parameter_packs) |
| 914 | << SourceRange(Elements[I].Key->getLocStart(), |
| 915 | Elements[I].Value->getLocEnd()); |
| 916 | return ExprError(); |
| 917 | } |
| 918 | |
| 919 | HasPackExpansions = true; |
| 920 | } |
| 921 | |
| 922 | |
| 923 | QualType Ty |
| 924 | = Context.getObjCObjectPointerType( |
Robert Wilhelm | 88e0249 | 2013-08-19 07:57:02 +0000 | [diff] [blame] | 925 | Context.getObjCInterfaceType(NSDictionaryDecl)); |
| 926 | return MaybeBindToTemporary(ObjCDictionaryLiteral::Create( |
| 927 | Context, makeArrayRef(Elements, NumElements), HasPackExpansions, Ty, |
| 928 | DictionaryWithObjectsMethod, SR)); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Argyrios Kyrtzidis | 7da04c6 | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 931 | ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, |
Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 932 | TypeSourceInfo *EncodedTypeInfo, |
Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 933 | SourceLocation RParenLoc) { |
Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 934 | QualType EncodedType = EncodedTypeInfo->getType(); |
Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 935 | QualType StrTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | if (EncodedType->isDependentType()) |
Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 937 | StrTy = Context.DependentTy; |
| 938 | else { |
Fariborz Jahanian | d9bc6c3 | 2011-06-16 22:34:44 +0000 | [diff] [blame] | 939 | if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled. |
| 940 | !EncodedType->isVoidType()) // void is handled too. |
Argyrios Kyrtzidis | 7da04c6 | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 941 | if (RequireCompleteType(AtLoc, EncodedType, |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 942 | diag::err_incomplete_type_objc_at_encode, |
| 943 | EncodedTypeInfo->getTypeLoc())) |
Argyrios Kyrtzidis | 7da04c6 | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 944 | return ExprError(); |
| 945 | |
Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 946 | std::string Str; |
| 947 | Context.getObjCEncodingForType(EncodedType, Str); |
| 948 | |
| 949 | // The type of @encode is the same as the type of the corresponding string, |
| 950 | // which is an array type. |
| 951 | StrTy = Context.CharTy; |
| 952 | // 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] | 953 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 954 | StrTy.addConst(); |
| 955 | StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), |
| 956 | ArrayType::Normal, 0); |
| 957 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 958 | |
Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 959 | return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc); |
Anders Carlsson | 315d229 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 960 | } |
| 961 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 962 | ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 963 | SourceLocation EncodeLoc, |
| 964 | SourceLocation LParenLoc, |
| 965 | ParsedType ty, |
| 966 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | c7148c9 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 967 | // FIXME: Preserve type source info ? |
Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 968 | TypeSourceInfo *TInfo; |
| 969 | QualType EncodedType = GetTypeFromParser(ty, &TInfo); |
| 970 | if (!TInfo) |
| 971 | TInfo = Context.getTrivialTypeSourceInfo(EncodedType, |
| 972 | PP.getLocForEndOfToken(LParenLoc)); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 973 | |
Douglas Gregor | abd9e96 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 974 | return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 977 | static bool HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S, |
| 978 | SourceLocation AtLoc, |
Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 979 | SourceLocation LParenLoc, |
| 980 | SourceLocation RParenLoc, |
Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 981 | ObjCMethodDecl *Method, |
| 982 | ObjCMethodList &MethList) { |
| 983 | ObjCMethodList *M = &MethList; |
| 984 | bool Warned = false; |
| 985 | for (M = M->getNext(); M; M=M->getNext()) { |
| 986 | ObjCMethodDecl *MatchingMethodDecl = M->Method; |
| 987 | if (MatchingMethodDecl == Method || |
| 988 | isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()) || |
| 989 | MatchingMethodDecl->getSelector() != Method->getSelector()) |
| 990 | continue; |
| 991 | if (!S.MatchTwoMethodDeclarations(Method, |
| 992 | MatchingMethodDecl, Sema::MMS_loose)) { |
| 993 | if (!Warned) { |
| 994 | Warned = true; |
| 995 | S.Diag(AtLoc, diag::warning_multiple_selectors) |
Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 996 | << Method->getSelector() << FixItHint::CreateInsertion(LParenLoc, "(") |
| 997 | << FixItHint::CreateInsertion(RParenLoc, ")"); |
Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 998 | S.Diag(Method->getLocation(), diag::note_method_declared_at) |
| 999 | << Method->getDeclName(); |
| 1000 | } |
| 1001 | S.Diag(MatchingMethodDecl->getLocation(), diag::note_method_declared_at) |
| 1002 | << MatchingMethodDecl->getDeclName(); |
| 1003 | } |
| 1004 | } |
| 1005 | return Warned; |
| 1006 | } |
| 1007 | |
| 1008 | static void DiagnoseMismatchedSelectors(Sema &S, SourceLocation AtLoc, |
Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1009 | ObjCMethodDecl *Method, |
| 1010 | SourceLocation LParenLoc, |
| 1011 | SourceLocation RParenLoc, |
| 1012 | bool WarnMultipleSelectors) { |
| 1013 | if (!WarnMultipleSelectors || |
| 1014 | S.Diags.isIgnored(diag::warning_multiple_selectors, SourceLocation())) |
Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1015 | return; |
| 1016 | bool Warned = false; |
| 1017 | for (Sema::GlobalMethodPool::iterator b = S.MethodPool.begin(), |
| 1018 | e = S.MethodPool.end(); b != e; b++) { |
| 1019 | // first, instance methods |
| 1020 | ObjCMethodList &InstMethList = b->second.first; |
Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1021 | if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc, |
Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1022 | Method, InstMethList)) |
| 1023 | Warned = true; |
| 1024 | |
| 1025 | // second, class methods |
| 1026 | ObjCMethodList &ClsMethList = b->second.second; |
Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1027 | if (HelperToDiagnoseMismatchedMethodsInGlobalPool(S, AtLoc, LParenLoc, RParenLoc, |
| 1028 | Method, ClsMethList) || Warned) |
Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | } |
| 1032 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1033 | ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 1034 | SourceLocation AtLoc, |
| 1035 | SourceLocation SelLoc, |
| 1036 | SourceLocation LParenLoc, |
Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1037 | SourceLocation RParenLoc, |
| 1038 | bool WarnMultipleSelectors) { |
Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 1039 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel, |
| 1040 | SourceRange(LParenLoc, RParenLoc), false, false); |
| 1041 | if (!Method) |
| 1042 | Method = LookupFactoryMethodInGlobalPool(Sel, |
Fariborz Jahanian | 0571d9b | 2009-06-16 16:25:00 +0000 | [diff] [blame] | 1043 | SourceRange(LParenLoc, RParenLoc)); |
Fariborz Jahanian | 0c0fc9e | 2013-06-05 18:46:14 +0000 | [diff] [blame] | 1044 | if (!Method) { |
| 1045 | if (const ObjCMethodDecl *OM = SelectorsForTypoCorrection(Sel)) { |
| 1046 | Selector MatchedSel = OM->getSelector(); |
| 1047 | SourceRange SelectorRange(LParenLoc.getLocWithOffset(1), |
| 1048 | RParenLoc.getLocWithOffset(-1)); |
| 1049 | Diag(SelLoc, diag::warn_undeclared_selector_with_typo) |
| 1050 | << Sel << MatchedSel |
| 1051 | << FixItHint::CreateReplacement(SelectorRange, MatchedSel.getAsString()); |
| 1052 | |
| 1053 | } else |
| 1054 | Diag(SelLoc, diag::warn_undeclared_selector) << Sel; |
Fariborz Jahanian | 44be154 | 2014-03-12 18:34:01 +0000 | [diff] [blame] | 1055 | } else |
Fariborz Jahanian | dacffc0 | 2014-06-24 17:02:19 +0000 | [diff] [blame] | 1056 | DiagnoseMismatchedSelectors(*this, AtLoc, Method, LParenLoc, RParenLoc, |
| 1057 | WarnMultipleSelectors); |
Fariborz Jahanian | 9a88101 | 2011-07-13 19:05:43 +0000 | [diff] [blame] | 1058 | |
Fariborz Jahanian | 65a78b5 | 2014-05-09 19:51:39 +0000 | [diff] [blame] | 1059 | if (Method && |
| 1060 | Method->getImplementationControl() != ObjCMethodDecl::Optional && |
| 1061 | !getSourceManager().isInSystemHeader(Method->getLocation())) { |
Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 1062 | llvm::DenseMap<Selector, SourceLocation>::iterator Pos |
| 1063 | = ReferencedSelectors.find(Sel); |
| 1064 | if (Pos == ReferencedSelectors.end()) |
| 1065 | ReferencedSelectors.insert(std::make_pair(Sel, AtLoc)); |
Fariborz Jahanian | 9a88101 | 2011-07-13 19:05:43 +0000 | [diff] [blame] | 1066 | } |
Fariborz Jahanian | 6e7e8cc | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 1067 | |
Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 1068 | // In ARC, forbid the user from using @selector for |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1069 | // retain/release/autorelease/dealloc/retainCount. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1070 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1071 | switch (Sel.getMethodFamily()) { |
| 1072 | case OMF_retain: |
| 1073 | case OMF_release: |
| 1074 | case OMF_autorelease: |
| 1075 | case OMF_retainCount: |
| 1076 | case OMF_dealloc: |
| 1077 | Diag(AtLoc, diag::err_arc_illegal_selector) << |
| 1078 | Sel << SourceRange(LParenLoc, RParenLoc); |
| 1079 | break; |
| 1080 | |
| 1081 | case OMF_None: |
| 1082 | case OMF_alloc: |
| 1083 | case OMF_copy: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 1084 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1085 | case OMF_init: |
| 1086 | case OMF_mutableCopy: |
| 1087 | case OMF_new: |
| 1088 | case OMF_self: |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1089 | case OMF_performSelector: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1090 | break; |
| 1091 | } |
| 1092 | } |
Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 1093 | QualType Ty = Context.getObjCSelType(); |
Daniel Dunbar | 45858d2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 1094 | return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1097 | ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 1098 | SourceLocation AtLoc, |
| 1099 | SourceLocation ProtoLoc, |
| 1100 | SourceLocation LParenLoc, |
Argyrios Kyrtzidis | b7e4367 | 2012-05-16 00:50:02 +0000 | [diff] [blame] | 1101 | SourceLocation ProtoIdLoc, |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1102 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | b7e4367 | 2012-05-16 00:50:02 +0000 | [diff] [blame] | 1103 | ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoIdLoc); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1104 | if (!PDecl) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1105 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1106 | return true; |
| 1107 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1108 | |
Chris Lattner | fffd6a7 | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 1109 | QualType Ty = Context.getObjCProtoType(); |
| 1110 | if (Ty.isNull()) |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1111 | return true; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1112 | Ty = Context.getObjCObjectPointerType(Ty); |
Argyrios Kyrtzidis | b7e4367 | 2012-05-16 00:50:02 +0000 | [diff] [blame] | 1113 | return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, ProtoIdLoc, RParenLoc); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1116 | /// Try to capture an implicit reference to 'self'. |
Eli Friedman | 24af850 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 1117 | ObjCMethodDecl *Sema::tryCaptureObjCSelf(SourceLocation Loc) { |
| 1118 | DeclContext *DC = getFunctionLevelDeclContext(); |
John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1119 | |
| 1120 | // If we're not in an ObjC method, error out. Note that, unlike the |
| 1121 | // C++ case, we don't require an instance method --- class methods |
| 1122 | // still have a 'self', and we really do still need to capture it! |
| 1123 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC); |
| 1124 | if (!method) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1125 | return nullptr; |
John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1126 | |
Douglas Gregor | fdf598e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 1127 | tryCaptureVariable(method->getSelfDecl(), Loc); |
John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1128 | |
| 1129 | return method; |
| 1130 | } |
| 1131 | |
Douglas Gregor | 64910ca | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1132 | static QualType stripObjCInstanceType(ASTContext &Context, QualType T) { |
| 1133 | if (T == Context.getObjCInstanceType()) |
| 1134 | return Context.getObjCIdType(); |
| 1135 | |
| 1136 | return T; |
| 1137 | } |
| 1138 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1139 | QualType Sema::getMessageSendResultType(QualType ReceiverType, |
| 1140 | ObjCMethodDecl *Method, |
| 1141 | bool isClassMessage, bool isSuperMessage) { |
| 1142 | assert(Method && "Must have a method"); |
| 1143 | if (!Method->hasRelatedResultType()) |
| 1144 | return Method->getSendResultType(); |
| 1145 | |
| 1146 | // If a method has a related return type: |
| 1147 | // - if the method found is an instance method, but the message send |
| 1148 | // was a class message send, T is the declared return type of the method |
| 1149 | // found |
| 1150 | if (Method->isInstanceMethod() && isClassMessage) |
Douglas Gregor | 64910ca | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1151 | return stripObjCInstanceType(Context, Method->getSendResultType()); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1152 | |
| 1153 | // - if the receiver is super, T is a pointer to the class of the |
| 1154 | // enclosing method definition |
| 1155 | if (isSuperMessage) { |
| 1156 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 1157 | if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface()) |
| 1158 | return Context.getObjCObjectPointerType( |
| 1159 | Context.getObjCInterfaceType(Class)); |
| 1160 | } |
| 1161 | |
| 1162 | // - if the receiver is the name of a class U, T is a pointer to U |
| 1163 | if (ReceiverType->getAs<ObjCInterfaceType>() || |
| 1164 | ReceiverType->isObjCQualifiedInterfaceType()) |
| 1165 | return Context.getObjCObjectPointerType(ReceiverType); |
| 1166 | // - if the receiver is of type Class or qualified Class type, |
| 1167 | // T is the declared return type of the method. |
| 1168 | if (ReceiverType->isObjCClassType() || |
| 1169 | ReceiverType->isObjCQualifiedClassType()) |
Douglas Gregor | 64910ca | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1170 | return stripObjCInstanceType(Context, Method->getSendResultType()); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1171 | |
| 1172 | // - if the receiver is id, qualified id, Class, or qualified Class, T |
| 1173 | // is the receiver type, otherwise |
| 1174 | // - T is the type of the receiver expression. |
| 1175 | return ReceiverType; |
| 1176 | } |
John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1177 | |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1178 | /// Look for an ObjC method whose result type exactly matches the given type. |
| 1179 | static const ObjCMethodDecl * |
| 1180 | findExplicitInstancetypeDeclarer(const ObjCMethodDecl *MD, |
| 1181 | QualType instancetype) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1182 | if (MD->getReturnType() == instancetype) |
| 1183 | return MD; |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1184 | |
| 1185 | // For these purposes, a method in an @implementation overrides a |
| 1186 | // declaration in the @interface. |
| 1187 | if (const ObjCImplDecl *impl = |
| 1188 | dyn_cast<ObjCImplDecl>(MD->getDeclContext())) { |
| 1189 | const ObjCContainerDecl *iface; |
| 1190 | if (const ObjCCategoryImplDecl *catImpl = |
| 1191 | dyn_cast<ObjCCategoryImplDecl>(impl)) { |
| 1192 | iface = catImpl->getCategoryDecl(); |
| 1193 | } else { |
| 1194 | iface = impl->getClassInterface(); |
| 1195 | } |
| 1196 | |
| 1197 | const ObjCMethodDecl *ifaceMD = |
| 1198 | iface->getMethod(MD->getSelector(), MD->isInstanceMethod()); |
| 1199 | if (ifaceMD) return findExplicitInstancetypeDeclarer(ifaceMD, instancetype); |
| 1200 | } |
| 1201 | |
| 1202 | SmallVector<const ObjCMethodDecl *, 4> overrides; |
| 1203 | MD->getOverriddenMethods(overrides); |
| 1204 | for (unsigned i = 0, e = overrides.size(); i != e; ++i) { |
| 1205 | if (const ObjCMethodDecl *result = |
| 1206 | findExplicitInstancetypeDeclarer(overrides[i], instancetype)) |
| 1207 | return result; |
| 1208 | } |
| 1209 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1210 | return nullptr; |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | void Sema::EmitRelatedResultTypeNoteForReturn(QualType destType) { |
| 1214 | // Only complain if we're in an ObjC method and the required return |
| 1215 | // type doesn't match the method's declared return type. |
| 1216 | ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurContext); |
| 1217 | if (!MD || !MD->hasRelatedResultType() || |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1218 | Context.hasSameUnqualifiedType(destType, MD->getReturnType())) |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1219 | return; |
| 1220 | |
| 1221 | // Look for a method overridden by this method which explicitly uses |
| 1222 | // 'instancetype'. |
| 1223 | if (const ObjCMethodDecl *overridden = |
| 1224 | findExplicitInstancetypeDeclarer(MD, Context.getObjCInstanceType())) { |
| 1225 | SourceLocation loc; |
| 1226 | SourceRange range; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1227 | if (TypeSourceInfo *TSI = overridden->getReturnTypeSourceInfo()) { |
John McCall | 5ec7e7d | 2013-03-19 07:04:25 +0000 | [diff] [blame] | 1228 | range = TSI->getTypeLoc().getSourceRange(); |
| 1229 | loc = range.getBegin(); |
| 1230 | } |
| 1231 | if (loc.isInvalid()) |
| 1232 | loc = overridden->getLocation(); |
| 1233 | Diag(loc, diag::note_related_result_type_explicit) |
| 1234 | << /*current method*/ 1 << range; |
| 1235 | return; |
| 1236 | } |
| 1237 | |
| 1238 | // Otherwise, if we have an interesting method family, note that. |
| 1239 | // This should always trigger if the above didn't. |
| 1240 | if (ObjCMethodFamily family = MD->getMethodFamily()) |
| 1241 | Diag(MD->getLocation(), diag::note_related_result_type_family) |
| 1242 | << /*current method*/ 1 |
| 1243 | << family; |
| 1244 | } |
| 1245 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1246 | void Sema::EmitRelatedResultTypeNote(const Expr *E) { |
| 1247 | E = E->IgnoreParenImpCasts(); |
| 1248 | const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E); |
| 1249 | if (!MsgSend) |
| 1250 | return; |
| 1251 | |
| 1252 | const ObjCMethodDecl *Method = MsgSend->getMethodDecl(); |
| 1253 | if (!Method) |
| 1254 | return; |
| 1255 | |
| 1256 | if (!Method->hasRelatedResultType()) |
| 1257 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1258 | |
| 1259 | if (Context.hasSameUnqualifiedType( |
| 1260 | Method->getReturnType().getNonReferenceType(), MsgSend->getType())) |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1261 | return; |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1262 | |
| 1263 | if (!Context.hasSameUnqualifiedType(Method->getReturnType(), |
Douglas Gregor | bab8a96 | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 1264 | Context.getObjCInstanceType())) |
| 1265 | return; |
| 1266 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1267 | Diag(Method->getLocation(), diag::note_related_result_type_inferred) |
| 1268 | << Method->isInstanceMethod() << Method->getSelector() |
| 1269 | << MsgSend->getType(); |
| 1270 | } |
| 1271 | |
| 1272 | bool Sema::CheckMessageArgumentTypes(QualType ReceiverType, |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1273 | MultiExprArg Args, |
| 1274 | Selector Sel, |
Fariborz Jahanian | 6ce25c0 | 2012-08-31 17:03:18 +0000 | [diff] [blame] | 1275 | ArrayRef<SourceLocation> SelectorLocs, |
| 1276 | ObjCMethodDecl *Method, |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1277 | bool isClassMessage, bool isSuperMessage, |
Daniel Dunbar | aa9326c | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1278 | SourceLocation lbrac, SourceLocation rbrac, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1279 | QualType &ReturnType, ExprValueKind &VK) { |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 1280 | SourceLocation SelLoc; |
| 1281 | if (!SelectorLocs.empty() && SelectorLocs.front().isValid()) |
| 1282 | SelLoc = SelectorLocs.front(); |
| 1283 | else |
| 1284 | SelLoc = lbrac; |
| 1285 | |
Daniel Dunbar | aa9326c | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1286 | if (!Method) { |
Daniel Dunbar | 83876b4 | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 1287 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1288 | for (unsigned i = 0, e = Args.size(); i != e; i++) { |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1289 | if (Args[i]->isTypeDependent()) |
| 1290 | continue; |
| 1291 | |
John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1292 | ExprResult result; |
| 1293 | if (getLangOpts().DebuggerSupport) { |
| 1294 | QualType paramTy; // ignored |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 1295 | result = checkUnknownAnyArg(SelLoc, Args[i], paramTy); |
John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1296 | } else { |
| 1297 | result = DefaultArgumentPromotion(Args[i]); |
| 1298 | } |
| 1299 | if (result.isInvalid()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1300 | return true; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1301 | Args[i] = result.get(); |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1302 | } |
Daniel Dunbar | 83876b4 | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 1303 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1304 | unsigned DiagID; |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1305 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1306 | DiagID = diag::err_arc_method_not_found; |
| 1307 | else |
| 1308 | DiagID = isClassMessage ? diag::warn_class_method_not_found |
| 1309 | : diag::warn_inst_method_not_found; |
Fariborz Jahanian | 773df4a | 2013-05-14 23:24:17 +0000 | [diff] [blame] | 1310 | if (!getLangOpts().DebuggerSupport) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 1311 | const ObjCMethodDecl *OMD = SelectorsForTypoCorrection(Sel, ReceiverType); |
Fariborz Jahanian | 0649923 | 2013-06-18 17:10:58 +0000 | [diff] [blame] | 1312 | if (OMD && !OMD->isInvalidDecl()) { |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 1313 | if (getLangOpts().ObjCAutoRefCount) |
| 1314 | DiagID = diag::error_method_not_found_with_typo; |
| 1315 | else |
| 1316 | DiagID = isClassMessage ? diag::warn_class_method_not_found_with_typo |
| 1317 | : diag::warn_instance_method_not_found_with_typo; |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 1318 | Selector MatchedSel = OMD->getSelector(); |
| 1319 | SourceRange SelectorRange(SelectorLocs.front(), SelectorLocs.back()); |
Fariborz Jahanian | 4cc5552 | 2013-06-18 15:31:36 +0000 | [diff] [blame] | 1320 | Diag(SelLoc, DiagID) |
| 1321 | << Sel<< isClassMessage << MatchedSel |
Fariborz Jahanian | 7548167 | 2013-06-17 17:10:54 +0000 | [diff] [blame] | 1322 | << FixItHint::CreateReplacement(SelectorRange, MatchedSel.getAsString()); |
| 1323 | } |
| 1324 | else |
| 1325 | Diag(SelLoc, DiagID) |
| 1326 | << Sel << isClassMessage << SourceRange(SelectorLocs.front(), |
Fariborz Jahanian | 6ce25c0 | 2012-08-31 17:03:18 +0000 | [diff] [blame] | 1327 | SelectorLocs.back()); |
Fariborz Jahanian | 773df4a | 2013-05-14 23:24:17 +0000 | [diff] [blame] | 1328 | // Find the class to which we are sending this message. |
| 1329 | if (ReceiverType->isObjCObjectPointerType()) { |
Fariborz Jahanian | 478536b | 2013-05-15 15:27:35 +0000 | [diff] [blame] | 1330 | if (ObjCInterfaceDecl *Class = |
| 1331 | ReceiverType->getAs<ObjCObjectPointerType>()->getInterfaceDecl()) |
| 1332 | Diag(Class->getLocation(), diag::note_receiver_class_declared); |
Fariborz Jahanian | 773df4a | 2013-05-14 23:24:17 +0000 | [diff] [blame] | 1333 | } |
| 1334 | } |
John McCall | 3f4138c | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 1335 | |
| 1336 | // In debuggers, we want to use __unknown_anytype for these |
| 1337 | // results so that clients can cast them. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1338 | if (getLangOpts().DebuggerSupport) { |
John McCall | 3f4138c | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 1339 | ReturnType = Context.UnknownAnyTy; |
| 1340 | } else { |
| 1341 | ReturnType = Context.getObjCIdType(); |
| 1342 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1343 | VK = VK_RValue; |
Daniel Dunbar | aa9326c | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1344 | return false; |
Daniel Dunbar | aa9326c | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1345 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1346 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1347 | ReturnType = getMessageSendResultType(ReceiverType, Method, isClassMessage, |
| 1348 | isSuperMessage); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1349 | VK = Expr::getValueKindForType(Method->getReturnType()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1350 | |
Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1351 | unsigned NumNamedArgs = Sel.getNumArgs(); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1352 | // Method might have more arguments than selector indicates. This is due |
| 1353 | // to addition of c-style arguments in method. |
| 1354 | if (Method->param_size() > Sel.getNumArgs()) |
| 1355 | NumNamedArgs = Method->param_size(); |
| 1356 | // FIXME. This need be cleaned up. |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1357 | if (Args.size() < NumNamedArgs) { |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 1358 | Diag(SelLoc, diag::err_typecheck_call_too_few_args) |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1359 | << 2 << NumNamedArgs << static_cast<unsigned>(Args.size()); |
Fariborz Jahanian | 6046209 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1360 | return false; |
| 1361 | } |
Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1362 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 1363 | bool IsError = false; |
Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1364 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1365 | // We can't do any type-checking on a type-dependent argument. |
| 1366 | if (Args[i]->isTypeDependent()) |
| 1367 | continue; |
| 1368 | |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1369 | Expr *argExpr = Args[i]; |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1370 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame^] | 1371 | ParmVarDecl *param = Method->parameters()[i]; |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1372 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1374 | // Strip the unbridged-cast placeholder expression off unless it's |
| 1375 | // a consumed argument. |
| 1376 | if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) && |
| 1377 | !param->hasAttr<CFConsumedAttr>()) |
| 1378 | argExpr = stripARCUnbridgedCast(argExpr); |
| 1379 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 1380 | // If the parameter is __unknown_anytype, infer its type |
| 1381 | // from the argument. |
| 1382 | if (param->getType() == Context.UnknownAnyTy) { |
John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1383 | QualType paramType; |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 1384 | ExprResult argE = checkUnknownAnyArg(SelLoc, argExpr, paramType); |
John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1385 | if (argE.isInvalid()) { |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 1386 | IsError = true; |
John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1387 | } else { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1388 | Args[i] = argE.get(); |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 1389 | |
John McCall | cc5788c | 2013-03-04 07:34:02 +0000 | [diff] [blame] | 1390 | // Update the parameter type in-place. |
| 1391 | param->setType(paramType); |
| 1392 | } |
| 1393 | continue; |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 1394 | } |
| 1395 | |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1396 | if (RequireCompleteType(argExpr->getSourceRange().getBegin(), |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1397 | param->getType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1398 | diag::err_call_incomplete_argument, argExpr)) |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1399 | return true; |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1400 | |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1401 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1402 | param); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1403 | ExprResult ArgE = PerformCopyInitialization(Entity, SelLoc, argExpr); |
Douglas Gregor | 6b7f12c | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1404 | if (ArgE.isInvalid()) |
| 1405 | IsError = true; |
| 1406 | else |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1407 | Args[i] = ArgE.getAs<Expr>(); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1408 | } |
Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1409 | |
| 1410 | // Promote additional arguments to variadic methods. |
| 1411 | if (Method->isVariadic()) { |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1412 | for (unsigned i = NumNamedArgs, e = Args.size(); i < e; ++i) { |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1413 | if (Args[i]->isTypeDependent()) |
| 1414 | continue; |
| 1415 | |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 1416 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1417 | nullptr); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1418 | IsError |= Arg.isInvalid(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 1419 | Args[i] = Arg.get(); |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1420 | } |
Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1421 | } else { |
| 1422 | // Check for extra arguments to non-variadic methods. |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1423 | if (Args.size() != NumNamedArgs) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1424 | Diag(Args[NumNamedArgs]->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1425 | diag::err_typecheck_call_too_many_args) |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1426 | << 2 /*method*/ << NumNamedArgs << static_cast<unsigned>(Args.size()) |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1427 | << Method->getSourceRange() |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1428 | << SourceRange(Args[NumNamedArgs]->getLocStart(), |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1429 | Args.back()->getLocEnd()); |
Daniel Dunbar | ce05c8e | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1433 | DiagnoseSentinelCalls(Method, SelLoc, Args); |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1434 | |
| 1435 | // Do additional checkings on method. |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 1436 | IsError |= CheckObjCMethodCall( |
Robert Wilhelm | 88e0249 | 2013-08-19 07:57:02 +0000 | [diff] [blame] | 1437 | Method, SelLoc, makeArrayRef<const Expr *>(Args.data(), Args.size())); |
Jean-Daniel Dupas | 0ae6e67 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1438 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 1439 | return IsError; |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1440 | } |
| 1441 | |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 1442 | bool Sema::isSelfExpr(Expr *RExpr) { |
Fariborz Jahanian | b3b1e17 | 2011-03-27 19:53:47 +0000 | [diff] [blame] | 1443 | // 'self' is objc 'self' in an objc method only. |
Argyrios Kyrtzidis | 2080d90 | 2014-01-03 18:32:18 +0000 | [diff] [blame] | 1444 | ObjCMethodDecl *Method = |
| 1445 | dyn_cast_or_null<ObjCMethodDecl>(CurContext->getNonClosureAncestor()); |
| 1446 | return isSelfExpr(RExpr, Method); |
| 1447 | } |
| 1448 | |
| 1449 | bool Sema::isSelfExpr(Expr *receiver, const ObjCMethodDecl *method) { |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1450 | if (!method) return false; |
| 1451 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1452 | receiver = receiver->IgnoreParenLValueCasts(); |
| 1453 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver)) |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1454 | if (DRE->getDecl() == method->getSelfDecl()) |
Douglas Gregor | 486b74e | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 1455 | return true; |
| 1456 | return false; |
Steve Naroff | 3f49fee | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1459 | /// LookupMethodInType - Look up a method in an ObjCObjectType. |
| 1460 | ObjCMethodDecl *Sema::LookupMethodInObjectType(Selector sel, QualType type, |
| 1461 | bool isInstance) { |
| 1462 | const ObjCObjectType *objType = type->castAs<ObjCObjectType>(); |
| 1463 | if (ObjCInterfaceDecl *iface = objType->getInterface()) { |
| 1464 | // Look it up in the main interface (and categories, etc.) |
| 1465 | if (ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance)) |
| 1466 | return method; |
| 1467 | |
| 1468 | // Okay, look for "private" methods declared in any |
| 1469 | // @implementations we've seen. |
Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 1470 | if (ObjCMethodDecl *method = iface->lookupPrivateMethod(sel, isInstance)) |
| 1471 | return method; |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | // Check qualifiers. |
Aaron Ballman | 1683f7b | 2014-03-17 15:55:30 +0000 | [diff] [blame] | 1475 | for (const auto *I : objType->quals()) |
| 1476 | if (ObjCMethodDecl *method = I->lookupMethod(sel, isInstance)) |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1477 | return method; |
| 1478 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1479 | return nullptr; |
John McCall | 526ab47 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Fariborz Jahanian | 3dc11ad | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 1482 | /// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier |
| 1483 | /// list of a qualified objective pointer type. |
| 1484 | ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel, |
| 1485 | const ObjCObjectPointerType *OPT, |
| 1486 | bool Instance) |
| 1487 | { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1488 | ObjCMethodDecl *MD = nullptr; |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 1489 | for (const auto *PROTO : OPT->quals()) { |
Fariborz Jahanian | 3dc11ad | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 1490 | if ((MD = PROTO->lookupMethod(Sel, Instance))) { |
| 1491 | return MD; |
| 1492 | } |
| 1493 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1494 | return nullptr; |
Fariborz Jahanian | 3dc11ad | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Fariborz Jahanian | d155c78 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1497 | static void DiagnoseARCUseOfWeakReceiver(Sema &S, Expr *Receiver) { |
| 1498 | if (!Receiver) |
Fariborz Jahanian | fce89c6 | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1499 | return; |
| 1500 | |
Fariborz Jahanian | 22535de | 2012-06-04 19:16:34 +0000 | [diff] [blame] | 1501 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Receiver)) |
| 1502 | Receiver = OVE->getSourceExpr(); |
| 1503 | |
Fariborz Jahanian | d155c78 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1504 | Expr *RExpr = Receiver->IgnoreParenImpCasts(); |
| 1505 | SourceLocation Loc = RExpr->getLocStart(); |
| 1506 | QualType T = RExpr->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1507 | const ObjCPropertyDecl *PDecl = nullptr; |
| 1508 | const ObjCMethodDecl *GDecl = nullptr; |
Fariborz Jahanian | d155c78 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1509 | if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(RExpr)) { |
| 1510 | RExpr = POE->getSyntacticForm(); |
| 1511 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(RExpr)) { |
| 1512 | if (PRE->isImplicitProperty()) { |
| 1513 | GDecl = PRE->getImplicitPropertyGetter(); |
| 1514 | if (GDecl) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1515 | T = GDecl->getReturnType(); |
Fariborz Jahanian | d155c78 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1516 | } |
| 1517 | } |
| 1518 | else { |
| 1519 | PDecl = PRE->getExplicitProperty(); |
| 1520 | if (PDecl) { |
| 1521 | T = PDecl->getType(); |
| 1522 | } |
| 1523 | } |
Fariborz Jahanian | fce89c6 | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1524 | } |
Fariborz Jahanian | d155c78 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1525 | } |
Fariborz Jahanian | 22535de | 2012-06-04 19:16:34 +0000 | [diff] [blame] | 1526 | else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RExpr)) { |
| 1527 | // See if receiver is a method which envokes a synthesized getter |
| 1528 | // backing a 'weak' property. |
| 1529 | ObjCMethodDecl *Method = ME->getMethodDecl(); |
Jordan Rose | 2bd991a | 2012-10-10 16:42:54 +0000 | [diff] [blame] | 1530 | if (Method && Method->getSelector().getNumArgs() == 0) { |
| 1531 | PDecl = Method->findPropertyDecl(); |
Fariborz Jahanian | 22535de | 2012-06-04 19:16:34 +0000 | [diff] [blame] | 1532 | if (PDecl) |
| 1533 | T = PDecl->getType(); |
| 1534 | } |
| 1535 | } |
Fariborz Jahanian | d155c78 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1536 | |
Jordan Rose | 13d6b71 | 2012-09-28 22:21:42 +0000 | [diff] [blame] | 1537 | if (T.getObjCLifetime() != Qualifiers::OCL_Weak) { |
| 1538 | if (!PDecl) |
| 1539 | return; |
| 1540 | if (!(PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)) |
| 1541 | return; |
Fariborz Jahanian | fce89c6 | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1542 | } |
Jordan Rose | 13d6b71 | 2012-09-28 22:21:42 +0000 | [diff] [blame] | 1543 | |
| 1544 | S.Diag(Loc, diag::warn_receiver_is_weak) |
| 1545 | << ((!PDecl && !GDecl) ? 0 : (PDecl ? 1 : 2)); |
| 1546 | |
| 1547 | if (PDecl) |
Fariborz Jahanian | d155c78 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1548 | S.Diag(PDecl->getLocation(), diag::note_property_declare); |
Jordan Rose | 13d6b71 | 2012-09-28 22:21:42 +0000 | [diff] [blame] | 1549 | else if (GDecl) |
| 1550 | S.Diag(GDecl->getLocation(), diag::note_method_declared_at) << GDecl; |
| 1551 | |
| 1552 | S.Diag(Loc, diag::note_arc_assign_to_strong); |
Fariborz Jahanian | fce89c6 | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1555 | /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an |
| 1556 | /// objective C interface. This is a property reference expression. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1557 | ExprResult Sema:: |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1558 | HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT, |
Fariborz Jahanian | c297cd8 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1559 | Expr *BaseExpr, SourceLocation OpLoc, |
| 1560 | DeclarationName MemberName, |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1561 | SourceLocation MemberLoc, |
| 1562 | SourceLocation SuperLoc, QualType SuperType, |
| 1563 | bool Super) { |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1564 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 1565 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Benjamin Kramer | 1458c1c | 2012-05-19 16:03:58 +0000 | [diff] [blame] | 1566 | |
Benjamin Kramer | 365082d | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1567 | if (!MemberName.isIdentifier()) { |
Douglas Gregor | d645931 | 2011-04-20 18:19:55 +0000 | [diff] [blame] | 1568 | Diag(MemberLoc, diag::err_invalid_property_name) |
| 1569 | << MemberName << QualType(OPT, 0); |
| 1570 | return ExprError(); |
| 1571 | } |
Benjamin Kramer | 365082d | 2012-05-19 16:34:46 +0000 | [diff] [blame] | 1572 | |
| 1573 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Douglas Gregor | d645931 | 2011-04-20 18:19:55 +0000 | [diff] [blame] | 1574 | |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1575 | SourceRange BaseRange = Super? SourceRange(SuperLoc) |
| 1576 | : BaseExpr->getSourceRange(); |
| 1577 | if (RequireCompleteType(MemberLoc, OPT->getPointeeType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1578 | diag::err_property_not_found_forward_class, |
| 1579 | MemberName, BaseRange)) |
Fariborz Jahanian | 7cabbe0 | 2010-12-16 00:56:28 +0000 | [diff] [blame] | 1580 | return ExprError(); |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1581 | |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1582 | // Search for a declared property first. |
Fariborz Jahanian | 3f88afa | 2012-05-24 22:48:38 +0000 | [diff] [blame] | 1583 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1584 | // Check whether we can reference this property. |
| 1585 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1586 | return ExprError(); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1587 | if (Super) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1588 | return new (Context) |
| 1589 | ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue, |
| 1590 | OK_ObjCProperty, MemberLoc, SuperLoc, SuperType); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1591 | else |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1592 | return new (Context) |
| 1593 | ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue, |
| 1594 | OK_ObjCProperty, MemberLoc, BaseExpr); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1595 | } |
| 1596 | // Check protocols on qualified interfaces. |
Aaron Ballman | 8373146 | 2014-03-17 16:14:00 +0000 | [diff] [blame] | 1597 | for (const auto *I : OPT->quals()) |
| 1598 | if (ObjCPropertyDecl *PD = I->FindPropertyDeclaration(Member)) { |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1599 | // Check whether we can reference this property. |
| 1600 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1601 | return ExprError(); |
Fariborz Jahanian | fce89c6 | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1602 | |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1603 | if (Super) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1604 | return new (Context) ObjCPropertyRefExpr( |
| 1605 | PD, Context.PseudoObjectTy, VK_LValue, OK_ObjCProperty, MemberLoc, |
| 1606 | SuperLoc, SuperType); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1607 | else |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1608 | return new (Context) |
| 1609 | ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, VK_LValue, |
| 1610 | OK_ObjCProperty, MemberLoc, BaseExpr); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1611 | } |
| 1612 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1613 | // selector is implemented. |
| 1614 | |
| 1615 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1616 | // shared with the code in ActOnInstanceMessage. |
| 1617 | |
| 1618 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 1619 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1620 | |
| 1621 | // May be founf in property's qualified list. |
| 1622 | if (!Getter) |
| 1623 | Getter = LookupMethodInQualifiedType(Sel, OPT, true); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1624 | |
| 1625 | // If this reference is in an @implementation, check for 'private' methods. |
| 1626 | if (!Getter) |
Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 1627 | Getter = IFace->lookupPrivateMethod(Sel); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1628 | |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1629 | if (Getter) { |
| 1630 | // Check if we can reference this property. |
| 1631 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1632 | return ExprError(); |
| 1633 | } |
| 1634 | // If we found a getter then this may be a valid dot-reference, we |
| 1635 | // will look for the matching setter, in case it is needed. |
| 1636 | Selector SetterSel = |
Adrian Prantl | a4ce906 | 2013-06-07 22:29:12 +0000 | [diff] [blame] | 1637 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1638 | PP.getSelectorTable(), Member); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1639 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Fariborz Jahanian | 3f88afa | 2012-05-24 22:48:38 +0000 | [diff] [blame] | 1640 | |
Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1641 | // May be founf in property's qualified list. |
| 1642 | if (!Setter) |
| 1643 | Setter = LookupMethodInQualifiedType(SetterSel, OPT, true); |
| 1644 | |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1645 | if (!Setter) { |
| 1646 | // If this reference is in an @implementation, also check for 'private' |
| 1647 | // methods. |
Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 1648 | Setter = IFace->lookupPrivateMethod(SetterSel); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1649 | } |
Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1651 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1652 | return ExprError(); |
| 1653 | |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1654 | if (Getter || Setter) { |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1655 | if (Super) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1656 | return new (Context) |
| 1657 | ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue, |
| 1658 | OK_ObjCProperty, MemberLoc, SuperLoc, SuperType); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1659 | else |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1660 | return new (Context) |
| 1661 | ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue, |
| 1662 | OK_ObjCProperty, MemberLoc, BaseExpr); |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1663 | |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | // Attempt to correct for typos in property names. |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1667 | DeclFilterCCC<ObjCPropertyDecl> Validator; |
| 1668 | if (TypoCorrection Corrected = CorrectTypo( |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1669 | DeclarationNameInfo(MemberName, MemberLoc), LookupOrdinaryName, |
| 1670 | nullptr, nullptr, Validator, CTK_ErrorRecovery, IFace, false, OPT)) { |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1671 | diagnoseTypo(Corrected, PDiag(diag::err_property_not_found_suggest) |
| 1672 | << MemberName << QualType(OPT, 0)); |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1673 | DeclarationName TypoResult = Corrected.getCorrection(); |
Fariborz Jahanian | c297cd8 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1674 | return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc, |
| 1675 | TypoResult, MemberLoc, |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1676 | SuperLoc, SuperType, Super); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1677 | } |
Fariborz Jahanian | 05d389f | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1678 | ObjCInterfaceDecl *ClassDeclared; |
| 1679 | if (ObjCIvarDecl *Ivar = |
| 1680 | IFace->lookupInstanceVariable(Member, ClassDeclared)) { |
| 1681 | QualType T = Ivar->getType(); |
| 1682 | if (const ObjCObjectPointerType * OBJPT = |
| 1683 | T->getAsObjCInterfacePointerType()) { |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1684 | if (RequireCompleteType(MemberLoc, OBJPT->getPointeeType(), |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 1685 | diag::err_property_not_as_forward_class, |
| 1686 | MemberName, BaseExpr)) |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1687 | return ExprError(); |
Fariborz Jahanian | 05d389f | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1688 | } |
Fariborz Jahanian | c297cd8 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1689 | Diag(MemberLoc, |
| 1690 | diag::err_ivar_access_using_property_syntax_suggest) |
| 1691 | << MemberName << QualType(OPT, 0) << Ivar->getDeclName() |
| 1692 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| 1693 | return ExprError(); |
Fariborz Jahanian | 05d389f | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1694 | } |
Chris Lattner | 90c58fa | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 1695 | |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1696 | Diag(MemberLoc, diag::err_property_not_found) |
| 1697 | << MemberName << QualType(OPT, 0); |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1698 | if (Setter) |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1699 | Diag(Setter->getLocation(), diag::note_getter_unavailable) |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1700 | << MemberName << BaseExpr->getSourceRange(); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1701 | return ExprError(); |
Chris Lattner | 2b1ca5f | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
| 1704 | |
| 1705 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1706 | ExprResult Sema:: |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1707 | ActOnClassPropertyRefExpr(IdentifierInfo &receiverName, |
| 1708 | IdentifierInfo &propertyName, |
| 1709 | SourceLocation receiverNameLoc, |
| 1710 | SourceLocation propertyNameLoc) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1711 | |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1712 | IdentifierInfo *receiverNamePtr = &receiverName; |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1713 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr, |
| 1714 | receiverNameLoc); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1715 | |
| 1716 | bool IsSuper = false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1717 | if (!IFace) { |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1718 | // If the "receiver" is 'super' in a method, handle it as an expression-like |
| 1719 | // property reference. |
John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1720 | if (receiverNamePtr->isStr("super")) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1721 | IsSuper = true; |
| 1722 | |
Eli Friedman | 24af850 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 1723 | if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf(receiverNameLoc)) { |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1724 | if (CurMethod->isInstanceMethod()) { |
Fariborz Jahanian | 05e2aaa | 2013-03-11 22:26:33 +0000 | [diff] [blame] | 1725 | ObjCInterfaceDecl *Super = |
| 1726 | CurMethod->getClassInterface()->getSuperClass(); |
| 1727 | if (!Super) { |
| 1728 | // The current class does not have a superclass. |
| 1729 | Diag(receiverNameLoc, diag::error_root_class_cannot_use_super) |
| 1730 | << CurMethod->getClassInterface()->getIdentifier(); |
| 1731 | return ExprError(); |
| 1732 | } |
| 1733 | QualType T = Context.getObjCInterfaceType(Super); |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1734 | T = Context.getObjCObjectPointerType(T); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1735 | |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1736 | return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1737 | /*BaseExpr*/nullptr, |
Fariborz Jahanian | c297cd8 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1738 | SourceLocation()/*OpLoc*/, |
| 1739 | &propertyName, |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1740 | propertyNameLoc, |
| 1741 | receiverNameLoc, T, true); |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1742 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1743 | |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1744 | // Otherwise, if this is a class method, try dispatching to our |
| 1745 | // superclass. |
| 1746 | IFace = CurMethod->getClassInterface()->getSuperClass(); |
| 1747 | } |
John McCall | 5f2d556 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1748 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1749 | |
| 1750 | if (!IFace) { |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1751 | Diag(receiverNameLoc, diag::err_expected_either) << tok::identifier |
| 1752 | << tok::l_paren; |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1753 | return ExprError(); |
| 1754 | } |
| 1755 | } |
| 1756 | |
| 1757 | // Search for a declared property first. |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1758 | Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1759 | ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1760 | |
| 1761 | // If this reference is in an @implementation, check for 'private' methods. |
| 1762 | if (!Getter) |
Fariborz Jahanian | 29cdbc6 | 2014-04-21 20:22:17 +0000 | [diff] [blame] | 1763 | Getter = IFace->lookupPrivateClassMethod(Sel); |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1764 | |
| 1765 | if (Getter) { |
| 1766 | // FIXME: refactor/share with ActOnMemberReference(). |
| 1767 | // Check if we can reference this property. |
| 1768 | if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) |
| 1769 | return ExprError(); |
| 1770 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1771 | |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1772 | // Look for the matching setter, in case it is needed. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1773 | Selector SetterSel = |
Adrian Prantl | a4ce906 | 2013-06-07 22:29:12 +0000 | [diff] [blame] | 1774 | SelectorTable::constructSetterSelector(PP.getIdentifierTable(), |
| 1775 | PP.getSelectorTable(), |
| 1776 | &propertyName); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1777 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1778 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1779 | if (!Setter) { |
| 1780 | // If this reference is in an @implementation, also check for 'private' |
| 1781 | // methods. |
Fariborz Jahanian | 29cdbc6 | 2014-04-21 20:22:17 +0000 | [diff] [blame] | 1782 | Setter = IFace->lookupPrivateClassMethod(SetterSel); |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1783 | } |
| 1784 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1559d67b | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 1785 | if (!Setter) |
| 1786 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1787 | |
| 1788 | if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) |
| 1789 | return ExprError(); |
| 1790 | |
| 1791 | if (Getter || Setter) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1792 | if (IsSuper) |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1793 | return new (Context) |
| 1794 | ObjCPropertyRefExpr(Getter, Setter, Context.PseudoObjectTy, VK_LValue, |
| 1795 | OK_ObjCProperty, propertyNameLoc, receiverNameLoc, |
| 1796 | Context.getObjCInterfaceType(IFace)); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1797 | |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 1798 | return new (Context) ObjCPropertyRefExpr( |
| 1799 | Getter, Setter, Context.PseudoObjectTy, VK_LValue, OK_ObjCProperty, |
| 1800 | propertyNameLoc, receiverNameLoc, IFace); |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1801 | } |
| 1802 | return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) |
| 1803 | << &propertyName << Context.getObjCInterfaceType(IFace)); |
| 1804 | } |
| 1805 | |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1806 | namespace { |
| 1807 | |
| 1808 | class ObjCInterfaceOrSuperCCC : public CorrectionCandidateCallback { |
| 1809 | public: |
| 1810 | ObjCInterfaceOrSuperCCC(ObjCMethodDecl *Method) { |
| 1811 | // Determine whether "super" is acceptable in the current context. |
| 1812 | if (Method && Method->getClassInterface()) |
| 1813 | WantObjCSuper = Method->getClassInterface()->getSuperClass(); |
| 1814 | } |
| 1815 | |
Craig Topper | e14c0f8 | 2014-03-12 04:55:44 +0000 | [diff] [blame] | 1816 | bool ValidateCandidate(const TypoCorrection &candidate) override { |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1817 | return candidate.getCorrectionDeclAs<ObjCInterfaceDecl>() || |
| 1818 | candidate.isKeyword("super"); |
| 1819 | } |
| 1820 | }; |
| 1821 | |
| 1822 | } |
| 1823 | |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1824 | Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S, |
Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1825 | IdentifierInfo *Name, |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1826 | SourceLocation NameLoc, |
| 1827 | bool IsSuper, |
Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1828 | bool HasTrailingDot, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1829 | ParsedType &ReceiverType) { |
| 1830 | ReceiverType = ParsedType(); |
Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1831 | |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1832 | // 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] | 1833 | // messaging super. If the identifier is "super" and there is a |
| 1834 | // trailing dot, it's an instance message. |
| 1835 | if (IsSuper && S->isInObjcMethodScope()) |
| 1836 | return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage; |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1837 | |
| 1838 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| 1839 | LookupName(Result, S); |
| 1840 | |
| 1841 | switch (Result.getResultKind()) { |
| 1842 | case LookupResult::NotFound: |
Douglas Gregor | ca7136b | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 1843 | // Normal name lookup didn't find anything. If we're in an |
| 1844 | // 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] | 1845 | // FIXME: This is a hack. Ivar lookup should be part of normal |
| 1846 | // lookup. |
Douglas Gregor | ca7136b | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 1847 | if (ObjCMethodDecl *Method = getCurMethodDecl()) { |
Argyrios Kyrtzidis | 3a8de5b | 2011-11-09 00:22:48 +0000 | [diff] [blame] | 1848 | if (!Method->getClassInterface()) { |
| 1849 | // Fall back: let the parser try to parse it as an instance message. |
| 1850 | return ObjCInstanceMessage; |
| 1851 | } |
| 1852 | |
Douglas Gregor | ca7136b | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 1853 | ObjCInterfaceDecl *ClassDeclared; |
| 1854 | if (Method->getClassInterface()->lookupInstanceVariable(Name, |
| 1855 | ClassDeclared)) |
| 1856 | return ObjCInstanceMessage; |
| 1857 | } |
Douglas Gregor | 57756ea | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1859 | // Break out; we'll perform typo correction below. |
| 1860 | break; |
| 1861 | |
| 1862 | case LookupResult::NotFoundInCurrentInstantiation: |
| 1863 | case LookupResult::FoundOverloaded: |
| 1864 | case LookupResult::FoundUnresolvedValue: |
| 1865 | case LookupResult::Ambiguous: |
| 1866 | Result.suppressDiagnostics(); |
| 1867 | return ObjCInstanceMessage; |
| 1868 | |
| 1869 | case LookupResult::Found: { |
Fariborz Jahanian | 14889fc | 2011-02-08 00:23:07 +0000 | [diff] [blame] | 1870 | // If the identifier is a class or not, and there is a trailing dot, |
| 1871 | // it's an instance message. |
| 1872 | if (HasTrailingDot) |
| 1873 | return ObjCInstanceMessage; |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1874 | // We found something. If it's a type, then we have a class |
| 1875 | // message. Otherwise, it's an instance message. |
| 1876 | NamedDecl *ND = Result.getFoundDecl(); |
Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1877 | QualType T; |
| 1878 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 1879 | T = Context.getObjCInterfaceType(Class); |
Fariborz Jahanian | 83f1be1 | 2013-04-04 18:45:52 +0000 | [diff] [blame] | 1880 | else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) { |
Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1881 | T = Context.getTypeDeclType(Type); |
Fariborz Jahanian | 83f1be1 | 2013-04-04 18:45:52 +0000 | [diff] [blame] | 1882 | DiagnoseUseOfDecl(Type, NameLoc); |
| 1883 | } |
| 1884 | else |
Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1885 | return ObjCInstanceMessage; |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1886 | |
Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1887 | // We have a class message, and T is the type we're |
| 1888 | // messaging. Build source-location information for it. |
| 1889 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1890 | ReceiverType = CreateParsedType(T, TSInfo); |
Douglas Gregor | e5798dc | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1891 | return ObjCClassMessage; |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1892 | } |
| 1893 | } |
| 1894 | |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1895 | ObjCInterfaceOrSuperCCC Validator(getCurMethodDecl()); |
Kaelyn Uhrain | 0e23844 | 2013-09-27 19:40:08 +0000 | [diff] [blame] | 1896 | if (TypoCorrection Corrected = |
| 1897 | CorrectTypo(Result.getLookupNameInfo(), Result.getLookupKind(), S, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1898 | nullptr, Validator, CTK_ErrorRecovery, nullptr, false, |
| 1899 | nullptr, false)) { |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1900 | if (Corrected.isKeyword()) { |
| 1901 | // If we've found the keyword "super" (the only keyword that would be |
| 1902 | // returned by CorrectTypo), this is a send to super. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1903 | diagnoseTypo(Corrected, |
| 1904 | PDiag(diag::err_unknown_receiver_suggest) << Name); |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1905 | return ObjCSuperMessage; |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1906 | } else if (ObjCInterfaceDecl *Class = |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1907 | Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1908 | // If we found a declaration, correct when it refers to an Objective-C |
| 1909 | // class. |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1910 | diagnoseTypo(Corrected, |
| 1911 | PDiag(diag::err_unknown_receiver_suggest) << Name); |
Kaelyn Uhrain | e31b888 | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1912 | QualType T = Context.getObjCInterfaceType(Class); |
| 1913 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
| 1914 | ReceiverType = CreateParsedType(T, TSInfo); |
| 1915 | return ObjCClassMessage; |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1916 | } |
| 1917 | } |
Richard Smith | f9b1510 | 2013-08-17 00:46:16 +0000 | [diff] [blame] | 1918 | |
Douglas Gregor | 8aa4ebf | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1919 | // Fall back: let the parser try to parse it as an instance message. |
| 1920 | return ObjCInstanceMessage; |
| 1921 | } |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1922 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1923 | ExprResult Sema::ActOnSuperMessage(Scope *S, |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1924 | SourceLocation SuperLoc, |
| 1925 | Selector Sel, |
| 1926 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1927 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1928 | SourceLocation RBracLoc, |
| 1929 | MultiExprArg Args) { |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1930 | // Determine whether we are inside a method or not. |
Eli Friedman | 24af850 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 1931 | ObjCMethodDecl *Method = tryCaptureObjCSelf(SuperLoc); |
Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1932 | if (!Method) { |
| 1933 | Diag(SuperLoc, diag::err_invalid_receiver_to_message_super); |
| 1934 | return ExprError(); |
| 1935 | } |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1937 | ObjCInterfaceDecl *Class = Method->getClassInterface(); |
| 1938 | if (!Class) { |
| 1939 | Diag(SuperLoc, diag::error_no_super_class_message) |
| 1940 | << Method->getDeclName(); |
| 1941 | return ExprError(); |
| 1942 | } |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1943 | |
Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1944 | ObjCInterfaceDecl *Super = Class->getSuperClass(); |
| 1945 | if (!Super) { |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1946 | // The current class does not have a superclass. |
Ted Kremenek | 499897b | 2011-01-23 17:21:34 +0000 | [diff] [blame] | 1947 | Diag(SuperLoc, diag::error_root_class_cannot_use_super) |
| 1948 | << Class->getIdentifier(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1949 | return ExprError(); |
Chris Lattner | c2ebb03 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 1950 | } |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1951 | |
Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1952 | // We are in a method whose class has a superclass, so 'super' |
| 1953 | // is acting as a keyword. |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 1954 | if (Method->getSelector() == Sel) |
| 1955 | getCurFunction()->ObjCShouldCallSuper = false; |
Nico Weber | 715abaf | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 1956 | |
Jordan Rose | 2afd661 | 2012-10-19 16:05:26 +0000 | [diff] [blame] | 1957 | if (Method->isInstanceMethod()) { |
Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1958 | // Since we are in an instance method, this is an instance |
| 1959 | // message to the superclass instance. |
| 1960 | QualType SuperTy = Context.getObjCInterfaceType(Super); |
| 1961 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1962 | return BuildInstanceMessage(nullptr, SuperTy, SuperLoc, |
| 1963 | Sel, /*Method=*/nullptr, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1964 | LBracLoc, SelectorLocs, RBracLoc, Args); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1965 | } |
Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1966 | |
| 1967 | // Since we are in a class method, this is a class message to |
| 1968 | // the superclass. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1969 | return BuildClassMessage(/*ReceiverTypeInfo=*/nullptr, |
Douglas Gregor | 4fdba13 | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1970 | Context.getObjCInterfaceType(Super), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1971 | SuperLoc, Sel, /*Method=*/nullptr, |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1972 | LBracLoc, SelectorLocs, RBracLoc, Args); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1975 | |
| 1976 | ExprResult Sema::BuildClassMessageImplicit(QualType ReceiverType, |
| 1977 | bool isSuperReceiver, |
| 1978 | SourceLocation Loc, |
| 1979 | Selector Sel, |
| 1980 | ObjCMethodDecl *Method, |
| 1981 | MultiExprArg Args) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1982 | TypeSourceInfo *receiverTypeInfo = nullptr; |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1983 | if (!ReceiverType.isNull()) |
| 1984 | receiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType); |
| 1985 | |
| 1986 | return BuildClassMessage(receiverTypeInfo, ReceiverType, |
| 1987 | /*SuperLoc=*/isSuperReceiver ? Loc : SourceLocation(), |
| 1988 | Sel, Method, Loc, Loc, Loc, Args, |
| 1989 | /*isImplicit=*/true); |
| 1990 | |
| 1991 | } |
| 1992 | |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1993 | static void applyCocoaAPICheck(Sema &S, const ObjCMessageExpr *Msg, |
| 1994 | unsigned DiagID, |
| 1995 | bool (*refactor)(const ObjCMessageExpr *, |
| 1996 | const NSAPI &, edit::Commit &)) { |
| 1997 | SourceLocation MsgLoc = Msg->getExprLoc(); |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 1998 | if (S.Diags.isIgnored(DiagID, MsgLoc)) |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1999 | return; |
| 2000 | |
| 2001 | SourceManager &SM = S.SourceMgr; |
| 2002 | edit::Commit ECommit(SM, S.LangOpts); |
| 2003 | if (refactor(Msg,*S.NSAPIObj, ECommit)) { |
| 2004 | DiagnosticBuilder Builder = S.Diag(MsgLoc, DiagID) |
| 2005 | << Msg->getSelector() << Msg->getSourceRange(); |
| 2006 | // FIXME: Don't emit diagnostic at all if fixits are non-commitable. |
| 2007 | if (!ECommit.isCommitable()) |
| 2008 | return; |
| 2009 | for (edit::Commit::edit_iterator |
| 2010 | I = ECommit.edit_begin(), E = ECommit.edit_end(); I != E; ++I) { |
| 2011 | const edit::Commit::Edit &Edit = *I; |
| 2012 | switch (Edit.Kind) { |
| 2013 | case edit::Commit::Act_Insert: |
| 2014 | Builder.AddFixItHint(FixItHint::CreateInsertion(Edit.OrigLoc, |
| 2015 | Edit.Text, |
| 2016 | Edit.BeforePrev)); |
| 2017 | break; |
| 2018 | case edit::Commit::Act_InsertFromRange: |
| 2019 | Builder.AddFixItHint( |
| 2020 | FixItHint::CreateInsertionFromRange(Edit.OrigLoc, |
| 2021 | Edit.getInsertFromRange(SM), |
| 2022 | Edit.BeforePrev)); |
| 2023 | break; |
| 2024 | case edit::Commit::Act_Remove: |
| 2025 | Builder.AddFixItHint(FixItHint::CreateRemoval(Edit.getFileRange(SM))); |
| 2026 | break; |
| 2027 | } |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | static void checkCocoaAPI(Sema &S, const ObjCMessageExpr *Msg) { |
| 2033 | applyCocoaAPICheck(S, Msg, diag::warn_objc_redundant_literal_use, |
| 2034 | edit::rewriteObjCRedundantCallWithLiteral); |
| 2035 | } |
| 2036 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2037 | /// \brief Build an Objective-C class message expression. |
| 2038 | /// |
| 2039 | /// This routine takes care of both normal class messages and |
| 2040 | /// class messages to the superclass. |
| 2041 | /// |
| 2042 | /// \param ReceiverTypeInfo Type source information that describes the |
| 2043 | /// receiver of this message. This may be NULL, in which case we are |
| 2044 | /// sending to the superclass and \p SuperLoc must be a valid source |
| 2045 | /// location. |
| 2046 | |
| 2047 | /// \param ReceiverType The type of the object receiving the |
| 2048 | /// message. When \p ReceiverTypeInfo is non-NULL, this is the same |
| 2049 | /// type as that refers to. For a superclass send, this is the type of |
| 2050 | /// the superclass. |
| 2051 | /// |
| 2052 | /// \param SuperLoc The location of the "super" keyword in a |
| 2053 | /// superclass message. |
| 2054 | /// |
| 2055 | /// \param Sel The selector to which the message is being sent. |
| 2056 | /// |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2057 | /// \param Method The method that this class message is invoking, if |
| 2058 | /// already known. |
| 2059 | /// |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2060 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 2061 | /// |
James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 2062 | /// \param RBracLoc The location of the closing square bracket ']'. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2063 | /// |
James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 2064 | /// \param ArgsIn The message arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2065 | ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo, |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2066 | QualType ReceiverType, |
| 2067 | SourceLocation SuperLoc, |
| 2068 | Selector Sel, |
| 2069 | ObjCMethodDecl *Method, |
| 2070 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2071 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2072 | SourceLocation RBracLoc, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2073 | MultiExprArg ArgsIn, |
| 2074 | bool isImplicit) { |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2075 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 2076 | : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin(); |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2077 | if (LBracLoc.isInvalid()) { |
| 2078 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 2079 | << FixItHint::CreateInsertion(Loc, "["); |
| 2080 | LBracLoc = Loc; |
| 2081 | } |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2082 | SourceLocation SelLoc; |
| 2083 | if (!SelectorLocs.empty() && SelectorLocs.front().isValid()) |
| 2084 | SelLoc = SelectorLocs.front(); |
| 2085 | else |
| 2086 | SelLoc = Loc; |
| 2087 | |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2088 | if (ReceiverType->isDependentType()) { |
| 2089 | // If the receiver type is dependent, we can't type-check anything |
| 2090 | // at this point. Build a dependent expression. |
| 2091 | unsigned NumArgs = ArgsIn.size(); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2092 | Expr **Args = ArgsIn.data(); |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2093 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2094 | return ObjCMessageExpr::Create( |
| 2095 | Context, ReceiverType, VK_RValue, LBracLoc, ReceiverTypeInfo, Sel, |
| 2096 | SelectorLocs, /*Method=*/nullptr, makeArrayRef(Args, NumArgs), RBracLoc, |
| 2097 | isImplicit); |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2098 | } |
Chris Lattner | c2ebb03 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 2099 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2100 | // Find the class to which we are sending this message. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2101 | ObjCInterfaceDecl *Class = nullptr; |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2102 | const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>(); |
| 2103 | if (!ClassType || !(Class = ClassType->getInterface())) { |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2104 | Diag(Loc, diag::err_invalid_receiver_class_message) |
| 2105 | << ReceiverType; |
| 2106 | return ExprError(); |
Steve Naroff | e2177fb | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 2107 | } |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2108 | assert(Class && "We don't know which class we're messaging?"); |
Fariborz Jahanian | c27cd1b | 2011-10-15 19:18:36 +0000 | [diff] [blame] | 2109 | // objc++ diagnoses during typename annotation. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2110 | if (!getLangOpts().CPlusPlus) |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2111 | (void)DiagnoseUseOfDecl(Class, SelLoc); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2112 | // Find the method we are messaging. |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2113 | if (!Method) { |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2114 | SourceRange TypeRange |
| 2115 | = SuperLoc.isValid()? SourceRange(SuperLoc) |
| 2116 | : ReceiverTypeInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2117 | if (RequireCompleteType(Loc, Context.getObjCInterfaceType(Class), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2118 | (getLangOpts().ObjCAutoRefCount |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2119 | ? diag::err_arc_receiver_forward_class |
| 2120 | : diag::warn_receiver_forward_class), |
| 2121 | TypeRange)) { |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2122 | // A forward class used in messaging is treated as a 'Class' |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2123 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 2124 | SourceRange(LBracLoc, RBracLoc)); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2125 | if (Method && !getLangOpts().ObjCAutoRefCount) |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2126 | Diag(Method->getLocation(), diag::note_method_sent_forward_class) |
| 2127 | << Method->getDeclName(); |
| 2128 | } |
| 2129 | if (!Method) |
| 2130 | Method = Class->lookupClassMethod(Sel); |
| 2131 | |
| 2132 | // If we have an implementation in scope, check "private" methods. |
| 2133 | if (!Method) |
Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 2134 | Method = Class->lookupPrivateClassMethod(Sel); |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2135 | |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2136 | if (Method && DiagnoseUseOfDecl(Method, SelLoc)) |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2137 | return ExprError(); |
Fariborz Jahanian | 1bd844d | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 2138 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2139 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2140 | // Check the argument types and determine the result type. |
| 2141 | QualType ReturnType; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2142 | ExprValueKind VK = VK_RValue; |
| 2143 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2144 | unsigned NumArgs = ArgsIn.size(); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2145 | Expr **Args = ArgsIn.data(); |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 2146 | if (CheckMessageArgumentTypes(ReceiverType, MultiExprArg(Args, NumArgs), |
| 2147 | Sel, SelectorLocs, |
Fariborz Jahanian | 6ce25c0 | 2012-08-31 17:03:18 +0000 | [diff] [blame] | 2148 | Method, true, |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2149 | SuperLoc.isValid(), LBracLoc, RBracLoc, |
| 2150 | ReturnType, VK)) |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2151 | return ExprError(); |
Ted Kremenek | a3a37ae | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 2152 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2153 | if (Method && !Method->getReturnType()->isVoidType() && |
| 2154 | RequireCompleteType(LBracLoc, Method->getReturnType(), |
Douglas Gregor | aec93c6 | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 2155 | diag::err_illegal_message_expr_incomplete_type)) |
| 2156 | return ExprError(); |
| 2157 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2158 | // Construct the appropriate ObjCMessageExpr. |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2159 | ObjCMessageExpr *Result; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2160 | if (SuperLoc.isValid()) |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2161 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Douglas Gregor | aae38d6 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2162 | SuperLoc, /*IsInstanceSuper=*/false, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2163 | ReceiverType, Sel, SelectorLocs, |
Argyrios Kyrtzidis | 59ad1e3 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2164 | Method, makeArrayRef(Args, NumArgs), |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2165 | RBracLoc, isImplicit); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2166 | else { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2167 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2168 | ReceiverTypeInfo, Sel, SelectorLocs, |
Argyrios Kyrtzidis | 59ad1e3 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2169 | Method, makeArrayRef(Args, NumArgs), |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2170 | RBracLoc, isImplicit); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2171 | if (!isImplicit) |
| 2172 | checkCocoaAPI(*this, Result); |
| 2173 | } |
Douglas Gregor | aae38d6 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2174 | return MaybeBindToTemporary(Result); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2175 | } |
| 2176 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2177 | // ActOnClassMessage - used for both unary and keyword messages. |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2178 | // ArgExprs is optional - if it is present, the number of expressions |
| 2179 | // is obtained from Sel.getNumArgs(). |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2180 | ExprResult Sema::ActOnClassMessage(Scope *S, |
Douglas Gregor | 3e97200 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 2181 | ParsedType Receiver, |
| 2182 | Selector Sel, |
| 2183 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2184 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | 3e97200 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 2185 | SourceLocation RBracLoc, |
| 2186 | MultiExprArg Args) { |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2187 | TypeSourceInfo *ReceiverTypeInfo; |
| 2188 | QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo); |
| 2189 | if (ReceiverType.isNull()) |
| 2190 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2192 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2193 | if (!ReceiverTypeInfo) |
| 2194 | ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc); |
| 2195 | |
| 2196 | return BuildClassMessage(ReceiverTypeInfo, ReceiverType, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2197 | /*SuperLoc=*/SourceLocation(), Sel, |
| 2198 | /*Method=*/nullptr, LBracLoc, SelectorLocs, RBracLoc, |
| 2199 | Args); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2202 | ExprResult Sema::BuildInstanceMessageImplicit(Expr *Receiver, |
| 2203 | QualType ReceiverType, |
| 2204 | SourceLocation Loc, |
| 2205 | Selector Sel, |
| 2206 | ObjCMethodDecl *Method, |
| 2207 | MultiExprArg Args) { |
| 2208 | return BuildInstanceMessage(Receiver, ReceiverType, |
| 2209 | /*SuperLoc=*/!Receiver ? Loc : SourceLocation(), |
| 2210 | Sel, Method, Loc, Loc, Loc, Args, |
| 2211 | /*isImplicit=*/true); |
| 2212 | } |
| 2213 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2214 | /// \brief Build an Objective-C instance message expression. |
| 2215 | /// |
| 2216 | /// This routine takes care of both normal instance messages and |
| 2217 | /// instance messages to the superclass instance. |
| 2218 | /// |
| 2219 | /// \param Receiver The expression that computes the object that will |
| 2220 | /// receive this message. This may be empty, in which case we are |
| 2221 | /// sending to the superclass instance and \p SuperLoc must be a valid |
| 2222 | /// source location. |
| 2223 | /// |
| 2224 | /// \param ReceiverType The (static) type of the object receiving the |
| 2225 | /// message. When a \p Receiver expression is provided, this is the |
| 2226 | /// same type as that expression. For a superclass instance send, this |
| 2227 | /// is a pointer to the type of the superclass. |
| 2228 | /// |
| 2229 | /// \param SuperLoc The location of the "super" keyword in a |
| 2230 | /// superclass instance message. |
| 2231 | /// |
| 2232 | /// \param Sel The selector to which the message is being sent. |
| 2233 | /// |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2234 | /// \param Method The method that this instance message is invoking, if |
| 2235 | /// already known. |
| 2236 | /// |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2237 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 2238 | /// |
James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 2239 | /// \param RBracLoc The location of the closing square bracket ']'. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2240 | /// |
James Dennett | ffad8b7 | 2012-06-22 08:10:18 +0000 | [diff] [blame] | 2241 | /// \param ArgsIn The message arguments. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2242 | ExprResult Sema::BuildInstanceMessage(Expr *Receiver, |
Argyrios Kyrtzidis | d0039e5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2243 | QualType ReceiverType, |
| 2244 | SourceLocation SuperLoc, |
| 2245 | Selector Sel, |
| 2246 | ObjCMethodDecl *Method, |
| 2247 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2248 | ArrayRef<SourceLocation> SelectorLocs, |
Argyrios Kyrtzidis | d0039e5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2249 | SourceLocation RBracLoc, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2250 | MultiExprArg ArgsIn, |
| 2251 | bool isImplicit) { |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2252 | // The location of the receiver. |
| 2253 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart(); |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2254 | SourceRange RecRange = |
| 2255 | SuperLoc.isValid()? SuperLoc : Receiver->getSourceRange(); |
| 2256 | SourceLocation SelLoc; |
| 2257 | if (!SelectorLocs.empty() && SelectorLocs.front().isValid()) |
| 2258 | SelLoc = SelectorLocs.front(); |
| 2259 | else |
| 2260 | SelLoc = Loc; |
| 2261 | |
Douglas Gregor | e9bba4f | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2262 | if (LBracLoc.isInvalid()) { |
| 2263 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 2264 | << FixItHint::CreateInsertion(Loc, "["); |
| 2265 | LBracLoc = Loc; |
| 2266 | } |
| 2267 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2268 | // If we have a receiver expression, perform appropriate promotions |
| 2269 | // and determine receiver type. |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2270 | if (Receiver) { |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2271 | if (Receiver->hasPlaceholderType()) { |
Douglas Gregor | d8fb1e3 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 2272 | ExprResult Result; |
| 2273 | if (Receiver->getType() == Context.UnknownAnyTy) |
| 2274 | Result = forceUnknownAnyToType(Receiver, Context.getObjCIdType()); |
| 2275 | else |
| 2276 | Result = CheckPlaceholderExpr(Receiver); |
| 2277 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2278 | Receiver = Result.get(); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2279 | } |
| 2280 | |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2281 | if (Receiver->isTypeDependent()) { |
| 2282 | // If the receiver is type-dependent, we can't type-check anything |
| 2283 | // at this point. Build a dependent expression. |
| 2284 | unsigned NumArgs = ArgsIn.size(); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2285 | Expr **Args = ArgsIn.data(); |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2286 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2287 | return ObjCMessageExpr::Create( |
| 2288 | Context, Context.DependentTy, VK_RValue, LBracLoc, Receiver, Sel, |
| 2289 | SelectorLocs, /*Method=*/nullptr, makeArrayRef(Args, NumArgs), |
| 2290 | RBracLoc, isImplicit); |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2293 | // If necessary, apply function/array conversion to the receiver. |
| 2294 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2295 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver); |
| 2296 | if (Result.isInvalid()) |
| 2297 | return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2298 | Receiver = Result.get(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2299 | ReceiverType = Receiver->getType(); |
John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2300 | |
| 2301 | // If the receiver is an ObjC pointer, a block pointer, or an |
| 2302 | // __attribute__((NSObject)) pointer, we don't need to do any |
| 2303 | // special conversion in order to look up a receiver. |
| 2304 | if (ReceiverType->isObjCRetainableType()) { |
| 2305 | // do nothing |
| 2306 | } else if (!getLangOpts().ObjCAutoRefCount && |
| 2307 | !Context.getObjCIdType().isNull() && |
| 2308 | (ReceiverType->isPointerType() || |
| 2309 | ReceiverType->isIntegerType())) { |
| 2310 | // Implicitly convert integers and pointers to 'id' but emit a warning. |
| 2311 | // But not in ARC. |
| 2312 | Diag(Loc, diag::warn_bad_receiver_type) |
| 2313 | << ReceiverType |
| 2314 | << Receiver->getSourceRange(); |
| 2315 | if (ReceiverType->isPointerType()) { |
| 2316 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2317 | CK_CPointerToObjCPointerCast).get(); |
John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2318 | } else { |
| 2319 | // TODO: specialized warning on null receivers? |
| 2320 | bool IsNull = Receiver->isNullPointerConstant(Context, |
| 2321 | Expr::NPC_ValueDependentIsNull); |
| 2322 | CastKind Kind = IsNull ? CK_NullToPointer : CK_IntegralToPointer; |
| 2323 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2324 | Kind).get(); |
John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2325 | } |
| 2326 | ReceiverType = Receiver->getType(); |
| 2327 | } else if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 4b60a15 | 2013-11-07 22:34:54 +0000 | [diff] [blame] | 2328 | // The receiver must be a complete type. |
| 2329 | if (RequireCompleteType(Loc, Receiver->getType(), |
| 2330 | diag::err_incomplete_receiver_type)) |
| 2331 | return ExprError(); |
| 2332 | |
John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2333 | ExprResult result = PerformContextuallyConvertToObjCPointer(Receiver); |
| 2334 | if (result.isUsable()) { |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2335 | Receiver = result.get(); |
John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2336 | ReceiverType = Receiver->getType(); |
| 2337 | } |
| 2338 | } |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2339 | } |
| 2340 | |
John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2341 | // There's a somewhat weird interaction here where we assume that we |
| 2342 | // won't actually have a method unless we also don't need to do some |
| 2343 | // of the more detailed type-checking on the receiver. |
| 2344 | |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2345 | if (!Method) { |
| 2346 | // Handle messages to id. |
Fariborz Jahanian | 32e59ba | 2010-08-10 18:10:50 +0000 | [diff] [blame] | 2347 | bool receiverIsId = ReceiverType->isObjCIdType(); |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2348 | if (receiverIsId || ReceiverType->isBlockPointerType() || |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2349 | (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) { |
| 2350 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2351 | SourceRange(LBracLoc, RBracLoc), |
| 2352 | receiverIsId); |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2353 | if (!Method) |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2354 | Method = LookupFactoryMethodInGlobalPool(Sel, |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 2355 | SourceRange(LBracLoc,RBracLoc), |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2356 | receiverIsId); |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2357 | } else if (ReceiverType->isObjCClassType() || |
| 2358 | ReceiverType->isObjCQualifiedClassType()) { |
| 2359 | // Handle messages to Class. |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2360 | // We allow sending a message to a qualified Class ("Class<foo>"), which |
| 2361 | // is ok as long as one of the protocols implements the selector (if not, warn). |
| 2362 | if (const ObjCObjectPointerType *QClassTy |
| 2363 | = ReceiverType->getAsObjCQualifiedClassType()) { |
| 2364 | // Search protocols for class methods. |
| 2365 | Method = LookupMethodInQualifiedType(Sel, QClassTy, false); |
| 2366 | if (!Method) { |
| 2367 | Method = LookupMethodInQualifiedType(Sel, QClassTy, true); |
| 2368 | // warn if instance method found for a Class message. |
| 2369 | if (Method) { |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2370 | Diag(SelLoc, diag::warn_instance_method_on_class_found) |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2371 | << Method->getSelector() << Sel; |
Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2372 | Diag(Method->getLocation(), diag::note_method_declared_at) |
| 2373 | << Method->getDeclName(); |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2374 | } |
Steve Naroff | 3f49fee | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 2375 | } |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2376 | } else { |
| 2377 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 2378 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { |
| 2379 | // First check the public methods in the class interface. |
| 2380 | Method = ClassDecl->lookupClassMethod(Sel); |
| 2381 | |
| 2382 | if (!Method) |
Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 2383 | Method = ClassDecl->lookupPrivateClassMethod(Sel); |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2384 | } |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2385 | if (Method && DiagnoseUseOfDecl(Method, SelLoc)) |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2386 | return ExprError(); |
| 2387 | } |
| 2388 | if (!Method) { |
| 2389 | // If not messaging 'self', look for any factory method named 'Sel'. |
Douglas Gregor | 486b74e | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 2390 | if (!Receiver || !isSelfExpr(Receiver)) { |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2391 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 2392 | SourceRange(LBracLoc, RBracLoc), |
| 2393 | true); |
| 2394 | if (!Method) { |
| 2395 | // If no class (factory) method was found, check if an _instance_ |
| 2396 | // method of the same name exists in the root class only. |
| 2397 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 3337b2e | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2398 | SourceRange(LBracLoc, RBracLoc), |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2399 | true); |
| 2400 | if (Method) |
| 2401 | if (const ObjCInterfaceDecl *ID = |
| 2402 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { |
| 2403 | if (ID->getSuperClass()) |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2404 | Diag(SelLoc, diag::warn_root_inst_method_not_found) |
Fariborz Jahanian | 3b9819b | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2405 | << Sel << SourceRange(LBracLoc, RBracLoc); |
| 2406 | } |
| 2407 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2408 | } |
| 2409 | } |
| 2410 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2411 | } else { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2412 | ObjCInterfaceDecl *ClassDecl = nullptr; |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2413 | |
| 2414 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 2415 | // long as one of the protocols implements the selector (if not, warn). |
Fariborz Jahanian | a245f19 | 2012-06-23 18:39:57 +0000 | [diff] [blame] | 2416 | // And as long as message is not deprecated/unavailable (warn if it is). |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2417 | if (const ObjCObjectPointerType *QIdTy |
| 2418 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 2419 | // Search protocols for instance methods. |
Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2420 | Method = LookupMethodInQualifiedType(Sel, QIdTy, true); |
| 2421 | if (!Method) |
| 2422 | Method = LookupMethodInQualifiedType(Sel, QIdTy, false); |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2423 | if (Method && DiagnoseUseOfDecl(Method, SelLoc)) |
Fariborz Jahanian | a245f19 | 2012-06-23 18:39:57 +0000 | [diff] [blame] | 2424 | return ExprError(); |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2425 | } else if (const ObjCObjectPointerType *OCIType |
| 2426 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 2427 | // We allow sending a message to a pointer to an interface (an object). |
| 2428 | ClassDecl = OCIType->getInterfaceDecl(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2429 | |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2430 | // Try to complete the type. Under ARC, this is a hard error from which |
| 2431 | // we don't try to recover. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2432 | const ObjCInterfaceDecl *forwardClass = nullptr; |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2433 | if (RequireCompleteType(Loc, OCIType->getPointeeType(), |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2434 | getLangOpts().ObjCAutoRefCount |
Douglas Gregor | 7bfb2d0 | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2435 | ? diag::err_arc_receiver_forward_instance |
| 2436 | : diag::warn_receiver_forward_instance, |
| 2437 | Receiver? Receiver->getSourceRange() |
| 2438 | : SourceRange(SuperLoc))) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2439 | if (getLangOpts().ObjCAutoRefCount) |
Douglas Gregor | 4123a86 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2440 | return ExprError(); |
| 2441 | |
| 2442 | forwardClass = OCIType->getInterfaceDecl(); |
Fariborz Jahanian | c934de6 | 2012-02-03 01:02:44 +0000 | [diff] [blame] | 2443 | Diag(Receiver ? Receiver->getLocStart() |
| 2444 | : SuperLoc, diag::note_receiver_is_id); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2445 | Method = nullptr; |
Douglas Gregor | c0ac7d6 | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2446 | } else { |
| 2447 | Method = ClassDecl->lookupInstanceMethod(Sel); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2448 | } |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2449 | |
Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2450 | if (!Method) |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2451 | // Search protocol qualifiers. |
Fariborz Jahanian | b296e33 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2452 | Method = LookupMethodInQualifiedType(Sel, OCIType, true); |
| 2453 | |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2454 | if (!Method) { |
| 2455 | // If we have implementations in scope, check "private" methods. |
Anna Zaks | c77a3b1 | 2012-07-27 19:07:44 +0000 | [diff] [blame] | 2456 | Method = ClassDecl->lookupPrivateMethod(Sel); |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2457 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2458 | if (!Method && getLangOpts().ObjCAutoRefCount) { |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2459 | Diag(SelLoc, diag::err_arc_may_not_respond) |
| 2460 | << OCIType->getPointeeType() << Sel << RecRange |
Fariborz Jahanian | 32c1350 | 2012-11-28 01:27:44 +0000 | [diff] [blame] | 2461 | << SourceRange(SelectorLocs.front(), SelectorLocs.back()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2462 | return ExprError(); |
| 2463 | } |
| 2464 | |
Douglas Gregor | 486b74e | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 2465 | if (!Method && (!Receiver || !isSelfExpr(Receiver))) { |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2466 | // If we still haven't found a method, look in the global pool. This |
| 2467 | // behavior isn't very desirable, however we need it for GCC |
| 2468 | // compatibility. FIXME: should we deviate?? |
| 2469 | if (OCIType->qual_empty()) { |
| 2470 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Jordy Rose | aca01f9 | 2012-05-12 17:32:52 +0000 | [diff] [blame] | 2471 | SourceRange(LBracLoc, RBracLoc)); |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 2472 | if (Method && !forwardClass) |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2473 | Diag(SelLoc, diag::warn_maynot_respond) |
| 2474 | << OCIType->getInterfaceDecl()->getIdentifier() |
| 2475 | << Sel << RecRange; |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | } |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2479 | if (Method && DiagnoseUseOfDecl(Method, SelLoc, forwardClass)) |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2480 | return ExprError(); |
John McCall | fec112d | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 2481 | } else { |
John McCall | 80c93a0 | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 2482 | // Reject other random receiver types (e.g. structs). |
| 2483 | Diag(Loc, diag::err_bad_receiver_type) |
| 2484 | << ReceiverType << Receiver->getSourceRange(); |
| 2485 | return ExprError(); |
Douglas Gregor | b5186b1 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2486 | } |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2487 | } |
Chris Lattner | 6b946cc | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 2488 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | |
Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 2490 | FunctionScopeInfo *DIFunctionScopeInfo = |
| 2491 | (Method && Method->getMethodFamily() == OMF_init) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2492 | ? getEnclosingFunction() : nullptr; |
| 2493 | |
Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 2494 | if (DIFunctionScopeInfo && |
| 2495 | DIFunctionScopeInfo->ObjCIsDesignatedInit && |
Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 2496 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
| 2497 | bool isDesignatedInitChain = false; |
| 2498 | if (SuperLoc.isValid()) { |
| 2499 | if (const ObjCObjectPointerType * |
| 2500 | OCIType = ReceiverType->getAsObjCInterfacePointerType()) { |
| 2501 | if (const ObjCInterfaceDecl *ID = OCIType->getInterfaceDecl()) { |
Argyrios Kyrtzidis | d664a34 | 2013-12-13 03:48:17 +0000 | [diff] [blame] | 2502 | // Either we know this is a designated initializer or we |
| 2503 | // conservatively assume it because we don't know for sure. |
| 2504 | if (!ID->declaresOrInheritsDesignatedInitializers() || |
| 2505 | ID->isDesignatedInitializer(Sel)) { |
Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 2506 | isDesignatedInitChain = true; |
Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 2507 | DIFunctionScopeInfo->ObjCWarnForNoDesignatedInitChain = false; |
Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 2508 | } |
| 2509 | } |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 2512 | if (!isDesignatedInitChain) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2513 | const ObjCMethodDecl *InitMethod = nullptr; |
Argyrios Kyrtzidis | fcded9b | 2013-12-03 21:11:43 +0000 | [diff] [blame] | 2514 | bool isDesignated = |
| 2515 | getCurMethodDecl()->isDesignatedInitializerForTheInterface(&InitMethod); |
| 2516 | assert(isDesignated && InitMethod); |
| 2517 | (void)isDesignated; |
| 2518 | Diag(SelLoc, SuperLoc.isValid() ? |
| 2519 | diag::warn_objc_designated_init_non_designated_init_call : |
| 2520 | diag::warn_objc_designated_init_non_super_designated_init_call); |
| 2521 | Diag(InitMethod->getLocation(), |
| 2522 | diag::note_objc_designated_init_marked_here); |
| 2523 | } |
Argyrios Kyrtzidis | 22bfa2c | 2013-12-03 21:11:36 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 2526 | if (DIFunctionScopeInfo && |
| 2527 | DIFunctionScopeInfo->ObjCIsSecondaryInit && |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 2528 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
| 2529 | if (SuperLoc.isValid()) { |
| 2530 | Diag(SelLoc, diag::warn_objc_secondary_init_super_init_call); |
| 2531 | } else { |
Fariborz Jahanian | ba419ce | 2014-03-17 21:41:40 +0000 | [diff] [blame] | 2532 | DIFunctionScopeInfo->ObjCWarnForNoInitDelegation = false; |
Argyrios Kyrtzidis | b66d3cf | 2013-12-03 21:11:49 +0000 | [diff] [blame] | 2533 | } |
| 2534 | } |
| 2535 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2536 | // Check the message arguments. |
| 2537 | unsigned NumArgs = ArgsIn.size(); |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 2538 | Expr **Args = ArgsIn.data(); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2539 | QualType ReturnType; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2540 | ExprValueKind VK = VK_RValue; |
Fariborz Jahanian | 6850091 | 2010-12-01 01:07:24 +0000 | [diff] [blame] | 2541 | bool ClassMessage = (ReceiverType->isObjCClassType() || |
| 2542 | ReceiverType->isObjCQualifiedClassType()); |
Dmitri Gribenko | 2a40f08 | 2013-05-10 00:27:15 +0000 | [diff] [blame] | 2543 | if (CheckMessageArgumentTypes(ReceiverType, MultiExprArg(Args, NumArgs), |
| 2544 | Sel, SelectorLocs, Method, |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2545 | ClassMessage, SuperLoc.isValid(), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2546 | LBracLoc, RBracLoc, ReturnType, VK)) |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2547 | return ExprError(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2548 | |
| 2549 | if (Method && !Method->getReturnType()->isVoidType() && |
| 2550 | RequireCompleteType(LBracLoc, Method->getReturnType(), |
Douglas Gregor | aec93c6 | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 2551 | diag::err_illegal_message_expr_incomplete_type)) |
| 2552 | return ExprError(); |
Douglas Gregor | 9a12919 | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2553 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2554 | // In ARC, forbid the user from sending messages to |
| 2555 | // retain/release/autorelease/dealloc/retainCount explicitly. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2556 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2557 | ObjCMethodFamily family = |
| 2558 | (Method ? Method->getMethodFamily() : Sel.getMethodFamily()); |
| 2559 | switch (family) { |
| 2560 | case OMF_init: |
| 2561 | if (Method) |
| 2562 | checkInitMethod(Method, ReceiverType); |
| 2563 | |
| 2564 | case OMF_None: |
| 2565 | case OMF_alloc: |
| 2566 | case OMF_copy: |
Nico Weber | 1fb8266 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 2567 | case OMF_finalize: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2568 | case OMF_mutableCopy: |
| 2569 | case OMF_new: |
| 2570 | case OMF_self: |
| 2571 | break; |
| 2572 | |
| 2573 | case OMF_dealloc: |
| 2574 | case OMF_retain: |
| 2575 | case OMF_release: |
| 2576 | case OMF_autorelease: |
| 2577 | case OMF_retainCount: |
Argyrios Kyrtzidis | bcf2bdc | 2013-05-01 00:24:09 +0000 | [diff] [blame] | 2578 | Diag(SelLoc, diag::err_arc_illegal_explicit_message) |
| 2579 | << Sel << RecRange; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2580 | break; |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2581 | |
| 2582 | case OMF_performSelector: |
| 2583 | if (Method && NumArgs >= 1) { |
| 2584 | if (ObjCSelectorExpr *SelExp = dyn_cast<ObjCSelectorExpr>(Args[0])) { |
| 2585 | Selector ArgSel = SelExp->getSelector(); |
| 2586 | ObjCMethodDecl *SelMethod = |
| 2587 | LookupInstanceMethodInGlobalPool(ArgSel, |
| 2588 | SelExp->getSourceRange()); |
| 2589 | if (!SelMethod) |
| 2590 | SelMethod = |
| 2591 | LookupFactoryMethodInGlobalPool(ArgSel, |
| 2592 | SelExp->getSourceRange()); |
| 2593 | if (SelMethod) { |
| 2594 | ObjCMethodFamily SelFamily = SelMethod->getMethodFamily(); |
| 2595 | switch (SelFamily) { |
| 2596 | case OMF_alloc: |
| 2597 | case OMF_copy: |
| 2598 | case OMF_mutableCopy: |
| 2599 | case OMF_new: |
| 2600 | case OMF_self: |
| 2601 | case OMF_init: |
| 2602 | // Issue error, unless ns_returns_not_retained. |
| 2603 | if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 2604 | // selector names a +1 method |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2605 | Diag(SelLoc, |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2606 | diag::err_arc_perform_selector_retains); |
Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2607 | Diag(SelMethod->getLocation(), diag::note_method_declared_at) |
| 2608 | << SelMethod->getDeclName(); |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2609 | } |
| 2610 | break; |
| 2611 | default: |
| 2612 | // +0 call. OK. unless ns_returns_retained. |
| 2613 | if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) { |
| 2614 | // selector names a +1 method |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2615 | Diag(SelLoc, |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2616 | diag::err_arc_perform_selector_retains); |
Ted Kremenek | 59b10db | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2617 | Diag(SelMethod->getLocation(), diag::note_method_declared_at) |
| 2618 | << SelMethod->getDeclName(); |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2619 | } |
| 2620 | break; |
| 2621 | } |
| 2622 | } |
| 2623 | } else { |
| 2624 | // error (may leak). |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2625 | Diag(SelLoc, diag::warn_arc_perform_selector_leaks); |
Fariborz Jahanian | b7a7736 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2626 | Diag(Args[0]->getExprLoc(), diag::note_used_here); |
| 2627 | } |
| 2628 | } |
| 2629 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2630 | } |
| 2631 | } |
| 2632 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2633 | // Construct the appropriate ObjCMessageExpr instance. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2634 | ObjCMessageExpr *Result; |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2635 | if (SuperLoc.isValid()) |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2636 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Douglas Gregor | aae38d6 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2637 | SuperLoc, /*IsInstanceSuper=*/true, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2638 | ReceiverType, Sel, SelectorLocs, Method, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2639 | makeArrayRef(Args, NumArgs), RBracLoc, |
| 2640 | isImplicit); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2641 | else { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2642 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2643 | Receiver, Sel, SelectorLocs, Method, |
Argyrios Kyrtzidis | a80f1bf | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2644 | makeArrayRef(Args, NumArgs), RBracLoc, |
| 2645 | isImplicit); |
Ted Kremenek | e65b086 | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2646 | if (!isImplicit) |
| 2647 | checkCocoaAPI(*this, Result); |
| 2648 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2649 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2650 | if (getLangOpts().ObjCAutoRefCount) { |
Fariborz Jahanian | 9277ff4 | 2014-06-17 23:35:13 +0000 | [diff] [blame] | 2651 | // Do not warn about IBOutlet weak property receivers being set to null |
| 2652 | // as this cannot asynchronously happen. |
| 2653 | bool WarnWeakReceiver = true; |
| 2654 | if (isImplicit && Method) |
| 2655 | if (const ObjCPropertyDecl *PropertyDecl = Method->findPropertyDecl()) |
| 2656 | WarnWeakReceiver = !PropertyDecl->hasAttr<IBOutletAttr>(); |
| 2657 | if (WarnWeakReceiver) |
| 2658 | DiagnoseARCUseOfWeakReceiver(*this, Receiver); |
Fariborz Jahanian | 6bd2226 | 2012-04-04 20:05:25 +0000 | [diff] [blame] | 2659 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2660 | // In ARC, annotate delegate init calls. |
| 2661 | if (Result->getMethodFamily() == OMF_init && |
Douglas Gregor | 486b74e | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 2662 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2663 | // Only consider init calls *directly* in init implementations, |
| 2664 | // not within blocks. |
| 2665 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext); |
| 2666 | if (method && method->getMethodFamily() == OMF_init) { |
| 2667 | // The implicit assignment to self means we also don't want to |
| 2668 | // consume the result. |
| 2669 | Result->setDelegateInitCall(true); |
Nikola Smiljanic | 03ff259 | 2014-05-29 14:05:12 +0000 | [diff] [blame] | 2670 | return Result; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | // In ARC, check for message sends which are likely to introduce |
| 2675 | // retain cycles. |
| 2676 | checkRetainCycles(Result); |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 2677 | |
| 2678 | if (!isImplicit && Method) { |
| 2679 | if (const ObjCPropertyDecl *Prop = Method->findPropertyDecl()) { |
| 2680 | bool IsWeak = |
| 2681 | Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak; |
| 2682 | if (!IsWeak && Sel.isUnarySelector()) |
| 2683 | IsWeak = ReturnType.getObjCLifetime() & Qualifiers::OCL_Weak; |
Alp Toker | d4a3f0e | 2014-06-15 23:30:39 +0000 | [diff] [blame] | 2684 | if (IsWeak && |
| 2685 | !Diags.isIgnored(diag::warn_arc_repeated_use_of_weak, LBracLoc)) |
| 2686 | getCurFunction()->recordUseOfWeak(Result, Prop); |
Jordan Rose | 2248765 | 2012-10-11 16:06:21 +0000 | [diff] [blame] | 2687 | } |
| 2688 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2689 | } |
Fariborz Jahanian | 5b3105d | 2014-01-02 22:42:09 +0000 | [diff] [blame] | 2690 | |
Douglas Gregor | aae38d6 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2691 | return MaybeBindToTemporary(Result); |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 2694 | static void RemoveSelectorFromWarningCache(Sema &S, Expr* Arg) { |
| 2695 | if (ObjCSelectorExpr *OSE = |
| 2696 | dyn_cast<ObjCSelectorExpr>(Arg->IgnoreParenCasts())) { |
| 2697 | Selector Sel = OSE->getSelector(); |
| 2698 | SourceLocation Loc = OSE->getAtLoc(); |
| 2699 | llvm::DenseMap<Selector, SourceLocation>::iterator Pos |
| 2700 | = S.ReferencedSelectors.find(Sel); |
| 2701 | if (Pos != S.ReferencedSelectors.end() && Pos->second == Loc) |
| 2702 | S.ReferencedSelectors.erase(Pos); |
| 2703 | } |
| 2704 | } |
| 2705 | |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2706 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 2707 | // ArgExprs is optional - if it is present, the number of expressions |
| 2708 | // is obtained from Sel.getNumArgs(). |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2709 | ExprResult Sema::ActOnInstanceMessage(Scope *S, |
| 2710 | Expr *Receiver, |
| 2711 | Selector Sel, |
| 2712 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | f934ec8 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2713 | ArrayRef<SourceLocation> SelectorLocs, |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2714 | SourceLocation RBracLoc, |
| 2715 | MultiExprArg Args) { |
Douglas Gregor | 0c78ad9 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2716 | if (!Receiver) |
| 2717 | return ExprError(); |
Argyrios Kyrtzidis | 336cc8b | 2013-02-15 18:34:15 +0000 | [diff] [blame] | 2718 | |
| 2719 | // A ParenListExpr can show up while doing error recovery with invalid code. |
| 2720 | if (isa<ParenListExpr>(Receiver)) { |
| 2721 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Receiver); |
| 2722 | if (Result.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 2723 | Receiver = Result.get(); |
Argyrios Kyrtzidis | 336cc8b | 2013-02-15 18:34:15 +0000 | [diff] [blame] | 2724 | } |
Fariborz Jahanian | 1774806 | 2013-01-22 19:05:17 +0000 | [diff] [blame] | 2725 | |
| 2726 | if (RespondsToSelectorSel.isNull()) { |
| 2727 | IdentifierInfo *SelectorId = &Context.Idents.get("respondsToSelector"); |
| 2728 | RespondsToSelectorSel = Context.Selectors.getUnarySelector(SelectorId); |
| 2729 | } |
| 2730 | if (Sel == RespondsToSelectorSel) |
Fariborz Jahanian | 02447d8 | 2013-01-22 18:35:43 +0000 | [diff] [blame] | 2731 | RemoveSelectorFromWarningCache(*this, Args[0]); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2732 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2733 | return BuildInstanceMessage(Receiver, Receiver->getType(), |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2734 | /*SuperLoc=*/SourceLocation(), Sel, |
| 2735 | /*Method=*/nullptr, LBracLoc, SelectorLocs, |
| 2736 | RBracLoc, Args); |
Chris Lattner | a3fc41d | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2737 | } |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2738 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2739 | enum ARCConversionTypeClass { |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2740 | /// int, void, struct A |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2741 | ACTC_none, |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2742 | |
| 2743 | /// id, void (^)() |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2744 | ACTC_retainable, |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2745 | |
| 2746 | /// id*, id***, void (^*)(), |
| 2747 | ACTC_indirectRetainable, |
| 2748 | |
| 2749 | /// void* might be a normal C type, or it might a CF type. |
| 2750 | ACTC_voidPtr, |
| 2751 | |
| 2752 | /// struct A* |
| 2753 | ACTC_coreFoundation |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2754 | }; |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2755 | static bool isAnyRetainable(ARCConversionTypeClass ACTC) { |
| 2756 | return (ACTC == ACTC_retainable || |
| 2757 | ACTC == ACTC_coreFoundation || |
| 2758 | ACTC == ACTC_voidPtr); |
| 2759 | } |
| 2760 | static bool isAnyCLike(ARCConversionTypeClass ACTC) { |
| 2761 | return ACTC == ACTC_none || |
| 2762 | ACTC == ACTC_voidPtr || |
| 2763 | ACTC == ACTC_coreFoundation; |
| 2764 | } |
| 2765 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2766 | static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) { |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2767 | bool isIndirect = false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2768 | |
| 2769 | // Ignore an outermost reference type. |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2770 | if (const ReferenceType *ref = type->getAs<ReferenceType>()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2771 | type = ref->getPointeeType(); |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2772 | isIndirect = true; |
| 2773 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2774 | |
| 2775 | // Drill through pointers and arrays recursively. |
| 2776 | while (true) { |
| 2777 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 2778 | type = ptr->getPointeeType(); |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2779 | |
| 2780 | // The first level of pointer may be the innermost pointer on a CF type. |
| 2781 | if (!isIndirect) { |
| 2782 | if (type->isVoidType()) return ACTC_voidPtr; |
| 2783 | if (type->isRecordType()) return ACTC_coreFoundation; |
| 2784 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2785 | } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) { |
| 2786 | type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0); |
| 2787 | } else { |
| 2788 | break; |
| 2789 | } |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2790 | isIndirect = true; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2793 | if (isIndirect) { |
| 2794 | if (type->isObjCARCBridgableType()) |
| 2795 | return ACTC_indirectRetainable; |
| 2796 | return ACTC_none; |
| 2797 | } |
| 2798 | |
| 2799 | if (type->isObjCARCBridgableType()) |
| 2800 | return ACTC_retainable; |
| 2801 | |
| 2802 | return ACTC_none; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2803 | } |
| 2804 | |
| 2805 | namespace { |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2806 | /// A result from the cast checker. |
| 2807 | enum ACCResult { |
| 2808 | /// Cannot be casted. |
| 2809 | ACC_invalid, |
| 2810 | |
| 2811 | /// Can be safely retained or not retained. |
| 2812 | ACC_bottom, |
| 2813 | |
| 2814 | /// Can be casted at +0. |
| 2815 | ACC_plusZero, |
| 2816 | |
| 2817 | /// Can be casted at +1. |
| 2818 | ACC_plusOne |
| 2819 | }; |
| 2820 | ACCResult merge(ACCResult left, ACCResult right) { |
| 2821 | if (left == right) return left; |
| 2822 | if (left == ACC_bottom) return right; |
| 2823 | if (right == ACC_bottom) return left; |
| 2824 | return ACC_invalid; |
| 2825 | } |
| 2826 | |
| 2827 | /// A checker which white-lists certain expressions whose conversion |
| 2828 | /// to or from retainable type would otherwise be forbidden in ARC. |
| 2829 | class ARCCastChecker : public StmtVisitor<ARCCastChecker, ACCResult> { |
| 2830 | typedef StmtVisitor<ARCCastChecker, ACCResult> super; |
| 2831 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2832 | ASTContext &Context; |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2833 | ARCConversionTypeClass SourceClass; |
| 2834 | ARCConversionTypeClass TargetClass; |
Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 2835 | bool Diagnose; |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2836 | |
| 2837 | static bool isCFType(QualType type) { |
| 2838 | // Someday this can use ns_bridged. For now, it has to do this. |
| 2839 | return type->isCARCBridgableType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2840 | } |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2841 | |
| 2842 | public: |
| 2843 | ARCCastChecker(ASTContext &Context, ARCConversionTypeClass source, |
Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 2844 | ARCConversionTypeClass target, bool diagnose) |
| 2845 | : Context(Context), SourceClass(source), TargetClass(target), |
| 2846 | Diagnose(diagnose) {} |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2847 | |
| 2848 | using super::Visit; |
| 2849 | ACCResult Visit(Expr *e) { |
| 2850 | return super::Visit(e->IgnoreParens()); |
| 2851 | } |
| 2852 | |
| 2853 | ACCResult VisitStmt(Stmt *s) { |
| 2854 | return ACC_invalid; |
| 2855 | } |
| 2856 | |
| 2857 | /// Null pointer constants can be casted however you please. |
| 2858 | ACCResult VisitExpr(Expr *e) { |
| 2859 | if (e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 2860 | return ACC_bottom; |
| 2861 | return ACC_invalid; |
| 2862 | } |
| 2863 | |
| 2864 | /// Objective-C string literals can be safely casted. |
| 2865 | ACCResult VisitObjCStringLiteral(ObjCStringLiteral *e) { |
| 2866 | // If we're casting to any retainable type, go ahead. Global |
| 2867 | // strings are immune to retains, so this is bottom. |
| 2868 | if (isAnyRetainable(TargetClass)) return ACC_bottom; |
| 2869 | |
| 2870 | return ACC_invalid; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2873 | /// Look through certain implicit and explicit casts. |
| 2874 | ACCResult VisitCastExpr(CastExpr *e) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2875 | switch (e->getCastKind()) { |
| 2876 | case CK_NullToPointer: |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2877 | return ACC_bottom; |
| 2878 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2879 | case CK_NoOp: |
| 2880 | case CK_LValueToRValue: |
| 2881 | case CK_BitCast: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2882 | case CK_CPointerToObjCPointerCast: |
| 2883 | case CK_BlockPointerToObjCPointerCast: |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2884 | case CK_AnyPointerToBlockPointerCast: |
| 2885 | return Visit(e->getSubExpr()); |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2886 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2887 | default: |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2888 | return ACC_invalid; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2889 | } |
| 2890 | } |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2891 | |
| 2892 | /// Look through unary extension. |
| 2893 | ACCResult VisitUnaryExtension(UnaryOperator *e) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2894 | return Visit(e->getSubExpr()); |
| 2895 | } |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2896 | |
| 2897 | /// Ignore the LHS of a comma operator. |
| 2898 | ACCResult VisitBinComma(BinaryOperator *e) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2899 | return Visit(e->getRHS()); |
| 2900 | } |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2901 | |
| 2902 | /// Conditional operators are okay if both sides are okay. |
| 2903 | ACCResult VisitConditionalOperator(ConditionalOperator *e) { |
| 2904 | ACCResult left = Visit(e->getTrueExpr()); |
| 2905 | if (left == ACC_invalid) return ACC_invalid; |
| 2906 | return merge(left, Visit(e->getFalseExpr())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2907 | } |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2908 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2909 | /// Look through pseudo-objects. |
| 2910 | ACCResult VisitPseudoObjectExpr(PseudoObjectExpr *e) { |
| 2911 | // If we're getting here, we should always have a result. |
| 2912 | return Visit(e->getResultExpr()); |
| 2913 | } |
| 2914 | |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2915 | /// Statement expressions are okay if their result expression is okay. |
| 2916 | ACCResult VisitStmtExpr(StmtExpr *e) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2917 | return Visit(e->getSubStmt()->body_back()); |
| 2918 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2919 | |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2920 | /// Some declaration references are okay. |
| 2921 | ACCResult VisitDeclRefExpr(DeclRefExpr *e) { |
| 2922 | // References to global constants from system headers are okay. |
| 2923 | // These are things like 'kCFStringTransformToLatin'. They are |
| 2924 | // can also be assumed to be immune to retains. |
| 2925 | VarDecl *var = dyn_cast<VarDecl>(e->getDecl()); |
| 2926 | if (isAnyRetainable(TargetClass) && |
| 2927 | isAnyRetainable(SourceClass) && |
| 2928 | var && |
| 2929 | var->getStorageClass() == SC_Extern && |
| 2930 | var->getType().isConstQualified() && |
| 2931 | Context.getSourceManager().isInSystemHeader(var->getLocation())) { |
| 2932 | return ACC_bottom; |
| 2933 | } |
| 2934 | |
| 2935 | // Nothing else. |
| 2936 | return ACC_invalid; |
Fariborz Jahanian | 7887637 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 2937 | } |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2938 | |
| 2939 | /// Some calls are okay. |
| 2940 | ACCResult VisitCallExpr(CallExpr *e) { |
| 2941 | if (FunctionDecl *fn = e->getDirectCallee()) |
| 2942 | if (ACCResult result = checkCallToFunction(fn)) |
| 2943 | return result; |
| 2944 | |
| 2945 | return super::VisitCallExpr(e); |
| 2946 | } |
| 2947 | |
| 2948 | ACCResult checkCallToFunction(FunctionDecl *fn) { |
| 2949 | // Require a CF*Ref return type. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2950 | if (!isCFType(fn->getReturnType())) |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2951 | return ACC_invalid; |
| 2952 | |
| 2953 | if (!isAnyRetainable(TargetClass)) |
| 2954 | return ACC_invalid; |
| 2955 | |
| 2956 | // Honor an explicit 'not retained' attribute. |
| 2957 | if (fn->hasAttr<CFReturnsNotRetainedAttr>()) |
| 2958 | return ACC_plusZero; |
| 2959 | |
| 2960 | // Honor an explicit 'retained' attribute, except that for |
| 2961 | // now we're not going to permit implicit handling of +1 results, |
| 2962 | // because it's a bit frightening. |
| 2963 | if (fn->hasAttr<CFReturnsRetainedAttr>()) |
Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 2964 | return Diagnose ? ACC_plusOne |
| 2965 | : ACC_invalid; // ACC_plusOne if we start accepting this |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2966 | |
| 2967 | // Recognize this specific builtin function, which is used by CFSTR. |
| 2968 | unsigned builtinID = fn->getBuiltinID(); |
| 2969 | if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString) |
| 2970 | return ACC_bottom; |
| 2971 | |
Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 2972 | // Otherwise, don't do anything implicit with an unaudited function. |
| 2973 | if (!fn->hasAttr<CFAuditedTransferAttr>()) |
| 2974 | return ACC_invalid; |
| 2975 | |
Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 2976 | // Otherwise, it's +0 unless it follows the create convention. |
| 2977 | if (ento::coreFoundation::followsCreateRule(fn)) |
| 2978 | return Diagnose ? ACC_plusOne |
| 2979 | : ACC_invalid; // ACC_plusOne if we start accepting this |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2980 | |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2981 | return ACC_plusZero; |
| 2982 | } |
| 2983 | |
| 2984 | ACCResult VisitObjCMessageExpr(ObjCMessageExpr *e) { |
| 2985 | return checkCallToMethod(e->getMethodDecl()); |
| 2986 | } |
| 2987 | |
| 2988 | ACCResult VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *e) { |
| 2989 | ObjCMethodDecl *method; |
| 2990 | if (e->isExplicitProperty()) |
| 2991 | method = e->getExplicitProperty()->getGetterMethodDecl(); |
| 2992 | else |
| 2993 | method = e->getImplicitPropertyGetter(); |
| 2994 | return checkCallToMethod(method); |
| 2995 | } |
| 2996 | |
| 2997 | ACCResult checkCallToMethod(ObjCMethodDecl *method) { |
| 2998 | if (!method) return ACC_invalid; |
| 2999 | |
| 3000 | // Check for message sends to functions returning CF types. We |
| 3001 | // just obey the Cocoa conventions with these, even though the |
| 3002 | // return type is CF. |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3003 | if (!isAnyRetainable(TargetClass) || !isCFType(method->getReturnType())) |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3004 | return ACC_invalid; |
| 3005 | |
| 3006 | // If the method is explicitly marked not-retained, it's +0. |
| 3007 | if (method->hasAttr<CFReturnsNotRetainedAttr>()) |
| 3008 | return ACC_plusZero; |
| 3009 | |
| 3010 | // If the method is explicitly marked as returning retained, or its |
| 3011 | // selector follows a +1 Cocoa convention, treat it as +1. |
| 3012 | if (method->hasAttr<CFReturnsRetainedAttr>()) |
| 3013 | return ACC_plusOne; |
| 3014 | |
| 3015 | switch (method->getSelector().getMethodFamily()) { |
| 3016 | case OMF_alloc: |
| 3017 | case OMF_copy: |
| 3018 | case OMF_mutableCopy: |
| 3019 | case OMF_new: |
| 3020 | return ACC_plusOne; |
| 3021 | |
| 3022 | default: |
| 3023 | // Otherwise, treat it as +0. |
| 3024 | return ACC_plusZero; |
Fariborz Jahanian | 9b83be8 | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 3025 | } |
| 3026 | } |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3027 | }; |
Fariborz Jahanian | 4ad5686 | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3030 | bool Sema::isKnownName(StringRef name) { |
| 3031 | if (name.empty()) |
| 3032 | return false; |
| 3033 | LookupResult R(*this, &Context.Idents.get(name), SourceLocation(), |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3034 | Sema::LookupOrdinaryName); |
Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3035 | return LookupName(R, TUScope, false); |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3038 | static void addFixitForObjCARCConversion(Sema &S, |
| 3039 | DiagnosticBuilder &DiagB, |
| 3040 | Sema::CheckedConversionKind CCK, |
| 3041 | SourceLocation afterLParen, |
| 3042 | QualType castType, |
| 3043 | Expr *castExpr, |
Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3044 | Expr *realCast, |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3045 | const char *bridgeKeyword, |
| 3046 | const char *CFBridgeName) { |
| 3047 | // We handle C-style and implicit casts here. |
| 3048 | switch (CCK) { |
| 3049 | case Sema::CCK_ImplicitConversion: |
| 3050 | case Sema::CCK_CStyleCast: |
Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3051 | case Sema::CCK_OtherCast: |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3052 | break; |
| 3053 | case Sema::CCK_FunctionalCast: |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3054 | return; |
| 3055 | } |
| 3056 | |
| 3057 | if (CFBridgeName) { |
Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3058 | if (CCK == Sema::CCK_OtherCast) { |
| 3059 | if (const CXXNamedCastExpr *NCE = dyn_cast<CXXNamedCastExpr>(realCast)) { |
| 3060 | SourceRange range(NCE->getOperatorLoc(), |
| 3061 | NCE->getAngleBrackets().getEnd()); |
| 3062 | SmallString<32> BridgeCall; |
| 3063 | |
| 3064 | SourceManager &SM = S.getSourceManager(); |
| 3065 | char PrevChar = *SM.getCharacterData(range.getBegin().getLocWithOffset(-1)); |
| 3066 | if (Lexer::isIdentifierBodyChar(PrevChar, S.getLangOpts())) |
| 3067 | BridgeCall += ' '; |
| 3068 | |
| 3069 | BridgeCall += CFBridgeName; |
| 3070 | DiagB.AddFixItHint(FixItHint::CreateReplacement(range, BridgeCall)); |
| 3071 | } |
| 3072 | return; |
| 3073 | } |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3074 | Expr *castedE = castExpr; |
| 3075 | if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(castedE)) |
| 3076 | castedE = CCE->getSubExpr(); |
| 3077 | castedE = castedE->IgnoreImpCasts(); |
| 3078 | SourceRange range = castedE->getSourceRange(); |
Jordan Rose | 288c421 | 2012-06-07 01:10:31 +0000 | [diff] [blame] | 3079 | |
| 3080 | SmallString<32> BridgeCall; |
| 3081 | |
| 3082 | SourceManager &SM = S.getSourceManager(); |
| 3083 | char PrevChar = *SM.getCharacterData(range.getBegin().getLocWithOffset(-1)); |
| 3084 | if (Lexer::isIdentifierBodyChar(PrevChar, S.getLangOpts())) |
| 3085 | BridgeCall += ' '; |
| 3086 | |
| 3087 | BridgeCall += CFBridgeName; |
| 3088 | |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3089 | if (isa<ParenExpr>(castedE)) { |
| 3090 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
Jordan Rose | 288c421 | 2012-06-07 01:10:31 +0000 | [diff] [blame] | 3091 | BridgeCall)); |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3092 | } else { |
Jordan Rose | 288c421 | 2012-06-07 01:10:31 +0000 | [diff] [blame] | 3093 | BridgeCall += '('; |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3094 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
Jordan Rose | 288c421 | 2012-06-07 01:10:31 +0000 | [diff] [blame] | 3095 | BridgeCall)); |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3096 | DiagB.AddFixItHint(FixItHint::CreateInsertion( |
| 3097 | S.PP.getLocForEndOfToken(range.getEnd()), |
| 3098 | ")")); |
| 3099 | } |
| 3100 | return; |
| 3101 | } |
| 3102 | |
| 3103 | if (CCK == Sema::CCK_CStyleCast) { |
| 3104 | DiagB.AddFixItHint(FixItHint::CreateInsertion(afterLParen, bridgeKeyword)); |
Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3105 | } else if (CCK == Sema::CCK_OtherCast) { |
| 3106 | if (const CXXNamedCastExpr *NCE = dyn_cast<CXXNamedCastExpr>(realCast)) { |
| 3107 | std::string castCode = "("; |
| 3108 | castCode += bridgeKeyword; |
| 3109 | castCode += castType.getAsString(); |
| 3110 | castCode += ")"; |
| 3111 | SourceRange Range(NCE->getOperatorLoc(), |
| 3112 | NCE->getAngleBrackets().getEnd()); |
| 3113 | DiagB.AddFixItHint(FixItHint::CreateReplacement(Range, castCode)); |
| 3114 | } |
Argyrios Kyrtzidis | 6aa70e2 | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 3115 | } else { |
| 3116 | std::string castCode = "("; |
| 3117 | castCode += bridgeKeyword; |
| 3118 | castCode += castType.getAsString(); |
| 3119 | castCode += ")"; |
| 3120 | Expr *castedE = castExpr->IgnoreImpCasts(); |
| 3121 | SourceRange range = castedE->getSourceRange(); |
| 3122 | if (isa<ParenExpr>(castedE)) { |
| 3123 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 3124 | castCode)); |
| 3125 | } else { |
| 3126 | castCode += "("; |
| 3127 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 3128 | castCode)); |
| 3129 | DiagB.AddFixItHint(FixItHint::CreateInsertion( |
| 3130 | S.PP.getLocForEndOfToken(range.getEnd()), |
| 3131 | ")")); |
| 3132 | } |
| 3133 | } |
| 3134 | } |
| 3135 | |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3136 | template <typename T> |
| 3137 | static inline T *getObjCBridgeAttr(const TypedefType *TD) { |
| 3138 | TypedefNameDecl *TDNDecl = TD->getDecl(); |
| 3139 | QualType QT = TDNDecl->getUnderlyingType(); |
| 3140 | if (QT->isPointerType()) { |
| 3141 | QT = QT->getPointeeType(); |
| 3142 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Fariborz Jahanian | 9af6a78 | 2014-06-11 19:10:46 +0000 | [diff] [blame] | 3143 | if (RecordDecl *RD = RT->getDecl()->getMostRecentDecl()) |
Aaron Ballman | 2084f8f | 2013-12-19 13:20:36 +0000 | [diff] [blame] | 3144 | return RD->getAttr<T>(); |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3145 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3146 | return nullptr; |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | static ObjCBridgeRelatedAttr *ObjCBridgeRelatedAttrFromType(QualType T, |
| 3150 | TypedefNameDecl *&TDNDecl) { |
| 3151 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) { |
| 3152 | TDNDecl = TD->getDecl(); |
| 3153 | if (ObjCBridgeRelatedAttr *ObjCBAttr = |
| 3154 | getObjCBridgeAttr<ObjCBridgeRelatedAttr>(TD)) |
| 3155 | return ObjCBAttr; |
| 3156 | T = TDNDecl->getUnderlyingType(); |
| 3157 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3158 | return nullptr; |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3161 | static void |
| 3162 | diagnoseObjCARCConversion(Sema &S, SourceRange castRange, |
| 3163 | QualType castType, ARCConversionTypeClass castACTC, |
Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3164 | Expr *castExpr, Expr *realCast, |
| 3165 | ARCConversionTypeClass exprACTC, |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3166 | Sema::CheckedConversionKind CCK) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3167 | SourceLocation loc = |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3168 | (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3169 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3170 | if (S.makeUnavailableInSystemHeader(loc, |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3171 | "converts between Objective-C and C pointers in -fobjc-arc")) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3172 | return; |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3173 | |
| 3174 | QualType castExprType = castExpr->getType(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3175 | TypedefNameDecl *TDNDecl = nullptr; |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3176 | if ((castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable && |
| 3177 | ObjCBridgeRelatedAttrFromType(castType, TDNDecl)) || |
| 3178 | (exprACTC == ACTC_coreFoundation && castACTC == ACTC_retainable && |
Fariborz Jahanian | 1ad83a3 | 2014-06-18 23:22:38 +0000 | [diff] [blame] | 3179 | ObjCBridgeRelatedAttrFromType(castExprType, TDNDecl))) |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3180 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3181 | |
John McCall | 640767f | 2011-06-17 06:50:50 +0000 | [diff] [blame] | 3182 | unsigned srcKind = 0; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3183 | switch (exprACTC) { |
John McCall | e4fe245 | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 3184 | case ACTC_none: |
| 3185 | case ACTC_coreFoundation: |
| 3186 | case ACTC_voidPtr: |
| 3187 | srcKind = (castExprType->isPointerType() ? 1 : 0); |
| 3188 | break; |
| 3189 | case ACTC_retainable: |
| 3190 | srcKind = (castExprType->isBlockPointerType() ? 2 : 3); |
| 3191 | break; |
| 3192 | case ACTC_indirectRetainable: |
| 3193 | srcKind = 4; |
| 3194 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3197 | // Check whether this could be fixed with a bridge cast. |
| 3198 | SourceLocation afterLParen = S.PP.getLocForEndOfToken(castRange.getBegin()); |
| 3199 | SourceLocation noteLoc = afterLParen.isValid() ? afterLParen : loc; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3200 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3201 | // Bridge from an ARC type to a CF type. |
| 3202 | if (castACTC == ACTC_retainable && isAnyRetainable(exprACTC)) { |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3203 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3204 | S.Diag(loc, diag::err_arc_cast_requires_bridge) |
| 3205 | << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit |
| 3206 | << 2 // of C pointer type |
| 3207 | << castExprType |
| 3208 | << unsigned(castType->isBlockPointerType()) // to ObjC|block type |
| 3209 | << castType |
| 3210 | << castRange |
| 3211 | << castExpr->getSourceRange(); |
Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3212 | bool br = S.isKnownName("CFBridgingRelease"); |
Jordan Rose | 4502b53 | 2012-07-31 01:07:43 +0000 | [diff] [blame] | 3213 | ACCResult CreateRule = |
Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3214 | ARCCastChecker(S.Context, exprACTC, castACTC, true).Visit(castExpr); |
Jordan Rose | 4502b53 | 2012-07-31 01:07:43 +0000 | [diff] [blame] | 3215 | assert(CreateRule != ACC_bottom && "This cast should already be accepted."); |
Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3216 | if (CreateRule != ACC_plusOne) |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3217 | { |
Fariborz Jahanian | ac2d082 | 2013-02-22 01:22:48 +0000 | [diff] [blame] | 3218 | DiagnosticBuilder DiagB = |
| 3219 | (CCK != Sema::CCK_OtherCast) ? S.Diag(noteLoc, diag::note_arc_bridge) |
| 3220 | : S.Diag(noteLoc, diag::note_arc_cstyle_bridge); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3221 | |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3222 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3223 | castType, castExpr, realCast, "__bridge ", |
| 3224 | nullptr); |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3225 | } |
Fariborz Jahanian | f7759e8 | 2012-07-28 18:59:49 +0000 | [diff] [blame] | 3226 | if (CreateRule != ACC_plusZero) |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3227 | { |
Fariborz Jahanian | ac2d082 | 2013-02-22 01:22:48 +0000 | [diff] [blame] | 3228 | DiagnosticBuilder DiagB = |
| 3229 | (CCK == Sema::CCK_OtherCast && !br) ? |
| 3230 | S.Diag(noteLoc, diag::note_arc_cstyle_bridge_transfer) << castExprType : |
| 3231 | S.Diag(br ? castExpr->getExprLoc() : noteLoc, |
| 3232 | diag::note_arc_bridge_transfer) |
| 3233 | << castExprType << br; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3234 | |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3235 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3236 | castType, castExpr, realCast, "__bridge_transfer ", |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3237 | br ? "CFBridgingRelease" : nullptr); |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3238 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3239 | |
| 3240 | return; |
| 3241 | } |
Fariborz Jahanian | f1a22f4 | 2014-04-29 16:12:56 +0000 | [diff] [blame] | 3242 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3243 | // Bridge from a CF type to an ARC type. |
| 3244 | if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC)) { |
Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3245 | bool br = S.isKnownName("CFBridgingRetain"); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3246 | S.Diag(loc, diag::err_arc_cast_requires_bridge) |
| 3247 | << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit |
| 3248 | << unsigned(castExprType->isBlockPointerType()) // of ObjC|block type |
| 3249 | << castExprType |
| 3250 | << 2 // to C pointer type |
| 3251 | << castType |
| 3252 | << castRange |
| 3253 | << castExpr->getSourceRange(); |
Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3254 | ACCResult CreateRule = |
| 3255 | ARCCastChecker(S.Context, exprACTC, castACTC, true).Visit(castExpr); |
Jordan Rose | 4502b53 | 2012-07-31 01:07:43 +0000 | [diff] [blame] | 3256 | assert(CreateRule != ACC_bottom && "This cast should already be accepted."); |
Fariborz Jahanian | ae5bbfc | 2012-07-27 23:55:46 +0000 | [diff] [blame] | 3257 | if (CreateRule != ACC_plusOne) |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3258 | { |
Fariborz Jahanian | ac2d082 | 2013-02-22 01:22:48 +0000 | [diff] [blame] | 3259 | DiagnosticBuilder DiagB = |
| 3260 | (CCK != Sema::CCK_OtherCast) ? S.Diag(noteLoc, diag::note_arc_bridge) |
| 3261 | : S.Diag(noteLoc, diag::note_arc_cstyle_bridge); |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3262 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3263 | castType, castExpr, realCast, "__bridge ", |
| 3264 | nullptr); |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3265 | } |
Fariborz Jahanian | f7759e8 | 2012-07-28 18:59:49 +0000 | [diff] [blame] | 3266 | if (CreateRule != ACC_plusZero) |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3267 | { |
Fariborz Jahanian | ac2d082 | 2013-02-22 01:22:48 +0000 | [diff] [blame] | 3268 | DiagnosticBuilder DiagB = |
| 3269 | (CCK == Sema::CCK_OtherCast && !br) ? |
| 3270 | S.Diag(noteLoc, diag::note_arc_cstyle_bridge_retained) << castType : |
| 3271 | S.Diag(br ? castExpr->getExprLoc() : noteLoc, |
| 3272 | diag::note_arc_bridge_retained) |
| 3273 | << castType << br; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3274 | |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3275 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3276 | castType, castExpr, realCast, "__bridge_retained ", |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3277 | br ? "CFBridgingRetain" : nullptr); |
Fariborz Jahanian | 84c97ca | 2012-07-27 21:34:23 +0000 | [diff] [blame] | 3278 | } |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3279 | |
| 3280 | return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3283 | S.Diag(loc, diag::err_arc_mismatched_cast) |
| 3284 | << (CCK != Sema::CCK_ImplicitConversion) |
| 3285 | << srcKind << castExprType << castType |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3286 | << castRange << castExpr->getSourceRange(); |
| 3287 | } |
| 3288 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3289 | template <typename TB> |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3290 | static bool CheckObjCBridgeNSCast(Sema &S, QualType castType, Expr *castExpr, |
| 3291 | bool &HadTheAttribute, bool warn) { |
Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3292 | QualType T = castExpr->getType(); |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3293 | HadTheAttribute = false; |
Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3294 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) { |
| 3295 | TypedefNameDecl *TDNDecl = TD->getDecl(); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3296 | if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) { |
Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 3297 | if (IdentifierInfo *Parm = ObjCBAttr->getBridgedType()) { |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3298 | HadTheAttribute = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3299 | NamedDecl *Target = nullptr; |
Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3300 | // Check for an existing type with this name. |
| 3301 | LookupResult R(S, DeclarationName(Parm), SourceLocation(), |
| 3302 | Sema::LookupOrdinaryName); |
| 3303 | if (S.LookupName(R, S.TUScope)) { |
Fariborz Jahanian | f07183c | 2013-11-16 01:45:25 +0000 | [diff] [blame] | 3304 | Target = R.getFoundDecl(); |
| 3305 | if (Target && isa<ObjCInterfaceDecl>(Target)) { |
| 3306 | ObjCInterfaceDecl *ExprClass = cast<ObjCInterfaceDecl>(Target); |
| 3307 | if (const ObjCObjectPointerType *InterfacePointerType = |
| 3308 | castType->getAsObjCInterfacePointerType()) { |
| 3309 | ObjCInterfaceDecl *CastClass |
| 3310 | = InterfacePointerType->getObjectType()->getInterface(); |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3311 | if ((CastClass == ExprClass) || |
Fariborz Jahanian | f1a22f4 | 2014-04-29 16:12:56 +0000 | [diff] [blame] | 3312 | (CastClass && ExprClass->isSuperClassOf(CastClass))) |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3313 | return true; |
| 3314 | if (warn) |
| 3315 | S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge) |
| 3316 | << T << Target->getName() << castType->getPointeeType(); |
| 3317 | return false; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3318 | } else if (castType->isObjCIdType() || |
| 3319 | (S.Context.ObjCObjectAdoptsQTypeProtocols( |
| 3320 | castType, ExprClass))) |
| 3321 | // ok to cast to 'id'. |
| 3322 | // casting to id<p-list> is ok if bridge type adopts all of |
| 3323 | // p-list protocols. |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3324 | return true; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3325 | else { |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3326 | if (warn) { |
| 3327 | S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge) |
| 3328 | << T << Target->getName() << castType; |
| 3329 | S.Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3330 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 3331 | } |
| 3332 | return false; |
Fariborz Jahanian | 2c31212 | 2013-11-16 23:22:37 +0000 | [diff] [blame] | 3333 | } |
Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3334 | } |
Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3335 | } |
Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3336 | S.Diag(castExpr->getLocStart(), diag::err_objc_cf_bridged_not_interface) |
Aaron Ballman | c055074 | 2014-01-03 02:07:43 +0000 | [diff] [blame] | 3337 | << castExpr->getType() << Parm; |
Fariborz Jahanian | f07183c | 2013-11-16 01:45:25 +0000 | [diff] [blame] | 3338 | S.Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3339 | if (Target) |
| 3340 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3341 | return true; |
Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3342 | } |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3343 | return false; |
Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3344 | } |
| 3345 | T = TDNDecl->getUnderlyingType(); |
| 3346 | } |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3347 | return true; |
Fariborz Jahanian | a649c82 | 2013-11-15 22:18:17 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3350 | template <typename TB> |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3351 | static bool CheckObjCBridgeCFCast(Sema &S, QualType castType, Expr *castExpr, |
| 3352 | bool &HadTheAttribute, bool warn) { |
Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3353 | QualType T = castType; |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3354 | HadTheAttribute = false; |
Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3355 | while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) { |
| 3356 | TypedefNameDecl *TDNDecl = TD->getDecl(); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3357 | if (TB *ObjCBAttr = getObjCBridgeAttr<TB>(TD)) { |
Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 3358 | if (IdentifierInfo *Parm = ObjCBAttr->getBridgedType()) { |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3359 | HadTheAttribute = true; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3360 | NamedDecl *Target = nullptr; |
Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3361 | // Check for an existing type with this name. |
| 3362 | LookupResult R(S, DeclarationName(Parm), SourceLocation(), |
| 3363 | Sema::LookupOrdinaryName); |
| 3364 | if (S.LookupName(R, S.TUScope)) { |
| 3365 | Target = R.getFoundDecl(); |
| 3366 | if (Target && isa<ObjCInterfaceDecl>(Target)) { |
| 3367 | ObjCInterfaceDecl *CastClass = cast<ObjCInterfaceDecl>(Target); |
| 3368 | if (const ObjCObjectPointerType *InterfacePointerType = |
| 3369 | castExpr->getType()->getAsObjCInterfacePointerType()) { |
| 3370 | ObjCInterfaceDecl *ExprClass |
| 3371 | = InterfacePointerType->getObjectType()->getInterface(); |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3372 | if ((CastClass == ExprClass) || |
| 3373 | (ExprClass && CastClass->isSuperClassOf(ExprClass))) |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3374 | return true; |
| 3375 | if (warn) { |
| 3376 | S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge_to_cf) |
| 3377 | << castExpr->getType()->getPointeeType() << T; |
| 3378 | S.Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3379 | } |
| 3380 | return false; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3381 | } else if (castExpr->getType()->isObjCIdType() || |
| 3382 | (S.Context.QIdProtocolsAdoptObjCObjectProtocols( |
| 3383 | castExpr->getType(), CastClass))) |
| 3384 | // ok to cast an 'id' expression to a CFtype. |
| 3385 | // ok to cast an 'id<plist>' expression to CFtype provided plist |
| 3386 | // adopts all of CFtype's ObjetiveC's class plist. |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3387 | return true; |
Fariborz Jahanian | 92ab298 | 2013-11-20 00:32:12 +0000 | [diff] [blame] | 3388 | else { |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3389 | if (warn) { |
| 3390 | S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge_to_cf) |
| 3391 | << castExpr->getType() << castType; |
| 3392 | S.Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3393 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 3394 | } |
| 3395 | return false; |
Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3396 | } |
| 3397 | } |
| 3398 | } |
| 3399 | S.Diag(castExpr->getLocStart(), diag::err_objc_ns_bridged_invalid_cfobject) |
| 3400 | << castExpr->getType() << castType; |
| 3401 | S.Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3402 | if (Target) |
| 3403 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3404 | return true; |
Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3405 | } |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3406 | return false; |
Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3407 | } |
| 3408 | T = TDNDecl->getUnderlyingType(); |
| 3409 | } |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3410 | return true; |
Fariborz Jahanian | 8a0210e | 2013-11-16 19:16:32 +0000 | [diff] [blame] | 3411 | } |
| 3412 | |
Fariborz Jahanian | 8c5b4be | 2013-11-21 00:39:36 +0000 | [diff] [blame] | 3413 | void Sema::CheckTollFreeBridgeCast(QualType castType, Expr *castExpr) { |
Fariborz Jahanian | f1a22f4 | 2014-04-29 16:12:56 +0000 | [diff] [blame] | 3414 | if (!getLangOpts().ObjC1) |
| 3415 | return; |
Alp Toker | f6a24ce | 2013-12-05 16:25:25 +0000 | [diff] [blame] | 3416 | // 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] | 3417 | ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExpr->getType()); |
| 3418 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3419 | if (castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) { |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3420 | bool HasObjCBridgeAttr; |
| 3421 | bool ObjCBridgeAttrWillNotWarn = |
| 3422 | CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr, |
| 3423 | false); |
| 3424 | if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr) |
| 3425 | return; |
| 3426 | bool HasObjCBridgeMutableAttr; |
| 3427 | bool ObjCBridgeMutableAttrWillNotWarn = |
| 3428 | CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, |
| 3429 | HasObjCBridgeMutableAttr, false); |
| 3430 | if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr) |
| 3431 | return; |
| 3432 | |
| 3433 | if (HasObjCBridgeAttr) |
| 3434 | CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr, |
| 3435 | true); |
| 3436 | else if (HasObjCBridgeMutableAttr) |
| 3437 | CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, |
| 3438 | HasObjCBridgeMutableAttr, true); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3439 | } |
| 3440 | else if (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable) { |
Fariborz Jahanian | 5cbbb1be | 2014-06-11 16:52:44 +0000 | [diff] [blame] | 3441 | bool HasObjCBridgeAttr; |
| 3442 | bool ObjCBridgeAttrWillNotWarn = |
| 3443 | CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr, |
| 3444 | false); |
| 3445 | if (ObjCBridgeAttrWillNotWarn && HasObjCBridgeAttr) |
| 3446 | return; |
| 3447 | bool HasObjCBridgeMutableAttr; |
| 3448 | bool ObjCBridgeMutableAttrWillNotWarn = |
| 3449 | CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, |
| 3450 | HasObjCBridgeMutableAttr, false); |
| 3451 | if (ObjCBridgeMutableAttrWillNotWarn && HasObjCBridgeMutableAttr) |
| 3452 | return; |
| 3453 | |
| 3454 | if (HasObjCBridgeAttr) |
| 3455 | CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr, HasObjCBridgeAttr, |
| 3456 | true); |
| 3457 | else if (HasObjCBridgeMutableAttr) |
| 3458 | CheckObjCBridgeCFCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, |
| 3459 | HasObjCBridgeMutableAttr, true); |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3460 | } |
Fariborz Jahanian | 8c5b4be | 2013-11-21 00:39:36 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Fariborz Jahanian | 53f867a | 2014-06-26 21:22:16 +0000 | [diff] [blame] | 3463 | void Sema::CheckObjCBridgeRelatedCast(QualType castType, Expr *castExpr) { |
| 3464 | QualType SrcType = castExpr->getType(); |
| 3465 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(castExpr)) { |
| 3466 | if (PRE->isExplicitProperty()) { |
| 3467 | if (ObjCPropertyDecl *PDecl = PRE->getExplicitProperty()) |
| 3468 | SrcType = PDecl->getType(); |
| 3469 | } |
| 3470 | else if (PRE->isImplicitProperty()) { |
| 3471 | if (ObjCMethodDecl *Getter = PRE->getImplicitPropertyGetter()) |
| 3472 | SrcType = Getter->getReturnType(); |
| 3473 | |
| 3474 | } |
| 3475 | } |
| 3476 | |
| 3477 | ARCConversionTypeClass srcExprACTC = classifyTypeForARCConversion(SrcType); |
| 3478 | ARCConversionTypeClass castExprACTC = classifyTypeForARCConversion(castType); |
| 3479 | if (srcExprACTC != ACTC_retainable || castExprACTC != ACTC_coreFoundation) |
| 3480 | return; |
| 3481 | CheckObjCBridgeRelatedConversions(castExpr->getLocStart(), |
| 3482 | castType, SrcType, castExpr); |
| 3483 | return; |
| 3484 | } |
| 3485 | |
Fariborz Jahanian | c70a543 | 2014-05-10 17:40:11 +0000 | [diff] [blame] | 3486 | bool Sema::CheckTollFreeBridgeStaticCast(QualType castType, Expr *castExpr, |
| 3487 | CastKind &Kind) { |
| 3488 | if (!getLangOpts().ObjC1) |
| 3489 | return false; |
| 3490 | ARCConversionTypeClass exprACTC = |
| 3491 | classifyTypeForARCConversion(castExpr->getType()); |
| 3492 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType); |
| 3493 | if ((castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) || |
| 3494 | (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable)) { |
| 3495 | CheckTollFreeBridgeCast(castType, castExpr); |
| 3496 | Kind = (castACTC == ACTC_coreFoundation) ? CK_BitCast |
| 3497 | : CK_CPointerToObjCPointerCast; |
| 3498 | return true; |
| 3499 | } |
| 3500 | return false; |
| 3501 | } |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3502 | |
| 3503 | bool Sema::checkObjCBridgeRelatedComponents(SourceLocation Loc, |
| 3504 | QualType DestType, QualType SrcType, |
| 3505 | ObjCInterfaceDecl *&RelatedClass, |
| 3506 | ObjCMethodDecl *&ClassMethod, |
| 3507 | ObjCMethodDecl *&InstanceMethod, |
| 3508 | TypedefNameDecl *&TDNDecl, |
| 3509 | bool CfToNs) { |
| 3510 | QualType T = CfToNs ? SrcType : DestType; |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3511 | ObjCBridgeRelatedAttr *ObjCBAttr = ObjCBridgeRelatedAttrFromType(T, TDNDecl); |
| 3512 | if (!ObjCBAttr) |
| 3513 | return false; |
| 3514 | |
| 3515 | IdentifierInfo *RCId = ObjCBAttr->getRelatedClass(); |
| 3516 | IdentifierInfo *CMId = ObjCBAttr->getClassMethod(); |
| 3517 | IdentifierInfo *IMId = ObjCBAttr->getInstanceMethod(); |
| 3518 | if (!RCId) |
| 3519 | return false; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3520 | NamedDecl *Target = nullptr; |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3521 | // Check for an existing type with this name. |
| 3522 | LookupResult R(*this, DeclarationName(RCId), SourceLocation(), |
| 3523 | Sema::LookupOrdinaryName); |
| 3524 | if (!LookupName(R, TUScope)) { |
| 3525 | Diag(Loc, diag::err_objc_bridged_related_invalid_class) << RCId |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3526 | << SrcType << DestType; |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3527 | Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3528 | return false; |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3529 | } |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3530 | Target = R.getFoundDecl(); |
| 3531 | if (Target && isa<ObjCInterfaceDecl>(Target)) |
| 3532 | RelatedClass = cast<ObjCInterfaceDecl>(Target); |
| 3533 | else { |
| 3534 | Diag(Loc, diag::err_objc_bridged_related_invalid_class_name) << RCId |
| 3535 | << SrcType << DestType; |
| 3536 | Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3537 | if (Target) |
| 3538 | Diag(Target->getLocStart(), diag::note_declared_at); |
| 3539 | return false; |
| 3540 | } |
| 3541 | |
| 3542 | // Check for an existing class method with the given selector name. |
| 3543 | if (CfToNs && CMId) { |
| 3544 | Selector Sel = Context.Selectors.getUnarySelector(CMId); |
| 3545 | ClassMethod = RelatedClass->lookupMethod(Sel, false); |
| 3546 | if (!ClassMethod) { |
| 3547 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
Fariborz Jahanian | 7c04a55 | 2013-12-10 19:22:41 +0000 | [diff] [blame] | 3548 | << SrcType << DestType << Sel << false; |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3549 | Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3550 | return false; |
| 3551 | } |
| 3552 | } |
| 3553 | |
| 3554 | // Check for an existing instance method with the given selector name. |
| 3555 | if (!CfToNs && IMId) { |
| 3556 | Selector Sel = Context.Selectors.getNullarySelector(IMId); |
| 3557 | InstanceMethod = RelatedClass->lookupMethod(Sel, true); |
| 3558 | if (!InstanceMethod) { |
| 3559 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
Fariborz Jahanian | 7c04a55 | 2013-12-10 19:22:41 +0000 | [diff] [blame] | 3560 | << SrcType << DestType << Sel << true; |
Fariborz Jahanian | 67379e2 | 2013-12-09 22:04:26 +0000 | [diff] [blame] | 3561 | Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3562 | return false; |
| 3563 | } |
| 3564 | } |
| 3565 | return true; |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3566 | } |
| 3567 | |
| 3568 | bool |
| 3569 | Sema::CheckObjCBridgeRelatedConversions(SourceLocation Loc, |
Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 3570 | QualType DestType, QualType SrcType, |
Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 3571 | Expr *&SrcExpr) { |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3572 | ARCConversionTypeClass rhsExprACTC = classifyTypeForARCConversion(SrcType); |
| 3573 | ARCConversionTypeClass lhsExprACTC = classifyTypeForARCConversion(DestType); |
| 3574 | bool CfToNs = (rhsExprACTC == ACTC_coreFoundation && lhsExprACTC == ACTC_retainable); |
| 3575 | bool NsToCf = (rhsExprACTC == ACTC_retainable && lhsExprACTC == ACTC_coreFoundation); |
| 3576 | if (!CfToNs && !NsToCf) |
| 3577 | return false; |
| 3578 | |
| 3579 | ObjCInterfaceDecl *RelatedClass; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3580 | ObjCMethodDecl *ClassMethod = nullptr; |
| 3581 | ObjCMethodDecl *InstanceMethod = nullptr; |
| 3582 | TypedefNameDecl *TDNDecl = nullptr; |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3583 | if (!checkObjCBridgeRelatedComponents(Loc, DestType, SrcType, RelatedClass, |
| 3584 | ClassMethod, InstanceMethod, TDNDecl, CfToNs)) |
| 3585 | return false; |
| 3586 | |
| 3587 | if (CfToNs) { |
| 3588 | // Implicit conversion from CF to ObjC object is needed. |
Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 3589 | if (ClassMethod) { |
| 3590 | std::string ExpressionString = "["; |
| 3591 | ExpressionString += RelatedClass->getNameAsString(); |
| 3592 | ExpressionString += " "; |
| 3593 | ExpressionString += ClassMethod->getSelector().getAsString(); |
| 3594 | SourceLocation SrcExprEndLoc = PP.getLocForEndOfToken(SrcExpr->getLocEnd()); |
| 3595 | // Provide a fixit: [RelatedClass ClassMethod SrcExpr] |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3596 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
Fariborz Jahanian | 7c04a55 | 2013-12-10 19:22:41 +0000 | [diff] [blame] | 3597 | << SrcType << DestType << ClassMethod->getSelector() << false |
Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 3598 | << FixItHint::CreateInsertion(SrcExpr->getLocStart(), ExpressionString) |
| 3599 | << FixItHint::CreateInsertion(SrcExprEndLoc, "]"); |
Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 3600 | Diag(RelatedClass->getLocStart(), diag::note_declared_at); |
| 3601 | Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3602 | |
| 3603 | QualType receiverType = |
| 3604 | Context.getObjCInterfaceType(RelatedClass); |
| 3605 | // Argument. |
| 3606 | Expr *args[] = { SrcExpr }; |
| 3607 | ExprResult msg = BuildClassMessageImplicit(receiverType, false, |
| 3608 | ClassMethod->getLocation(), |
| 3609 | ClassMethod->getSelector(), ClassMethod, |
| 3610 | MultiExprArg(args, 1)); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3611 | SrcExpr = msg.get(); |
Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 3612 | return true; |
Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 3613 | } |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3614 | } |
| 3615 | else { |
| 3616 | // Implicit conversion from ObjC type to CF object is needed. |
Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 3617 | if (InstanceMethod) { |
Fariborz Jahanian | 88b6898 | 2013-12-10 23:18:06 +0000 | [diff] [blame] | 3618 | std::string ExpressionString; |
Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 3619 | SourceLocation SrcExprEndLoc = PP.getLocForEndOfToken(SrcExpr->getLocEnd()); |
Fariborz Jahanian | 88b6898 | 2013-12-10 23:18:06 +0000 | [diff] [blame] | 3620 | if (InstanceMethod->isPropertyAccessor()) |
| 3621 | if (const ObjCPropertyDecl *PDecl = InstanceMethod->findPropertyDecl()) { |
| 3622 | // fixit: ObjectExpr.propertyname when it is aproperty accessor. |
| 3623 | ExpressionString = "."; |
| 3624 | ExpressionString += PDecl->getNameAsString(); |
| 3625 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
| 3626 | << SrcType << DestType << InstanceMethod->getSelector() << true |
| 3627 | << FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString); |
| 3628 | } |
| 3629 | if (ExpressionString.empty()) { |
| 3630 | // Provide a fixit: [ObjectExpr InstanceMethod] |
| 3631 | ExpressionString = " "; |
| 3632 | ExpressionString += InstanceMethod->getSelector().getAsString(); |
| 3633 | ExpressionString += "]"; |
Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 3634 | |
Fariborz Jahanian | 88b6898 | 2013-12-10 23:18:06 +0000 | [diff] [blame] | 3635 | Diag(Loc, diag::err_objc_bridged_related_known_method) |
| 3636 | << SrcType << DestType << InstanceMethod->getSelector() << true |
| 3637 | << FixItHint::CreateInsertion(SrcExpr->getLocStart(), "[") |
| 3638 | << FixItHint::CreateInsertion(SrcExprEndLoc, ExpressionString); |
| 3639 | } |
Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 3640 | Diag(RelatedClass->getLocStart(), diag::note_declared_at); |
| 3641 | Diag(TDNDecl->getLocStart(), diag::note_declared_at); |
| 3642 | |
| 3643 | ExprResult msg = |
| 3644 | BuildInstanceMessageImplicit(SrcExpr, SrcType, |
| 3645 | InstanceMethod->getLocation(), |
| 3646 | InstanceMethod->getSelector(), |
| 3647 | InstanceMethod, None); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3648 | SrcExpr = msg.get(); |
Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 3649 | return true; |
Fariborz Jahanian | db76577 | 2013-12-10 17:08:13 +0000 | [diff] [blame] | 3650 | } |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3651 | } |
Fariborz Jahanian | 381edf5 | 2013-12-16 22:54:37 +0000 | [diff] [blame] | 3652 | return false; |
Fariborz Jahanian | 1f0b3bf | 2013-12-07 00:34:23 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3655 | Sema::ARCConversionResult |
| 3656 | Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType, |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 3657 | Expr *&castExpr, CheckedConversionKind CCK, |
Fariborz Jahanian | c03ef57 | 2014-06-18 23:52:49 +0000 | [diff] [blame] | 3658 | bool DiagnoseCFAudited, |
| 3659 | BinaryOperatorKind Opc) { |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3660 | QualType castExprType = castExpr->getType(); |
| 3661 | |
| 3662 | // For the purposes of the classification, we assume reference types |
| 3663 | // will bind to temporaries. |
| 3664 | QualType effCastType = castType; |
| 3665 | if (const ReferenceType *ref = castType->getAs<ReferenceType>()) |
| 3666 | effCastType = ref->getPointeeType(); |
| 3667 | |
| 3668 | ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType); |
| 3669 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(effCastType); |
Fariborz Jahanian | 2fa646d | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 3670 | if (exprACTC == castACTC) { |
| 3671 | // check for viablity and report error if casting an rvalue to a |
| 3672 | // life-time qualifier. |
Fariborz Jahanian | 244b187 | 2011-10-29 00:06:10 +0000 | [diff] [blame] | 3673 | if ((castACTC == ACTC_retainable) && |
Fariborz Jahanian | 2fa646d | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 3674 | (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) && |
Fariborz Jahanian | 244b187 | 2011-10-29 00:06:10 +0000 | [diff] [blame] | 3675 | (castType != castExprType)) { |
| 3676 | const Type *DT = castType.getTypePtr(); |
| 3677 | QualType QDT = castType; |
| 3678 | // We desugar some types but not others. We ignore those |
| 3679 | // that cannot happen in a cast; i.e. auto, and those which |
| 3680 | // should not be de-sugared; i.e typedef. |
| 3681 | if (const ParenType *PT = dyn_cast<ParenType>(DT)) |
| 3682 | QDT = PT->desugar(); |
| 3683 | else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT)) |
| 3684 | QDT = TP->desugar(); |
| 3685 | else if (const AttributedType *AT = dyn_cast<AttributedType>(DT)) |
| 3686 | QDT = AT->desugar(); |
| 3687 | if (QDT != castType && |
| 3688 | QDT.getObjCLifetime() != Qualifiers::OCL_None) { |
| 3689 | SourceLocation loc = |
| 3690 | (castRange.isValid() ? castRange.getBegin() |
| 3691 | : castExpr->getExprLoc()); |
| 3692 | Diag(loc, diag::err_arc_nolifetime_behavior); |
| 3693 | } |
Fariborz Jahanian | 2fa646d | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 3694 | } |
| 3695 | return ACR_okay; |
| 3696 | } |
| 3697 | |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3698 | if (isAnyCLike(exprACTC) && isAnyCLike(castACTC)) return ACR_okay; |
| 3699 | |
| 3700 | // Allow all of these types to be cast to integer types (but not |
| 3701 | // vice-versa). |
| 3702 | if (castACTC == ACTC_none && castType->isIntegralType(Context)) |
| 3703 | return ACR_okay; |
| 3704 | |
| 3705 | // Allow casts between pointers to lifetime types (e.g., __strong id*) |
| 3706 | // and pointers to void (e.g., cv void *). Casting from void* to lifetime* |
| 3707 | // must be explicit. |
| 3708 | if (exprACTC == ACTC_indirectRetainable && castACTC == ACTC_voidPtr) |
| 3709 | return ACR_okay; |
| 3710 | if (castACTC == ACTC_indirectRetainable && exprACTC == ACTC_voidPtr && |
| 3711 | CCK != CCK_ImplicitConversion) |
| 3712 | return ACR_okay; |
| 3713 | |
Fariborz Jahanian | 36986c6 | 2012-07-27 22:37:07 +0000 | [diff] [blame] | 3714 | switch (ARCCastChecker(Context, exprACTC, castACTC, false).Visit(castExpr)) { |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3715 | // For invalid casts, fall through. |
| 3716 | case ACC_invalid: |
| 3717 | break; |
| 3718 | |
| 3719 | // Do nothing for both bottom and +0. |
| 3720 | case ACC_bottom: |
| 3721 | case ACC_plusZero: |
| 3722 | return ACR_okay; |
| 3723 | |
| 3724 | // If the result is +1, consume it here. |
| 3725 | case ACC_plusOne: |
| 3726 | castExpr = ImplicitCastExpr::Create(Context, castExpr->getType(), |
| 3727 | CK_ARCConsumeObject, castExpr, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3728 | nullptr, VK_RValue); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3729 | ExprNeedsCleanups = true; |
| 3730 | return ACR_okay; |
| 3731 | } |
| 3732 | |
| 3733 | // If this is a non-implicit cast from id or block type to a |
| 3734 | // CoreFoundation type, delay complaining in case the cast is used |
| 3735 | // in an acceptable context. |
| 3736 | if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC) && |
| 3737 | CCK != CCK_ImplicitConversion) |
| 3738 | return ACR_unbridged; |
| 3739 | |
Fariborz Jahanian | bd714e9 | 2013-12-17 19:33:43 +0000 | [diff] [blame] | 3740 | // Do not issue bridge cast" diagnostic when implicit casting a cstring |
| 3741 | // to 'NSString *'. Let caller issue a normal mismatched diagnostic with |
| 3742 | // suitable fix-it. |
Fariborz Jahanian | 283bf89 | 2013-12-18 21:04:43 +0000 | [diff] [blame] | 3743 | if (castACTC == ACTC_retainable && exprACTC == ACTC_none && |
| 3744 | ConversionToObjCStringLiteralCheck(castType, castExpr)) |
| 3745 | return ACR_okay; |
Fariborz Jahanian | bd714e9 | 2013-12-17 19:33:43 +0000 | [diff] [blame] | 3746 | |
Fariborz Jahanian | 25eef19 | 2013-07-31 21:40:51 +0000 | [diff] [blame] | 3747 | // Do not issue "bridge cast" diagnostic when implicit casting |
| 3748 | // a retainable object to a CF type parameter belonging to an audited |
| 3749 | // CF API function. Let caller issue a normal type mismatched diagnostic |
| 3750 | // instead. |
| 3751 | if (!DiagnoseCFAudited || exprACTC != ACTC_retainable || |
| 3752 | castACTC != ACTC_coreFoundation) |
Fariborz Jahanian | c03ef57 | 2014-06-18 23:52:49 +0000 | [diff] [blame] | 3753 | if (!(exprACTC == ACTC_voidPtr && castACTC == ACTC_retainable && |
| 3754 | (Opc == BO_NE || Opc == BO_EQ))) |
| 3755 | diagnoseObjCARCConversion(*this, castRange, castType, castACTC, |
| 3756 | castExpr, castExpr, exprACTC, CCK); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3757 | return ACR_okay; |
| 3758 | } |
| 3759 | |
| 3760 | /// Given that we saw an expression with the ARCUnbridgedCastTy |
| 3761 | /// placeholder type, complain bitterly. |
| 3762 | void Sema::diagnoseARCUnbridgedCast(Expr *e) { |
| 3763 | // We expect the spurious ImplicitCastExpr to already have been stripped. |
| 3764 | assert(!e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 3765 | CastExpr *realCast = cast<CastExpr>(e->IgnoreParens()); |
| 3766 | |
| 3767 | SourceRange castRange; |
| 3768 | QualType castType; |
| 3769 | CheckedConversionKind CCK; |
| 3770 | |
| 3771 | if (CStyleCastExpr *cast = dyn_cast<CStyleCastExpr>(realCast)) { |
| 3772 | castRange = SourceRange(cast->getLParenLoc(), cast->getRParenLoc()); |
| 3773 | castType = cast->getTypeAsWritten(); |
| 3774 | CCK = CCK_CStyleCast; |
| 3775 | } else if (ExplicitCastExpr *cast = dyn_cast<ExplicitCastExpr>(realCast)) { |
| 3776 | castRange = cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange(); |
| 3777 | castType = cast->getTypeAsWritten(); |
| 3778 | CCK = CCK_OtherCast; |
| 3779 | } else { |
| 3780 | castType = cast->getType(); |
| 3781 | CCK = CCK_ImplicitConversion; |
| 3782 | } |
| 3783 | |
| 3784 | ARCConversionTypeClass castACTC = |
| 3785 | classifyTypeForARCConversion(castType.getNonReferenceType()); |
| 3786 | |
| 3787 | Expr *castExpr = realCast->getSubExpr(); |
| 3788 | assert(classifyTypeForARCConversion(castExpr->getType()) == ACTC_retainable); |
| 3789 | |
| 3790 | diagnoseObjCARCConversion(*this, castRange, castType, castACTC, |
Fariborz Jahanian | f073871 | 2013-02-22 22:02:53 +0000 | [diff] [blame] | 3791 | castExpr, realCast, ACTC_retainable, CCK); |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | /// stripARCUnbridgedCast - Given an expression of ARCUnbridgedCast |
| 3795 | /// type, remove the placeholder cast. |
| 3796 | Expr *Sema::stripARCUnbridgedCast(Expr *e) { |
| 3797 | assert(e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 3798 | |
| 3799 | if (ParenExpr *pe = dyn_cast<ParenExpr>(e)) { |
| 3800 | Expr *sub = stripARCUnbridgedCast(pe->getSubExpr()); |
| 3801 | return new (Context) ParenExpr(pe->getLParen(), pe->getRParen(), sub); |
| 3802 | } else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) { |
| 3803 | assert(uo->getOpcode() == UO_Extension); |
| 3804 | Expr *sub = stripARCUnbridgedCast(uo->getSubExpr()); |
| 3805 | return new (Context) UnaryOperator(sub, UO_Extension, sub->getType(), |
| 3806 | sub->getValueKind(), sub->getObjectKind(), |
| 3807 | uo->getOperatorLoc()); |
| 3808 | } else if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) { |
| 3809 | assert(!gse->isResultDependent()); |
| 3810 | |
| 3811 | unsigned n = gse->getNumAssocs(); |
| 3812 | SmallVector<Expr*, 4> subExprs(n); |
| 3813 | SmallVector<TypeSourceInfo*, 4> subTypes(n); |
| 3814 | for (unsigned i = 0; i != n; ++i) { |
| 3815 | subTypes[i] = gse->getAssocTypeSourceInfo(i); |
| 3816 | Expr *sub = gse->getAssocExpr(i); |
| 3817 | if (i == gse->getResultIndex()) |
| 3818 | sub = stripARCUnbridgedCast(sub); |
| 3819 | subExprs[i] = sub; |
| 3820 | } |
| 3821 | |
| 3822 | return new (Context) GenericSelectionExpr(Context, gse->getGenericLoc(), |
| 3823 | gse->getControllingExpr(), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3824 | subTypes, subExprs, |
| 3825 | gse->getDefaultLoc(), |
John McCall | 4124c49 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3826 | gse->getRParenLoc(), |
| 3827 | gse->containsUnexpandedParameterPack(), |
| 3828 | gse->getResultIndex()); |
| 3829 | } else { |
| 3830 | assert(isa<ImplicitCastExpr>(e) && "bad form of unbridged cast!"); |
| 3831 | return cast<ImplicitCastExpr>(e)->getSubExpr(); |
| 3832 | } |
| 3833 | } |
| 3834 | |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 3835 | bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType, |
| 3836 | QualType exprType) { |
| 3837 | QualType canCastType = |
| 3838 | Context.getCanonicalType(castType).getUnqualifiedType(); |
| 3839 | QualType canExprType = |
| 3840 | Context.getCanonicalType(exprType).getUnqualifiedType(); |
| 3841 | if (isa<ObjCObjectPointerType>(canCastType) && |
| 3842 | castType.getObjCLifetime() == Qualifiers::OCL_Weak && |
| 3843 | canExprType->isObjCObjectPointerType()) { |
| 3844 | if (const ObjCObjectPointerType *ObjT = |
| 3845 | canExprType->getAs<ObjCObjectPointerType>()) |
Richard Smith | 802c4b7 | 2012-08-23 06:16:52 +0000 | [diff] [blame] | 3846 | if (const ObjCInterfaceDecl *ObjI = ObjT->getInterfaceDecl()) |
| 3847 | return !ObjI->isArcWeakrefUnavailable(); |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 3848 | } |
| 3849 | return true; |
| 3850 | } |
| 3851 | |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 3852 | /// Look for an ObjCReclaimReturnedObject cast and destroy it. |
| 3853 | static Expr *maybeUndoReclaimObject(Expr *e) { |
| 3854 | // For now, we just undo operands that are *immediately* reclaim |
| 3855 | // expressions, which prevents the vast majority of potential |
| 3856 | // problems here. To catch them all, we'd need to rebuild arbitrary |
| 3857 | // value-propagating subexpressions --- we can't reliably rebuild |
| 3858 | // in-place because of expression sharing. |
| 3859 | if (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3860 | if (ice->getCastKind() == CK_ARCReclaimReturnedObject) |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 3861 | return ice->getSubExpr(); |
| 3862 | |
| 3863 | return e; |
| 3864 | } |
| 3865 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3866 | ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc, |
| 3867 | ObjCBridgeCastKind Kind, |
| 3868 | SourceLocation BridgeKeywordLoc, |
| 3869 | TypeSourceInfo *TSInfo, |
| 3870 | Expr *SubExpr) { |
John McCall | eb07554 | 2011-08-26 00:48:42 +0000 | [diff] [blame] | 3871 | ExprResult SubResult = UsualUnaryConversions(SubExpr); |
| 3872 | if (SubResult.isInvalid()) return ExprError(); |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3873 | SubExpr = SubResult.get(); |
John McCall | eb07554 | 2011-08-26 00:48:42 +0000 | [diff] [blame] | 3874 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3875 | QualType T = TSInfo->getType(); |
| 3876 | QualType FromType = SubExpr->getType(); |
| 3877 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3878 | CastKind CK; |
| 3879 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3880 | bool MustConsume = false; |
| 3881 | if (T->isDependentType() || SubExpr->isTypeDependent()) { |
| 3882 | // Okay: we'll build a dependent expression type. |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3883 | CK = CK_Dependent; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3884 | } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) { |
| 3885 | // Casting CF -> id |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3886 | CK = (T->isBlockPointerType() ? CK_AnyPointerToBlockPointerCast |
| 3887 | : CK_CPointerToObjCPointerCast); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3888 | switch (Kind) { |
| 3889 | case OBC_Bridge: |
| 3890 | break; |
| 3891 | |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3892 | case OBC_BridgeRetained: { |
Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3893 | bool br = isKnownName("CFBridgingRelease"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3894 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 3895 | << 2 |
| 3896 | << FromType |
| 3897 | << (T->isBlockPointerType()? 1 : 0) |
| 3898 | << T |
| 3899 | << SubExpr->getSourceRange() |
| 3900 | << Kind; |
| 3901 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 3902 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge"); |
| 3903 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer) |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3904 | << FromType << br |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3905 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3906 | br ? "CFBridgingRelease " |
| 3907 | : "__bridge_transfer "); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3908 | |
| 3909 | Kind = OBC_Bridge; |
| 3910 | break; |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3911 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3912 | |
| 3913 | case OBC_BridgeTransfer: |
| 3914 | // We must consume the Objective-C object produced by the cast. |
| 3915 | MustConsume = true; |
| 3916 | break; |
| 3917 | } |
| 3918 | } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) { |
| 3919 | // Okay: id -> CF |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3920 | CK = CK_BitCast; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3921 | switch (Kind) { |
| 3922 | case OBC_Bridge: |
John McCall | 4db5c3c | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 3923 | // Reclaiming a value that's going to be __bridge-casted to CF |
| 3924 | // is very dangerous, so we don't do it. |
| 3925 | SubExpr = maybeUndoReclaimObject(SubExpr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3926 | break; |
| 3927 | |
| 3928 | case OBC_BridgeRetained: |
| 3929 | // Produce the object before casting it. |
| 3930 | SubExpr = ImplicitCastExpr::Create(Context, FromType, |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3931 | CK_ARCProduceObject, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3932 | SubExpr, nullptr, VK_RValue); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3933 | break; |
| 3934 | |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3935 | case OBC_BridgeTransfer: { |
Argyrios Kyrtzidis | 273c7c4 | 2012-06-01 00:10:47 +0000 | [diff] [blame] | 3936 | bool br = isKnownName("CFBridgingRetain"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3937 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 3938 | << (FromType->isBlockPointerType()? 1 : 0) |
| 3939 | << FromType |
| 3940 | << 2 |
| 3941 | << T |
| 3942 | << SubExpr->getSourceRange() |
| 3943 | << Kind; |
| 3944 | |
| 3945 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 3946 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge "); |
| 3947 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained) |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3948 | << T << br |
| 3949 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
| 3950 | br ? "CFBridgingRetain " : "__bridge_retained"); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3951 | |
| 3952 | Kind = OBC_Bridge; |
| 3953 | break; |
| 3954 | } |
Fariborz Jahanian | 30febeb | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3955 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3956 | } else { |
| 3957 | Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible) |
| 3958 | << FromType << T << Kind |
| 3959 | << SubExpr->getSourceRange() |
| 3960 | << TSInfo->getTypeLoc().getSourceRange(); |
| 3961 | return ExprError(); |
| 3962 | } |
| 3963 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3964 | Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, CK, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3965 | BridgeKeywordLoc, |
| 3966 | TSInfo, SubExpr); |
| 3967 | |
| 3968 | if (MustConsume) { |
| 3969 | ExprNeedsCleanups = true; |
John McCall | 2d637d2 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3970 | Result = ImplicitCastExpr::Create(Context, T, CK_ARCConsumeObject, Result, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3971 | nullptr, VK_RValue); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
| 3974 | return Result; |
| 3975 | } |
| 3976 | |
| 3977 | ExprResult Sema::ActOnObjCBridgedCast(Scope *S, |
| 3978 | SourceLocation LParenLoc, |
| 3979 | ObjCBridgeCastKind Kind, |
| 3980 | SourceLocation BridgeKeywordLoc, |
| 3981 | ParsedType Type, |
| 3982 | SourceLocation RParenLoc, |
| 3983 | Expr *SubExpr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3984 | TypeSourceInfo *TSInfo = nullptr; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3985 | QualType T = GetTypeFromParser(Type, &TSInfo); |
Fariborz Jahanian | 8c5b4be | 2013-11-21 00:39:36 +0000 | [diff] [blame] | 3986 | if (Kind == OBC_Bridge) |
| 3987 | CheckTollFreeBridgeCast(T, SubExpr); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3988 | if (!TSInfo) |
| 3989 | TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc); |
| 3990 | return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo, |
| 3991 | SubExpr); |
| 3992 | } |