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