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