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" |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTContext.h" |
| 20 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 21 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 23 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallString.h" |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
| 26 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 27 | using namespace clang; |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 28 | using namespace sema; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 29 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 30 | ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
| 31 | Expr **strings, |
| 32 | unsigned NumStrings) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 33 | StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); |
| 34 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 35 | // Most ObjC strings are formed out of a single piece. However, we *can* |
| 36 | // have strings formed out of multiple @ strings with multiple pptokens in |
| 37 | // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one |
| 38 | // StringLiteral for ObjCStringLiteral to hold onto. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 39 | StringLiteral *S = Strings[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 40 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 41 | // If we have a multi-part string, merge it all together. |
| 42 | if (NumStrings != 1) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 43 | // Concatenate objc strings. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 44 | llvm::SmallString<128> StrBuf; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 45 | SmallVector<SourceLocation, 8> StrLocs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 47 | for (unsigned i = 0; i != NumStrings; ++i) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 48 | S = Strings[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 49 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame^] | 50 | // ObjC strings can't be wide or UTF. |
| 51 | if (!S->isAscii()) { |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 52 | Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 53 | << S->getSourceRange(); |
| 54 | return true; |
| 55 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 57 | // Append the string. |
| 58 | StrBuf += S->getString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 60 | // Get the locations of the string tokens. |
| 61 | StrLocs.append(S->tokloc_begin(), S->tokloc_end()); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 62 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 64 | // Create the aggregate string with the appropriate content and location |
| 65 | // information. |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 66 | S = StringLiteral::Create(Context, StrBuf, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame^] | 67 | StringLiteral::Ascii, /*Pascal=*/false, |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 68 | Context.getPointerType(Context.CharTy), |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 69 | &StrLocs[0], StrLocs.size()); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 70 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 72 | // Verify that this composite string is acceptable for ObjC strings. |
| 73 | if (CheckObjCString(S)) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 74 | return true; |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 75 | |
| 76 | // Initialize the constant string interface lazily. This assumes |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 77 | // the NSString interface is seen in this translation unit. Note: We |
| 78 | // don't use NSConstantString, since the runtime team considers this |
| 79 | // interface private (even though it appears in the header files). |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 80 | QualType Ty = Context.getObjCConstantStringInterface(); |
| 81 | if (!Ty.isNull()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 82 | Ty = Context.getObjCObjectPointerType(Ty); |
Fariborz Jahanian | 8a43776 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 83 | } else if (getLangOptions().NoConstantCFStrings) { |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 84 | IdentifierInfo *NSIdent=0; |
| 85 | std::string StringClass(getLangOptions().ObjCConstantStringClass); |
| 86 | |
| 87 | if (StringClass.empty()) |
| 88 | NSIdent = &Context.Idents.get("NSConstantString"); |
| 89 | else |
| 90 | NSIdent = &Context.Idents.get(StringClass); |
| 91 | |
Fariborz Jahanian | 8a43776 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 92 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0], |
| 93 | LookupOrdinaryName); |
| 94 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 95 | Context.setObjCConstantStringInterface(StrIF); |
| 96 | Ty = Context.getObjCConstantStringInterface(); |
| 97 | Ty = Context.getObjCObjectPointerType(Ty); |
| 98 | } else { |
| 99 | // If there is no NSConstantString interface defined then treat this |
| 100 | // as error and recover from it. |
| 101 | Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent |
| 102 | << S->getSourceRange(); |
| 103 | Ty = Context.getObjCIdType(); |
| 104 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 105 | } else { |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 106 | IdentifierInfo *NSIdent = &Context.Idents.get("NSString"); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 107 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0], |
| 108 | LookupOrdinaryName); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 109 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 110 | Context.setObjCConstantStringInterface(StrIF); |
| 111 | Ty = Context.getObjCConstantStringInterface(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 112 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 113 | } else { |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 114 | // If there is no NSString interface defined then treat constant |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 115 | // strings as untyped objects and let the runtime figure it out later. |
| 116 | Ty = Context.getObjCIdType(); |
| 117 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 120 | return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Argyrios Kyrtzidis | 3b5904b | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 123 | ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 124 | TypeSourceInfo *EncodedTypeInfo, |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 125 | SourceLocation RParenLoc) { |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 126 | QualType EncodedType = EncodedTypeInfo->getType(); |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 127 | QualType StrTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | if (EncodedType->isDependentType()) |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 129 | StrTy = Context.DependentTy; |
| 130 | else { |
Fariborz Jahanian | 6c91615 | 2011-06-16 22:34:44 +0000 | [diff] [blame] | 131 | if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled. |
| 132 | !EncodedType->isVoidType()) // void is handled too. |
Argyrios Kyrtzidis | 3b5904b | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 133 | if (RequireCompleteType(AtLoc, EncodedType, |
| 134 | PDiag(diag::err_incomplete_type_objc_at_encode) |
| 135 | << EncodedTypeInfo->getTypeLoc().getSourceRange())) |
| 136 | return ExprError(); |
| 137 | |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 138 | std::string Str; |
| 139 | Context.getObjCEncodingForType(EncodedType, Str); |
| 140 | |
| 141 | // The type of @encode is the same as the type of the corresponding string, |
| 142 | // which is an array type. |
| 143 | StrTy = Context.CharTy; |
| 144 | // 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] | 145 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 146 | StrTy.addConst(); |
| 147 | StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), |
| 148 | ArrayType::Normal, 0); |
| 149 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 151 | return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc); |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 152 | } |
| 153 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 154 | ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 155 | SourceLocation EncodeLoc, |
| 156 | SourceLocation LParenLoc, |
| 157 | ParsedType ty, |
| 158 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 159 | // FIXME: Preserve type source info ? |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 160 | TypeSourceInfo *TInfo; |
| 161 | QualType EncodedType = GetTypeFromParser(ty, &TInfo); |
| 162 | if (!TInfo) |
| 163 | TInfo = Context.getTrivialTypeSourceInfo(EncodedType, |
| 164 | PP.getLocForEndOfToken(LParenLoc)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 165 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 166 | return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 169 | ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 170 | SourceLocation AtLoc, |
| 171 | SourceLocation SelLoc, |
| 172 | SourceLocation LParenLoc, |
| 173 | SourceLocation RParenLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 174 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 175 | SourceRange(LParenLoc, RParenLoc), false, false); |
Fariborz Jahanian | 7ff22de | 2009-06-16 16:25:00 +0000 | [diff] [blame] | 176 | if (!Method) |
| 177 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 178 | SourceRange(LParenLoc, RParenLoc)); |
| 179 | if (!Method) |
| 180 | Diag(SelLoc, diag::warn_undeclared_selector) << Sel; |
Fariborz Jahanian | 4c91d89 | 2011-07-13 19:05:43 +0000 | [diff] [blame] | 181 | |
| 182 | if (!Method || |
| 183 | Method->getImplementationControl() != ObjCMethodDecl::Optional) { |
| 184 | llvm::DenseMap<Selector, SourceLocation>::iterator Pos |
| 185 | = ReferencedSelectors.find(Sel); |
| 186 | if (Pos == ReferencedSelectors.end()) |
| 187 | ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); |
| 188 | } |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 189 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 190 | // In ARC, forbid the user from using @selector for |
| 191 | // retain/release/autorelease/dealloc/retainCount. |
| 192 | if (getLangOptions().ObjCAutoRefCount) { |
| 193 | switch (Sel.getMethodFamily()) { |
| 194 | case OMF_retain: |
| 195 | case OMF_release: |
| 196 | case OMF_autorelease: |
| 197 | case OMF_retainCount: |
| 198 | case OMF_dealloc: |
| 199 | Diag(AtLoc, diag::err_arc_illegal_selector) << |
| 200 | Sel << SourceRange(LParenLoc, RParenLoc); |
| 201 | break; |
| 202 | |
| 203 | case OMF_None: |
| 204 | case OMF_alloc: |
| 205 | case OMF_copy: |
| 206 | case OMF_init: |
| 207 | case OMF_mutableCopy: |
| 208 | case OMF_new: |
| 209 | case OMF_self: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 210 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | } |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 214 | QualType Ty = Context.getObjCSelType(); |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 215 | return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 216 | } |
| 217 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 218 | ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 219 | SourceLocation AtLoc, |
| 220 | SourceLocation ProtoLoc, |
| 221 | SourceLocation LParenLoc, |
| 222 | SourceLocation RParenLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 223 | ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 224 | if (!PDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 225 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 226 | return true; |
| 227 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 229 | QualType Ty = Context.getObjCProtoType(); |
| 230 | if (Ty.isNull()) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 231 | return true; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 232 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 233 | return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 234 | } |
| 235 | |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 236 | /// Try to capture an implicit reference to 'self'. |
| 237 | ObjCMethodDecl *Sema::tryCaptureObjCSelf() { |
| 238 | // Ignore block scopes: we can capture through them. |
| 239 | DeclContext *DC = CurContext; |
| 240 | while (true) { |
| 241 | if (isa<BlockDecl>(DC)) DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 242 | else if (isa<EnumDecl>(DC)) DC = cast<EnumDecl>(DC)->getDeclContext(); |
| 243 | else break; |
| 244 | } |
| 245 | |
| 246 | // If we're not in an ObjC method, error out. Note that, unlike the |
| 247 | // C++ case, we don't require an instance method --- class methods |
| 248 | // still have a 'self', and we really do still need to capture it! |
| 249 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC); |
| 250 | if (!method) |
| 251 | return 0; |
| 252 | |
| 253 | ImplicitParamDecl *self = method->getSelfDecl(); |
| 254 | assert(self && "capturing 'self' in non-definition?"); |
| 255 | |
| 256 | // Mark that we're closing on 'this' in all the block scopes, if applicable. |
| 257 | for (unsigned idx = FunctionScopes.size() - 1; |
| 258 | isa<BlockScopeInfo>(FunctionScopes[idx]); |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 259 | --idx) { |
| 260 | BlockScopeInfo *blockScope = cast<BlockScopeInfo>(FunctionScopes[idx]); |
| 261 | unsigned &captureIndex = blockScope->CaptureMap[self]; |
| 262 | if (captureIndex) break; |
| 263 | |
| 264 | bool nested = isa<BlockScopeInfo>(FunctionScopes[idx-1]); |
| 265 | blockScope->Captures.push_back( |
| 266 | BlockDecl::Capture(self, /*byref*/ false, nested, /*copy*/ 0)); |
| 267 | captureIndex = blockScope->Captures.size(); // +1 |
| 268 | } |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 269 | |
| 270 | return method; |
| 271 | } |
| 272 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 273 | QualType Sema::getMessageSendResultType(QualType ReceiverType, |
| 274 | ObjCMethodDecl *Method, |
| 275 | bool isClassMessage, bool isSuperMessage) { |
| 276 | assert(Method && "Must have a method"); |
| 277 | if (!Method->hasRelatedResultType()) |
| 278 | return Method->getSendResultType(); |
| 279 | |
| 280 | // If a method has a related return type: |
| 281 | // - if the method found is an instance method, but the message send |
| 282 | // was a class message send, T is the declared return type of the method |
| 283 | // found |
| 284 | if (Method->isInstanceMethod() && isClassMessage) |
| 285 | return Method->getSendResultType(); |
| 286 | |
| 287 | // - if the receiver is super, T is a pointer to the class of the |
| 288 | // enclosing method definition |
| 289 | if (isSuperMessage) { |
| 290 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 291 | if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface()) |
| 292 | return Context.getObjCObjectPointerType( |
| 293 | Context.getObjCInterfaceType(Class)); |
| 294 | } |
| 295 | |
| 296 | // - if the receiver is the name of a class U, T is a pointer to U |
| 297 | if (ReceiverType->getAs<ObjCInterfaceType>() || |
| 298 | ReceiverType->isObjCQualifiedInterfaceType()) |
| 299 | return Context.getObjCObjectPointerType(ReceiverType); |
| 300 | // - if the receiver is of type Class or qualified Class type, |
| 301 | // T is the declared return type of the method. |
| 302 | if (ReceiverType->isObjCClassType() || |
| 303 | ReceiverType->isObjCQualifiedClassType()) |
| 304 | return Method->getSendResultType(); |
| 305 | |
| 306 | // - if the receiver is id, qualified id, Class, or qualified Class, T |
| 307 | // is the receiver type, otherwise |
| 308 | // - T is the type of the receiver expression. |
| 309 | return ReceiverType; |
| 310 | } |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 311 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 312 | void Sema::EmitRelatedResultTypeNote(const Expr *E) { |
| 313 | E = E->IgnoreParenImpCasts(); |
| 314 | const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E); |
| 315 | if (!MsgSend) |
| 316 | return; |
| 317 | |
| 318 | const ObjCMethodDecl *Method = MsgSend->getMethodDecl(); |
| 319 | if (!Method) |
| 320 | return; |
| 321 | |
| 322 | if (!Method->hasRelatedResultType()) |
| 323 | return; |
| 324 | |
| 325 | if (Context.hasSameUnqualifiedType(Method->getResultType() |
| 326 | .getNonReferenceType(), |
| 327 | MsgSend->getType())) |
| 328 | return; |
| 329 | |
| 330 | Diag(Method->getLocation(), diag::note_related_result_type_inferred) |
| 331 | << Method->isInstanceMethod() << Method->getSelector() |
| 332 | << MsgSend->getType(); |
| 333 | } |
| 334 | |
| 335 | bool Sema::CheckMessageArgumentTypes(QualType ReceiverType, |
| 336 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 337 | Selector Sel, ObjCMethodDecl *Method, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 338 | bool isClassMessage, bool isSuperMessage, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 339 | SourceLocation lbrac, SourceLocation rbrac, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 340 | QualType &ReturnType, ExprValueKind &VK) { |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 341 | if (!Method) { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 342 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 343 | for (unsigned i = 0; i != NumArgs; i++) { |
| 344 | if (Args[i]->isTypeDependent()) |
| 345 | continue; |
| 346 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 347 | ExprResult Result = DefaultArgumentPromotion(Args[i]); |
| 348 | if (Result.isInvalid()) |
| 349 | return true; |
| 350 | Args[i] = Result.take(); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 351 | } |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 352 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 353 | unsigned DiagID; |
| 354 | if (getLangOptions().ObjCAutoRefCount) |
| 355 | DiagID = diag::err_arc_method_not_found; |
| 356 | else |
| 357 | DiagID = isClassMessage ? diag::warn_class_method_not_found |
| 358 | : diag::warn_inst_method_not_found; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 359 | Diag(lbrac, DiagID) |
| 360 | << Sel << isClassMessage << SourceRange(lbrac, rbrac); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 361 | |
| 362 | // In debuggers, we want to use __unknown_anytype for these |
| 363 | // results so that clients can cast them. |
| 364 | if (getLangOptions().DebuggerSupport) { |
| 365 | ReturnType = Context.UnknownAnyTy; |
| 366 | } else { |
| 367 | ReturnType = Context.getObjCIdType(); |
| 368 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 369 | VK = VK_RValue; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 370 | return false; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 371 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 373 | ReturnType = getMessageSendResultType(ReceiverType, Method, isClassMessage, |
| 374 | isSuperMessage); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 375 | VK = Expr::getValueKindForType(Method->getResultType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 376 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 377 | unsigned NumNamedArgs = Sel.getNumArgs(); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 378 | // Method might have more arguments than selector indicates. This is due |
| 379 | // to addition of c-style arguments in method. |
| 380 | if (Method->param_size() > Sel.getNumArgs()) |
| 381 | NumNamedArgs = Method->param_size(); |
| 382 | // FIXME. This need be cleaned up. |
| 383 | if (NumArgs < NumNamedArgs) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 384 | Diag(lbrac, diag::err_typecheck_call_too_few_args) |
| 385 | << 2 << NumNamedArgs << NumArgs; |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 386 | return false; |
| 387 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 389 | bool IsError = false; |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 390 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 391 | // We can't do any type-checking on a type-dependent argument. |
| 392 | if (Args[i]->isTypeDependent()) |
| 393 | continue; |
| 394 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 395 | Expr *argExpr = Args[i]; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 396 | |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 397 | ParmVarDecl *Param = Method->param_begin()[i]; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 398 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 399 | |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 400 | if (RequireCompleteType(argExpr->getSourceRange().getBegin(), |
| 401 | Param->getType(), |
| 402 | PDiag(diag::err_call_incomplete_argument) |
| 403 | << argExpr->getSourceRange())) |
| 404 | return true; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 405 | |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 406 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 407 | Param); |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 408 | ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr)); |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 409 | if (ArgE.isInvalid()) |
| 410 | IsError = true; |
| 411 | else |
| 412 | Args[i] = ArgE.takeAs<Expr>(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 413 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 414 | |
| 415 | // Promote additional arguments to variadic methods. |
| 416 | if (Method->isVariadic()) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 417 | for (unsigned i = NumNamedArgs; i < NumArgs; ++i) { |
| 418 | if (Args[i]->isTypeDependent()) |
| 419 | continue; |
| 420 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 421 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 422 | IsError |= Arg.isInvalid(); |
| 423 | Args[i] = Arg.take(); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 424 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 425 | } else { |
| 426 | // Check for extra arguments to non-variadic methods. |
| 427 | if (NumArgs != NumNamedArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | Diag(Args[NumNamedArgs]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 429 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 430 | << 2 /*method*/ << NumNamedArgs << NumArgs |
| 431 | << Method->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 432 | << SourceRange(Args[NumNamedArgs]->getLocStart(), |
| 433 | Args[NumArgs-1]->getLocEnd()); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 434 | } |
| 435 | } |
Fariborz Jahanian | 5272adf | 2011-04-15 22:06:22 +0000 | [diff] [blame] | 436 | // diagnose nonnull arguments. |
| 437 | for (specific_attr_iterator<NonNullAttr> |
| 438 | i = Method->specific_attr_begin<NonNullAttr>(), |
| 439 | e = Method->specific_attr_end<NonNullAttr>(); i != e; ++i) { |
| 440 | CheckNonNullArguments(*i, Args, lbrac); |
| 441 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 442 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 443 | DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 444 | return IsError; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 445 | } |
| 446 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 447 | bool Sema::isSelfExpr(Expr *receiver) { |
Fariborz Jahanian | f2d74cc | 2011-03-27 19:53:47 +0000 | [diff] [blame] | 448 | // 'self' is objc 'self' in an objc method only. |
Fariborz Jahanian | b460210 | 2011-03-28 16:23:34 +0000 | [diff] [blame] | 449 | DeclContext *DC = CurContext; |
| 450 | while (isa<BlockDecl>(DC)) |
| 451 | DC = DC->getParent(); |
| 452 | if (DC && !isa<ObjCMethodDecl>(DC)) |
Fariborz Jahanian | f2d74cc | 2011-03-27 19:53:47 +0000 | [diff] [blame] | 453 | return false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 454 | receiver = receiver->IgnoreParenLValueCasts(); |
| 455 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver)) |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 456 | if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self")) |
| 457 | return true; |
| 458 | return false; |
| 459 | } |
| 460 | |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 461 | // Helper method for ActOnClassMethod/ActOnInstanceMethod. |
| 462 | // Will search "local" class/category implementations for a method decl. |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 463 | // 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] | 464 | // Returns 0 if no method is found. |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 465 | ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel, |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 466 | ObjCInterfaceDecl *ClassDecl) { |
| 467 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 468 | // lookup in class and all superclasses |
| 469 | while (ClassDecl && !Method) { |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 470 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 471 | Method = ImpDecl->getClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 472 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 473 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 474 | if (!Method) |
| 475 | Method = ClassDecl->getCategoryClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 477 | // Before we give up, check if the selector is an instance method. |
| 478 | // But only in the root. This matches gcc's behaviour and what the |
| 479 | // runtime expects. |
| 480 | if (!Method && !ClassDecl->getSuperClass()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 481 | Method = ClassDecl->lookupInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 482 | // Look through local category implementations associated |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 483 | // with the root class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | if (!Method) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 485 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 486 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 487 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 488 | ClassDecl = ClassDecl->getSuperClass(); |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 489 | } |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 490 | return Method; |
| 491 | } |
| 492 | |
| 493 | ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, |
| 494 | ObjCInterfaceDecl *ClassDecl) { |
| 495 | ObjCMethodDecl *Method = 0; |
| 496 | while (ClassDecl && !Method) { |
| 497 | // If we have implementations in scope, check "private" methods. |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 498 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 499 | Method = ImpDecl->getInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 501 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 502 | if (!Method) |
| 503 | Method = ClassDecl->getCategoryInstanceMethod(Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 504 | ClassDecl = ClassDecl->getSuperClass(); |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 505 | } |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 506 | return Method; |
| 507 | } |
| 508 | |
Fariborz Jahanian | 6147806 | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 509 | /// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier |
| 510 | /// list of a qualified objective pointer type. |
| 511 | ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel, |
| 512 | const ObjCObjectPointerType *OPT, |
| 513 | bool Instance) |
| 514 | { |
| 515 | ObjCMethodDecl *MD = 0; |
| 516 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 517 | E = OPT->qual_end(); I != E; ++I) { |
| 518 | ObjCProtocolDecl *PROTO = (*I); |
| 519 | if ((MD = PROTO->lookupMethod(Sel, Instance))) { |
| 520 | return MD; |
| 521 | } |
| 522 | } |
| 523 | return 0; |
| 524 | } |
| 525 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 526 | /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an |
| 527 | /// objective C interface. This is a property reference expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 528 | ExprResult Sema:: |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 529 | HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT, |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 530 | Expr *BaseExpr, SourceLocation OpLoc, |
| 531 | DeclarationName MemberName, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 532 | SourceLocation MemberLoc, |
| 533 | SourceLocation SuperLoc, QualType SuperType, |
| 534 | bool Super) { |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 535 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 536 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Douglas Gregor | 109ec1b | 2011-04-20 18:19:55 +0000 | [diff] [blame] | 537 | |
| 538 | if (MemberName.getNameKind() != DeclarationName::Identifier) { |
| 539 | Diag(MemberLoc, diag::err_invalid_property_name) |
| 540 | << MemberName << QualType(OPT, 0); |
| 541 | return ExprError(); |
| 542 | } |
| 543 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 544 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 545 | |
Fariborz Jahanian | 8b1aba4 | 2010-12-16 00:56:28 +0000 | [diff] [blame] | 546 | if (IFace->isForwardDecl()) { |
| 547 | Diag(MemberLoc, diag::err_property_not_found_forward_class) |
| 548 | << MemberName << QualType(OPT, 0); |
| 549 | Diag(IFace->getLocation(), diag::note_forward_class); |
| 550 | return ExprError(); |
| 551 | } |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 552 | // Search for a declared property first. |
| 553 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
| 554 | // Check whether we can reference this property. |
| 555 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 556 | return ExprError(); |
| 557 | QualType ResTy = PD->getType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 558 | ResTy = ResTy.getNonLValueExprType(Context); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 559 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 560 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 561 | if (Getter && |
| 562 | (Getter->hasRelatedResultType() |
| 563 | || DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc))) |
| 564 | ResTy = getMessageSendResultType(QualType(OPT, 0), Getter, false, |
| 565 | Super); |
| 566 | |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 567 | if (Super) |
| 568 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 569 | VK_LValue, OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 570 | MemberLoc, |
| 571 | SuperLoc, SuperType)); |
| 572 | else |
| 573 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 574 | VK_LValue, OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 575 | MemberLoc, BaseExpr)); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 576 | } |
| 577 | // Check protocols on qualified interfaces. |
| 578 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 579 | E = OPT->qual_end(); I != E; ++I) |
| 580 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 581 | // Check whether we can reference this property. |
| 582 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 583 | return ExprError(); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 584 | |
| 585 | QualType T = PD->getType(); |
| 586 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 587 | T = getMessageSendResultType(QualType(OPT, 0), Getter, false, Super); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 588 | if (Super) |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 589 | return Owned(new (Context) ObjCPropertyRefExpr(PD, T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 590 | VK_LValue, |
| 591 | OK_ObjCProperty, |
| 592 | MemberLoc, |
| 593 | SuperLoc, SuperType)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 594 | else |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 595 | return Owned(new (Context) ObjCPropertyRefExpr(PD, T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 596 | VK_LValue, |
| 597 | OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 598 | MemberLoc, |
| 599 | BaseExpr)); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 600 | } |
| 601 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 602 | // selector is implemented. |
| 603 | |
| 604 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 605 | // shared with the code in ActOnInstanceMessage. |
| 606 | |
| 607 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 608 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 609 | |
| 610 | // May be founf in property's qualified list. |
| 611 | if (!Getter) |
| 612 | Getter = LookupMethodInQualifiedType(Sel, OPT, true); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 613 | |
| 614 | // If this reference is in an @implementation, check for 'private' methods. |
| 615 | if (!Getter) |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 616 | Getter = IFace->lookupPrivateMethod(Sel); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 617 | |
| 618 | // Look through local category implementations associated with the class. |
| 619 | if (!Getter) |
| 620 | Getter = IFace->getCategoryInstanceMethod(Sel); |
| 621 | if (Getter) { |
| 622 | // Check if we can reference this property. |
| 623 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 624 | return ExprError(); |
| 625 | } |
| 626 | // If we found a getter then this may be a valid dot-reference, we |
| 627 | // will look for the matching setter, in case it is needed. |
| 628 | Selector SetterSel = |
| 629 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 630 | PP.getSelectorTable(), Member); |
| 631 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 632 | |
| 633 | // May be founf in property's qualified list. |
| 634 | if (!Setter) |
| 635 | Setter = LookupMethodInQualifiedType(SetterSel, OPT, true); |
| 636 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 637 | if (!Setter) { |
| 638 | // If this reference is in an @implementation, also check for 'private' |
| 639 | // methods. |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 640 | Setter = IFace->lookupPrivateMethod(SetterSel); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 641 | } |
| 642 | // Look through local category implementations associated with the class. |
| 643 | if (!Setter) |
| 644 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 645 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 646 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 647 | return ExprError(); |
| 648 | |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 649 | if (Getter || Setter) { |
| 650 | QualType PType; |
| 651 | if (Getter) |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 652 | PType = getMessageSendResultType(QualType(OPT, 0), Getter, false, Super); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 653 | else { |
| 654 | ParmVarDecl *ArgDecl = *Setter->param_begin(); |
| 655 | PType = ArgDecl->getType(); |
| 656 | } |
| 657 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 658 | ExprValueKind VK = VK_LValue; |
| 659 | ExprObjectKind OK = OK_ObjCProperty; |
| 660 | if (!getLangOptions().CPlusPlus && !PType.hasQualifiers() && |
| 661 | PType->isVoidType()) |
| 662 | VK = VK_RValue, OK = OK_Ordinary; |
| 663 | |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 664 | if (Super) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 665 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 666 | PType, VK, OK, |
| 667 | MemberLoc, |
| 668 | SuperLoc, SuperType)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 669 | else |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 670 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 671 | PType, VK, OK, |
| 672 | MemberLoc, BaseExpr)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | // Attempt to correct for typos in property names. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 677 | TypoCorrection Corrected = CorrectTypo( |
| 678 | DeclarationNameInfo(MemberName, MemberLoc), LookupOrdinaryName, NULL, |
| 679 | NULL, IFace, false, CTC_NoKeywords, OPT); |
| 680 | if (ObjCPropertyDecl *Property = |
| 681 | Corrected.getCorrectionDeclAs<ObjCPropertyDecl>()) { |
| 682 | DeclarationName TypoResult = Corrected.getCorrection(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 683 | Diag(MemberLoc, diag::err_property_not_found_suggest) |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 684 | << MemberName << QualType(OPT, 0) << TypoResult |
| 685 | << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString()); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 686 | Diag(Property->getLocation(), diag::note_previous_decl) |
| 687 | << Property->getDeclName(); |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 688 | return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc, |
| 689 | TypoResult, MemberLoc, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 690 | SuperLoc, SuperType, Super); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 691 | } |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 692 | ObjCInterfaceDecl *ClassDeclared; |
| 693 | if (ObjCIvarDecl *Ivar = |
| 694 | IFace->lookupInstanceVariable(Member, ClassDeclared)) { |
| 695 | QualType T = Ivar->getType(); |
| 696 | if (const ObjCObjectPointerType * OBJPT = |
| 697 | T->getAsObjCInterfacePointerType()) { |
| 698 | const ObjCInterfaceType *IFaceT = OBJPT->getInterfaceType(); |
| 699 | if (ObjCInterfaceDecl *IFace = IFaceT->getDecl()) |
| 700 | if (IFace->isForwardDecl()) { |
| 701 | Diag(MemberLoc, diag::err_property_not_as_forward_class) |
Fariborz Jahanian | 2a96bf5 | 2011-02-17 17:30:05 +0000 | [diff] [blame] | 702 | << MemberName << IFace; |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 703 | Diag(IFace->getLocation(), diag::note_forward_class); |
| 704 | return ExprError(); |
| 705 | } |
| 706 | } |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 707 | Diag(MemberLoc, |
| 708 | diag::err_ivar_access_using_property_syntax_suggest) |
| 709 | << MemberName << QualType(OPT, 0) << Ivar->getDeclName() |
| 710 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| 711 | return ExprError(); |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 712 | } |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 714 | Diag(MemberLoc, diag::err_property_not_found) |
| 715 | << MemberName << QualType(OPT, 0); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 716 | if (Setter) |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 717 | Diag(Setter->getLocation(), diag::note_getter_unavailable) |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 718 | << MemberName << BaseExpr->getSourceRange(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 719 | return ExprError(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | |
| 723 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 724 | ExprResult Sema:: |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 725 | ActOnClassPropertyRefExpr(IdentifierInfo &receiverName, |
| 726 | IdentifierInfo &propertyName, |
| 727 | SourceLocation receiverNameLoc, |
| 728 | SourceLocation propertyNameLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 729 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 730 | IdentifierInfo *receiverNamePtr = &receiverName; |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 731 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr, |
| 732 | receiverNameLoc); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 733 | |
| 734 | bool IsSuper = false; |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 735 | if (IFace == 0) { |
| 736 | // If the "receiver" is 'super' in a method, handle it as an expression-like |
| 737 | // property reference. |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 738 | if (receiverNamePtr->isStr("super")) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 739 | IsSuper = true; |
| 740 | |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 741 | if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf()) { |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 742 | if (CurMethod->isInstanceMethod()) { |
| 743 | QualType T = |
| 744 | Context.getObjCInterfaceType(CurMethod->getClassInterface()); |
| 745 | T = Context.getObjCObjectPointerType(T); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 746 | |
| 747 | return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(), |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 748 | /*BaseExpr*/0, |
| 749 | SourceLocation()/*OpLoc*/, |
| 750 | &propertyName, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 751 | propertyNameLoc, |
| 752 | receiverNameLoc, T, true); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 753 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 754 | |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 755 | // Otherwise, if this is a class method, try dispatching to our |
| 756 | // superclass. |
| 757 | IFace = CurMethod->getClassInterface()->getSuperClass(); |
| 758 | } |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 759 | } |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 760 | |
| 761 | if (IFace == 0) { |
| 762 | Diag(receiverNameLoc, diag::err_expected_ident_or_lparen); |
| 763 | return ExprError(); |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | // Search for a declared property first. |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 768 | Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 769 | ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 770 | |
| 771 | // If this reference is in an @implementation, check for 'private' methods. |
| 772 | if (!Getter) |
| 773 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 774 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 775 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 776 | Getter = ImpDecl->getClassMethod(Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 777 | |
| 778 | if (Getter) { |
| 779 | // FIXME: refactor/share with ActOnMemberReference(). |
| 780 | // Check if we can reference this property. |
| 781 | if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) |
| 782 | return ExprError(); |
| 783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 784 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 785 | // Look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 786 | Selector SetterSel = |
| 787 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Steve Naroff | fdc92b7 | 2009-03-10 17:24:38 +0000 | [diff] [blame] | 788 | PP.getSelectorTable(), &propertyName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 790 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 791 | if (!Setter) { |
| 792 | // If this reference is in an @implementation, also check for 'private' |
| 793 | // methods. |
| 794 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 795 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 796 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 797 | Setter = ImpDecl->getClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 798 | } |
| 799 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 800 | if (!Setter) |
| 801 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 802 | |
| 803 | if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) |
| 804 | return ExprError(); |
| 805 | |
| 806 | if (Getter || Setter) { |
| 807 | QualType PType; |
| 808 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 809 | ExprValueKind VK = VK_LValue; |
| 810 | if (Getter) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 811 | PType = getMessageSendResultType(Context.getObjCInterfaceType(IFace), |
| 812 | Getter, true, |
| 813 | receiverNamePtr->isStr("super")); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 814 | if (!getLangOptions().CPlusPlus && |
| 815 | !PType.hasQualifiers() && PType->isVoidType()) |
| 816 | VK = VK_RValue; |
| 817 | } else { |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 818 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 819 | E = Setter->param_end(); PI != E; ++PI) |
| 820 | PType = (*PI)->getType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 821 | VK = VK_LValue; |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 822 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 823 | |
| 824 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 825 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 826 | if (IsSuper) |
| 827 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 828 | PType, VK, OK, |
| 829 | propertyNameLoc, |
| 830 | receiverNameLoc, |
| 831 | Context.getObjCInterfaceType(IFace))); |
| 832 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 833 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 834 | PType, VK, OK, |
| 835 | propertyNameLoc, |
| 836 | receiverNameLoc, IFace)); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 837 | } |
| 838 | return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) |
| 839 | << &propertyName << Context.getObjCInterfaceType(IFace)); |
| 840 | } |
| 841 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 842 | Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 843 | IdentifierInfo *Name, |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 844 | SourceLocation NameLoc, |
| 845 | bool IsSuper, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 846 | bool HasTrailingDot, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 847 | ParsedType &ReceiverType) { |
| 848 | ReceiverType = ParsedType(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 849 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 850 | // 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] | 851 | // messaging super. If the identifier is "super" and there is a |
| 852 | // trailing dot, it's an instance message. |
| 853 | if (IsSuper && S->isInObjcMethodScope()) |
| 854 | return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 855 | |
| 856 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| 857 | LookupName(Result, S); |
| 858 | |
| 859 | switch (Result.getResultKind()) { |
| 860 | case LookupResult::NotFound: |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 861 | // Normal name lookup didn't find anything. If we're in an |
| 862 | // 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] | 863 | // FIXME: This is a hack. Ivar lookup should be part of normal |
| 864 | // lookup. |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 865 | if (ObjCMethodDecl *Method = getCurMethodDecl()) { |
| 866 | ObjCInterfaceDecl *ClassDeclared; |
| 867 | if (Method->getClassInterface()->lookupInstanceVariable(Name, |
| 868 | ClassDeclared)) |
| 869 | return ObjCInstanceMessage; |
| 870 | } |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 871 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 872 | // Break out; we'll perform typo correction below. |
| 873 | break; |
| 874 | |
| 875 | case LookupResult::NotFoundInCurrentInstantiation: |
| 876 | case LookupResult::FoundOverloaded: |
| 877 | case LookupResult::FoundUnresolvedValue: |
| 878 | case LookupResult::Ambiguous: |
| 879 | Result.suppressDiagnostics(); |
| 880 | return ObjCInstanceMessage; |
| 881 | |
| 882 | case LookupResult::Found: { |
Fariborz Jahanian | 8348de3 | 2011-02-08 00:23:07 +0000 | [diff] [blame] | 883 | // If the identifier is a class or not, and there is a trailing dot, |
| 884 | // it's an instance message. |
| 885 | if (HasTrailingDot) |
| 886 | return ObjCInstanceMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 887 | // We found something. If it's a type, then we have a class |
| 888 | // message. Otherwise, it's an instance message. |
| 889 | NamedDecl *ND = Result.getFoundDecl(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 890 | QualType T; |
| 891 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 892 | T = Context.getObjCInterfaceType(Class); |
| 893 | else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| 894 | T = Context.getTypeDeclType(Type); |
| 895 | else |
| 896 | return ObjCInstanceMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 898 | // We have a class message, and T is the type we're |
| 899 | // messaging. Build source-location information for it. |
| 900 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 901 | ReceiverType = CreateParsedType(T, TSInfo); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 902 | return ObjCClassMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 906 | // Determine our typo-correction context. |
| 907 | CorrectTypoContext CTC = CTC_Expression; |
| 908 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 909 | if (Method->getClassInterface() && |
| 910 | Method->getClassInterface()->getSuperClass()) |
| 911 | CTC = CTC_ObjCMessageReceiver; |
| 912 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 913 | if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), |
| 914 | Result.getLookupKind(), S, NULL, |
| 915 | NULL, false, CTC)) { |
| 916 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 917 | // If we found a declaration, correct when it refers to an Objective-C |
| 918 | // class. |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 919 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 920 | Diag(NameLoc, diag::err_unknown_receiver_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 921 | << Name << Corrected.getCorrection() |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 922 | << FixItHint::CreateReplacement(SourceRange(NameLoc), |
| 923 | ND->getNameAsString()); |
| 924 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 925 | << Corrected.getCorrection(); |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 926 | |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 927 | QualType T = Context.getObjCInterfaceType(Class); |
| 928 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 929 | ReceiverType = CreateParsedType(T, TSInfo); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 930 | return ObjCClassMessage; |
| 931 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 932 | } else if (Corrected.isKeyword() && |
| 933 | Corrected.getCorrectionAsIdentifierInfo()->isStr("super")) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 934 | // If we've found the keyword "super", this is a send to super. |
| 935 | Diag(NameLoc, diag::err_unknown_receiver_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 936 | << Name << Corrected.getCorrection() |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 937 | << FixItHint::CreateReplacement(SourceRange(NameLoc), "super"); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 938 | return ObjCSuperMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 939 | } |
| 940 | } |
| 941 | |
| 942 | // Fall back: let the parser try to parse it as an instance message. |
| 943 | return ObjCInstanceMessage; |
| 944 | } |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 945 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 946 | ExprResult Sema::ActOnSuperMessage(Scope *S, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 947 | SourceLocation SuperLoc, |
| 948 | Selector Sel, |
| 949 | SourceLocation LBracLoc, |
| 950 | SourceLocation SelectorLoc, |
| 951 | SourceLocation RBracLoc, |
| 952 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 953 | // Determine whether we are inside a method or not. |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 954 | ObjCMethodDecl *Method = tryCaptureObjCSelf(); |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 955 | if (!Method) { |
| 956 | Diag(SuperLoc, diag::err_invalid_receiver_to_message_super); |
| 957 | return ExprError(); |
| 958 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 959 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 960 | ObjCInterfaceDecl *Class = Method->getClassInterface(); |
| 961 | if (!Class) { |
| 962 | Diag(SuperLoc, diag::error_no_super_class_message) |
| 963 | << Method->getDeclName(); |
| 964 | return ExprError(); |
| 965 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 966 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 967 | ObjCInterfaceDecl *Super = Class->getSuperClass(); |
| 968 | if (!Super) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 969 | // The current class does not have a superclass. |
Ted Kremenek | e00909a | 2011-01-23 17:21:34 +0000 | [diff] [blame] | 970 | Diag(SuperLoc, diag::error_root_class_cannot_use_super) |
| 971 | << Class->getIdentifier(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 972 | return ExprError(); |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 973 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 974 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 975 | // We are in a method whose class has a superclass, so 'super' |
| 976 | // is acting as a keyword. |
| 977 | if (Method->isInstanceMethod()) { |
| 978 | // Since we are in an instance method, this is an instance |
| 979 | // message to the superclass instance. |
| 980 | QualType SuperTy = Context.getObjCInterfaceType(Super); |
| 981 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 982 | return BuildInstanceMessage(0, SuperTy, SuperLoc, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 983 | Sel, /*Method=*/0, |
| 984 | LBracLoc, SelectorLoc, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 985 | } |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 986 | |
| 987 | // Since we are in a class method, this is a class message to |
| 988 | // the superclass. |
| 989 | return BuildClassMessage(/*ReceiverTypeInfo=*/0, |
| 990 | Context.getObjCInterfaceType(Super), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 991 | SuperLoc, Sel, /*Method=*/0, |
| 992 | LBracLoc, SelectorLoc, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | /// \brief Build an Objective-C class message expression. |
| 996 | /// |
| 997 | /// This routine takes care of both normal class messages and |
| 998 | /// class messages to the superclass. |
| 999 | /// |
| 1000 | /// \param ReceiverTypeInfo Type source information that describes the |
| 1001 | /// receiver of this message. This may be NULL, in which case we are |
| 1002 | /// sending to the superclass and \p SuperLoc must be a valid source |
| 1003 | /// location. |
| 1004 | |
| 1005 | /// \param ReceiverType The type of the object receiving the |
| 1006 | /// message. When \p ReceiverTypeInfo is non-NULL, this is the same |
| 1007 | /// type as that refers to. For a superclass send, this is the type of |
| 1008 | /// the superclass. |
| 1009 | /// |
| 1010 | /// \param SuperLoc The location of the "super" keyword in a |
| 1011 | /// superclass message. |
| 1012 | /// |
| 1013 | /// \param Sel The selector to which the message is being sent. |
| 1014 | /// |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1015 | /// \param Method The method that this class message is invoking, if |
| 1016 | /// already known. |
| 1017 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1018 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 1019 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1020 | /// \param RBrac The location of the closing square bracket ']'. |
| 1021 | /// |
| 1022 | /// \param Args The message arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1023 | ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1024 | QualType ReceiverType, |
| 1025 | SourceLocation SuperLoc, |
| 1026 | Selector Sel, |
| 1027 | ObjCMethodDecl *Method, |
| 1028 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1029 | SourceLocation SelectorLoc, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1030 | SourceLocation RBracLoc, |
| 1031 | MultiExprArg ArgsIn) { |
| 1032 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 1033 | : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin(); |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1034 | if (LBracLoc.isInvalid()) { |
| 1035 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 1036 | << FixItHint::CreateInsertion(Loc, "["); |
| 1037 | LBracLoc = Loc; |
| 1038 | } |
| 1039 | |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1040 | if (ReceiverType->isDependentType()) { |
| 1041 | // If the receiver type is dependent, we can't type-check anything |
| 1042 | // at this point. Build a dependent expression. |
| 1043 | unsigned NumArgs = ArgsIn.size(); |
| 1044 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 1045 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1046 | return Owned(ObjCMessageExpr::Create(Context, ReceiverType, |
| 1047 | VK_RValue, LBracLoc, ReceiverTypeInfo, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1048 | Sel, SelectorLoc, /*Method=*/0, |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1049 | Args, NumArgs, RBracLoc)); |
| 1050 | } |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 1051 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1052 | // Find the class to which we are sending this message. |
| 1053 | ObjCInterfaceDecl *Class = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1054 | const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>(); |
| 1055 | if (!ClassType || !(Class = ClassType->getInterface())) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1056 | Diag(Loc, diag::err_invalid_receiver_class_message) |
| 1057 | << ReceiverType; |
| 1058 | return ExprError(); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 1059 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1060 | assert(Class && "We don't know which class we're messaging?"); |
Fariborz Jahanian | 02b0d65 | 2011-03-08 19:12:46 +0000 | [diff] [blame] | 1061 | (void)DiagnoseUseOfDecl(Class, Loc); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1062 | // Find the method we are messaging. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1063 | if (!Method) { |
| 1064 | if (Class->isForwardDecl()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1065 | if (getLangOptions().ObjCAutoRefCount) { |
| 1066 | Diag(Loc, diag::err_arc_receiver_forward_class) << ReceiverType; |
| 1067 | } else { |
| 1068 | Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName(); |
| 1069 | } |
| 1070 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1071 | // A forward class used in messaging is treated as a 'Class' |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1072 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 1073 | SourceRange(LBracLoc, RBracLoc)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1074 | if (Method && !getLangOptions().ObjCAutoRefCount) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1075 | Diag(Method->getLocation(), diag::note_method_sent_forward_class) |
| 1076 | << Method->getDeclName(); |
| 1077 | } |
| 1078 | if (!Method) |
| 1079 | Method = Class->lookupClassMethod(Sel); |
| 1080 | |
| 1081 | // If we have an implementation in scope, check "private" methods. |
| 1082 | if (!Method) |
| 1083 | Method = LookupPrivateClassMethod(Sel, Class); |
| 1084 | |
| 1085 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 1086 | return ExprError(); |
Fariborz Jahanian | 89bc314 | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 1087 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1088 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1089 | // Check the argument types and determine the result type. |
| 1090 | QualType ReturnType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1091 | ExprValueKind VK = VK_RValue; |
| 1092 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1093 | unsigned NumArgs = ArgsIn.size(); |
| 1094 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1095 | if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, true, |
| 1096 | SuperLoc.isValid(), LBracLoc, RBracLoc, |
| 1097 | ReturnType, VK)) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1098 | return ExprError(); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1099 | |
Douglas Gregor | 483dd2f | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 1100 | if (Method && !Method->getResultType()->isVoidType() && |
| 1101 | RequireCompleteType(LBracLoc, Method->getResultType(), |
| 1102 | diag::err_illegal_message_expr_incomplete_type)) |
| 1103 | return ExprError(); |
| 1104 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1105 | // Construct the appropriate ObjCMessageExpr. |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1106 | Expr *Result; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1107 | if (SuperLoc.isValid()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1108 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1109 | SuperLoc, /*IsInstanceSuper=*/false, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1110 | ReceiverType, Sel, SelectorLoc, |
| 1111 | Method, Args, NumArgs, RBracLoc); |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1112 | else |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1113 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1114 | ReceiverTypeInfo, Sel, SelectorLoc, |
| 1115 | Method, Args, NumArgs, RBracLoc); |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1116 | return MaybeBindToTemporary(Result); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1119 | // ActOnClassMessage - used for both unary and keyword messages. |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1120 | // ArgExprs is optional - if it is present, the number of expressions |
| 1121 | // is obtained from Sel.getNumArgs(). |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1122 | ExprResult Sema::ActOnClassMessage(Scope *S, |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 1123 | ParsedType Receiver, |
| 1124 | Selector Sel, |
| 1125 | SourceLocation LBracLoc, |
| 1126 | SourceLocation SelectorLoc, |
| 1127 | SourceLocation RBracLoc, |
| 1128 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1129 | TypeSourceInfo *ReceiverTypeInfo; |
| 1130 | QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo); |
| 1131 | if (ReceiverType.isNull()) |
| 1132 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1134 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1135 | if (!ReceiverTypeInfo) |
| 1136 | ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc); |
| 1137 | |
| 1138 | return BuildClassMessage(ReceiverTypeInfo, ReceiverType, |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1139 | /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1140 | LBracLoc, SelectorLoc, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /// \brief Build an Objective-C instance message expression. |
| 1144 | /// |
| 1145 | /// This routine takes care of both normal instance messages and |
| 1146 | /// instance messages to the superclass instance. |
| 1147 | /// |
| 1148 | /// \param Receiver The expression that computes the object that will |
| 1149 | /// receive this message. This may be empty, in which case we are |
| 1150 | /// sending to the superclass instance and \p SuperLoc must be a valid |
| 1151 | /// source location. |
| 1152 | /// |
| 1153 | /// \param ReceiverType The (static) type of the object receiving the |
| 1154 | /// message. When a \p Receiver expression is provided, this is the |
| 1155 | /// same type as that expression. For a superclass instance send, this |
| 1156 | /// is a pointer to the type of the superclass. |
| 1157 | /// |
| 1158 | /// \param SuperLoc The location of the "super" keyword in a |
| 1159 | /// superclass instance message. |
| 1160 | /// |
| 1161 | /// \param Sel The selector to which the message is being sent. |
| 1162 | /// |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1163 | /// \param Method The method that this instance message is invoking, if |
| 1164 | /// already known. |
| 1165 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1166 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 1167 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1168 | /// \param RBrac The location of the closing square bracket ']'. |
| 1169 | /// |
| 1170 | /// \param Args The message arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1171 | ExprResult Sema::BuildInstanceMessage(Expr *Receiver, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1172 | QualType ReceiverType, |
| 1173 | SourceLocation SuperLoc, |
| 1174 | Selector Sel, |
| 1175 | ObjCMethodDecl *Method, |
| 1176 | SourceLocation LBracLoc, |
| 1177 | SourceLocation SelectorLoc, |
| 1178 | SourceLocation RBracLoc, |
| 1179 | MultiExprArg ArgsIn) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1180 | // The location of the receiver. |
| 1181 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart(); |
| 1182 | |
| 1183 | if (LBracLoc.isInvalid()) { |
| 1184 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 1185 | << FixItHint::CreateInsertion(Loc, "["); |
| 1186 | LBracLoc = Loc; |
| 1187 | } |
| 1188 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1189 | // If we have a receiver expression, perform appropriate promotions |
| 1190 | // and determine receiver type. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1191 | if (Receiver) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1192 | if (Receiver->isTypeDependent()) { |
| 1193 | // If the receiver is type-dependent, we can't type-check anything |
| 1194 | // at this point. Build a dependent expression. |
| 1195 | unsigned NumArgs = ArgsIn.size(); |
| 1196 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 1197 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
| 1198 | return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1199 | VK_RValue, LBracLoc, Receiver, Sel, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1200 | SelectorLoc, /*Method=*/0, |
| 1201 | Args, NumArgs, RBracLoc)); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1202 | } |
| 1203 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1204 | // If necessary, apply function/array conversion to the receiver. |
| 1205 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1206 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver); |
| 1207 | if (Result.isInvalid()) |
| 1208 | return ExprError(); |
| 1209 | Receiver = Result.take(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1210 | ReceiverType = Receiver->getType(); |
| 1211 | } |
| 1212 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1213 | if (!Method) { |
| 1214 | // Handle messages to id. |
Fariborz Jahanian | ba55198 | 2010-08-10 18:10:50 +0000 | [diff] [blame] | 1215 | bool receiverIsId = ReceiverType->isObjCIdType(); |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1216 | if (receiverIsId || ReceiverType->isBlockPointerType() || |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1217 | (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) { |
| 1218 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1219 | SourceRange(LBracLoc, RBracLoc), |
| 1220 | receiverIsId); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1221 | if (!Method) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1222 | Method = LookupFactoryMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1223 | SourceRange(LBracLoc, RBracLoc), |
| 1224 | receiverIsId); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1225 | } else if (ReceiverType->isObjCClassType() || |
| 1226 | ReceiverType->isObjCQualifiedClassType()) { |
| 1227 | // Handle messages to Class. |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 1228 | // We allow sending a message to a qualified Class ("Class<foo>"), which |
| 1229 | // is ok as long as one of the protocols implements the selector (if not, warn). |
| 1230 | if (const ObjCObjectPointerType *QClassTy |
| 1231 | = ReceiverType->getAsObjCQualifiedClassType()) { |
| 1232 | // Search protocols for class methods. |
| 1233 | Method = LookupMethodInQualifiedType(Sel, QClassTy, false); |
| 1234 | if (!Method) { |
| 1235 | Method = LookupMethodInQualifiedType(Sel, QClassTy, true); |
| 1236 | // warn if instance method found for a Class message. |
| 1237 | if (Method) { |
| 1238 | Diag(Loc, diag::warn_instance_method_on_class_found) |
| 1239 | << Method->getSelector() << Sel; |
| 1240 | Diag(Method->getLocation(), diag::note_method_declared_at); |
| 1241 | } |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 1242 | } |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 1243 | } else { |
| 1244 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 1245 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { |
| 1246 | // First check the public methods in the class interface. |
| 1247 | Method = ClassDecl->lookupClassMethod(Sel); |
| 1248 | |
| 1249 | if (!Method) |
| 1250 | Method = LookupPrivateClassMethod(Sel, ClassDecl); |
| 1251 | } |
| 1252 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 1253 | return ExprError(); |
| 1254 | } |
| 1255 | if (!Method) { |
| 1256 | // If not messaging 'self', look for any factory method named 'Sel'. |
| 1257 | if (!Receiver || !isSelfExpr(Receiver)) { |
| 1258 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 1259 | SourceRange(LBracLoc, RBracLoc), |
| 1260 | true); |
| 1261 | if (!Method) { |
| 1262 | // If no class (factory) method was found, check if an _instance_ |
| 1263 | // method of the same name exists in the root class only. |
| 1264 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 1265 | SourceRange(LBracLoc, RBracLoc), |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 1266 | true); |
| 1267 | if (Method) |
| 1268 | if (const ObjCInterfaceDecl *ID = |
| 1269 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { |
| 1270 | if (ID->getSuperClass()) |
| 1271 | Diag(Loc, diag::warn_root_inst_method_not_found) |
| 1272 | << Sel << SourceRange(LBracLoc, RBracLoc); |
| 1273 | } |
| 1274 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1278 | } else { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1279 | ObjCInterfaceDecl* ClassDecl = 0; |
| 1280 | |
| 1281 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 1282 | // long as one of the protocols implements the selector (if not, warn). |
| 1283 | if (const ObjCObjectPointerType *QIdTy |
| 1284 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 1285 | // Search protocols for instance methods. |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1286 | Method = LookupMethodInQualifiedType(Sel, QIdTy, true); |
| 1287 | if (!Method) |
| 1288 | Method = LookupMethodInQualifiedType(Sel, QIdTy, false); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1289 | } else if (const ObjCObjectPointerType *OCIType |
| 1290 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 1291 | // We allow sending a message to a pointer to an interface (an object). |
| 1292 | ClassDecl = OCIType->getInterfaceDecl(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1293 | |
| 1294 | if (ClassDecl->isForwardDecl() && getLangOptions().ObjCAutoRefCount) { |
| 1295 | Diag(Loc, diag::err_arc_receiver_forward_instance) |
| 1296 | << OCIType->getPointeeType() |
| 1297 | << (Receiver ? Receiver->getSourceRange() : SourceRange(SuperLoc)); |
| 1298 | return ExprError(); |
| 1299 | } |
| 1300 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1301 | // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be |
| 1302 | // faster than the following method (which can do *many* linear searches). |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 1303 | // The idea is to add class info to MethodPool. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1304 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 1305 | |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1306 | if (!Method) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1307 | // Search protocol qualifiers. |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1308 | Method = LookupMethodInQualifiedType(Sel, OCIType, true); |
| 1309 | |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 1310 | const ObjCInterfaceDecl *forwardClass = 0; |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1311 | if (!Method) { |
| 1312 | // If we have implementations in scope, check "private" methods. |
| 1313 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 1314 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1315 | if (!Method && getLangOptions().ObjCAutoRefCount) { |
| 1316 | Diag(Loc, diag::err_arc_may_not_respond) |
| 1317 | << OCIType->getPointeeType() << Sel; |
| 1318 | return ExprError(); |
| 1319 | } |
| 1320 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1321 | if (!Method && (!Receiver || !isSelfExpr(Receiver))) { |
| 1322 | // If we still haven't found a method, look in the global pool. This |
| 1323 | // behavior isn't very desirable, however we need it for GCC |
| 1324 | // compatibility. FIXME: should we deviate?? |
| 1325 | if (OCIType->qual_empty()) { |
| 1326 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 1327 | SourceRange(LBracLoc, RBracLoc)); |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 1328 | if (OCIType->getInterfaceDecl()->isForwardDecl()) |
| 1329 | forwardClass = OCIType->getInterfaceDecl(); |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 1330 | if (Method && !forwardClass) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1331 | Diag(Loc, diag::warn_maynot_respond) |
| 1332 | << OCIType->getInterfaceDecl()->getIdentifier() << Sel; |
| 1333 | } |
| 1334 | } |
| 1335 | } |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 1336 | if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass)) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1337 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1338 | } else if (!getLangOptions().ObjCAutoRefCount && |
| 1339 | !Context.getObjCIdType().isNull() && |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 1340 | (ReceiverType->isPointerType() || |
| 1341 | ReceiverType->isIntegerType())) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1342 | // Implicitly convert integers and pointers to 'id' but emit a warning. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1343 | // But not in ARC. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1344 | Diag(Loc, diag::warn_bad_receiver_type) |
| 1345 | << ReceiverType |
| 1346 | << Receiver->getSourceRange(); |
| 1347 | if (ReceiverType->isPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1348 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
| 1349 | CK_BitCast).take(); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1350 | else { |
| 1351 | // TODO: specialized warning on null receivers? |
| 1352 | bool IsNull = Receiver->isNullPointerConstant(Context, |
| 1353 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1354 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
| 1355 | IsNull ? CK_NullToPointer : CK_IntegralToPointer).take(); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1356 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1357 | ReceiverType = Receiver->getType(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 1358 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1359 | else { |
| 1360 | ExprResult ReceiverRes; |
| 1361 | if (getLangOptions().CPlusPlus) |
| 1362 | ReceiverRes = PerformContextuallyConvertToObjCId(Receiver); |
| 1363 | if (ReceiverRes.isUsable()) { |
| 1364 | Receiver = ReceiverRes.take(); |
| 1365 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) { |
| 1366 | Receiver = ICE->getSubExpr(); |
| 1367 | ReceiverType = Receiver->getType(); |
| 1368 | } |
| 1369 | return BuildInstanceMessage(Receiver, |
| 1370 | ReceiverType, |
| 1371 | SuperLoc, |
| 1372 | Sel, |
| 1373 | Method, |
| 1374 | LBracLoc, |
| 1375 | SelectorLoc, |
| 1376 | RBracLoc, |
| 1377 | move(ArgsIn)); |
| 1378 | } else { |
| 1379 | // Reject other random receiver types (e.g. structs). |
| 1380 | Diag(Loc, diag::err_bad_receiver_type) |
| 1381 | << ReceiverType << Receiver->getSourceRange(); |
| 1382 | return ExprError(); |
Fariborz Jahanian | 3ba6061 | 2010-05-13 17:19:25 +0000 | [diff] [blame] | 1383 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1384 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1385 | } |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 1386 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1388 | // Check the message arguments. |
| 1389 | unsigned NumArgs = ArgsIn.size(); |
| 1390 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 1391 | QualType ReturnType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1392 | ExprValueKind VK = VK_RValue; |
Fariborz Jahanian | 2600503 | 2010-12-01 01:07:24 +0000 | [diff] [blame] | 1393 | bool ClassMessage = (ReceiverType->isObjCClassType() || |
| 1394 | ReceiverType->isObjCQualifiedClassType()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1395 | if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, |
| 1396 | ClassMessage, SuperLoc.isValid(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1397 | LBracLoc, RBracLoc, ReturnType, VK)) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1398 | return ExprError(); |
Fariborz Jahanian | da59e09 | 2010-06-16 19:56:08 +0000 | [diff] [blame] | 1399 | |
Douglas Gregor | 483dd2f | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 1400 | if (Method && !Method->getResultType()->isVoidType() && |
| 1401 | RequireCompleteType(LBracLoc, Method->getResultType(), |
| 1402 | diag::err_illegal_message_expr_incomplete_type)) |
| 1403 | return ExprError(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1404 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1405 | // In ARC, forbid the user from sending messages to |
| 1406 | // retain/release/autorelease/dealloc/retainCount explicitly. |
| 1407 | if (getLangOptions().ObjCAutoRefCount) { |
| 1408 | ObjCMethodFamily family = |
| 1409 | (Method ? Method->getMethodFamily() : Sel.getMethodFamily()); |
| 1410 | switch (family) { |
| 1411 | case OMF_init: |
| 1412 | if (Method) |
| 1413 | checkInitMethod(Method, ReceiverType); |
| 1414 | |
| 1415 | case OMF_None: |
| 1416 | case OMF_alloc: |
| 1417 | case OMF_copy: |
| 1418 | case OMF_mutableCopy: |
| 1419 | case OMF_new: |
| 1420 | case OMF_self: |
| 1421 | break; |
| 1422 | |
| 1423 | case OMF_dealloc: |
| 1424 | case OMF_retain: |
| 1425 | case OMF_release: |
| 1426 | case OMF_autorelease: |
| 1427 | case OMF_retainCount: |
| 1428 | Diag(Loc, diag::err_arc_illegal_explicit_message) |
| 1429 | << Sel << SelectorLoc; |
| 1430 | break; |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 1431 | |
| 1432 | case OMF_performSelector: |
| 1433 | if (Method && NumArgs >= 1) { |
| 1434 | if (ObjCSelectorExpr *SelExp = dyn_cast<ObjCSelectorExpr>(Args[0])) { |
| 1435 | Selector ArgSel = SelExp->getSelector(); |
| 1436 | ObjCMethodDecl *SelMethod = |
| 1437 | LookupInstanceMethodInGlobalPool(ArgSel, |
| 1438 | SelExp->getSourceRange()); |
| 1439 | if (!SelMethod) |
| 1440 | SelMethod = |
| 1441 | LookupFactoryMethodInGlobalPool(ArgSel, |
| 1442 | SelExp->getSourceRange()); |
| 1443 | if (SelMethod) { |
| 1444 | ObjCMethodFamily SelFamily = SelMethod->getMethodFamily(); |
| 1445 | switch (SelFamily) { |
| 1446 | case OMF_alloc: |
| 1447 | case OMF_copy: |
| 1448 | case OMF_mutableCopy: |
| 1449 | case OMF_new: |
| 1450 | case OMF_self: |
| 1451 | case OMF_init: |
| 1452 | // Issue error, unless ns_returns_not_retained. |
| 1453 | if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 1454 | // selector names a +1 method |
| 1455 | Diag(SelectorLoc, |
| 1456 | diag::err_arc_perform_selector_retains); |
| 1457 | Diag(SelMethod->getLocation(), diag::note_method_declared_at); |
| 1458 | } |
| 1459 | break; |
| 1460 | default: |
| 1461 | // +0 call. OK. unless ns_returns_retained. |
| 1462 | if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) { |
| 1463 | // selector names a +1 method |
| 1464 | Diag(SelectorLoc, |
| 1465 | diag::err_arc_perform_selector_retains); |
| 1466 | Diag(SelMethod->getLocation(), diag::note_method_declared_at); |
| 1467 | } |
| 1468 | break; |
| 1469 | } |
| 1470 | } |
| 1471 | } else { |
| 1472 | // error (may leak). |
| 1473 | Diag(SelectorLoc, diag::warn_arc_perform_selector_leaks); |
| 1474 | Diag(Args[0]->getExprLoc(), diag::note_used_here); |
| 1475 | } |
| 1476 | } |
| 1477 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1481 | // Construct the appropriate ObjCMessageExpr instance. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1482 | ObjCMessageExpr *Result; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1483 | if (SuperLoc.isValid()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1484 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1485 | SuperLoc, /*IsInstanceSuper=*/true, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1486 | ReceiverType, Sel, SelectorLoc, Method, |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1487 | Args, NumArgs, RBracLoc); |
| 1488 | else |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1489 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1490 | Receiver, Sel, SelectorLoc, Method, |
| 1491 | Args, NumArgs, RBracLoc); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1492 | |
| 1493 | if (getLangOptions().ObjCAutoRefCount) { |
| 1494 | // In ARC, annotate delegate init calls. |
| 1495 | if (Result->getMethodFamily() == OMF_init && |
| 1496 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
| 1497 | // Only consider init calls *directly* in init implementations, |
| 1498 | // not within blocks. |
| 1499 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext); |
| 1500 | if (method && method->getMethodFamily() == OMF_init) { |
| 1501 | // The implicit assignment to self means we also don't want to |
| 1502 | // consume the result. |
| 1503 | Result->setDelegateInitCall(true); |
| 1504 | return Owned(Result); |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | // In ARC, check for message sends which are likely to introduce |
| 1509 | // retain cycles. |
| 1510 | checkRetainCycles(Result); |
| 1511 | } |
| 1512 | |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1513 | return MaybeBindToTemporary(Result); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 1517 | // ArgExprs is optional - if it is present, the number of expressions |
| 1518 | // is obtained from Sel.getNumArgs(). |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1519 | ExprResult Sema::ActOnInstanceMessage(Scope *S, |
| 1520 | Expr *Receiver, |
| 1521 | Selector Sel, |
| 1522 | SourceLocation LBracLoc, |
| 1523 | SourceLocation SelectorLoc, |
| 1524 | SourceLocation RBracLoc, |
| 1525 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1526 | if (!Receiver) |
| 1527 | return ExprError(); |
| 1528 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1529 | return BuildInstanceMessage(Receiver, Receiver->getType(), |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1530 | /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1531 | LBracLoc, SelectorLoc, RBracLoc, move(Args)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1532 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1533 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1534 | enum ARCConversionTypeClass { |
| 1535 | ACTC_none, |
| 1536 | ACTC_retainable, |
| 1537 | ACTC_indirectRetainable |
| 1538 | }; |
| 1539 | static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) { |
| 1540 | ARCConversionTypeClass ACTC = ACTC_retainable; |
| 1541 | |
| 1542 | // Ignore an outermost reference type. |
| 1543 | if (const ReferenceType *ref = type->getAs<ReferenceType>()) |
| 1544 | type = ref->getPointeeType(); |
| 1545 | |
| 1546 | // Drill through pointers and arrays recursively. |
| 1547 | while (true) { |
| 1548 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 1549 | type = ptr->getPointeeType(); |
| 1550 | } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) { |
| 1551 | type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0); |
| 1552 | } else { |
| 1553 | break; |
| 1554 | } |
| 1555 | ACTC = ACTC_indirectRetainable; |
| 1556 | } |
| 1557 | |
| 1558 | if (!type->isObjCRetainableType()) return ACTC_none; |
| 1559 | return ACTC; |
| 1560 | } |
| 1561 | |
| 1562 | namespace { |
| 1563 | /// Return true if the given expression can be reasonably converted |
| 1564 | /// between a retainable pointer type and a C pointer type. |
| 1565 | struct ARCCastChecker : StmtVisitor<ARCCastChecker, bool> { |
| 1566 | ASTContext &Context; |
| 1567 | ARCCastChecker(ASTContext &Context) : Context(Context) {} |
| 1568 | bool VisitStmt(Stmt *s) { |
| 1569 | return false; |
| 1570 | } |
| 1571 | bool VisitExpr(Expr *e) { |
| 1572 | return e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull); |
| 1573 | } |
| 1574 | |
| 1575 | bool VisitParenExpr(ParenExpr *e) { |
| 1576 | return Visit(e->getSubExpr()); |
| 1577 | } |
| 1578 | bool VisitCastExpr(CastExpr *e) { |
| 1579 | switch (e->getCastKind()) { |
| 1580 | case CK_NullToPointer: |
| 1581 | return true; |
| 1582 | case CK_NoOp: |
| 1583 | case CK_LValueToRValue: |
| 1584 | case CK_BitCast: |
| 1585 | case CK_AnyPointerToObjCPointerCast: |
| 1586 | case CK_AnyPointerToBlockPointerCast: |
| 1587 | return Visit(e->getSubExpr()); |
| 1588 | default: |
| 1589 | return false; |
| 1590 | } |
| 1591 | } |
| 1592 | bool VisitUnaryExtension(UnaryOperator *e) { |
| 1593 | return Visit(e->getSubExpr()); |
| 1594 | } |
| 1595 | bool VisitBinComma(BinaryOperator *e) { |
| 1596 | return Visit(e->getRHS()); |
| 1597 | } |
| 1598 | bool VisitConditionalOperator(ConditionalOperator *e) { |
| 1599 | // Conditional operators are okay if both sides are okay. |
| 1600 | return Visit(e->getTrueExpr()) && Visit(e->getFalseExpr()); |
| 1601 | } |
| 1602 | bool VisitObjCStringLiteral(ObjCStringLiteral *e) { |
| 1603 | // Always white-list Objective-C string literals. |
| 1604 | return true; |
| 1605 | } |
| 1606 | bool VisitStmtExpr(StmtExpr *e) { |
| 1607 | return Visit(e->getSubStmt()->body_back()); |
| 1608 | } |
| 1609 | bool VisitDeclRefExpr(DeclRefExpr *e) { |
| 1610 | // White-list references to global extern strings from system |
| 1611 | // headers. |
| 1612 | if (VarDecl *var = dyn_cast<VarDecl>(e->getDecl())) |
| 1613 | if (var->getStorageClass() == SC_Extern && |
| 1614 | var->getType().isConstQualified() && |
| 1615 | Context.getSourceManager().isInSystemHeader(var->getLocation())) |
| 1616 | return true; |
| 1617 | return false; |
| 1618 | } |
| 1619 | }; |
| 1620 | } |
| 1621 | |
Fariborz Jahanian | 1522a7c | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 1622 | bool |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1623 | Sema::ValidObjCARCNoBridgeCastExpr(Expr *&Exp, QualType castType) { |
Fariborz Jahanian | c8505ad | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 1624 | Expr *NewExp = Exp->IgnoreParenCasts(); |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1625 | |
Fariborz Jahanian | c8505ad | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 1626 | if (!isa<ObjCMessageExpr>(NewExp) && !isa<ObjCPropertyRefExpr>(NewExp) |
| 1627 | && !isa<CallExpr>(NewExp)) |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1628 | return false; |
| 1629 | ObjCMethodDecl *method = 0; |
Fariborz Jahanian | c8505ad | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 1630 | bool MethodReturnsPlusOne = false; |
| 1631 | |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1632 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(NewExp)) { |
| 1633 | method = PRE->getExplicitProperty()->getGetterMethodDecl(); |
| 1634 | } |
Fariborz Jahanian | c8505ad | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 1635 | else if (ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(NewExp)) |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1636 | method = ME->getMethodDecl(); |
Fariborz Jahanian | c8505ad | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 1637 | else { |
| 1638 | CallExpr *CE = cast<CallExpr>(NewExp); |
| 1639 | Decl *CallDecl = CE->getCalleeDecl(); |
| 1640 | if (!CallDecl) |
| 1641 | return false; |
| 1642 | if (CallDecl->hasAttr<CFReturnsNotRetainedAttr>()) |
| 1643 | return true; |
| 1644 | MethodReturnsPlusOne = CallDecl->hasAttr<CFReturnsRetainedAttr>(); |
| 1645 | if (!MethodReturnsPlusOne) { |
| 1646 | if (NamedDecl *ND = dyn_cast<NamedDecl>(CallDecl)) |
| 1647 | if (const IdentifierInfo *Id = ND->getIdentifier()) |
| 1648 | if (Id->isStr("__builtin___CFStringMakeConstantString")) |
| 1649 | return true; |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1650 | } |
| 1651 | } |
Fariborz Jahanian | c8505ad | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 1652 | |
| 1653 | if (!MethodReturnsPlusOne) { |
| 1654 | if (!method) |
| 1655 | return false; |
| 1656 | if (method->hasAttr<CFReturnsNotRetainedAttr>()) |
| 1657 | return true; |
| 1658 | MethodReturnsPlusOne = method->hasAttr<CFReturnsRetainedAttr>(); |
| 1659 | if (!MethodReturnsPlusOne) { |
| 1660 | ObjCMethodFamily family = method->getSelector().getMethodFamily(); |
| 1661 | switch (family) { |
| 1662 | case OMF_alloc: |
| 1663 | case OMF_copy: |
| 1664 | case OMF_mutableCopy: |
| 1665 | case OMF_new: |
| 1666 | MethodReturnsPlusOne = true; |
| 1667 | break; |
| 1668 | default: |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
| 1672 | } |
| 1673 | |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1674 | if (MethodReturnsPlusOne) { |
| 1675 | TypeSourceInfo *TSInfo = |
| 1676 | Context.getTrivialTypeSourceInfo(castType, SourceLocation()); |
| 1677 | ExprResult ExpRes = BuildObjCBridgedCast(SourceLocation(), OBC_BridgeTransfer, |
| 1678 | SourceLocation(), TSInfo, Exp); |
| 1679 | Exp = ExpRes.take(); |
| 1680 | } |
| 1681 | return true; |
Fariborz Jahanian | 1522a7c | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1684 | void |
| 1685 | Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType, |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1686 | Expr *&castExpr, CheckedConversionKind CCK) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1687 | QualType castExprType = castExpr->getType(); |
| 1688 | |
| 1689 | ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType); |
| 1690 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType); |
| 1691 | if (exprACTC == castACTC) return; |
Fariborz Jahanian | 8295b7b | 2011-06-22 16:36:45 +0000 | [diff] [blame] | 1692 | if (exprACTC && castType->isIntegralType(Context)) return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1693 | |
| 1694 | // Allow casts between pointers to lifetime types (e.g., __strong id*) |
| 1695 | // and pointers to void (e.g., cv void *). Casting from void* to lifetime* |
| 1696 | // must be explicit. |
| 1697 | if (const PointerType *CastPtr = castType->getAs<PointerType>()) { |
| 1698 | if (const PointerType *CastExprPtr = castExprType->getAs<PointerType>()) { |
| 1699 | QualType CastPointee = CastPtr->getPointeeType(); |
| 1700 | QualType CastExprPointee = CastExprPtr->getPointeeType(); |
| 1701 | if ((CCK != CCK_ImplicitConversion && |
| 1702 | CastPointee->isObjCIndirectLifetimeType() && |
| 1703 | CastExprPointee->isVoidType()) || |
| 1704 | (CastPointee->isVoidType() && |
| 1705 | CastExprPointee->isObjCIndirectLifetimeType())) |
| 1706 | return; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | if (ARCCastChecker(Context).Visit(castExpr)) |
| 1711 | return; |
| 1712 | |
| 1713 | SourceLocation loc = |
| 1714 | (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc()); |
| 1715 | |
| 1716 | if (makeUnavailableInSystemHeader(loc, |
| 1717 | "converts between Objective-C and C pointers in -fobjc-arc")) |
| 1718 | return; |
| 1719 | |
John McCall | 71c482c | 2011-06-17 06:50:50 +0000 | [diff] [blame] | 1720 | unsigned srcKind = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1721 | switch (exprACTC) { |
| 1722 | case ACTC_none: |
| 1723 | srcKind = (castExprType->isPointerType() ? 1 : 0); |
| 1724 | break; |
| 1725 | case ACTC_retainable: |
| 1726 | srcKind = (castExprType->isBlockPointerType() ? 2 : 3); |
| 1727 | break; |
| 1728 | case ACTC_indirectRetainable: |
| 1729 | srcKind = 4; |
| 1730 | break; |
| 1731 | } |
| 1732 | |
| 1733 | if (CCK == CCK_CStyleCast) { |
| 1734 | // Check whether this could be fixed with a bridge cast. |
| 1735 | SourceLocation AfterLParen = PP.getLocForEndOfToken(castRange.getBegin()); |
| 1736 | SourceLocation NoteLoc = AfterLParen.isValid()? AfterLParen : loc; |
| 1737 | |
| 1738 | if (castType->isObjCARCBridgableType() && |
| 1739 | castExprType->isCARCBridgableType()) { |
Fariborz Jahanian | 1522a7c | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 1740 | // explicit unbridged casts are allowed if the source of the cast is a |
| 1741 | // message sent to an objc method (or property access) |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 1742 | if (ValidObjCARCNoBridgeCastExpr(castExpr, castType)) |
Fariborz Jahanian | 1522a7c | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 1743 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1744 | Diag(loc, diag::err_arc_cast_requires_bridge) |
| 1745 | << 2 |
| 1746 | << castExprType |
| 1747 | << (castType->isBlockPointerType()? 1 : 0) |
| 1748 | << castType |
| 1749 | << castRange |
| 1750 | << castExpr->getSourceRange(); |
| 1751 | Diag(NoteLoc, diag::note_arc_bridge) |
| 1752 | << FixItHint::CreateInsertion(AfterLParen, "__bridge "); |
| 1753 | Diag(NoteLoc, diag::note_arc_bridge_transfer) |
| 1754 | << castExprType |
| 1755 | << FixItHint::CreateInsertion(AfterLParen, "__bridge_transfer "); |
| 1756 | |
| 1757 | return; |
| 1758 | } |
| 1759 | |
| 1760 | if (castType->isCARCBridgableType() && |
| 1761 | castExprType->isObjCARCBridgableType()){ |
| 1762 | Diag(loc, diag::err_arc_cast_requires_bridge) |
| 1763 | << (castExprType->isBlockPointerType()? 1 : 0) |
| 1764 | << castExprType |
| 1765 | << 2 |
| 1766 | << castType |
| 1767 | << castRange |
| 1768 | << castExpr->getSourceRange(); |
| 1769 | |
| 1770 | Diag(NoteLoc, diag::note_arc_bridge) |
| 1771 | << FixItHint::CreateInsertion(AfterLParen, "__bridge "); |
| 1772 | Diag(NoteLoc, diag::note_arc_bridge_retained) |
| 1773 | << castType |
| 1774 | << FixItHint::CreateInsertion(AfterLParen, "__bridge_retained "); |
| 1775 | return; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | Diag(loc, diag::err_arc_mismatched_cast) |
| 1780 | << (CCK != CCK_ImplicitConversion) << srcKind << castExprType << castType |
| 1781 | << castRange << castExpr->getSourceRange(); |
| 1782 | } |
| 1783 | |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 1784 | bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType, |
| 1785 | QualType exprType) { |
| 1786 | QualType canCastType = |
| 1787 | Context.getCanonicalType(castType).getUnqualifiedType(); |
| 1788 | QualType canExprType = |
| 1789 | Context.getCanonicalType(exprType).getUnqualifiedType(); |
| 1790 | if (isa<ObjCObjectPointerType>(canCastType) && |
| 1791 | castType.getObjCLifetime() == Qualifiers::OCL_Weak && |
| 1792 | canExprType->isObjCObjectPointerType()) { |
| 1793 | if (const ObjCObjectPointerType *ObjT = |
| 1794 | canExprType->getAs<ObjCObjectPointerType>()) |
| 1795 | if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) |
| 1796 | return false; |
| 1797 | } |
| 1798 | return true; |
| 1799 | } |
| 1800 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 1801 | /// Look for an ObjCReclaimReturnedObject cast and destroy it. |
| 1802 | static Expr *maybeUndoReclaimObject(Expr *e) { |
| 1803 | // For now, we just undo operands that are *immediately* reclaim |
| 1804 | // expressions, which prevents the vast majority of potential |
| 1805 | // problems here. To catch them all, we'd need to rebuild arbitrary |
| 1806 | // value-propagating subexpressions --- we can't reliably rebuild |
| 1807 | // in-place because of expression sharing. |
| 1808 | if (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) |
| 1809 | if (ice->getCastKind() == CK_ObjCReclaimReturnedObject) |
| 1810 | return ice->getSubExpr(); |
| 1811 | |
| 1812 | return e; |
| 1813 | } |
| 1814 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1815 | ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc, |
| 1816 | ObjCBridgeCastKind Kind, |
| 1817 | SourceLocation BridgeKeywordLoc, |
| 1818 | TypeSourceInfo *TSInfo, |
| 1819 | Expr *SubExpr) { |
| 1820 | QualType T = TSInfo->getType(); |
| 1821 | QualType FromType = SubExpr->getType(); |
| 1822 | |
| 1823 | bool MustConsume = false; |
| 1824 | if (T->isDependentType() || SubExpr->isTypeDependent()) { |
| 1825 | // Okay: we'll build a dependent expression type. |
| 1826 | } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) { |
| 1827 | // Casting CF -> id |
| 1828 | switch (Kind) { |
| 1829 | case OBC_Bridge: |
| 1830 | break; |
| 1831 | |
| 1832 | case OBC_BridgeRetained: |
| 1833 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 1834 | << 2 |
| 1835 | << FromType |
| 1836 | << (T->isBlockPointerType()? 1 : 0) |
| 1837 | << T |
| 1838 | << SubExpr->getSourceRange() |
| 1839 | << Kind; |
| 1840 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 1841 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge"); |
| 1842 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer) |
| 1843 | << FromType |
| 1844 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
| 1845 | "__bridge_transfer "); |
| 1846 | |
| 1847 | Kind = OBC_Bridge; |
| 1848 | break; |
| 1849 | |
| 1850 | case OBC_BridgeTransfer: |
| 1851 | // We must consume the Objective-C object produced by the cast. |
| 1852 | MustConsume = true; |
| 1853 | break; |
| 1854 | } |
| 1855 | } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) { |
| 1856 | // Okay: id -> CF |
| 1857 | switch (Kind) { |
| 1858 | case OBC_Bridge: |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 1859 | // Reclaiming a value that's going to be __bridge-casted to CF |
| 1860 | // is very dangerous, so we don't do it. |
| 1861 | SubExpr = maybeUndoReclaimObject(SubExpr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1862 | break; |
| 1863 | |
| 1864 | case OBC_BridgeRetained: |
| 1865 | // Produce the object before casting it. |
| 1866 | SubExpr = ImplicitCastExpr::Create(Context, FromType, |
| 1867 | CK_ObjCProduceObject, |
| 1868 | SubExpr, 0, VK_RValue); |
| 1869 | break; |
| 1870 | |
| 1871 | case OBC_BridgeTransfer: |
| 1872 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 1873 | << (FromType->isBlockPointerType()? 1 : 0) |
| 1874 | << FromType |
| 1875 | << 2 |
| 1876 | << T |
| 1877 | << SubExpr->getSourceRange() |
| 1878 | << Kind; |
| 1879 | |
| 1880 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 1881 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge "); |
| 1882 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained) |
| 1883 | << T |
| 1884 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge_retained "); |
| 1885 | |
| 1886 | Kind = OBC_Bridge; |
| 1887 | break; |
| 1888 | } |
| 1889 | } else { |
| 1890 | Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible) |
| 1891 | << FromType << T << Kind |
| 1892 | << SubExpr->getSourceRange() |
| 1893 | << TSInfo->getTypeLoc().getSourceRange(); |
| 1894 | return ExprError(); |
| 1895 | } |
| 1896 | |
| 1897 | Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, |
| 1898 | BridgeKeywordLoc, |
| 1899 | TSInfo, SubExpr); |
| 1900 | |
| 1901 | if (MustConsume) { |
| 1902 | ExprNeedsCleanups = true; |
| 1903 | Result = ImplicitCastExpr::Create(Context, T, CK_ObjCConsumeObject, Result, |
| 1904 | 0, VK_RValue); |
| 1905 | } |
| 1906 | |
| 1907 | return Result; |
| 1908 | } |
| 1909 | |
| 1910 | ExprResult Sema::ActOnObjCBridgedCast(Scope *S, |
| 1911 | SourceLocation LParenLoc, |
| 1912 | ObjCBridgeCastKind Kind, |
| 1913 | SourceLocation BridgeKeywordLoc, |
| 1914 | ParsedType Type, |
| 1915 | SourceLocation RParenLoc, |
| 1916 | Expr *SubExpr) { |
| 1917 | TypeSourceInfo *TSInfo = 0; |
| 1918 | QualType T = GetTypeFromParser(Type, &TSInfo); |
| 1919 | if (!TSInfo) |
| 1920 | TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc); |
| 1921 | return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo, |
| 1922 | SubExpr); |
| 1923 | } |