Chris Lattner | 85a932e | 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 | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Scope.h" |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
| 21 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 24 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
| 27 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 28 | using namespace clang; |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 29 | using namespace sema; |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 30 | using llvm::makeArrayRef; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 31 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 32 | ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
| 33 | Expr **strings, |
| 34 | unsigned NumStrings) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 35 | StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); |
| 36 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 37 | // Most ObjC strings are formed out of a single piece. However, we *can* |
| 38 | // have strings formed out of multiple @ strings with multiple pptokens in |
| 39 | // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one |
| 40 | // StringLiteral for ObjCStringLiteral to hold onto. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 41 | StringLiteral *S = Strings[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 43 | // If we have a multi-part string, merge it all together. |
| 44 | if (NumStrings != 1) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 45 | // Concatenate objc strings. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 46 | SmallString<128> StrBuf; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 47 | SmallVector<SourceLocation, 8> StrLocs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 49 | for (unsigned i = 0; i != NumStrings; ++i) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 50 | S = Strings[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 52 | // ObjC strings can't be wide or UTF. |
| 53 | if (!S->isAscii()) { |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 54 | Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 55 | << S->getSourceRange(); |
| 56 | return true; |
| 57 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 59 | // Append the string. |
| 60 | StrBuf += S->getString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 62 | // Get the locations of the string tokens. |
| 63 | StrLocs.append(S->tokloc_begin(), S->tokloc_end()); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 64 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 66 | // Create the aggregate string with the appropriate content and location |
| 67 | // information. |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 68 | S = StringLiteral::Create(Context, StrBuf, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 69 | StringLiteral::Ascii, /*Pascal=*/false, |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 70 | Context.getPointerType(Context.CharTy), |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 71 | &StrLocs[0], StrLocs.size()); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 72 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 74 | // Verify that this composite string is acceptable for ObjC strings. |
| 75 | if (CheckObjCString(S)) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 76 | return true; |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 77 | |
| 78 | // Initialize the constant string interface lazily. This assumes |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 79 | // the NSString interface is seen in this translation unit. Note: We |
| 80 | // don't use NSConstantString, since the runtime team considers this |
| 81 | // interface private (even though it appears in the header files). |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 82 | QualType Ty = Context.getObjCConstantStringInterface(); |
| 83 | if (!Ty.isNull()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 84 | Ty = Context.getObjCObjectPointerType(Ty); |
Fariborz Jahanian | 8a43776 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 85 | } else if (getLangOptions().NoConstantCFStrings) { |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 86 | IdentifierInfo *NSIdent=0; |
| 87 | std::string StringClass(getLangOptions().ObjCConstantStringClass); |
| 88 | |
| 89 | if (StringClass.empty()) |
| 90 | NSIdent = &Context.Idents.get("NSConstantString"); |
| 91 | else |
| 92 | NSIdent = &Context.Idents.get(StringClass); |
| 93 | |
Fariborz Jahanian | 8a43776 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 94 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0], |
| 95 | LookupOrdinaryName); |
| 96 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 97 | Context.setObjCConstantStringInterface(StrIF); |
| 98 | Ty = Context.getObjCConstantStringInterface(); |
| 99 | Ty = Context.getObjCObjectPointerType(Ty); |
| 100 | } else { |
| 101 | // If there is no NSConstantString interface defined then treat this |
| 102 | // as error and recover from it. |
| 103 | Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent |
| 104 | << S->getSourceRange(); |
| 105 | Ty = Context.getObjCIdType(); |
| 106 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 107 | } else { |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 108 | IdentifierInfo *NSIdent = &Context.Idents.get("NSString"); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 109 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0], |
| 110 | LookupOrdinaryName); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 111 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 112 | Context.setObjCConstantStringInterface(StrIF); |
| 113 | Ty = Context.getObjCConstantStringInterface(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 114 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 115 | } else { |
Fariborz Jahanian | f64bc20 | 2012-02-23 22:51:36 +0000 | [diff] [blame^] | 116 | // If there is no NSString interface defined, implicitly declare |
| 117 | // a @class NSString; and use that instead. This is to make sure |
| 118 | // type of an NSString literal is represented correctly, instead of |
| 119 | // being an 'id' type. |
| 120 | Ty = Context.getObjCNSStringType(); |
| 121 | if (Ty.isNull()) { |
| 122 | ObjCInterfaceDecl *NSStringIDecl = |
| 123 | ObjCInterfaceDecl::Create (Context, |
| 124 | Context.getTranslationUnitDecl(), |
| 125 | SourceLocation(), NSIdent, |
| 126 | 0, SourceLocation()); |
| 127 | Ty = Context.getObjCInterfaceType(NSStringIDecl); |
| 128 | Context.setObjCNSStringType(Ty); |
| 129 | } |
| 130 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 131 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 134 | return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Argyrios Kyrtzidis | 3b5904b | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 137 | ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 138 | TypeSourceInfo *EncodedTypeInfo, |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 139 | SourceLocation RParenLoc) { |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 140 | QualType EncodedType = EncodedTypeInfo->getType(); |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 141 | QualType StrTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | if (EncodedType->isDependentType()) |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 143 | StrTy = Context.DependentTy; |
| 144 | else { |
Fariborz Jahanian | 6c91615 | 2011-06-16 22:34:44 +0000 | [diff] [blame] | 145 | if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled. |
| 146 | !EncodedType->isVoidType()) // void is handled too. |
Argyrios Kyrtzidis | 3b5904b | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 147 | if (RequireCompleteType(AtLoc, EncodedType, |
| 148 | PDiag(diag::err_incomplete_type_objc_at_encode) |
| 149 | << EncodedTypeInfo->getTypeLoc().getSourceRange())) |
| 150 | return ExprError(); |
| 151 | |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 152 | std::string Str; |
| 153 | Context.getObjCEncodingForType(EncodedType, Str); |
| 154 | |
| 155 | // The type of @encode is the same as the type of the corresponding string, |
| 156 | // which is an array type. |
| 157 | StrTy = Context.CharTy; |
| 158 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
John McCall | 4b7a834 | 2010-03-15 10:54:44 +0000 | [diff] [blame] | 159 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 160 | StrTy.addConst(); |
| 161 | StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), |
| 162 | ArrayType::Normal, 0); |
| 163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 165 | return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc); |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 166 | } |
| 167 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 168 | ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 169 | SourceLocation EncodeLoc, |
| 170 | SourceLocation LParenLoc, |
| 171 | ParsedType ty, |
| 172 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 173 | // FIXME: Preserve type source info ? |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 174 | TypeSourceInfo *TInfo; |
| 175 | QualType EncodedType = GetTypeFromParser(ty, &TInfo); |
| 176 | if (!TInfo) |
| 177 | TInfo = Context.getTrivialTypeSourceInfo(EncodedType, |
| 178 | PP.getLocForEndOfToken(LParenLoc)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 179 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 180 | return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 181 | } |
| 182 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 183 | ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 184 | SourceLocation AtLoc, |
| 185 | SourceLocation SelLoc, |
| 186 | SourceLocation LParenLoc, |
| 187 | SourceLocation RParenLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 189 | SourceRange(LParenLoc, RParenLoc), false, false); |
Fariborz Jahanian | 7ff22de | 2009-06-16 16:25:00 +0000 | [diff] [blame] | 190 | if (!Method) |
| 191 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 192 | SourceRange(LParenLoc, RParenLoc)); |
| 193 | if (!Method) |
| 194 | Diag(SelLoc, diag::warn_undeclared_selector) << Sel; |
Fariborz Jahanian | 4c91d89 | 2011-07-13 19:05:43 +0000 | [diff] [blame] | 195 | |
| 196 | if (!Method || |
| 197 | Method->getImplementationControl() != ObjCMethodDecl::Optional) { |
| 198 | llvm::DenseMap<Selector, SourceLocation>::iterator Pos |
| 199 | = ReferencedSelectors.find(Sel); |
| 200 | if (Pos == ReferencedSelectors.end()) |
| 201 | ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); |
| 202 | } |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 203 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 204 | // In ARC, forbid the user from using @selector for |
| 205 | // retain/release/autorelease/dealloc/retainCount. |
| 206 | if (getLangOptions().ObjCAutoRefCount) { |
| 207 | switch (Sel.getMethodFamily()) { |
| 208 | case OMF_retain: |
| 209 | case OMF_release: |
| 210 | case OMF_autorelease: |
| 211 | case OMF_retainCount: |
| 212 | case OMF_dealloc: |
| 213 | Diag(AtLoc, diag::err_arc_illegal_selector) << |
| 214 | Sel << SourceRange(LParenLoc, RParenLoc); |
| 215 | break; |
| 216 | |
| 217 | case OMF_None: |
| 218 | case OMF_alloc: |
| 219 | case OMF_copy: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 220 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 221 | case OMF_init: |
| 222 | case OMF_mutableCopy: |
| 223 | case OMF_new: |
| 224 | case OMF_self: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 225 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | } |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 229 | QualType Ty = Context.getObjCSelType(); |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 230 | return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 231 | } |
| 232 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 233 | ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 234 | SourceLocation AtLoc, |
| 235 | SourceLocation ProtoLoc, |
| 236 | SourceLocation LParenLoc, |
| 237 | SourceLocation RParenLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 238 | ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 239 | if (!PDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 240 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 241 | return true; |
| 242 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 244 | QualType Ty = Context.getObjCProtoType(); |
| 245 | if (Ty.isNull()) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 246 | return true; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 247 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 248 | return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 249 | } |
| 250 | |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 251 | /// Try to capture an implicit reference to 'self'. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 252 | ObjCMethodDecl *Sema::tryCaptureObjCSelf(SourceLocation Loc) { |
| 253 | DeclContext *DC = getFunctionLevelDeclContext(); |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 254 | |
| 255 | // If we're not in an ObjC method, error out. Note that, unlike the |
| 256 | // C++ case, we don't require an instance method --- class methods |
| 257 | // still have a 'self', and we really do still need to capture it! |
| 258 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC); |
| 259 | if (!method) |
| 260 | return 0; |
| 261 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 262 | tryCaptureVariable(method->getSelfDecl(), Loc); |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 263 | |
| 264 | return method; |
| 265 | } |
| 266 | |
Douglas Gregor | 5c16d63 | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 267 | static QualType stripObjCInstanceType(ASTContext &Context, QualType T) { |
| 268 | if (T == Context.getObjCInstanceType()) |
| 269 | return Context.getObjCIdType(); |
| 270 | |
| 271 | return T; |
| 272 | } |
| 273 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 274 | QualType Sema::getMessageSendResultType(QualType ReceiverType, |
| 275 | ObjCMethodDecl *Method, |
| 276 | bool isClassMessage, bool isSuperMessage) { |
| 277 | assert(Method && "Must have a method"); |
| 278 | if (!Method->hasRelatedResultType()) |
| 279 | return Method->getSendResultType(); |
| 280 | |
| 281 | // If a method has a related return type: |
| 282 | // - if the method found is an instance method, but the message send |
| 283 | // was a class message send, T is the declared return type of the method |
| 284 | // found |
| 285 | if (Method->isInstanceMethod() && isClassMessage) |
Douglas Gregor | 5c16d63 | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 286 | return stripObjCInstanceType(Context, Method->getSendResultType()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 287 | |
| 288 | // - if the receiver is super, T is a pointer to the class of the |
| 289 | // enclosing method definition |
| 290 | if (isSuperMessage) { |
| 291 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 292 | if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface()) |
| 293 | return Context.getObjCObjectPointerType( |
| 294 | Context.getObjCInterfaceType(Class)); |
| 295 | } |
| 296 | |
| 297 | // - if the receiver is the name of a class U, T is a pointer to U |
| 298 | if (ReceiverType->getAs<ObjCInterfaceType>() || |
| 299 | ReceiverType->isObjCQualifiedInterfaceType()) |
| 300 | return Context.getObjCObjectPointerType(ReceiverType); |
| 301 | // - if the receiver is of type Class or qualified Class type, |
| 302 | // T is the declared return type of the method. |
| 303 | if (ReceiverType->isObjCClassType() || |
| 304 | ReceiverType->isObjCQualifiedClassType()) |
Douglas Gregor | 5c16d63 | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 305 | return stripObjCInstanceType(Context, Method->getSendResultType()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 306 | |
| 307 | // - if the receiver is id, qualified id, Class, or qualified Class, T |
| 308 | // is the receiver type, otherwise |
| 309 | // - T is the type of the receiver expression. |
| 310 | return ReceiverType; |
| 311 | } |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 312 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 313 | void Sema::EmitRelatedResultTypeNote(const Expr *E) { |
| 314 | E = E->IgnoreParenImpCasts(); |
| 315 | const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E); |
| 316 | if (!MsgSend) |
| 317 | return; |
| 318 | |
| 319 | const ObjCMethodDecl *Method = MsgSend->getMethodDecl(); |
| 320 | if (!Method) |
| 321 | return; |
| 322 | |
| 323 | if (!Method->hasRelatedResultType()) |
| 324 | return; |
| 325 | |
| 326 | if (Context.hasSameUnqualifiedType(Method->getResultType() |
| 327 | .getNonReferenceType(), |
| 328 | MsgSend->getType())) |
| 329 | return; |
| 330 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 331 | if (!Context.hasSameUnqualifiedType(Method->getResultType(), |
| 332 | Context.getObjCInstanceType())) |
| 333 | return; |
| 334 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 335 | Diag(Method->getLocation(), diag::note_related_result_type_inferred) |
| 336 | << Method->isInstanceMethod() << Method->getSelector() |
| 337 | << MsgSend->getType(); |
| 338 | } |
| 339 | |
| 340 | bool Sema::CheckMessageArgumentTypes(QualType ReceiverType, |
| 341 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | Selector Sel, ObjCMethodDecl *Method, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 343 | bool isClassMessage, bool isSuperMessage, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 344 | SourceLocation lbrac, SourceLocation rbrac, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 345 | QualType &ReturnType, ExprValueKind &VK) { |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 346 | if (!Method) { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 347 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 348 | for (unsigned i = 0; i != NumArgs; i++) { |
| 349 | if (Args[i]->isTypeDependent()) |
| 350 | continue; |
| 351 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 352 | ExprResult Result = DefaultArgumentPromotion(Args[i]); |
| 353 | if (Result.isInvalid()) |
| 354 | return true; |
| 355 | Args[i] = Result.take(); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 356 | } |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 357 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 358 | unsigned DiagID; |
| 359 | if (getLangOptions().ObjCAutoRefCount) |
| 360 | DiagID = diag::err_arc_method_not_found; |
| 361 | else |
| 362 | DiagID = isClassMessage ? diag::warn_class_method_not_found |
| 363 | : diag::warn_inst_method_not_found; |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 364 | if (!getLangOptions().DebuggerSupport) |
| 365 | Diag(lbrac, DiagID) |
| 366 | << Sel << isClassMessage << SourceRange(lbrac, rbrac); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 367 | |
| 368 | // In debuggers, we want to use __unknown_anytype for these |
| 369 | // results so that clients can cast them. |
| 370 | if (getLangOptions().DebuggerSupport) { |
| 371 | ReturnType = Context.UnknownAnyTy; |
| 372 | } else { |
| 373 | ReturnType = Context.getObjCIdType(); |
| 374 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 375 | VK = VK_RValue; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 376 | return false; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 377 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 379 | ReturnType = getMessageSendResultType(ReceiverType, Method, isClassMessage, |
| 380 | isSuperMessage); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 381 | VK = Expr::getValueKindForType(Method->getResultType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 383 | unsigned NumNamedArgs = Sel.getNumArgs(); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 384 | // Method might have more arguments than selector indicates. This is due |
| 385 | // to addition of c-style arguments in method. |
| 386 | if (Method->param_size() > Sel.getNumArgs()) |
| 387 | NumNamedArgs = Method->param_size(); |
| 388 | // FIXME. This need be cleaned up. |
| 389 | if (NumArgs < NumNamedArgs) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 390 | Diag(lbrac, diag::err_typecheck_call_too_few_args) |
| 391 | << 2 << NumNamedArgs << NumArgs; |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 392 | return false; |
| 393 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 395 | bool IsError = false; |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 396 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 397 | // We can't do any type-checking on a type-dependent argument. |
| 398 | if (Args[i]->isTypeDependent()) |
| 399 | continue; |
| 400 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 401 | Expr *argExpr = Args[i]; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 402 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 403 | ParmVarDecl *param = Method->param_begin()[i]; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 404 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 406 | // Strip the unbridged-cast placeholder expression off unless it's |
| 407 | // a consumed argument. |
| 408 | if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) && |
| 409 | !param->hasAttr<CFConsumedAttr>()) |
| 410 | argExpr = stripARCUnbridgedCast(argExpr); |
| 411 | |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 412 | if (RequireCompleteType(argExpr->getSourceRange().getBegin(), |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 413 | param->getType(), |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 414 | PDiag(diag::err_call_incomplete_argument) |
| 415 | << argExpr->getSourceRange())) |
| 416 | return true; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 417 | |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 418 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 419 | param); |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 420 | ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr)); |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 421 | if (ArgE.isInvalid()) |
| 422 | IsError = true; |
| 423 | else |
| 424 | Args[i] = ArgE.takeAs<Expr>(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 425 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 426 | |
| 427 | // Promote additional arguments to variadic methods. |
| 428 | if (Method->isVariadic()) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 429 | for (unsigned i = NumNamedArgs; i < NumArgs; ++i) { |
| 430 | if (Args[i]->isTypeDependent()) |
| 431 | continue; |
| 432 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 433 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 434 | IsError |= Arg.isInvalid(); |
| 435 | Args[i] = Arg.take(); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 436 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 437 | } else { |
| 438 | // Check for extra arguments to non-variadic methods. |
| 439 | if (NumArgs != NumNamedArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | Diag(Args[NumNamedArgs]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 441 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 442 | << 2 /*method*/ << NumNamedArgs << NumArgs |
| 443 | << Method->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 444 | << SourceRange(Args[NumNamedArgs]->getLocStart(), |
| 445 | Args[NumArgs-1]->getLocEnd()); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 449 | DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 450 | |
| 451 | // Do additional checkings on method. |
| 452 | IsError |= CheckObjCMethodCall(Method, lbrac, Args, NumArgs); |
| 453 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 454 | return IsError; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 457 | bool Sema::isSelfExpr(Expr *receiver) { |
Fariborz Jahanian | f2d74cc | 2011-03-27 19:53:47 +0000 | [diff] [blame] | 458 | // 'self' is objc 'self' in an objc method only. |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 459 | ObjCMethodDecl *method = |
| 460 | dyn_cast<ObjCMethodDecl>(CurContext->getNonClosureAncestor()); |
| 461 | if (!method) return false; |
| 462 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 463 | receiver = receiver->IgnoreParenLValueCasts(); |
| 464 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver)) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 465 | if (DRE->getDecl() == method->getSelfDecl()) |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 466 | return true; |
| 467 | return false; |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 470 | // Helper method for ActOnClassMethod/ActOnInstanceMethod. |
| 471 | // Will search "local" class/category implementations for a method decl. |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 472 | // If failed, then we search in class's root for an instance method. |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 473 | // Returns 0 if no method is found. |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 474 | ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel, |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 475 | ObjCInterfaceDecl *ClassDecl) { |
| 476 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 477 | // lookup in class and all superclasses |
| 478 | while (ClassDecl && !Method) { |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 479 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 480 | Method = ImpDecl->getClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 482 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 483 | if (!Method) |
| 484 | Method = ClassDecl->getCategoryClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 486 | // Before we give up, check if the selector is an instance method. |
| 487 | // But only in the root. This matches gcc's behaviour and what the |
| 488 | // runtime expects. |
| 489 | if (!Method && !ClassDecl->getSuperClass()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 490 | Method = ClassDecl->lookupInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | // Look through local category implementations associated |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 492 | // with the root class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | if (!Method) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 494 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 496 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 497 | ClassDecl = ClassDecl->getSuperClass(); |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 498 | } |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 499 | return Method; |
| 500 | } |
| 501 | |
| 502 | ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, |
| 503 | ObjCInterfaceDecl *ClassDecl) { |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 504 | if (!ClassDecl->hasDefinition()) |
| 505 | return 0; |
| 506 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 507 | ObjCMethodDecl *Method = 0; |
| 508 | while (ClassDecl && !Method) { |
| 509 | // If we have implementations in scope, check "private" methods. |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 510 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 511 | Method = ImpDecl->getInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 512 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 513 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 514 | if (!Method) |
| 515 | Method = ClassDecl->getCategoryInstanceMethod(Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 516 | ClassDecl = ClassDecl->getSuperClass(); |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 517 | } |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 518 | return Method; |
| 519 | } |
| 520 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 521 | /// LookupMethodInType - Look up a method in an ObjCObjectType. |
| 522 | ObjCMethodDecl *Sema::LookupMethodInObjectType(Selector sel, QualType type, |
| 523 | bool isInstance) { |
| 524 | const ObjCObjectType *objType = type->castAs<ObjCObjectType>(); |
| 525 | if (ObjCInterfaceDecl *iface = objType->getInterface()) { |
| 526 | // Look it up in the main interface (and categories, etc.) |
| 527 | if (ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance)) |
| 528 | return method; |
| 529 | |
| 530 | // Okay, look for "private" methods declared in any |
| 531 | // @implementations we've seen. |
| 532 | if (isInstance) { |
| 533 | if (ObjCMethodDecl *method = LookupPrivateInstanceMethod(sel, iface)) |
| 534 | return method; |
| 535 | } else { |
| 536 | if (ObjCMethodDecl *method = LookupPrivateClassMethod(sel, iface)) |
| 537 | return method; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | // Check qualifiers. |
| 542 | for (ObjCObjectType::qual_iterator |
| 543 | i = objType->qual_begin(), e = objType->qual_end(); i != e; ++i) |
| 544 | if (ObjCMethodDecl *method = (*i)->lookupMethod(sel, isInstance)) |
| 545 | return method; |
| 546 | |
| 547 | return 0; |
| 548 | } |
| 549 | |
Fariborz Jahanian | 6147806 | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 550 | /// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier |
| 551 | /// list of a qualified objective pointer type. |
| 552 | ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel, |
| 553 | const ObjCObjectPointerType *OPT, |
| 554 | bool Instance) |
| 555 | { |
| 556 | ObjCMethodDecl *MD = 0; |
| 557 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 558 | E = OPT->qual_end(); I != E; ++I) { |
| 559 | ObjCProtocolDecl *PROTO = (*I); |
| 560 | if ((MD = PROTO->lookupMethod(Sel, Instance))) { |
| 561 | return MD; |
| 562 | } |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 567 | /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an |
| 568 | /// objective C interface. This is a property reference expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 569 | ExprResult Sema:: |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 570 | HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT, |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 571 | Expr *BaseExpr, SourceLocation OpLoc, |
| 572 | DeclarationName MemberName, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 573 | SourceLocation MemberLoc, |
| 574 | SourceLocation SuperLoc, QualType SuperType, |
| 575 | bool Super) { |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 576 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 577 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Douglas Gregor | 109ec1b | 2011-04-20 18:19:55 +0000 | [diff] [blame] | 578 | |
| 579 | if (MemberName.getNameKind() != DeclarationName::Identifier) { |
| 580 | Diag(MemberLoc, diag::err_invalid_property_name) |
| 581 | << MemberName << QualType(OPT, 0); |
| 582 | return ExprError(); |
| 583 | } |
| 584 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 585 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 586 | SourceRange BaseRange = Super? SourceRange(SuperLoc) |
| 587 | : BaseExpr->getSourceRange(); |
| 588 | if (RequireCompleteType(MemberLoc, OPT->getPointeeType(), |
| 589 | PDiag(diag::err_property_not_found_forward_class) |
| 590 | << MemberName << BaseRange)) |
Fariborz Jahanian | 8b1aba4 | 2010-12-16 00:56:28 +0000 | [diff] [blame] | 591 | return ExprError(); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 592 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 593 | // Search for a declared property first. |
| 594 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
| 595 | // Check whether we can reference this property. |
| 596 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 597 | return ExprError(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 598 | |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 599 | if (Super) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 600 | return Owned(new (Context) ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 601 | VK_LValue, OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 602 | MemberLoc, |
| 603 | SuperLoc, SuperType)); |
| 604 | else |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 605 | return Owned(new (Context) ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 606 | VK_LValue, OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 607 | MemberLoc, BaseExpr)); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 608 | } |
| 609 | // Check protocols on qualified interfaces. |
| 610 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 611 | E = OPT->qual_end(); I != E; ++I) |
| 612 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 613 | // Check whether we can reference this property. |
| 614 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 615 | return ExprError(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 616 | |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 617 | if (Super) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 618 | return Owned(new (Context) ObjCPropertyRefExpr(PD, |
| 619 | Context.PseudoObjectTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 620 | VK_LValue, |
| 621 | OK_ObjCProperty, |
| 622 | MemberLoc, |
| 623 | SuperLoc, SuperType)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 624 | else |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 625 | return Owned(new (Context) ObjCPropertyRefExpr(PD, |
| 626 | Context.PseudoObjectTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 627 | VK_LValue, |
| 628 | OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 629 | MemberLoc, |
| 630 | BaseExpr)); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 631 | } |
| 632 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 633 | // selector is implemented. |
| 634 | |
| 635 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 636 | // shared with the code in ActOnInstanceMessage. |
| 637 | |
| 638 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 639 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 640 | |
| 641 | // May be founf in property's qualified list. |
| 642 | if (!Getter) |
| 643 | Getter = LookupMethodInQualifiedType(Sel, OPT, true); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 644 | |
| 645 | // If this reference is in an @implementation, check for 'private' methods. |
| 646 | if (!Getter) |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 647 | Getter = IFace->lookupPrivateMethod(Sel); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 648 | |
| 649 | // Look through local category implementations associated with the class. |
| 650 | if (!Getter) |
| 651 | Getter = IFace->getCategoryInstanceMethod(Sel); |
| 652 | if (Getter) { |
| 653 | // Check if we can reference this property. |
| 654 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 655 | return ExprError(); |
| 656 | } |
| 657 | // If we found a getter then this may be a valid dot-reference, we |
| 658 | // will look for the matching setter, in case it is needed. |
| 659 | Selector SetterSel = |
| 660 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 661 | PP.getSelectorTable(), Member); |
| 662 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 663 | |
| 664 | // May be founf in property's qualified list. |
| 665 | if (!Setter) |
| 666 | Setter = LookupMethodInQualifiedType(SetterSel, OPT, true); |
| 667 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 668 | if (!Setter) { |
| 669 | // If this reference is in an @implementation, also check for 'private' |
| 670 | // methods. |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 671 | Setter = IFace->lookupPrivateMethod(SetterSel); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 672 | } |
| 673 | // Look through local category implementations associated with the class. |
| 674 | if (!Setter) |
| 675 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 676 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 677 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 678 | return ExprError(); |
| 679 | |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 680 | if (Getter || Setter) { |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 681 | if (Super) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 682 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 683 | Context.PseudoObjectTy, |
| 684 | VK_LValue, OK_ObjCProperty, |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 685 | MemberLoc, |
| 686 | SuperLoc, SuperType)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 687 | else |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 688 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 689 | Context.PseudoObjectTy, |
| 690 | VK_LValue, OK_ObjCProperty, |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 691 | MemberLoc, BaseExpr)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | // Attempt to correct for typos in property names. |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 696 | DeclFilterCCC<ObjCPropertyDecl> Validator; |
| 697 | if (TypoCorrection Corrected = CorrectTypo( |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 698 | DeclarationNameInfo(MemberName, MemberLoc), LookupOrdinaryName, NULL, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 699 | NULL, Validator, IFace, false, OPT)) { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 700 | ObjCPropertyDecl *Property = |
| 701 | Corrected.getCorrectionDeclAs<ObjCPropertyDecl>(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 702 | DeclarationName TypoResult = Corrected.getCorrection(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 703 | Diag(MemberLoc, diag::err_property_not_found_suggest) |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 704 | << MemberName << QualType(OPT, 0) << TypoResult |
| 705 | << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString()); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 706 | Diag(Property->getLocation(), diag::note_previous_decl) |
| 707 | << Property->getDeclName(); |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 708 | return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc, |
| 709 | TypoResult, MemberLoc, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 710 | SuperLoc, SuperType, Super); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 711 | } |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 712 | ObjCInterfaceDecl *ClassDeclared; |
| 713 | if (ObjCIvarDecl *Ivar = |
| 714 | IFace->lookupInstanceVariable(Member, ClassDeclared)) { |
| 715 | QualType T = Ivar->getType(); |
| 716 | if (const ObjCObjectPointerType * OBJPT = |
| 717 | T->getAsObjCInterfacePointerType()) { |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 718 | if (RequireCompleteType(MemberLoc, OBJPT->getPointeeType(), |
| 719 | PDiag(diag::err_property_not_as_forward_class) |
| 720 | << MemberName << BaseExpr->getSourceRange())) |
| 721 | return ExprError(); |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 722 | } |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 723 | Diag(MemberLoc, |
| 724 | diag::err_ivar_access_using_property_syntax_suggest) |
| 725 | << MemberName << QualType(OPT, 0) << Ivar->getDeclName() |
| 726 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| 727 | return ExprError(); |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 728 | } |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 729 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 730 | Diag(MemberLoc, diag::err_property_not_found) |
| 731 | << MemberName << QualType(OPT, 0); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 732 | if (Setter) |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 733 | Diag(Setter->getLocation(), diag::note_getter_unavailable) |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 734 | << MemberName << BaseExpr->getSourceRange(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 735 | return ExprError(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | |
| 739 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 740 | ExprResult Sema:: |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 741 | ActOnClassPropertyRefExpr(IdentifierInfo &receiverName, |
| 742 | IdentifierInfo &propertyName, |
| 743 | SourceLocation receiverNameLoc, |
| 744 | SourceLocation propertyNameLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 745 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 746 | IdentifierInfo *receiverNamePtr = &receiverName; |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 747 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr, |
| 748 | receiverNameLoc); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 749 | |
| 750 | bool IsSuper = false; |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 751 | if (IFace == 0) { |
| 752 | // If the "receiver" is 'super' in a method, handle it as an expression-like |
| 753 | // property reference. |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 754 | if (receiverNamePtr->isStr("super")) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 755 | IsSuper = true; |
| 756 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 757 | if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf(receiverNameLoc)) { |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 758 | if (CurMethod->isInstanceMethod()) { |
| 759 | QualType T = |
| 760 | Context.getObjCInterfaceType(CurMethod->getClassInterface()); |
| 761 | T = Context.getObjCObjectPointerType(T); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 762 | |
| 763 | return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(), |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 764 | /*BaseExpr*/0, |
| 765 | SourceLocation()/*OpLoc*/, |
| 766 | &propertyName, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 767 | propertyNameLoc, |
| 768 | receiverNameLoc, T, true); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 769 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 771 | // Otherwise, if this is a class method, try dispatching to our |
| 772 | // superclass. |
| 773 | IFace = CurMethod->getClassInterface()->getSuperClass(); |
| 774 | } |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 775 | } |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 776 | |
| 777 | if (IFace == 0) { |
| 778 | Diag(receiverNameLoc, diag::err_expected_ident_or_lparen); |
| 779 | return ExprError(); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | // Search for a declared property first. |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 784 | Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 785 | ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 786 | |
| 787 | // If this reference is in an @implementation, check for 'private' methods. |
| 788 | if (!Getter) |
| 789 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 790 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 791 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 792 | Getter = ImpDecl->getClassMethod(Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 793 | |
| 794 | if (Getter) { |
| 795 | // FIXME: refactor/share with ActOnMemberReference(). |
| 796 | // Check if we can reference this property. |
| 797 | if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) |
| 798 | return ExprError(); |
| 799 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 801 | // Look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | Selector SetterSel = |
| 803 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Steve Naroff | fdc92b7 | 2009-03-10 17:24:38 +0000 | [diff] [blame] | 804 | PP.getSelectorTable(), &propertyName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 806 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 807 | if (!Setter) { |
| 808 | // If this reference is in an @implementation, also check for 'private' |
| 809 | // methods. |
| 810 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 811 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 812 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 813 | Setter = ImpDecl->getClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 814 | } |
| 815 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 816 | if (!Setter) |
| 817 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 818 | |
| 819 | if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) |
| 820 | return ExprError(); |
| 821 | |
| 822 | if (Getter || Setter) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 823 | if (IsSuper) |
| 824 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 825 | Context.PseudoObjectTy, |
| 826 | VK_LValue, OK_ObjCProperty, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 827 | propertyNameLoc, |
| 828 | receiverNameLoc, |
| 829 | Context.getObjCInterfaceType(IFace))); |
| 830 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 831 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 832 | Context.PseudoObjectTy, |
| 833 | VK_LValue, OK_ObjCProperty, |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 834 | propertyNameLoc, |
| 835 | receiverNameLoc, IFace)); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 836 | } |
| 837 | return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) |
| 838 | << &propertyName << Context.getObjCInterfaceType(IFace)); |
| 839 | } |
| 840 | |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 841 | namespace { |
| 842 | |
| 843 | class ObjCInterfaceOrSuperCCC : public CorrectionCandidateCallback { |
| 844 | public: |
| 845 | ObjCInterfaceOrSuperCCC(ObjCMethodDecl *Method) { |
| 846 | // Determine whether "super" is acceptable in the current context. |
| 847 | if (Method && Method->getClassInterface()) |
| 848 | WantObjCSuper = Method->getClassInterface()->getSuperClass(); |
| 849 | } |
| 850 | |
| 851 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 852 | return candidate.getCorrectionDeclAs<ObjCInterfaceDecl>() || |
| 853 | candidate.isKeyword("super"); |
| 854 | } |
| 855 | }; |
| 856 | |
| 857 | } |
| 858 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 859 | Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 860 | IdentifierInfo *Name, |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 861 | SourceLocation NameLoc, |
| 862 | bool IsSuper, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 863 | bool HasTrailingDot, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 864 | ParsedType &ReceiverType) { |
| 865 | ReceiverType = ParsedType(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 866 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 867 | // If the identifier is "super" and there is no trailing dot, we're |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 868 | // messaging super. If the identifier is "super" and there is a |
| 869 | // trailing dot, it's an instance message. |
| 870 | if (IsSuper && S->isInObjcMethodScope()) |
| 871 | return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 872 | |
| 873 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| 874 | LookupName(Result, S); |
| 875 | |
| 876 | switch (Result.getResultKind()) { |
| 877 | case LookupResult::NotFound: |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 878 | // Normal name lookup didn't find anything. If we're in an |
| 879 | // Objective-C method, look for ivars. If we find one, we're done! |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 880 | // FIXME: This is a hack. Ivar lookup should be part of normal |
| 881 | // lookup. |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 882 | if (ObjCMethodDecl *Method = getCurMethodDecl()) { |
Argyrios Kyrtzidis | ccc9e76 | 2011-11-09 00:22:48 +0000 | [diff] [blame] | 883 | if (!Method->getClassInterface()) { |
| 884 | // Fall back: let the parser try to parse it as an instance message. |
| 885 | return ObjCInstanceMessage; |
| 886 | } |
| 887 | |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 888 | ObjCInterfaceDecl *ClassDeclared; |
| 889 | if (Method->getClassInterface()->lookupInstanceVariable(Name, |
| 890 | ClassDeclared)) |
| 891 | return ObjCInstanceMessage; |
| 892 | } |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 894 | // Break out; we'll perform typo correction below. |
| 895 | break; |
| 896 | |
| 897 | case LookupResult::NotFoundInCurrentInstantiation: |
| 898 | case LookupResult::FoundOverloaded: |
| 899 | case LookupResult::FoundUnresolvedValue: |
| 900 | case LookupResult::Ambiguous: |
| 901 | Result.suppressDiagnostics(); |
| 902 | return ObjCInstanceMessage; |
| 903 | |
| 904 | case LookupResult::Found: { |
Fariborz Jahanian | 8348de3 | 2011-02-08 00:23:07 +0000 | [diff] [blame] | 905 | // If the identifier is a class or not, and there is a trailing dot, |
| 906 | // it's an instance message. |
| 907 | if (HasTrailingDot) |
| 908 | return ObjCInstanceMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 909 | // We found something. If it's a type, then we have a class |
| 910 | // message. Otherwise, it's an instance message. |
| 911 | NamedDecl *ND = Result.getFoundDecl(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 912 | QualType T; |
| 913 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 914 | T = Context.getObjCInterfaceType(Class); |
| 915 | else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| 916 | T = Context.getTypeDeclType(Type); |
| 917 | else |
| 918 | return ObjCInstanceMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 919 | |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 920 | // We have a class message, and T is the type we're |
| 921 | // messaging. Build source-location information for it. |
| 922 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 923 | ReceiverType = CreateParsedType(T, TSInfo); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 924 | return ObjCClassMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 925 | } |
| 926 | } |
| 927 | |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 928 | ObjCInterfaceOrSuperCCC Validator(getCurMethodDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 929 | if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), |
| 930 | Result.getLookupKind(), S, NULL, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 931 | Validator)) { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 932 | if (Corrected.isKeyword()) { |
| 933 | // If we've found the keyword "super" (the only keyword that would be |
| 934 | // returned by CorrectTypo), this is a send to super. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 935 | Diag(NameLoc, diag::err_unknown_receiver_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 936 | << Name << Corrected.getCorrection() |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 937 | << FixItHint::CreateReplacement(SourceRange(NameLoc), "super"); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 938 | return ObjCSuperMessage; |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 939 | } else if (ObjCInterfaceDecl *Class = |
| 940 | Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 941 | // If we found a declaration, correct when it refers to an Objective-C |
| 942 | // class. |
| 943 | Diag(NameLoc, diag::err_unknown_receiver_suggest) |
| 944 | << Name << Corrected.getCorrection() |
| 945 | << FixItHint::CreateReplacement(SourceRange(NameLoc), |
| 946 | Class->getNameAsString()); |
| 947 | Diag(Class->getLocation(), diag::note_previous_decl) |
| 948 | << Corrected.getCorrection(); |
| 949 | |
| 950 | QualType T = Context.getObjCInterfaceType(Class); |
| 951 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
| 952 | ReceiverType = CreateParsedType(T, TSInfo); |
| 953 | return ObjCClassMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 954 | } |
| 955 | } |
| 956 | |
| 957 | // Fall back: let the parser try to parse it as an instance message. |
| 958 | return ObjCInstanceMessage; |
| 959 | } |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 960 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 961 | ExprResult Sema::ActOnSuperMessage(Scope *S, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 962 | SourceLocation SuperLoc, |
| 963 | Selector Sel, |
| 964 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 965 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 966 | SourceLocation RBracLoc, |
| 967 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 968 | // Determine whether we are inside a method or not. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 969 | ObjCMethodDecl *Method = tryCaptureObjCSelf(SuperLoc); |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 970 | if (!Method) { |
| 971 | Diag(SuperLoc, diag::err_invalid_receiver_to_message_super); |
| 972 | return ExprError(); |
| 973 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 974 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 975 | ObjCInterfaceDecl *Class = Method->getClassInterface(); |
| 976 | if (!Class) { |
| 977 | Diag(SuperLoc, diag::error_no_super_class_message) |
| 978 | << Method->getDeclName(); |
| 979 | return ExprError(); |
| 980 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 981 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 982 | ObjCInterfaceDecl *Super = Class->getSuperClass(); |
| 983 | if (!Super) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 984 | // The current class does not have a superclass. |
Ted Kremenek | e00909a | 2011-01-23 17:21:34 +0000 | [diff] [blame] | 985 | Diag(SuperLoc, diag::error_root_class_cannot_use_super) |
| 986 | << Class->getIdentifier(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 987 | return ExprError(); |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 988 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 989 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 990 | // We are in a method whose class has a superclass, so 'super' |
| 991 | // is acting as a keyword. |
| 992 | if (Method->isInstanceMethod()) { |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 993 | if (Sel.getMethodFamily() == OMF_dealloc) |
| 994 | ObjCShouldCallSuperDealloc = false; |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 995 | if (Sel.getMethodFamily() == OMF_finalize) |
| 996 | ObjCShouldCallSuperFinalize = false; |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 997 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 998 | // Since we are in an instance method, this is an instance |
| 999 | // message to the superclass instance. |
| 1000 | QualType SuperTy = Context.getObjCInterfaceType(Super); |
| 1001 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1002 | return BuildInstanceMessage(0, SuperTy, SuperLoc, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1003 | Sel, /*Method=*/0, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1004 | LBracLoc, SelectorLocs, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1005 | } |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1006 | |
| 1007 | // Since we are in a class method, this is a class message to |
| 1008 | // the superclass. |
| 1009 | return BuildClassMessage(/*ReceiverTypeInfo=*/0, |
| 1010 | Context.getObjCInterfaceType(Super), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1011 | SuperLoc, Sel, /*Method=*/0, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1012 | LBracLoc, SelectorLocs, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1015 | |
| 1016 | ExprResult Sema::BuildClassMessageImplicit(QualType ReceiverType, |
| 1017 | bool isSuperReceiver, |
| 1018 | SourceLocation Loc, |
| 1019 | Selector Sel, |
| 1020 | ObjCMethodDecl *Method, |
| 1021 | MultiExprArg Args) { |
| 1022 | TypeSourceInfo *receiverTypeInfo = 0; |
| 1023 | if (!ReceiverType.isNull()) |
| 1024 | receiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType); |
| 1025 | |
| 1026 | return BuildClassMessage(receiverTypeInfo, ReceiverType, |
| 1027 | /*SuperLoc=*/isSuperReceiver ? Loc : SourceLocation(), |
| 1028 | Sel, Method, Loc, Loc, Loc, Args, |
| 1029 | /*isImplicit=*/true); |
| 1030 | |
| 1031 | } |
| 1032 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1033 | /// \brief Build an Objective-C class message expression. |
| 1034 | /// |
| 1035 | /// This routine takes care of both normal class messages and |
| 1036 | /// class messages to the superclass. |
| 1037 | /// |
| 1038 | /// \param ReceiverTypeInfo Type source information that describes the |
| 1039 | /// receiver of this message. This may be NULL, in which case we are |
| 1040 | /// sending to the superclass and \p SuperLoc must be a valid source |
| 1041 | /// location. |
| 1042 | |
| 1043 | /// \param ReceiverType The type of the object receiving the |
| 1044 | /// message. When \p ReceiverTypeInfo is non-NULL, this is the same |
| 1045 | /// type as that refers to. For a superclass send, this is the type of |
| 1046 | /// the superclass. |
| 1047 | /// |
| 1048 | /// \param SuperLoc The location of the "super" keyword in a |
| 1049 | /// superclass message. |
| 1050 | /// |
| 1051 | /// \param Sel The selector to which the message is being sent. |
| 1052 | /// |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1053 | /// \param Method The method that this class message is invoking, if |
| 1054 | /// already known. |
| 1055 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1056 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 1057 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1058 | /// \param RBrac The location of the closing square bracket ']'. |
| 1059 | /// |
| 1060 | /// \param Args The message arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1061 | ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1062 | QualType ReceiverType, |
| 1063 | SourceLocation SuperLoc, |
| 1064 | Selector Sel, |
| 1065 | ObjCMethodDecl *Method, |
| 1066 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1067 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1068 | SourceLocation RBracLoc, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1069 | MultiExprArg ArgsIn, |
| 1070 | bool isImplicit) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1071 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 1072 | : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin(); |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1073 | if (LBracLoc.isInvalid()) { |
| 1074 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 1075 | << FixItHint::CreateInsertion(Loc, "["); |
| 1076 | LBracLoc = Loc; |
| 1077 | } |
| 1078 | |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1079 | if (ReceiverType->isDependentType()) { |
| 1080 | // If the receiver type is dependent, we can't type-check anything |
| 1081 | // at this point. Build a dependent expression. |
| 1082 | unsigned NumArgs = ArgsIn.size(); |
| 1083 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 1084 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1085 | return Owned(ObjCMessageExpr::Create(Context, ReceiverType, |
| 1086 | VK_RValue, LBracLoc, ReceiverTypeInfo, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1087 | Sel, SelectorLocs, /*Method=*/0, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1088 | makeArrayRef(Args, NumArgs),RBracLoc, |
| 1089 | isImplicit)); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1090 | } |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 1091 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1092 | // Find the class to which we are sending this message. |
| 1093 | ObjCInterfaceDecl *Class = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1094 | const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>(); |
| 1095 | if (!ClassType || !(Class = ClassType->getInterface())) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1096 | Diag(Loc, diag::err_invalid_receiver_class_message) |
| 1097 | << ReceiverType; |
| 1098 | return ExprError(); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 1099 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1100 | assert(Class && "We don't know which class we're messaging?"); |
Fariborz Jahanian | 43bcdb2 | 2011-10-15 19:18:36 +0000 | [diff] [blame] | 1101 | // objc++ diagnoses during typename annotation. |
| 1102 | if (!getLangOptions().CPlusPlus) |
| 1103 | (void)DiagnoseUseOfDecl(Class, Loc); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1104 | // Find the method we are messaging. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1105 | if (!Method) { |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1106 | SourceRange TypeRange |
| 1107 | = SuperLoc.isValid()? SourceRange(SuperLoc) |
| 1108 | : ReceiverTypeInfo->getTypeLoc().getSourceRange(); |
| 1109 | if (RequireCompleteType(Loc, Context.getObjCInterfaceType(Class), |
| 1110 | (getLangOptions().ObjCAutoRefCount |
| 1111 | ? PDiag(diag::err_arc_receiver_forward_class) |
| 1112 | : PDiag(diag::warn_receiver_forward_class)) |
| 1113 | << TypeRange)) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1114 | // A forward class used in messaging is treated as a 'Class' |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1115 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 1116 | SourceRange(LBracLoc, RBracLoc)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1117 | if (Method && !getLangOptions().ObjCAutoRefCount) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1118 | Diag(Method->getLocation(), diag::note_method_sent_forward_class) |
| 1119 | << Method->getDeclName(); |
| 1120 | } |
| 1121 | if (!Method) |
| 1122 | Method = Class->lookupClassMethod(Sel); |
| 1123 | |
| 1124 | // If we have an implementation in scope, check "private" methods. |
| 1125 | if (!Method) |
| 1126 | Method = LookupPrivateClassMethod(Sel, Class); |
| 1127 | |
| 1128 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 1129 | return ExprError(); |
Fariborz Jahanian | 89bc314 | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 1130 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1131 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1132 | // Check the argument types and determine the result type. |
| 1133 | QualType ReturnType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1134 | ExprValueKind VK = VK_RValue; |
| 1135 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1136 | unsigned NumArgs = ArgsIn.size(); |
| 1137 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1138 | if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, true, |
| 1139 | SuperLoc.isValid(), LBracLoc, RBracLoc, |
| 1140 | ReturnType, VK)) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1141 | return ExprError(); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1142 | |
Douglas Gregor | 483dd2f | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 1143 | if (Method && !Method->getResultType()->isVoidType() && |
| 1144 | RequireCompleteType(LBracLoc, Method->getResultType(), |
| 1145 | diag::err_illegal_message_expr_incomplete_type)) |
| 1146 | return ExprError(); |
| 1147 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1148 | // Construct the appropriate ObjCMessageExpr. |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1149 | Expr *Result; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1150 | if (SuperLoc.isValid()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1151 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1152 | SuperLoc, /*IsInstanceSuper=*/false, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1153 | ReceiverType, Sel, SelectorLocs, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 1154 | Method, makeArrayRef(Args, NumArgs), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1155 | RBracLoc, isImplicit); |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1156 | else |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1157 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1158 | ReceiverTypeInfo, Sel, SelectorLocs, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 1159 | Method, makeArrayRef(Args, NumArgs), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1160 | RBracLoc, isImplicit); |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1161 | return MaybeBindToTemporary(Result); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1164 | // ActOnClassMessage - used for both unary and keyword messages. |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1165 | // ArgExprs is optional - if it is present, the number of expressions |
| 1166 | // is obtained from Sel.getNumArgs(). |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1167 | ExprResult Sema::ActOnClassMessage(Scope *S, |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1168 | ParsedType Receiver, |
| 1169 | Selector Sel, |
| 1170 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1171 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1172 | SourceLocation RBracLoc, |
| 1173 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1174 | TypeSourceInfo *ReceiverTypeInfo; |
| 1175 | QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo); |
| 1176 | if (ReceiverType.isNull()) |
| 1177 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1179 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1180 | if (!ReceiverTypeInfo) |
| 1181 | ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc); |
| 1182 | |
| 1183 | return BuildClassMessage(ReceiverTypeInfo, ReceiverType, |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1184 | /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1185 | LBracLoc, SelectorLocs, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1188 | ExprResult Sema::BuildInstanceMessageImplicit(Expr *Receiver, |
| 1189 | QualType ReceiverType, |
| 1190 | SourceLocation Loc, |
| 1191 | Selector Sel, |
| 1192 | ObjCMethodDecl *Method, |
| 1193 | MultiExprArg Args) { |
| 1194 | return BuildInstanceMessage(Receiver, ReceiverType, |
| 1195 | /*SuperLoc=*/!Receiver ? Loc : SourceLocation(), |
| 1196 | Sel, Method, Loc, Loc, Loc, Args, |
| 1197 | /*isImplicit=*/true); |
| 1198 | } |
| 1199 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1200 | /// \brief Build an Objective-C instance message expression. |
| 1201 | /// |
| 1202 | /// This routine takes care of both normal instance messages and |
| 1203 | /// instance messages to the superclass instance. |
| 1204 | /// |
| 1205 | /// \param Receiver The expression that computes the object that will |
| 1206 | /// receive this message. This may be empty, in which case we are |
| 1207 | /// sending to the superclass instance and \p SuperLoc must be a valid |
| 1208 | /// source location. |
| 1209 | /// |
| 1210 | /// \param ReceiverType The (static) type of the object receiving the |
| 1211 | /// message. When a \p Receiver expression is provided, this is the |
| 1212 | /// same type as that expression. For a superclass instance send, this |
| 1213 | /// is a pointer to the type of the superclass. |
| 1214 | /// |
| 1215 | /// \param SuperLoc The location of the "super" keyword in a |
| 1216 | /// superclass instance message. |
| 1217 | /// |
| 1218 | /// \param Sel The selector to which the message is being sent. |
| 1219 | /// |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1220 | /// \param Method The method that this instance message is invoking, if |
| 1221 | /// already known. |
| 1222 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1223 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 1224 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1225 | /// \param RBrac The location of the closing square bracket ']'. |
| 1226 | /// |
| 1227 | /// \param Args The message arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1228 | ExprResult Sema::BuildInstanceMessage(Expr *Receiver, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1229 | QualType ReceiverType, |
| 1230 | SourceLocation SuperLoc, |
| 1231 | Selector Sel, |
| 1232 | ObjCMethodDecl *Method, |
| 1233 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1234 | ArrayRef<SourceLocation> SelectorLocs, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1235 | SourceLocation RBracLoc, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1236 | MultiExprArg ArgsIn, |
| 1237 | bool isImplicit) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1238 | // The location of the receiver. |
| 1239 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart(); |
| 1240 | |
| 1241 | if (LBracLoc.isInvalid()) { |
| 1242 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 1243 | << FixItHint::CreateInsertion(Loc, "["); |
| 1244 | LBracLoc = Loc; |
| 1245 | } |
| 1246 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1247 | // If we have a receiver expression, perform appropriate promotions |
| 1248 | // and determine receiver type. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1249 | if (Receiver) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1250 | if (Receiver->hasPlaceholderType()) { |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 1251 | ExprResult Result; |
| 1252 | if (Receiver->getType() == Context.UnknownAnyTy) |
| 1253 | Result = forceUnknownAnyToType(Receiver, Context.getObjCIdType()); |
| 1254 | else |
| 1255 | Result = CheckPlaceholderExpr(Receiver); |
| 1256 | if (Result.isInvalid()) return ExprError(); |
| 1257 | Receiver = Result.take(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1260 | if (Receiver->isTypeDependent()) { |
| 1261 | // If the receiver is type-dependent, we can't type-check anything |
| 1262 | // at this point. Build a dependent expression. |
| 1263 | unsigned NumArgs = ArgsIn.size(); |
| 1264 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 1265 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
| 1266 | return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1267 | VK_RValue, LBracLoc, Receiver, Sel, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1268 | SelectorLocs, /*Method=*/0, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 1269 | makeArrayRef(Args, NumArgs), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1270 | RBracLoc, isImplicit)); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1273 | // If necessary, apply function/array conversion to the receiver. |
| 1274 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1275 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver); |
| 1276 | if (Result.isInvalid()) |
| 1277 | return ExprError(); |
| 1278 | Receiver = Result.take(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1279 | ReceiverType = Receiver->getType(); |
| 1280 | } |
| 1281 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1282 | if (!Method) { |
| 1283 | // Handle messages to id. |
Fariborz Jahanian | ba55198 | 2010-08-10 18:10:50 +0000 | [diff] [blame] | 1284 | bool receiverIsId = ReceiverType->isObjCIdType(); |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1285 | if (receiverIsId || ReceiverType->isBlockPointerType() || |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1286 | (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) { |
| 1287 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1288 | SourceRange(LBracLoc, RBracLoc), |
| 1289 | receiverIsId); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1290 | if (!Method) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1291 | Method = LookupFactoryMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1292 | SourceRange(LBracLoc, RBracLoc), |
| 1293 | receiverIsId); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1294 | } else if (ReceiverType->isObjCClassType() || |
| 1295 | ReceiverType->isObjCQualifiedClassType()) { |
| 1296 | // Handle messages to Class. |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 1297 | // We allow sending a message to a qualified Class ("Class<foo>"), which |
| 1298 | // is ok as long as one of the protocols implements the selector (if not, warn). |
| 1299 | if (const ObjCObjectPointerType *QClassTy |
| 1300 | = ReceiverType->getAsObjCQualifiedClassType()) { |
| 1301 | // Search protocols for class methods. |
| 1302 | Method = LookupMethodInQualifiedType(Sel, QClassTy, false); |
| 1303 | if (!Method) { |
| 1304 | Method = LookupMethodInQualifiedType(Sel, QClassTy, true); |
| 1305 | // warn if instance method found for a Class message. |
| 1306 | if (Method) { |
| 1307 | Diag(Loc, diag::warn_instance_method_on_class_found) |
| 1308 | << Method->getSelector() << Sel; |
| 1309 | Diag(Method->getLocation(), diag::note_method_declared_at); |
| 1310 | } |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 1311 | } |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 1312 | } else { |
| 1313 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 1314 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { |
| 1315 | // First check the public methods in the class interface. |
| 1316 | Method = ClassDecl->lookupClassMethod(Sel); |
| 1317 | |
| 1318 | if (!Method) |
| 1319 | Method = LookupPrivateClassMethod(Sel, ClassDecl); |
| 1320 | } |
| 1321 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 1322 | return ExprError(); |
| 1323 | } |
| 1324 | if (!Method) { |
| 1325 | // If not messaging 'self', look for any factory method named 'Sel'. |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 1326 | if (!Receiver || !isSelfExpr(Receiver)) { |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 1327 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 1328 | SourceRange(LBracLoc, RBracLoc), |
| 1329 | true); |
| 1330 | if (!Method) { |
| 1331 | // If no class (factory) method was found, check if an _instance_ |
| 1332 | // method of the same name exists in the root class only. |
| 1333 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1334 | SourceRange(LBracLoc, RBracLoc), |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 1335 | true); |
| 1336 | if (Method) |
| 1337 | if (const ObjCInterfaceDecl *ID = |
| 1338 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { |
| 1339 | if (ID->getSuperClass()) |
| 1340 | Diag(Loc, diag::warn_root_inst_method_not_found) |
| 1341 | << Sel << SourceRange(LBracLoc, RBracLoc); |
| 1342 | } |
| 1343 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } |
| 1346 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1347 | } else { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1348 | ObjCInterfaceDecl* ClassDecl = 0; |
| 1349 | |
| 1350 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 1351 | // long as one of the protocols implements the selector (if not, warn). |
| 1352 | if (const ObjCObjectPointerType *QIdTy |
| 1353 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 1354 | // Search protocols for instance methods. |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1355 | Method = LookupMethodInQualifiedType(Sel, QIdTy, true); |
| 1356 | if (!Method) |
| 1357 | Method = LookupMethodInQualifiedType(Sel, QIdTy, false); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1358 | } else if (const ObjCObjectPointerType *OCIType |
| 1359 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 1360 | // We allow sending a message to a pointer to an interface (an object). |
| 1361 | ClassDecl = OCIType->getInterfaceDecl(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1362 | |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1363 | // Try to complete the type. Under ARC, this is a hard error from which |
| 1364 | // we don't try to recover. |
| 1365 | const ObjCInterfaceDecl *forwardClass = 0; |
| 1366 | if (RequireCompleteType(Loc, OCIType->getPointeeType(), |
| 1367 | getLangOptions().ObjCAutoRefCount |
| 1368 | ? PDiag(diag::err_arc_receiver_forward_instance) |
| 1369 | << (Receiver ? Receiver->getSourceRange() |
| 1370 | : SourceRange(SuperLoc)) |
Fariborz Jahanian | 4cc9b10 | 2012-02-03 01:02:44 +0000 | [diff] [blame] | 1371 | : PDiag(diag::warn_receiver_forward_instance) |
| 1372 | << (Receiver ? Receiver->getSourceRange() |
| 1373 | : SourceRange(SuperLoc)))) { |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1374 | if (getLangOptions().ObjCAutoRefCount) |
| 1375 | return ExprError(); |
| 1376 | |
| 1377 | forwardClass = OCIType->getInterfaceDecl(); |
Fariborz Jahanian | 4cc9b10 | 2012-02-03 01:02:44 +0000 | [diff] [blame] | 1378 | Diag(Receiver ? Receiver->getLocStart() |
| 1379 | : SuperLoc, diag::note_receiver_is_id); |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1380 | Method = 0; |
| 1381 | } else { |
| 1382 | Method = ClassDecl->lookupInstanceMethod(Sel); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1383 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1384 | |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1385 | if (!Method) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1386 | // Search protocol qualifiers. |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1387 | Method = LookupMethodInQualifiedType(Sel, OCIType, true); |
| 1388 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1389 | if (!Method) { |
| 1390 | // If we have implementations in scope, check "private" methods. |
| 1391 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 1392 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1393 | if (!Method && getLangOptions().ObjCAutoRefCount) { |
| 1394 | Diag(Loc, diag::err_arc_may_not_respond) |
| 1395 | << OCIType->getPointeeType() << Sel; |
| 1396 | return ExprError(); |
| 1397 | } |
| 1398 | |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 1399 | if (!Method && (!Receiver || !isSelfExpr(Receiver))) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1400 | // If we still haven't found a method, look in the global pool. This |
| 1401 | // behavior isn't very desirable, however we need it for GCC |
| 1402 | // compatibility. FIXME: should we deviate?? |
| 1403 | if (OCIType->qual_empty()) { |
| 1404 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 1405 | SourceRange(LBracLoc, RBracLoc)); |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 1406 | if (Method && !forwardClass) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1407 | Diag(Loc, diag::warn_maynot_respond) |
| 1408 | << OCIType->getInterfaceDecl()->getIdentifier() << Sel; |
| 1409 | } |
| 1410 | } |
| 1411 | } |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 1412 | if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass)) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1413 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1414 | } else if (!getLangOptions().ObjCAutoRefCount && |
| 1415 | !Context.getObjCIdType().isNull() && |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1416 | (ReceiverType->isPointerType() || |
| 1417 | ReceiverType->isIntegerType())) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1418 | // Implicitly convert integers and pointers to 'id' but emit a warning. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1419 | // But not in ARC. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1420 | Diag(Loc, diag::warn_bad_receiver_type) |
| 1421 | << ReceiverType |
| 1422 | << Receiver->getSourceRange(); |
| 1423 | if (ReceiverType->isPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1424 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1425 | CK_CPointerToObjCPointerCast).take(); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1426 | else { |
| 1427 | // TODO: specialized warning on null receivers? |
| 1428 | bool IsNull = Receiver->isNullPointerConstant(Context, |
| 1429 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1430 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
| 1431 | IsNull ? CK_NullToPointer : CK_IntegralToPointer).take(); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1432 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1433 | ReceiverType = Receiver->getType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 1434 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1435 | ExprResult ReceiverRes; |
| 1436 | if (getLangOptions().CPlusPlus) |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 1437 | ReceiverRes = PerformContextuallyConvertToObjCPointer(Receiver); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1438 | if (ReceiverRes.isUsable()) { |
| 1439 | Receiver = ReceiverRes.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1440 | return BuildInstanceMessage(Receiver, |
| 1441 | ReceiverType, |
| 1442 | SuperLoc, |
| 1443 | Sel, |
| 1444 | Method, |
| 1445 | LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1446 | SelectorLocs, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1447 | RBracLoc, |
| 1448 | move(ArgsIn)); |
| 1449 | } else { |
| 1450 | // Reject other random receiver types (e.g. structs). |
| 1451 | Diag(Loc, diag::err_bad_receiver_type) |
| 1452 | << ReceiverType << Receiver->getSourceRange(); |
| 1453 | return ExprError(); |
Fariborz Jahanian | 3ba6061 | 2010-05-13 17:19:25 +0000 | [diff] [blame] | 1454 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1455 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1456 | } |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 1457 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1458 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1459 | // Check the message arguments. |
| 1460 | unsigned NumArgs = ArgsIn.size(); |
| 1461 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 1462 | QualType ReturnType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1463 | ExprValueKind VK = VK_RValue; |
Fariborz Jahanian | 2600503 | 2010-12-01 01:07:24 +0000 | [diff] [blame] | 1464 | bool ClassMessage = (ReceiverType->isObjCClassType() || |
| 1465 | ReceiverType->isObjCQualifiedClassType()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1466 | if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, |
| 1467 | ClassMessage, SuperLoc.isValid(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1468 | LBracLoc, RBracLoc, ReturnType, VK)) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1469 | return ExprError(); |
Fariborz Jahanian | da59e09 | 2010-06-16 19:56:08 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | 483dd2f | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 1471 | if (Method && !Method->getResultType()->isVoidType() && |
| 1472 | RequireCompleteType(LBracLoc, Method->getResultType(), |
| 1473 | diag::err_illegal_message_expr_incomplete_type)) |
| 1474 | return ExprError(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1475 | |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1476 | SourceLocation SelLoc = SelectorLocs.front(); |
| 1477 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1478 | // In ARC, forbid the user from sending messages to |
| 1479 | // retain/release/autorelease/dealloc/retainCount explicitly. |
| 1480 | if (getLangOptions().ObjCAutoRefCount) { |
| 1481 | ObjCMethodFamily family = |
| 1482 | (Method ? Method->getMethodFamily() : Sel.getMethodFamily()); |
| 1483 | switch (family) { |
| 1484 | case OMF_init: |
| 1485 | if (Method) |
| 1486 | checkInitMethod(Method, ReceiverType); |
| 1487 | |
| 1488 | case OMF_None: |
| 1489 | case OMF_alloc: |
| 1490 | case OMF_copy: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 1491 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1492 | case OMF_mutableCopy: |
| 1493 | case OMF_new: |
| 1494 | case OMF_self: |
| 1495 | break; |
| 1496 | |
| 1497 | case OMF_dealloc: |
| 1498 | case OMF_retain: |
| 1499 | case OMF_release: |
| 1500 | case OMF_autorelease: |
| 1501 | case OMF_retainCount: |
| 1502 | Diag(Loc, diag::err_arc_illegal_explicit_message) |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1503 | << Sel << SelLoc; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1504 | break; |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1505 | |
| 1506 | case OMF_performSelector: |
| 1507 | if (Method && NumArgs >= 1) { |
| 1508 | if (ObjCSelectorExpr *SelExp = dyn_cast<ObjCSelectorExpr>(Args[0])) { |
| 1509 | Selector ArgSel = SelExp->getSelector(); |
| 1510 | ObjCMethodDecl *SelMethod = |
| 1511 | LookupInstanceMethodInGlobalPool(ArgSel, |
| 1512 | SelExp->getSourceRange()); |
| 1513 | if (!SelMethod) |
| 1514 | SelMethod = |
| 1515 | LookupFactoryMethodInGlobalPool(ArgSel, |
| 1516 | SelExp->getSourceRange()); |
| 1517 | if (SelMethod) { |
| 1518 | ObjCMethodFamily SelFamily = SelMethod->getMethodFamily(); |
| 1519 | switch (SelFamily) { |
| 1520 | case OMF_alloc: |
| 1521 | case OMF_copy: |
| 1522 | case OMF_mutableCopy: |
| 1523 | case OMF_new: |
| 1524 | case OMF_self: |
| 1525 | case OMF_init: |
| 1526 | // Issue error, unless ns_returns_not_retained. |
| 1527 | if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 1528 | // selector names a +1 method |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1529 | Diag(SelLoc, |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1530 | diag::err_arc_perform_selector_retains); |
| 1531 | Diag(SelMethod->getLocation(), diag::note_method_declared_at); |
| 1532 | } |
| 1533 | break; |
| 1534 | default: |
| 1535 | // +0 call. OK. unless ns_returns_retained. |
| 1536 | if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) { |
| 1537 | // selector names a +1 method |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1538 | Diag(SelLoc, |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1539 | diag::err_arc_perform_selector_retains); |
| 1540 | Diag(SelMethod->getLocation(), diag::note_method_declared_at); |
| 1541 | } |
| 1542 | break; |
| 1543 | } |
| 1544 | } |
| 1545 | } else { |
| 1546 | // error (may leak). |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1547 | Diag(SelLoc, diag::warn_arc_perform_selector_leaks); |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1548 | Diag(Args[0]->getExprLoc(), diag::note_used_here); |
| 1549 | } |
| 1550 | } |
| 1551 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1555 | // Construct the appropriate ObjCMessageExpr instance. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1556 | ObjCMessageExpr *Result; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1557 | if (SuperLoc.isValid()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1558 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1559 | SuperLoc, /*IsInstanceSuper=*/true, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1560 | ReceiverType, Sel, SelectorLocs, Method, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1561 | makeArrayRef(Args, NumArgs), RBracLoc, |
| 1562 | isImplicit); |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1563 | else |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1564 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1565 | Receiver, Sel, SelectorLocs, Method, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1566 | makeArrayRef(Args, NumArgs), RBracLoc, |
| 1567 | isImplicit); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1568 | |
| 1569 | if (getLangOptions().ObjCAutoRefCount) { |
| 1570 | // In ARC, annotate delegate init calls. |
| 1571 | if (Result->getMethodFamily() == OMF_init && |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 1572 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1573 | // Only consider init calls *directly* in init implementations, |
| 1574 | // not within blocks. |
| 1575 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext); |
| 1576 | if (method && method->getMethodFamily() == OMF_init) { |
| 1577 | // The implicit assignment to self means we also don't want to |
| 1578 | // consume the result. |
| 1579 | Result->setDelegateInitCall(true); |
| 1580 | return Owned(Result); |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | // In ARC, check for message sends which are likely to introduce |
| 1585 | // retain cycles. |
| 1586 | checkRetainCycles(Result); |
| 1587 | } |
| 1588 | |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1589 | return MaybeBindToTemporary(Result); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 1593 | // ArgExprs is optional - if it is present, the number of expressions |
| 1594 | // is obtained from Sel.getNumArgs(). |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1595 | ExprResult Sema::ActOnInstanceMessage(Scope *S, |
| 1596 | Expr *Receiver, |
| 1597 | Selector Sel, |
| 1598 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1599 | ArrayRef<SourceLocation> SelectorLocs, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1600 | SourceLocation RBracLoc, |
| 1601 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1602 | if (!Receiver) |
| 1603 | return ExprError(); |
| 1604 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1605 | return BuildInstanceMessage(Receiver, Receiver->getType(), |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1606 | /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1607 | LBracLoc, SelectorLocs, RBracLoc, move(Args)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1608 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1609 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1610 | enum ARCConversionTypeClass { |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1611 | /// int, void, struct A |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1612 | ACTC_none, |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1613 | |
| 1614 | /// id, void (^)() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1615 | ACTC_retainable, |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1616 | |
| 1617 | /// id*, id***, void (^*)(), |
| 1618 | ACTC_indirectRetainable, |
| 1619 | |
| 1620 | /// void* might be a normal C type, or it might a CF type. |
| 1621 | ACTC_voidPtr, |
| 1622 | |
| 1623 | /// struct A* |
| 1624 | ACTC_coreFoundation |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1625 | }; |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1626 | static bool isAnyRetainable(ARCConversionTypeClass ACTC) { |
| 1627 | return (ACTC == ACTC_retainable || |
| 1628 | ACTC == ACTC_coreFoundation || |
| 1629 | ACTC == ACTC_voidPtr); |
| 1630 | } |
| 1631 | static bool isAnyCLike(ARCConversionTypeClass ACTC) { |
| 1632 | return ACTC == ACTC_none || |
| 1633 | ACTC == ACTC_voidPtr || |
| 1634 | ACTC == ACTC_coreFoundation; |
| 1635 | } |
| 1636 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1637 | static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) { |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1638 | bool isIndirect = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1639 | |
| 1640 | // Ignore an outermost reference type. |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1641 | if (const ReferenceType *ref = type->getAs<ReferenceType>()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1642 | type = ref->getPointeeType(); |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1643 | isIndirect = true; |
| 1644 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1645 | |
| 1646 | // Drill through pointers and arrays recursively. |
| 1647 | while (true) { |
| 1648 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 1649 | type = ptr->getPointeeType(); |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1650 | |
| 1651 | // The first level of pointer may be the innermost pointer on a CF type. |
| 1652 | if (!isIndirect) { |
| 1653 | if (type->isVoidType()) return ACTC_voidPtr; |
| 1654 | if (type->isRecordType()) return ACTC_coreFoundation; |
| 1655 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1656 | } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) { |
| 1657 | type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0); |
| 1658 | } else { |
| 1659 | break; |
| 1660 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1661 | isIndirect = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1664 | if (isIndirect) { |
| 1665 | if (type->isObjCARCBridgableType()) |
| 1666 | return ACTC_indirectRetainable; |
| 1667 | return ACTC_none; |
| 1668 | } |
| 1669 | |
| 1670 | if (type->isObjCARCBridgableType()) |
| 1671 | return ACTC_retainable; |
| 1672 | |
| 1673 | return ACTC_none; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
| 1676 | namespace { |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1677 | /// A result from the cast checker. |
| 1678 | enum ACCResult { |
| 1679 | /// Cannot be casted. |
| 1680 | ACC_invalid, |
| 1681 | |
| 1682 | /// Can be safely retained or not retained. |
| 1683 | ACC_bottom, |
| 1684 | |
| 1685 | /// Can be casted at +0. |
| 1686 | ACC_plusZero, |
| 1687 | |
| 1688 | /// Can be casted at +1. |
| 1689 | ACC_plusOne |
| 1690 | }; |
| 1691 | ACCResult merge(ACCResult left, ACCResult right) { |
| 1692 | if (left == right) return left; |
| 1693 | if (left == ACC_bottom) return right; |
| 1694 | if (right == ACC_bottom) return left; |
| 1695 | return ACC_invalid; |
| 1696 | } |
| 1697 | |
| 1698 | /// A checker which white-lists certain expressions whose conversion |
| 1699 | /// to or from retainable type would otherwise be forbidden in ARC. |
| 1700 | class ARCCastChecker : public StmtVisitor<ARCCastChecker, ACCResult> { |
| 1701 | typedef StmtVisitor<ARCCastChecker, ACCResult> super; |
| 1702 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1703 | ASTContext &Context; |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1704 | ARCConversionTypeClass SourceClass; |
| 1705 | ARCConversionTypeClass TargetClass; |
| 1706 | |
| 1707 | static bool isCFType(QualType type) { |
| 1708 | // Someday this can use ns_bridged. For now, it has to do this. |
| 1709 | return type->isCARCBridgableType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1710 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1711 | |
| 1712 | public: |
| 1713 | ARCCastChecker(ASTContext &Context, ARCConversionTypeClass source, |
| 1714 | ARCConversionTypeClass target) |
| 1715 | : Context(Context), SourceClass(source), TargetClass(target) {} |
| 1716 | |
| 1717 | using super::Visit; |
| 1718 | ACCResult Visit(Expr *e) { |
| 1719 | return super::Visit(e->IgnoreParens()); |
| 1720 | } |
| 1721 | |
| 1722 | ACCResult VisitStmt(Stmt *s) { |
| 1723 | return ACC_invalid; |
| 1724 | } |
| 1725 | |
| 1726 | /// Null pointer constants can be casted however you please. |
| 1727 | ACCResult VisitExpr(Expr *e) { |
| 1728 | if (e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 1729 | return ACC_bottom; |
| 1730 | return ACC_invalid; |
| 1731 | } |
| 1732 | |
| 1733 | /// Objective-C string literals can be safely casted. |
| 1734 | ACCResult VisitObjCStringLiteral(ObjCStringLiteral *e) { |
| 1735 | // If we're casting to any retainable type, go ahead. Global |
| 1736 | // strings are immune to retains, so this is bottom. |
| 1737 | if (isAnyRetainable(TargetClass)) return ACC_bottom; |
| 1738 | |
| 1739 | return ACC_invalid; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1742 | /// Look through certain implicit and explicit casts. |
| 1743 | ACCResult VisitCastExpr(CastExpr *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1744 | switch (e->getCastKind()) { |
| 1745 | case CK_NullToPointer: |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1746 | return ACC_bottom; |
| 1747 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1748 | case CK_NoOp: |
| 1749 | case CK_LValueToRValue: |
| 1750 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1751 | case CK_CPointerToObjCPointerCast: |
| 1752 | case CK_BlockPointerToObjCPointerCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1753 | case CK_AnyPointerToBlockPointerCast: |
| 1754 | return Visit(e->getSubExpr()); |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1755 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1756 | default: |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1757 | return ACC_invalid; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1758 | } |
| 1759 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1760 | |
| 1761 | /// Look through unary extension. |
| 1762 | ACCResult VisitUnaryExtension(UnaryOperator *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1763 | return Visit(e->getSubExpr()); |
| 1764 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1765 | |
| 1766 | /// Ignore the LHS of a comma operator. |
| 1767 | ACCResult VisitBinComma(BinaryOperator *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1768 | return Visit(e->getRHS()); |
| 1769 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1770 | |
| 1771 | /// Conditional operators are okay if both sides are okay. |
| 1772 | ACCResult VisitConditionalOperator(ConditionalOperator *e) { |
| 1773 | ACCResult left = Visit(e->getTrueExpr()); |
| 1774 | if (left == ACC_invalid) return ACC_invalid; |
| 1775 | return merge(left, Visit(e->getFalseExpr())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1776 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1777 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1778 | /// Look through pseudo-objects. |
| 1779 | ACCResult VisitPseudoObjectExpr(PseudoObjectExpr *e) { |
| 1780 | // If we're getting here, we should always have a result. |
| 1781 | return Visit(e->getResultExpr()); |
| 1782 | } |
| 1783 | |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1784 | /// Statement expressions are okay if their result expression is okay. |
| 1785 | ACCResult VisitStmtExpr(StmtExpr *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1786 | return Visit(e->getSubStmt()->body_back()); |
| 1787 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1788 | |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1789 | /// Some declaration references are okay. |
| 1790 | ACCResult VisitDeclRefExpr(DeclRefExpr *e) { |
| 1791 | // References to global constants from system headers are okay. |
| 1792 | // These are things like 'kCFStringTransformToLatin'. They are |
| 1793 | // can also be assumed to be immune to retains. |
| 1794 | VarDecl *var = dyn_cast<VarDecl>(e->getDecl()); |
| 1795 | if (isAnyRetainable(TargetClass) && |
| 1796 | isAnyRetainable(SourceClass) && |
| 1797 | var && |
| 1798 | var->getStorageClass() == SC_Extern && |
| 1799 | var->getType().isConstQualified() && |
| 1800 | Context.getSourceManager().isInSystemHeader(var->getLocation())) { |
| 1801 | return ACC_bottom; |
| 1802 | } |
| 1803 | |
| 1804 | // Nothing else. |
| 1805 | return ACC_invalid; |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1806 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1807 | |
| 1808 | /// Some calls are okay. |
| 1809 | ACCResult VisitCallExpr(CallExpr *e) { |
| 1810 | if (FunctionDecl *fn = e->getDirectCallee()) |
| 1811 | if (ACCResult result = checkCallToFunction(fn)) |
| 1812 | return result; |
| 1813 | |
| 1814 | return super::VisitCallExpr(e); |
| 1815 | } |
| 1816 | |
| 1817 | ACCResult checkCallToFunction(FunctionDecl *fn) { |
| 1818 | // Require a CF*Ref return type. |
| 1819 | if (!isCFType(fn->getResultType())) |
| 1820 | return ACC_invalid; |
| 1821 | |
| 1822 | if (!isAnyRetainable(TargetClass)) |
| 1823 | return ACC_invalid; |
| 1824 | |
| 1825 | // Honor an explicit 'not retained' attribute. |
| 1826 | if (fn->hasAttr<CFReturnsNotRetainedAttr>()) |
| 1827 | return ACC_plusZero; |
| 1828 | |
| 1829 | // Honor an explicit 'retained' attribute, except that for |
| 1830 | // now we're not going to permit implicit handling of +1 results, |
| 1831 | // because it's a bit frightening. |
| 1832 | if (fn->hasAttr<CFReturnsRetainedAttr>()) |
| 1833 | return ACC_invalid; // ACC_plusOne if we start accepting this |
| 1834 | |
| 1835 | // Recognize this specific builtin function, which is used by CFSTR. |
| 1836 | unsigned builtinID = fn->getBuiltinID(); |
| 1837 | if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString) |
| 1838 | return ACC_bottom; |
| 1839 | |
| 1840 | // Otherwise, don't do anything implicit with an unaudited function. |
| 1841 | if (!fn->hasAttr<CFAuditedTransferAttr>()) |
| 1842 | return ACC_invalid; |
| 1843 | |
| 1844 | // Otherwise, it's +0 unless it follows the create convention. |
| 1845 | if (ento::coreFoundation::followsCreateRule(fn)) |
| 1846 | return ACC_invalid; // ACC_plusOne if we start accepting this |
| 1847 | |
| 1848 | return ACC_plusZero; |
| 1849 | } |
| 1850 | |
| 1851 | ACCResult VisitObjCMessageExpr(ObjCMessageExpr *e) { |
| 1852 | return checkCallToMethod(e->getMethodDecl()); |
| 1853 | } |
| 1854 | |
| 1855 | ACCResult VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *e) { |
| 1856 | ObjCMethodDecl *method; |
| 1857 | if (e->isExplicitProperty()) |
| 1858 | method = e->getExplicitProperty()->getGetterMethodDecl(); |
| 1859 | else |
| 1860 | method = e->getImplicitPropertyGetter(); |
| 1861 | return checkCallToMethod(method); |
| 1862 | } |
| 1863 | |
| 1864 | ACCResult checkCallToMethod(ObjCMethodDecl *method) { |
| 1865 | if (!method) return ACC_invalid; |
| 1866 | |
| 1867 | // Check for message sends to functions returning CF types. We |
| 1868 | // just obey the Cocoa conventions with these, even though the |
| 1869 | // return type is CF. |
| 1870 | if (!isAnyRetainable(TargetClass) || !isCFType(method->getResultType())) |
| 1871 | return ACC_invalid; |
| 1872 | |
| 1873 | // If the method is explicitly marked not-retained, it's +0. |
| 1874 | if (method->hasAttr<CFReturnsNotRetainedAttr>()) |
| 1875 | return ACC_plusZero; |
| 1876 | |
| 1877 | // If the method is explicitly marked as returning retained, or its |
| 1878 | // selector follows a +1 Cocoa convention, treat it as +1. |
| 1879 | if (method->hasAttr<CFReturnsRetainedAttr>()) |
| 1880 | return ACC_plusOne; |
| 1881 | |
| 1882 | switch (method->getSelector().getMethodFamily()) { |
| 1883 | case OMF_alloc: |
| 1884 | case OMF_copy: |
| 1885 | case OMF_mutableCopy: |
| 1886 | case OMF_new: |
| 1887 | return ACC_plusOne; |
| 1888 | |
| 1889 | default: |
| 1890 | // Otherwise, treat it as +0. |
| 1891 | return ACC_plusZero; |
Fariborz Jahanian | c8505ad | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 1892 | } |
| 1893 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1894 | }; |
Fariborz Jahanian | 1522a7c | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 1897 | static bool |
| 1898 | KnownName(Sema &S, const char *name) { |
| 1899 | LookupResult R(S, &S.Context.Idents.get(name), SourceLocation(), |
| 1900 | Sema::LookupOrdinaryName); |
| 1901 | return S.LookupName(R, S.TUScope, false); |
| 1902 | } |
| 1903 | |
Argyrios Kyrtzidis | ae1b4af | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 1904 | static void addFixitForObjCARCConversion(Sema &S, |
| 1905 | DiagnosticBuilder &DiagB, |
| 1906 | Sema::CheckedConversionKind CCK, |
| 1907 | SourceLocation afterLParen, |
| 1908 | QualType castType, |
| 1909 | Expr *castExpr, |
| 1910 | const char *bridgeKeyword, |
| 1911 | const char *CFBridgeName) { |
| 1912 | // We handle C-style and implicit casts here. |
| 1913 | switch (CCK) { |
| 1914 | case Sema::CCK_ImplicitConversion: |
| 1915 | case Sema::CCK_CStyleCast: |
| 1916 | break; |
| 1917 | case Sema::CCK_FunctionalCast: |
| 1918 | case Sema::CCK_OtherCast: |
| 1919 | return; |
| 1920 | } |
| 1921 | |
| 1922 | if (CFBridgeName) { |
| 1923 | Expr *castedE = castExpr; |
| 1924 | if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(castedE)) |
| 1925 | castedE = CCE->getSubExpr(); |
| 1926 | castedE = castedE->IgnoreImpCasts(); |
| 1927 | SourceRange range = castedE->getSourceRange(); |
| 1928 | if (isa<ParenExpr>(castedE)) { |
| 1929 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 1930 | CFBridgeName)); |
| 1931 | } else { |
| 1932 | std::string namePlusParen = CFBridgeName; |
| 1933 | namePlusParen += "("; |
| 1934 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 1935 | namePlusParen)); |
| 1936 | DiagB.AddFixItHint(FixItHint::CreateInsertion( |
| 1937 | S.PP.getLocForEndOfToken(range.getEnd()), |
| 1938 | ")")); |
| 1939 | } |
| 1940 | return; |
| 1941 | } |
| 1942 | |
| 1943 | if (CCK == Sema::CCK_CStyleCast) { |
| 1944 | DiagB.AddFixItHint(FixItHint::CreateInsertion(afterLParen, bridgeKeyword)); |
| 1945 | } else { |
| 1946 | std::string castCode = "("; |
| 1947 | castCode += bridgeKeyword; |
| 1948 | castCode += castType.getAsString(); |
| 1949 | castCode += ")"; |
| 1950 | Expr *castedE = castExpr->IgnoreImpCasts(); |
| 1951 | SourceRange range = castedE->getSourceRange(); |
| 1952 | if (isa<ParenExpr>(castedE)) { |
| 1953 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 1954 | castCode)); |
| 1955 | } else { |
| 1956 | castCode += "("; |
| 1957 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 1958 | castCode)); |
| 1959 | DiagB.AddFixItHint(FixItHint::CreateInsertion( |
| 1960 | S.PP.getLocForEndOfToken(range.getEnd()), |
| 1961 | ")")); |
| 1962 | } |
| 1963 | } |
| 1964 | } |
| 1965 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1966 | static void |
| 1967 | diagnoseObjCARCConversion(Sema &S, SourceRange castRange, |
| 1968 | QualType castType, ARCConversionTypeClass castACTC, |
| 1969 | Expr *castExpr, ARCConversionTypeClass exprACTC, |
| 1970 | Sema::CheckedConversionKind CCK) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1971 | SourceLocation loc = |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1972 | (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1973 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1974 | if (S.makeUnavailableInSystemHeader(loc, |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1975 | "converts between Objective-C and C pointers in -fobjc-arc")) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1976 | return; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1977 | |
| 1978 | QualType castExprType = castExpr->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1979 | |
John McCall | 71c482c | 2011-06-17 06:50:50 +0000 | [diff] [blame] | 1980 | unsigned srcKind = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1981 | switch (exprACTC) { |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 1982 | case ACTC_none: |
| 1983 | case ACTC_coreFoundation: |
| 1984 | case ACTC_voidPtr: |
| 1985 | srcKind = (castExprType->isPointerType() ? 1 : 0); |
| 1986 | break; |
| 1987 | case ACTC_retainable: |
| 1988 | srcKind = (castExprType->isBlockPointerType() ? 2 : 3); |
| 1989 | break; |
| 1990 | case ACTC_indirectRetainable: |
| 1991 | srcKind = 4; |
| 1992 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1995 | // Check whether this could be fixed with a bridge cast. |
| 1996 | SourceLocation afterLParen = S.PP.getLocForEndOfToken(castRange.getBegin()); |
| 1997 | SourceLocation noteLoc = afterLParen.isValid() ? afterLParen : loc; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1998 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1999 | // Bridge from an ARC type to a CF type. |
| 2000 | if (castACTC == ACTC_retainable && isAnyRetainable(exprACTC)) { |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2001 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2002 | S.Diag(loc, diag::err_arc_cast_requires_bridge) |
| 2003 | << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit |
| 2004 | << 2 // of C pointer type |
| 2005 | << castExprType |
| 2006 | << unsigned(castType->isBlockPointerType()) // to ObjC|block type |
| 2007 | << castType |
| 2008 | << castRange |
| 2009 | << castExpr->getSourceRange(); |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2010 | bool br = KnownName(S, "CFBridgingRelease"); |
Argyrios Kyrtzidis | ae1b4af | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 2011 | { |
| 2012 | DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge); |
| 2013 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| 2014 | castType, castExpr, "__bridge ", 0); |
| 2015 | } |
| 2016 | { |
| 2017 | DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge_transfer) |
| 2018 | << castExprType << br; |
| 2019 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| 2020 | castType, castExpr, "__bridge_transfer ", |
| 2021 | br ? "CFBridgingRelease" : 0); |
| 2022 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2023 | |
| 2024 | return; |
| 2025 | } |
| 2026 | |
| 2027 | // Bridge from a CF type to an ARC type. |
| 2028 | if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC)) { |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2029 | bool br = KnownName(S, "CFBridgingRetain"); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2030 | S.Diag(loc, diag::err_arc_cast_requires_bridge) |
| 2031 | << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit |
| 2032 | << unsigned(castExprType->isBlockPointerType()) // of ObjC|block type |
| 2033 | << castExprType |
| 2034 | << 2 // to C pointer type |
| 2035 | << castType |
| 2036 | << castRange |
| 2037 | << castExpr->getSourceRange(); |
| 2038 | |
Argyrios Kyrtzidis | ae1b4af | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 2039 | { |
| 2040 | DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge); |
| 2041 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| 2042 | castType, castExpr, "__bridge ", 0); |
| 2043 | } |
| 2044 | { |
| 2045 | DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge_retained) |
| 2046 | << castType << br; |
| 2047 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| 2048 | castType, castExpr, "__bridge_retained ", |
| 2049 | br ? "CFBridgingRetain" : 0); |
| 2050 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2051 | |
| 2052 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2055 | S.Diag(loc, diag::err_arc_mismatched_cast) |
| 2056 | << (CCK != Sema::CCK_ImplicitConversion) |
| 2057 | << srcKind << castExprType << castType |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2058 | << castRange << castExpr->getSourceRange(); |
| 2059 | } |
| 2060 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2061 | Sema::ARCConversionResult |
| 2062 | Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType, |
| 2063 | Expr *&castExpr, CheckedConversionKind CCK) { |
| 2064 | QualType castExprType = castExpr->getType(); |
| 2065 | |
| 2066 | // For the purposes of the classification, we assume reference types |
| 2067 | // will bind to temporaries. |
| 2068 | QualType effCastType = castType; |
| 2069 | if (const ReferenceType *ref = castType->getAs<ReferenceType>()) |
| 2070 | effCastType = ref->getPointeeType(); |
| 2071 | |
| 2072 | ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType); |
| 2073 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(effCastType); |
Fariborz Jahanian | 6d09f01 | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 2074 | if (exprACTC == castACTC) { |
| 2075 | // check for viablity and report error if casting an rvalue to a |
| 2076 | // life-time qualifier. |
Fariborz Jahanian | fc2eff5 | 2011-10-29 00:06:10 +0000 | [diff] [blame] | 2077 | if ((castACTC == ACTC_retainable) && |
Fariborz Jahanian | 6d09f01 | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 2078 | (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) && |
Fariborz Jahanian | fc2eff5 | 2011-10-29 00:06:10 +0000 | [diff] [blame] | 2079 | (castType != castExprType)) { |
| 2080 | const Type *DT = castType.getTypePtr(); |
| 2081 | QualType QDT = castType; |
| 2082 | // We desugar some types but not others. We ignore those |
| 2083 | // that cannot happen in a cast; i.e. auto, and those which |
| 2084 | // should not be de-sugared; i.e typedef. |
| 2085 | if (const ParenType *PT = dyn_cast<ParenType>(DT)) |
| 2086 | QDT = PT->desugar(); |
| 2087 | else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT)) |
| 2088 | QDT = TP->desugar(); |
| 2089 | else if (const AttributedType *AT = dyn_cast<AttributedType>(DT)) |
| 2090 | QDT = AT->desugar(); |
| 2091 | if (QDT != castType && |
| 2092 | QDT.getObjCLifetime() != Qualifiers::OCL_None) { |
| 2093 | SourceLocation loc = |
| 2094 | (castRange.isValid() ? castRange.getBegin() |
| 2095 | : castExpr->getExprLoc()); |
| 2096 | Diag(loc, diag::err_arc_nolifetime_behavior); |
| 2097 | } |
Fariborz Jahanian | 6d09f01 | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 2098 | } |
| 2099 | return ACR_okay; |
| 2100 | } |
| 2101 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2102 | if (isAnyCLike(exprACTC) && isAnyCLike(castACTC)) return ACR_okay; |
| 2103 | |
| 2104 | // Allow all of these types to be cast to integer types (but not |
| 2105 | // vice-versa). |
| 2106 | if (castACTC == ACTC_none && castType->isIntegralType(Context)) |
| 2107 | return ACR_okay; |
| 2108 | |
| 2109 | // Allow casts between pointers to lifetime types (e.g., __strong id*) |
| 2110 | // and pointers to void (e.g., cv void *). Casting from void* to lifetime* |
| 2111 | // must be explicit. |
| 2112 | if (exprACTC == ACTC_indirectRetainable && castACTC == ACTC_voidPtr) |
| 2113 | return ACR_okay; |
| 2114 | if (castACTC == ACTC_indirectRetainable && exprACTC == ACTC_voidPtr && |
| 2115 | CCK != CCK_ImplicitConversion) |
| 2116 | return ACR_okay; |
| 2117 | |
| 2118 | switch (ARCCastChecker(Context, exprACTC, castACTC).Visit(castExpr)) { |
| 2119 | // For invalid casts, fall through. |
| 2120 | case ACC_invalid: |
| 2121 | break; |
| 2122 | |
| 2123 | // Do nothing for both bottom and +0. |
| 2124 | case ACC_bottom: |
| 2125 | case ACC_plusZero: |
| 2126 | return ACR_okay; |
| 2127 | |
| 2128 | // If the result is +1, consume it here. |
| 2129 | case ACC_plusOne: |
| 2130 | castExpr = ImplicitCastExpr::Create(Context, castExpr->getType(), |
| 2131 | CK_ARCConsumeObject, castExpr, |
| 2132 | 0, VK_RValue); |
| 2133 | ExprNeedsCleanups = true; |
| 2134 | return ACR_okay; |
| 2135 | } |
| 2136 | |
| 2137 | // If this is a non-implicit cast from id or block type to a |
| 2138 | // CoreFoundation type, delay complaining in case the cast is used |
| 2139 | // in an acceptable context. |
| 2140 | if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC) && |
| 2141 | CCK != CCK_ImplicitConversion) |
| 2142 | return ACR_unbridged; |
| 2143 | |
| 2144 | diagnoseObjCARCConversion(*this, castRange, castType, castACTC, |
| 2145 | castExpr, exprACTC, CCK); |
| 2146 | return ACR_okay; |
| 2147 | } |
| 2148 | |
| 2149 | /// Given that we saw an expression with the ARCUnbridgedCastTy |
| 2150 | /// placeholder type, complain bitterly. |
| 2151 | void Sema::diagnoseARCUnbridgedCast(Expr *e) { |
| 2152 | // We expect the spurious ImplicitCastExpr to already have been stripped. |
| 2153 | assert(!e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 2154 | CastExpr *realCast = cast<CastExpr>(e->IgnoreParens()); |
| 2155 | |
| 2156 | SourceRange castRange; |
| 2157 | QualType castType; |
| 2158 | CheckedConversionKind CCK; |
| 2159 | |
| 2160 | if (CStyleCastExpr *cast = dyn_cast<CStyleCastExpr>(realCast)) { |
| 2161 | castRange = SourceRange(cast->getLParenLoc(), cast->getRParenLoc()); |
| 2162 | castType = cast->getTypeAsWritten(); |
| 2163 | CCK = CCK_CStyleCast; |
| 2164 | } else if (ExplicitCastExpr *cast = dyn_cast<ExplicitCastExpr>(realCast)) { |
| 2165 | castRange = cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange(); |
| 2166 | castType = cast->getTypeAsWritten(); |
| 2167 | CCK = CCK_OtherCast; |
| 2168 | } else { |
| 2169 | castType = cast->getType(); |
| 2170 | CCK = CCK_ImplicitConversion; |
| 2171 | } |
| 2172 | |
| 2173 | ARCConversionTypeClass castACTC = |
| 2174 | classifyTypeForARCConversion(castType.getNonReferenceType()); |
| 2175 | |
| 2176 | Expr *castExpr = realCast->getSubExpr(); |
| 2177 | assert(classifyTypeForARCConversion(castExpr->getType()) == ACTC_retainable); |
| 2178 | |
| 2179 | diagnoseObjCARCConversion(*this, castRange, castType, castACTC, |
| 2180 | castExpr, ACTC_retainable, CCK); |
| 2181 | } |
| 2182 | |
| 2183 | /// stripARCUnbridgedCast - Given an expression of ARCUnbridgedCast |
| 2184 | /// type, remove the placeholder cast. |
| 2185 | Expr *Sema::stripARCUnbridgedCast(Expr *e) { |
| 2186 | assert(e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 2187 | |
| 2188 | if (ParenExpr *pe = dyn_cast<ParenExpr>(e)) { |
| 2189 | Expr *sub = stripARCUnbridgedCast(pe->getSubExpr()); |
| 2190 | return new (Context) ParenExpr(pe->getLParen(), pe->getRParen(), sub); |
| 2191 | } else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) { |
| 2192 | assert(uo->getOpcode() == UO_Extension); |
| 2193 | Expr *sub = stripARCUnbridgedCast(uo->getSubExpr()); |
| 2194 | return new (Context) UnaryOperator(sub, UO_Extension, sub->getType(), |
| 2195 | sub->getValueKind(), sub->getObjectKind(), |
| 2196 | uo->getOperatorLoc()); |
| 2197 | } else if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) { |
| 2198 | assert(!gse->isResultDependent()); |
| 2199 | |
| 2200 | unsigned n = gse->getNumAssocs(); |
| 2201 | SmallVector<Expr*, 4> subExprs(n); |
| 2202 | SmallVector<TypeSourceInfo*, 4> subTypes(n); |
| 2203 | for (unsigned i = 0; i != n; ++i) { |
| 2204 | subTypes[i] = gse->getAssocTypeSourceInfo(i); |
| 2205 | Expr *sub = gse->getAssocExpr(i); |
| 2206 | if (i == gse->getResultIndex()) |
| 2207 | sub = stripARCUnbridgedCast(sub); |
| 2208 | subExprs[i] = sub; |
| 2209 | } |
| 2210 | |
| 2211 | return new (Context) GenericSelectionExpr(Context, gse->getGenericLoc(), |
| 2212 | gse->getControllingExpr(), |
| 2213 | subTypes.data(), subExprs.data(), |
| 2214 | n, gse->getDefaultLoc(), |
| 2215 | gse->getRParenLoc(), |
| 2216 | gse->containsUnexpandedParameterPack(), |
| 2217 | gse->getResultIndex()); |
| 2218 | } else { |
| 2219 | assert(isa<ImplicitCastExpr>(e) && "bad form of unbridged cast!"); |
| 2220 | return cast<ImplicitCastExpr>(e)->getSubExpr(); |
| 2221 | } |
| 2222 | } |
| 2223 | |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 2224 | bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType, |
| 2225 | QualType exprType) { |
| 2226 | QualType canCastType = |
| 2227 | Context.getCanonicalType(castType).getUnqualifiedType(); |
| 2228 | QualType canExprType = |
| 2229 | Context.getCanonicalType(exprType).getUnqualifiedType(); |
| 2230 | if (isa<ObjCObjectPointerType>(canCastType) && |
| 2231 | castType.getObjCLifetime() == Qualifiers::OCL_Weak && |
| 2232 | canExprType->isObjCObjectPointerType()) { |
| 2233 | if (const ObjCObjectPointerType *ObjT = |
| 2234 | canExprType->getAs<ObjCObjectPointerType>()) |
| 2235 | if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) |
| 2236 | return false; |
| 2237 | } |
| 2238 | return true; |
| 2239 | } |
| 2240 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 2241 | /// Look for an ObjCReclaimReturnedObject cast and destroy it. |
| 2242 | static Expr *maybeUndoReclaimObject(Expr *e) { |
| 2243 | // For now, we just undo operands that are *immediately* reclaim |
| 2244 | // expressions, which prevents the vast majority of potential |
| 2245 | // problems here. To catch them all, we'd need to rebuild arbitrary |
| 2246 | // value-propagating subexpressions --- we can't reliably rebuild |
| 2247 | // in-place because of expression sharing. |
| 2248 | if (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2249 | if (ice->getCastKind() == CK_ARCReclaimReturnedObject) |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 2250 | return ice->getSubExpr(); |
| 2251 | |
| 2252 | return e; |
| 2253 | } |
| 2254 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2255 | ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc, |
| 2256 | ObjCBridgeCastKind Kind, |
| 2257 | SourceLocation BridgeKeywordLoc, |
| 2258 | TypeSourceInfo *TSInfo, |
| 2259 | Expr *SubExpr) { |
John McCall | 4906cf9 | 2011-08-26 00:48:42 +0000 | [diff] [blame] | 2260 | ExprResult SubResult = UsualUnaryConversions(SubExpr); |
| 2261 | if (SubResult.isInvalid()) return ExprError(); |
| 2262 | SubExpr = SubResult.take(); |
| 2263 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2264 | QualType T = TSInfo->getType(); |
| 2265 | QualType FromType = SubExpr->getType(); |
| 2266 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2267 | CastKind CK; |
| 2268 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2269 | bool MustConsume = false; |
| 2270 | if (T->isDependentType() || SubExpr->isTypeDependent()) { |
| 2271 | // Okay: we'll build a dependent expression type. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2272 | CK = CK_Dependent; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2273 | } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) { |
| 2274 | // Casting CF -> id |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2275 | CK = (T->isBlockPointerType() ? CK_AnyPointerToBlockPointerCast |
| 2276 | : CK_CPointerToObjCPointerCast); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2277 | switch (Kind) { |
| 2278 | case OBC_Bridge: |
| 2279 | break; |
| 2280 | |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2281 | case OBC_BridgeRetained: { |
| 2282 | bool br = KnownName(*this, "CFBridgingRelease"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2283 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 2284 | << 2 |
| 2285 | << FromType |
| 2286 | << (T->isBlockPointerType()? 1 : 0) |
| 2287 | << T |
| 2288 | << SubExpr->getSourceRange() |
| 2289 | << Kind; |
| 2290 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 2291 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge"); |
| 2292 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer) |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2293 | << FromType << br |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2294 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2295 | br ? "CFBridgingRelease " |
| 2296 | : "__bridge_transfer "); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2297 | |
| 2298 | Kind = OBC_Bridge; |
| 2299 | break; |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2300 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2301 | |
| 2302 | case OBC_BridgeTransfer: |
| 2303 | // We must consume the Objective-C object produced by the cast. |
| 2304 | MustConsume = true; |
| 2305 | break; |
| 2306 | } |
| 2307 | } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) { |
| 2308 | // Okay: id -> CF |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2309 | CK = CK_BitCast; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2310 | switch (Kind) { |
| 2311 | case OBC_Bridge: |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 2312 | // Reclaiming a value that's going to be __bridge-casted to CF |
| 2313 | // is very dangerous, so we don't do it. |
| 2314 | SubExpr = maybeUndoReclaimObject(SubExpr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2315 | break; |
| 2316 | |
| 2317 | case OBC_BridgeRetained: |
| 2318 | // Produce the object before casting it. |
| 2319 | SubExpr = ImplicitCastExpr::Create(Context, FromType, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2320 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2321 | SubExpr, 0, VK_RValue); |
| 2322 | break; |
| 2323 | |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2324 | case OBC_BridgeTransfer: { |
| 2325 | bool br = KnownName(*this, "CFBridgingRetain"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2326 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 2327 | << (FromType->isBlockPointerType()? 1 : 0) |
| 2328 | << FromType |
| 2329 | << 2 |
| 2330 | << T |
| 2331 | << SubExpr->getSourceRange() |
| 2332 | << Kind; |
| 2333 | |
| 2334 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 2335 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge "); |
| 2336 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained) |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2337 | << T << br |
| 2338 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
| 2339 | br ? "CFBridgingRetain " : "__bridge_retained"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2340 | |
| 2341 | Kind = OBC_Bridge; |
| 2342 | break; |
| 2343 | } |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2344 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2345 | } else { |
| 2346 | Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible) |
| 2347 | << FromType << T << Kind |
| 2348 | << SubExpr->getSourceRange() |
| 2349 | << TSInfo->getTypeLoc().getSourceRange(); |
| 2350 | return ExprError(); |
| 2351 | } |
| 2352 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2353 | Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, CK, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2354 | BridgeKeywordLoc, |
| 2355 | TSInfo, SubExpr); |
| 2356 | |
| 2357 | if (MustConsume) { |
| 2358 | ExprNeedsCleanups = true; |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 2359 | Result = ImplicitCastExpr::Create(Context, T, CK_ARCConsumeObject, Result, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2360 | 0, VK_RValue); |
| 2361 | } |
| 2362 | |
| 2363 | return Result; |
| 2364 | } |
| 2365 | |
| 2366 | ExprResult Sema::ActOnObjCBridgedCast(Scope *S, |
| 2367 | SourceLocation LParenLoc, |
| 2368 | ObjCBridgeCastKind Kind, |
| 2369 | SourceLocation BridgeKeywordLoc, |
| 2370 | ParsedType Type, |
| 2371 | SourceLocation RParenLoc, |
| 2372 | Expr *SubExpr) { |
| 2373 | TypeSourceInfo *TSInfo = 0; |
| 2374 | QualType T = GetTypeFromParser(Type, &TSInfo); |
| 2375 | if (!TSInfo) |
| 2376 | TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc); |
| 2377 | return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo, |
| 2378 | SubExpr); |
| 2379 | } |