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