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