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