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