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