Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1 | //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective-C expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Lookup.h" |
John McCall | 5f1e094 | 2010-08-24 08:50:51 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Scope.h" |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 17 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Initialization.h" |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 19 | #include "clang/Analysis/DomainSpecific/CocoaConventions.h" |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 20 | #include "clang/Edit/Rewriters.h" |
| 21 | #include "clang/Edit/Commit.h" |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTContext.h" |
| 23 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 24 | #include "clang/AST/ExprObjC.h" |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 25 | #include "clang/AST/StmtVisitor.h" |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 26 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
| 29 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 30 | using namespace clang; |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 31 | using namespace sema; |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 32 | using llvm::makeArrayRef; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 33 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 34 | ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
| 35 | Expr **strings, |
| 36 | unsigned NumStrings) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 37 | StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); |
| 38 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 39 | // Most ObjC strings are formed out of a single piece. However, we *can* |
| 40 | // have strings formed out of multiple @ strings with multiple pptokens in |
| 41 | // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one |
| 42 | // StringLiteral for ObjCStringLiteral to hold onto. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 43 | StringLiteral *S = Strings[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 45 | // If we have a multi-part string, merge it all together. |
| 46 | if (NumStrings != 1) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 47 | // Concatenate objc strings. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 48 | SmallString<128> StrBuf; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 49 | SmallVector<SourceLocation, 8> StrLocs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 51 | for (unsigned i = 0; i != NumStrings; ++i) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 52 | S = Strings[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 54 | // ObjC strings can't be wide or UTF. |
| 55 | if (!S->isAscii()) { |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 56 | Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 57 | << S->getSourceRange(); |
| 58 | return true; |
| 59 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 61 | // Append the string. |
| 62 | StrBuf += S->getString(); |
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 | // Get the locations of the string tokens. |
| 65 | StrLocs.append(S->tokloc_begin(), S->tokloc_end()); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 66 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 68 | // Create the aggregate string with the appropriate content and location |
| 69 | // information. |
Jay Foad | 65aa688 | 2011-06-21 15:13:30 +0000 | [diff] [blame] | 70 | S = StringLiteral::Create(Context, StrBuf, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 71 | StringLiteral::Ascii, /*Pascal=*/false, |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 72 | Context.getPointerType(Context.CharTy), |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 73 | &StrLocs[0], StrLocs.size()); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 74 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 75 | |
| 76 | return BuildObjCStringLiteral(AtLocs[0], S); |
| 77 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 79 | ExprResult Sema::BuildObjCStringLiteral(SourceLocation AtLoc, StringLiteral *S){ |
Chris Lattner | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 80 | // Verify that this composite string is acceptable for ObjC strings. |
| 81 | if (CheckObjCString(S)) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 82 | return true; |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 83 | |
| 84 | // Initialize the constant string interface lazily. This assumes |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 85 | // the NSString interface is seen in this translation unit. Note: We |
| 86 | // don't use NSConstantString, since the runtime team considers this |
| 87 | // interface private (even though it appears in the header files). |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 88 | QualType Ty = Context.getObjCConstantStringInterface(); |
| 89 | if (!Ty.isNull()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 90 | Ty = Context.getObjCObjectPointerType(Ty); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 91 | } else if (getLangOpts().NoConstantCFStrings) { |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 92 | IdentifierInfo *NSIdent=0; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 93 | std::string StringClass(getLangOpts().ObjCConstantStringClass); |
Fariborz Jahanian | 4c73307 | 2010-10-19 17:19:29 +0000 | [diff] [blame] | 94 | |
| 95 | if (StringClass.empty()) |
| 96 | NSIdent = &Context.Idents.get("NSConstantString"); |
| 97 | else |
| 98 | NSIdent = &Context.Idents.get(StringClass); |
| 99 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 100 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc, |
Fariborz Jahanian | 8a43776 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 101 | LookupOrdinaryName); |
| 102 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 103 | Context.setObjCConstantStringInterface(StrIF); |
| 104 | Ty = Context.getObjCConstantStringInterface(); |
| 105 | Ty = Context.getObjCObjectPointerType(Ty); |
| 106 | } else { |
| 107 | // If there is no NSConstantString interface defined then treat this |
| 108 | // as error and recover from it. |
| 109 | Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent |
| 110 | << S->getSourceRange(); |
| 111 | Ty = Context.getObjCIdType(); |
| 112 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 113 | } else { |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 114 | IdentifierInfo *NSIdent = NSAPIObj->getNSClassId(NSAPI::ClassId_NSString); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 115 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLoc, |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 116 | LookupOrdinaryName); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 117 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 118 | Context.setObjCConstantStringInterface(StrIF); |
| 119 | Ty = Context.getObjCConstantStringInterface(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 120 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 121 | } else { |
Fariborz Jahanian | f64bc20 | 2012-02-23 22:51:36 +0000 | [diff] [blame] | 122 | // If there is no NSString interface defined, implicitly declare |
| 123 | // a @class NSString; and use that instead. This is to make sure |
| 124 | // type of an NSString literal is represented correctly, instead of |
| 125 | // being an 'id' type. |
| 126 | Ty = Context.getObjCNSStringType(); |
| 127 | if (Ty.isNull()) { |
| 128 | ObjCInterfaceDecl *NSStringIDecl = |
| 129 | ObjCInterfaceDecl::Create (Context, |
| 130 | Context.getTranslationUnitDecl(), |
| 131 | SourceLocation(), NSIdent, |
| 132 | 0, SourceLocation()); |
| 133 | Ty = Context.getObjCInterfaceType(NSStringIDecl); |
| 134 | Context.setObjCNSStringType(Ty); |
| 135 | } |
| 136 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 137 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 140 | return new (Context) ObjCStringLiteral(S, Ty, AtLoc); |
| 141 | } |
| 142 | |
| 143 | /// \brief Retrieve the NSNumber factory method that should be used to create |
| 144 | /// an Objective-C literal for the given type. |
| 145 | static ObjCMethodDecl *getNSNumberFactoryMethod(Sema &S, SourceLocation Loc, |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 146 | QualType NumberType, |
| 147 | bool isLiteral = false, |
| 148 | SourceRange R = SourceRange()) { |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 149 | llvm::Optional<NSAPI::NSNumberLiteralMethodKind> Kind |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 150 | = S.NSAPIObj->getNSNumberFactoryMethodKind(NumberType); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 151 | |
| 152 | if (!Kind) { |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 153 | if (isLiteral) { |
| 154 | S.Diag(Loc, diag::err_invalid_nsnumber_type) |
| 155 | << NumberType << R; |
| 156 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | } |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 160 | // If we already looked up this method, we're done. |
| 161 | if (S.NSNumberLiteralMethods[*Kind]) |
| 162 | return S.NSNumberLiteralMethods[*Kind]; |
| 163 | |
| 164 | Selector Sel = S.NSAPIObj->getNSNumberLiteralSelector(*Kind, |
| 165 | /*Instance=*/false); |
| 166 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 167 | ASTContext &CX = S.Context; |
| 168 | |
| 169 | // Look up the NSNumber class, if we haven't done so already. It's cached |
| 170 | // in the Sema instance. |
| 171 | if (!S.NSNumberDecl) { |
| 172 | IdentifierInfo *NSNumberId = S.NSAPIObj->getNSClassId(NSAPI::ClassId_NSNumber); |
| 173 | NamedDecl *IF = S.LookupSingleName(S.TUScope, NSNumberId, |
| 174 | Loc, Sema::LookupOrdinaryName); |
| 175 | S.NSNumberDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
| 176 | if (!S.NSNumberDecl) { |
| 177 | if (S.getLangOpts().DebuggerObjCLiteral) { |
| 178 | // Create a stub definition of NSNumber. |
| 179 | S.NSNumberDecl = ObjCInterfaceDecl::Create (CX, |
| 180 | CX.getTranslationUnitDecl(), |
| 181 | SourceLocation(), NSNumberId, |
| 182 | 0, SourceLocation()); |
| 183 | } else { |
| 184 | // Otherwise, require a declaration of NSNumber. |
| 185 | S.Diag(Loc, diag::err_undeclared_nsnumber); |
| 186 | return 0; |
| 187 | } |
| 188 | } else if (!S.NSNumberDecl->hasDefinition()) { |
| 189 | S.Diag(Loc, diag::err_undeclared_nsnumber); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | // generate the pointer to NSNumber type. |
| 194 | S.NSNumberPointer = CX.getObjCObjectPointerType(CX.getObjCInterfaceType(S.NSNumberDecl)); |
| 195 | } |
| 196 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 197 | // Look for the appropriate method within NSNumber. |
| 198 | ObjCMethodDecl *Method = S.NSNumberDecl->lookupClassMethod(Sel);; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 199 | if (!Method && S.getLangOpts().DebuggerObjCLiteral) { |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 200 | // create a stub definition this NSNumber factory method. |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 201 | TypeSourceInfo *ResultTInfo = 0; |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 202 | Method = ObjCMethodDecl::Create(CX, SourceLocation(), SourceLocation(), Sel, |
| 203 | S.NSNumberPointer, ResultTInfo, S.NSNumberDecl, |
| 204 | /*isInstance=*/false, /*isVariadic=*/false, |
| 205 | /*isSynthesized=*/false, |
| 206 | /*isImplicitlyDeclared=*/true, |
| 207 | /*isDefined=*/false, ObjCMethodDecl::Required, |
| 208 | /*HasRelatedResultType=*/false); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 209 | ParmVarDecl *value = ParmVarDecl::Create(S.Context, Method, |
| 210 | SourceLocation(), SourceLocation(), |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 211 | &CX.Idents.get("value"), |
| 212 | NumberType, /*TInfo=*/0, SC_None, SC_None, 0); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 213 | Method->setMethodParams(S.Context, value, ArrayRef<SourceLocation>()); |
| 214 | } |
| 215 | |
| 216 | if (!Method) { |
| 217 | S.Diag(Loc, diag::err_undeclared_nsnumber_method) << Sel; |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | // Make sure the return type is reasonable. |
| 222 | if (!Method->getResultType()->isObjCObjectPointerType()) { |
| 223 | S.Diag(Loc, diag::err_objc_literal_method_sig) |
| 224 | << Sel; |
| 225 | S.Diag(Method->getLocation(), diag::note_objc_literal_method_return) |
| 226 | << Method->getResultType(); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | // Note: if the parameter type is out-of-line, we'll catch it later in the |
| 231 | // implicit conversion. |
| 232 | |
| 233 | S.NSNumberLiteralMethods[*Kind] = Method; |
| 234 | return Method; |
| 235 | } |
| 236 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 237 | /// BuildObjCNumericLiteral - builds an ObjCBoxedExpr AST node for the |
| 238 | /// numeric literal expression. Type of the expression will be "NSNumber *". |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 239 | ExprResult Sema::BuildObjCNumericLiteral(SourceLocation AtLoc, Expr *Number) { |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 240 | // Determine the type of the literal. |
| 241 | QualType NumberType = Number->getType(); |
| 242 | if (CharacterLiteral *Char = dyn_cast<CharacterLiteral>(Number)) { |
| 243 | // In C, character literals have type 'int'. That's not the type we want |
| 244 | // to use to determine the Objective-c literal kind. |
| 245 | switch (Char->getKind()) { |
| 246 | case CharacterLiteral::Ascii: |
| 247 | NumberType = Context.CharTy; |
| 248 | break; |
| 249 | |
| 250 | case CharacterLiteral::Wide: |
| 251 | NumberType = Context.getWCharType(); |
| 252 | break; |
| 253 | |
| 254 | case CharacterLiteral::UTF16: |
| 255 | NumberType = Context.Char16Ty; |
| 256 | break; |
| 257 | |
| 258 | case CharacterLiteral::UTF32: |
| 259 | NumberType = Context.Char32Ty; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 264 | // Look for the appropriate method within NSNumber. |
| 265 | // Construct the literal. |
Patrick Beard | e0fdadf | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 266 | SourceRange NR(Number->getSourceRange()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 267 | ObjCMethodDecl *Method = getNSNumberFactoryMethod(*this, AtLoc, NumberType, |
Patrick Beard | e0fdadf | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 268 | true, NR); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 269 | if (!Method) |
| 270 | return ExprError(); |
| 271 | |
| 272 | // Convert the number to the type that the parameter expects. |
Patrick Beard | e0fdadf | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 273 | ParmVarDecl *ParamDecl = Method->param_begin()[0]; |
| 274 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 275 | ParamDecl); |
| 276 | ExprResult ConvertedNumber = PerformCopyInitialization(Entity, |
| 277 | SourceLocation(), |
| 278 | Owned(Number)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 279 | if (ConvertedNumber.isInvalid()) |
| 280 | return ExprError(); |
| 281 | Number = ConvertedNumber.get(); |
| 282 | |
Patrick Beard | e0fdadf | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 283 | // Use the effective source range of the literal, including the leading '@'. |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 284 | return MaybeBindToTemporary( |
Patrick Beard | e0fdadf | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 285 | new (Context) ObjCBoxedExpr(Number, NSNumberPointer, Method, |
| 286 | SourceRange(AtLoc, NR.getEnd()))); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | ExprResult Sema::ActOnObjCBoolLiteral(SourceLocation AtLoc, |
| 290 | SourceLocation ValueLoc, |
| 291 | bool Value) { |
| 292 | ExprResult Inner; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 293 | if (getLangOpts().CPlusPlus) { |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 294 | Inner = ActOnCXXBoolLiteral(ValueLoc, Value? tok::kw_true : tok::kw_false); |
| 295 | } else { |
| 296 | // C doesn't actually have a way to represent literal values of type |
| 297 | // _Bool. So, we'll use 0/1 and implicit cast to _Bool. |
| 298 | Inner = ActOnIntegerConstant(ValueLoc, Value? 1 : 0); |
| 299 | Inner = ImpCastExprToType(Inner.get(), Context.BoolTy, |
| 300 | CK_IntegralToBoolean); |
| 301 | } |
| 302 | |
| 303 | return BuildObjCNumericLiteral(AtLoc, Inner.get()); |
| 304 | } |
| 305 | |
| 306 | /// \brief Check that the given expression is a valid element of an Objective-C |
| 307 | /// collection literal. |
| 308 | static ExprResult CheckObjCCollectionLiteralElement(Sema &S, Expr *Element, |
| 309 | QualType T) { |
| 310 | // If the expression is type-dependent, there's nothing for us to do. |
| 311 | if (Element->isTypeDependent()) |
| 312 | return Element; |
| 313 | |
| 314 | ExprResult Result = S.CheckPlaceholderExpr(Element); |
| 315 | if (Result.isInvalid()) |
| 316 | return ExprError(); |
| 317 | Element = Result.get(); |
| 318 | |
| 319 | // In C++, check for an implicit conversion to an Objective-C object pointer |
| 320 | // type. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 321 | if (S.getLangOpts().CPlusPlus && Element->getType()->isRecordType()) { |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 322 | InitializedEntity Entity |
| 323 | = InitializedEntity::InitializeParameter(S.Context, T, /*Consumed=*/false); |
| 324 | InitializationKind Kind |
| 325 | = InitializationKind::CreateCopy(Element->getLocStart(), SourceLocation()); |
| 326 | InitializationSequence Seq(S, Entity, Kind, &Element, 1); |
| 327 | if (!Seq.Failed()) |
| 328 | return Seq.Perform(S, Entity, Kind, MultiExprArg(S, &Element, 1)); |
| 329 | } |
| 330 | |
| 331 | Expr *OrigElement = Element; |
| 332 | |
| 333 | // Perform lvalue-to-rvalue conversion. |
| 334 | Result = S.DefaultLvalueConversion(Element); |
| 335 | if (Result.isInvalid()) |
| 336 | return ExprError(); |
| 337 | Element = Result.get(); |
| 338 | |
| 339 | // Make sure that we have an Objective-C pointer type or block. |
| 340 | if (!Element->getType()->isObjCObjectPointerType() && |
| 341 | !Element->getType()->isBlockPointerType()) { |
| 342 | bool Recovered = false; |
| 343 | |
| 344 | // If this is potentially an Objective-C numeric literal, add the '@'. |
| 345 | if (isa<IntegerLiteral>(OrigElement) || |
| 346 | isa<CharacterLiteral>(OrigElement) || |
| 347 | isa<FloatingLiteral>(OrigElement) || |
| 348 | isa<ObjCBoolLiteralExpr>(OrigElement) || |
| 349 | isa<CXXBoolLiteralExpr>(OrigElement)) { |
| 350 | if (S.NSAPIObj->getNSNumberFactoryMethodKind(OrigElement->getType())) { |
| 351 | int Which = isa<CharacterLiteral>(OrigElement) ? 1 |
| 352 | : (isa<CXXBoolLiteralExpr>(OrigElement) || |
| 353 | isa<ObjCBoolLiteralExpr>(OrigElement)) ? 2 |
| 354 | : 3; |
| 355 | |
| 356 | S.Diag(OrigElement->getLocStart(), diag::err_box_literal_collection) |
| 357 | << Which << OrigElement->getSourceRange() |
| 358 | << FixItHint::CreateInsertion(OrigElement->getLocStart(), "@"); |
| 359 | |
| 360 | Result = S.BuildObjCNumericLiteral(OrigElement->getLocStart(), |
| 361 | OrigElement); |
| 362 | if (Result.isInvalid()) |
| 363 | return ExprError(); |
| 364 | |
| 365 | Element = Result.get(); |
| 366 | Recovered = true; |
| 367 | } |
| 368 | } |
| 369 | // If this is potentially an Objective-C string literal, add the '@'. |
| 370 | else if (StringLiteral *String = dyn_cast<StringLiteral>(OrigElement)) { |
| 371 | if (String->isAscii()) { |
| 372 | S.Diag(OrigElement->getLocStart(), diag::err_box_literal_collection) |
| 373 | << 0 << OrigElement->getSourceRange() |
| 374 | << FixItHint::CreateInsertion(OrigElement->getLocStart(), "@"); |
| 375 | |
| 376 | Result = S.BuildObjCStringLiteral(OrigElement->getLocStart(), String); |
| 377 | if (Result.isInvalid()) |
| 378 | return ExprError(); |
| 379 | |
| 380 | Element = Result.get(); |
| 381 | Recovered = true; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | if (!Recovered) { |
| 386 | S.Diag(Element->getLocStart(), diag::err_invalid_collection_element) |
| 387 | << Element->getType(); |
| 388 | return ExprError(); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | // Make sure that the element has the type that the container factory |
| 393 | // function expects. |
| 394 | return S.PerformCopyInitialization( |
| 395 | InitializedEntity::InitializeParameter(S.Context, T, |
| 396 | /*Consumed=*/false), |
| 397 | Element->getLocStart(), Element); |
| 398 | } |
| 399 | |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 400 | ExprResult Sema::BuildObjCBoxedExpr(SourceRange SR, Expr *ValueExpr) { |
| 401 | if (ValueExpr->isTypeDependent()) { |
| 402 | ObjCBoxedExpr *BoxedExpr = |
| 403 | new (Context) ObjCBoxedExpr(ValueExpr, Context.DependentTy, NULL, SR); |
| 404 | return Owned(BoxedExpr); |
| 405 | } |
| 406 | ObjCMethodDecl *BoxingMethod = NULL; |
| 407 | QualType BoxedType; |
| 408 | // Convert the expression to an RValue, so we can check for pointer types... |
| 409 | ExprResult RValue = DefaultFunctionArrayLvalueConversion(ValueExpr); |
| 410 | if (RValue.isInvalid()) { |
| 411 | return ExprError(); |
| 412 | } |
| 413 | ValueExpr = RValue.get(); |
Patrick Beard | e0fdadf | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 414 | QualType ValueType(ValueExpr->getType()); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 415 | if (const PointerType *PT = ValueType->getAs<PointerType>()) { |
| 416 | QualType PointeeType = PT->getPointeeType(); |
| 417 | if (Context.hasSameUnqualifiedType(PointeeType, Context.CharTy)) { |
| 418 | |
| 419 | if (!NSStringDecl) { |
| 420 | IdentifierInfo *NSStringId = |
| 421 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSString); |
| 422 | NamedDecl *Decl = LookupSingleName(TUScope, NSStringId, |
| 423 | SR.getBegin(), LookupOrdinaryName); |
| 424 | NSStringDecl = dyn_cast_or_null<ObjCInterfaceDecl>(Decl); |
| 425 | if (!NSStringDecl) { |
| 426 | if (getLangOpts().DebuggerObjCLiteral) { |
| 427 | // Support boxed expressions in the debugger w/o NSString declaration. |
| 428 | NSStringDecl = ObjCInterfaceDecl::Create(Context, |
| 429 | Context.getTranslationUnitDecl(), |
| 430 | SourceLocation(), NSStringId, |
| 431 | 0, SourceLocation()); |
| 432 | } else { |
| 433 | Diag(SR.getBegin(), diag::err_undeclared_nsstring); |
| 434 | return ExprError(); |
| 435 | } |
| 436 | } else if (!NSStringDecl->hasDefinition()) { |
| 437 | Diag(SR.getBegin(), diag::err_undeclared_nsstring); |
| 438 | return ExprError(); |
| 439 | } |
| 440 | assert(NSStringDecl && "NSStringDecl should not be NULL"); |
| 441 | NSStringPointer = |
| 442 | Context.getObjCObjectPointerType(Context.getObjCInterfaceType(NSStringDecl)); |
| 443 | } |
| 444 | |
| 445 | if (!StringWithUTF8StringMethod) { |
| 446 | IdentifierInfo *II = &Context.Idents.get("stringWithUTF8String"); |
| 447 | Selector stringWithUTF8String = Context.Selectors.getUnarySelector(II); |
| 448 | |
| 449 | // Look for the appropriate method within NSString. |
| 450 | StringWithUTF8StringMethod = NSStringDecl->lookupClassMethod(stringWithUTF8String); |
| 451 | if (!StringWithUTF8StringMethod && getLangOpts().DebuggerObjCLiteral) { |
| 452 | // Debugger needs to work even if NSString hasn't been defined. |
| 453 | TypeSourceInfo *ResultTInfo = 0; |
| 454 | ObjCMethodDecl *M = |
| 455 | ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(), |
| 456 | stringWithUTF8String, NSStringPointer, |
| 457 | ResultTInfo, NSStringDecl, |
| 458 | /*isInstance=*/false, /*isVariadic=*/false, |
| 459 | /*isSynthesized=*/false, |
| 460 | /*isImplicitlyDeclared=*/true, |
| 461 | /*isDefined=*/false, |
| 462 | ObjCMethodDecl::Required, |
| 463 | /*HasRelatedResultType=*/false); |
| 464 | ParmVarDecl *value = |
| 465 | ParmVarDecl::Create(Context, M, |
| 466 | SourceLocation(), SourceLocation(), |
| 467 | &Context.Idents.get("value"), |
| 468 | Context.getPointerType(Context.CharTy.withConst()), |
| 469 | /*TInfo=*/0, |
| 470 | SC_None, SC_None, 0); |
| 471 | M->setMethodParams(Context, value, ArrayRef<SourceLocation>()); |
| 472 | StringWithUTF8StringMethod = M; |
| 473 | } |
| 474 | assert(StringWithUTF8StringMethod && |
| 475 | "StringWithUTF8StringMethod should not be NULL"); |
| 476 | } |
| 477 | |
| 478 | BoxingMethod = StringWithUTF8StringMethod; |
| 479 | BoxedType = NSStringPointer; |
| 480 | } |
Patrick Beard | e0fdadf | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 481 | } else if (ValueType->isBuiltinType()) { |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 482 | // The other types we support are numeric, char and BOOL/bool. We could also |
| 483 | // provide limited support for structure types, such as NSRange, NSRect, and |
| 484 | // NSSize. See NSValue (NSValueGeometryExtensions) in <Foundation/NSGeometry.h> |
| 485 | // for more details. |
| 486 | |
| 487 | // Check for a top-level character literal. |
| 488 | if (const CharacterLiteral *Char = |
| 489 | dyn_cast<CharacterLiteral>(ValueExpr->IgnoreParens())) { |
| 490 | // In C, character literals have type 'int'. That's not the type we want |
| 491 | // to use to determine the Objective-c literal kind. |
| 492 | switch (Char->getKind()) { |
| 493 | case CharacterLiteral::Ascii: |
| 494 | ValueType = Context.CharTy; |
| 495 | break; |
| 496 | |
| 497 | case CharacterLiteral::Wide: |
| 498 | ValueType = Context.getWCharType(); |
| 499 | break; |
| 500 | |
| 501 | case CharacterLiteral::UTF16: |
| 502 | ValueType = Context.Char16Ty; |
| 503 | break; |
| 504 | |
| 505 | case CharacterLiteral::UTF32: |
| 506 | ValueType = Context.Char32Ty; |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // FIXME: Do I need to do anything special with BoolTy expressions? |
| 512 | |
| 513 | // Look for the appropriate method within NSNumber. |
| 514 | BoxingMethod = getNSNumberFactoryMethod(*this, SR.getBegin(), ValueType); |
| 515 | BoxedType = NSNumberPointer; |
| 516 | } |
| 517 | |
| 518 | if (!BoxingMethod) { |
| 519 | Diag(SR.getBegin(), diag::err_objc_illegal_boxed_expression_type) |
| 520 | << ValueType << ValueExpr->getSourceRange(); |
| 521 | return ExprError(); |
| 522 | } |
| 523 | |
| 524 | // Convert the expression to the type that the parameter requires. |
Patrick Beard | e0fdadf | 2012-05-01 21:47:19 +0000 | [diff] [blame] | 525 | ParmVarDecl *ParamDecl = BoxingMethod->param_begin()[0]; |
| 526 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
| 527 | ParamDecl); |
| 528 | ExprResult ConvertedValueExpr = PerformCopyInitialization(Entity, |
| 529 | SourceLocation(), |
| 530 | Owned(ValueExpr)); |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 531 | if (ConvertedValueExpr.isInvalid()) |
| 532 | return ExprError(); |
| 533 | ValueExpr = ConvertedValueExpr.get(); |
| 534 | |
| 535 | ObjCBoxedExpr *BoxedExpr = |
| 536 | new (Context) ObjCBoxedExpr(ValueExpr, BoxedType, |
| 537 | BoxingMethod, SR); |
| 538 | return MaybeBindToTemporary(BoxedExpr); |
| 539 | } |
| 540 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 541 | ExprResult Sema::BuildObjCSubscriptExpression(SourceLocation RB, Expr *BaseExpr, |
| 542 | Expr *IndexExpr, |
| 543 | ObjCMethodDecl *getterMethod, |
| 544 | ObjCMethodDecl *setterMethod) { |
| 545 | // Feature support is for modern abi. |
| 546 | if (!LangOpts.ObjCNonFragileABI) |
| 547 | return ExprError(); |
| 548 | // If the expression is type-dependent, there's nothing for us to do. |
| 549 | assert ((!BaseExpr->isTypeDependent() && !IndexExpr->isTypeDependent()) && |
| 550 | "base or index cannot have dependent type here"); |
| 551 | ExprResult Result = CheckPlaceholderExpr(IndexExpr); |
| 552 | if (Result.isInvalid()) |
| 553 | return ExprError(); |
| 554 | IndexExpr = Result.get(); |
| 555 | |
| 556 | // Perform lvalue-to-rvalue conversion. |
| 557 | Result = DefaultLvalueConversion(BaseExpr); |
| 558 | if (Result.isInvalid()) |
| 559 | return ExprError(); |
| 560 | BaseExpr = Result.get(); |
| 561 | return Owned(ObjCSubscriptRefExpr::Create(Context, |
| 562 | BaseExpr, |
| 563 | IndexExpr, |
| 564 | Context.PseudoObjectTy, |
| 565 | getterMethod, |
| 566 | setterMethod, RB)); |
| 567 | |
| 568 | } |
| 569 | |
| 570 | ExprResult Sema::BuildObjCArrayLiteral(SourceRange SR, MultiExprArg Elements) { |
| 571 | // Look up the NSArray class, if we haven't done so already. |
| 572 | if (!NSArrayDecl) { |
| 573 | NamedDecl *IF = LookupSingleName(TUScope, |
| 574 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSArray), |
| 575 | SR.getBegin(), |
| 576 | LookupOrdinaryName); |
| 577 | NSArrayDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 578 | if (!NSArrayDecl && getLangOpts().DebuggerObjCLiteral) |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 579 | NSArrayDecl = ObjCInterfaceDecl::Create (Context, |
| 580 | Context.getTranslationUnitDecl(), |
| 581 | SourceLocation(), |
| 582 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSArray), |
| 583 | 0, SourceLocation()); |
| 584 | |
| 585 | if (!NSArrayDecl) { |
| 586 | Diag(SR.getBegin(), diag::err_undeclared_nsarray); |
| 587 | return ExprError(); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | // Find the arrayWithObjects:count: method, if we haven't done so already. |
| 592 | QualType IdT = Context.getObjCIdType(); |
| 593 | if (!ArrayWithObjectsMethod) { |
| 594 | Selector |
| 595 | Sel = NSAPIObj->getNSArraySelector(NSAPI::NSArr_arrayWithObjectsCount); |
| 596 | ArrayWithObjectsMethod = NSArrayDecl->lookupClassMethod(Sel); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 597 | if (!ArrayWithObjectsMethod && getLangOpts().DebuggerObjCLiteral) { |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 598 | TypeSourceInfo *ResultTInfo = 0; |
| 599 | ArrayWithObjectsMethod = |
| 600 | ObjCMethodDecl::Create(Context, |
| 601 | SourceLocation(), SourceLocation(), Sel, |
| 602 | IdT, |
| 603 | ResultTInfo, |
| 604 | Context.getTranslationUnitDecl(), |
| 605 | false /*Instance*/, false/*isVariadic*/, |
| 606 | /*isSynthesized=*/false, |
| 607 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
| 608 | ObjCMethodDecl::Required, |
| 609 | false); |
| 610 | SmallVector<ParmVarDecl *, 2> Params; |
| 611 | ParmVarDecl *objects = ParmVarDecl::Create(Context, ArrayWithObjectsMethod, |
| 612 | SourceLocation(), SourceLocation(), |
| 613 | &Context.Idents.get("objects"), |
| 614 | Context.getPointerType(IdT), |
| 615 | /*TInfo=*/0, |
| 616 | SC_None, |
| 617 | SC_None, |
| 618 | 0); |
| 619 | Params.push_back(objects); |
| 620 | ParmVarDecl *cnt = ParmVarDecl::Create(Context, ArrayWithObjectsMethod, |
| 621 | SourceLocation(), SourceLocation(), |
| 622 | &Context.Idents.get("cnt"), |
| 623 | Context.UnsignedLongTy, |
| 624 | /*TInfo=*/0, |
| 625 | SC_None, |
| 626 | SC_None, |
| 627 | 0); |
| 628 | Params.push_back(cnt); |
| 629 | ArrayWithObjectsMethod->setMethodParams(Context, Params, |
| 630 | ArrayRef<SourceLocation>()); |
| 631 | |
| 632 | |
| 633 | } |
| 634 | |
| 635 | if (!ArrayWithObjectsMethod) { |
| 636 | Diag(SR.getBegin(), diag::err_undeclared_arraywithobjects) << Sel; |
| 637 | return ExprError(); |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | // Make sure the return type is reasonable. |
| 642 | if (!ArrayWithObjectsMethod->getResultType()->isObjCObjectPointerType()) { |
| 643 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 644 | << ArrayWithObjectsMethod->getSelector(); |
| 645 | Diag(ArrayWithObjectsMethod->getLocation(), |
| 646 | diag::note_objc_literal_method_return) |
| 647 | << ArrayWithObjectsMethod->getResultType(); |
| 648 | return ExprError(); |
| 649 | } |
| 650 | |
| 651 | // Dig out the type that all elements should be converted to. |
| 652 | QualType T = ArrayWithObjectsMethod->param_begin()[0]->getType(); |
| 653 | const PointerType *PtrT = T->getAs<PointerType>(); |
| 654 | if (!PtrT || |
| 655 | !Context.hasSameUnqualifiedType(PtrT->getPointeeType(), IdT)) { |
| 656 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 657 | << ArrayWithObjectsMethod->getSelector(); |
| 658 | Diag(ArrayWithObjectsMethod->param_begin()[0]->getLocation(), |
| 659 | diag::note_objc_literal_method_param) |
| 660 | << 0 << T |
| 661 | << Context.getPointerType(IdT.withConst()); |
| 662 | return ExprError(); |
| 663 | } |
| 664 | T = PtrT->getPointeeType(); |
| 665 | |
| 666 | // Check that the 'count' parameter is integral. |
| 667 | if (!ArrayWithObjectsMethod->param_begin()[1]->getType()->isIntegerType()) { |
| 668 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 669 | << ArrayWithObjectsMethod->getSelector(); |
| 670 | Diag(ArrayWithObjectsMethod->param_begin()[1]->getLocation(), |
| 671 | diag::note_objc_literal_method_param) |
| 672 | << 1 |
| 673 | << ArrayWithObjectsMethod->param_begin()[1]->getType() |
| 674 | << "integral"; |
| 675 | return ExprError(); |
| 676 | } |
| 677 | |
| 678 | // Check that each of the elements provided is valid in a collection literal, |
| 679 | // performing conversions as necessary. |
| 680 | Expr **ElementsBuffer = Elements.get(); |
| 681 | for (unsigned I = 0, N = Elements.size(); I != N; ++I) { |
| 682 | ExprResult Converted = CheckObjCCollectionLiteralElement(*this, |
| 683 | ElementsBuffer[I], |
| 684 | T); |
| 685 | if (Converted.isInvalid()) |
| 686 | return ExprError(); |
| 687 | |
| 688 | ElementsBuffer[I] = Converted.get(); |
| 689 | } |
| 690 | |
| 691 | QualType Ty |
| 692 | = Context.getObjCObjectPointerType( |
| 693 | Context.getObjCInterfaceType(NSArrayDecl)); |
| 694 | |
| 695 | return MaybeBindToTemporary( |
| 696 | ObjCArrayLiteral::Create(Context, |
| 697 | llvm::makeArrayRef(Elements.get(), |
| 698 | Elements.size()), |
| 699 | Ty, ArrayWithObjectsMethod, SR)); |
| 700 | } |
| 701 | |
| 702 | ExprResult Sema::BuildObjCDictionaryLiteral(SourceRange SR, |
| 703 | ObjCDictionaryElement *Elements, |
| 704 | unsigned NumElements) { |
| 705 | // Look up the NSDictionary class, if we haven't done so already. |
| 706 | if (!NSDictionaryDecl) { |
| 707 | NamedDecl *IF = LookupSingleName(TUScope, |
| 708 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSDictionary), |
| 709 | SR.getBegin(), LookupOrdinaryName); |
| 710 | NSDictionaryDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 711 | if (!NSDictionaryDecl && getLangOpts().DebuggerObjCLiteral) |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 712 | NSDictionaryDecl = ObjCInterfaceDecl::Create (Context, |
| 713 | Context.getTranslationUnitDecl(), |
| 714 | SourceLocation(), |
| 715 | NSAPIObj->getNSClassId(NSAPI::ClassId_NSDictionary), |
| 716 | 0, SourceLocation()); |
| 717 | |
| 718 | if (!NSDictionaryDecl) { |
| 719 | Diag(SR.getBegin(), diag::err_undeclared_nsdictionary); |
| 720 | return ExprError(); |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | // Find the dictionaryWithObjects:forKeys:count: method, if we haven't done |
| 725 | // so already. |
| 726 | QualType IdT = Context.getObjCIdType(); |
| 727 | if (!DictionaryWithObjectsMethod) { |
| 728 | Selector Sel = NSAPIObj->getNSDictionarySelector( |
| 729 | NSAPI::NSDict_dictionaryWithObjectsForKeysCount); |
| 730 | DictionaryWithObjectsMethod = NSDictionaryDecl->lookupClassMethod(Sel); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 731 | if (!DictionaryWithObjectsMethod && getLangOpts().DebuggerObjCLiteral) { |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 732 | DictionaryWithObjectsMethod = |
| 733 | ObjCMethodDecl::Create(Context, |
| 734 | SourceLocation(), SourceLocation(), Sel, |
| 735 | IdT, |
| 736 | 0 /*TypeSourceInfo */, |
| 737 | Context.getTranslationUnitDecl(), |
| 738 | false /*Instance*/, false/*isVariadic*/, |
| 739 | /*isSynthesized=*/false, |
| 740 | /*isImplicitlyDeclared=*/true, /*isDefined=*/false, |
| 741 | ObjCMethodDecl::Required, |
| 742 | false); |
| 743 | SmallVector<ParmVarDecl *, 3> Params; |
| 744 | ParmVarDecl *objects = ParmVarDecl::Create(Context, DictionaryWithObjectsMethod, |
| 745 | SourceLocation(), SourceLocation(), |
| 746 | &Context.Idents.get("objects"), |
| 747 | Context.getPointerType(IdT), |
| 748 | /*TInfo=*/0, |
| 749 | SC_None, |
| 750 | SC_None, |
| 751 | 0); |
| 752 | Params.push_back(objects); |
| 753 | ParmVarDecl *keys = ParmVarDecl::Create(Context, DictionaryWithObjectsMethod, |
| 754 | SourceLocation(), SourceLocation(), |
| 755 | &Context.Idents.get("keys"), |
| 756 | Context.getPointerType(IdT), |
| 757 | /*TInfo=*/0, |
| 758 | SC_None, |
| 759 | SC_None, |
| 760 | 0); |
| 761 | Params.push_back(keys); |
| 762 | ParmVarDecl *cnt = ParmVarDecl::Create(Context, DictionaryWithObjectsMethod, |
| 763 | SourceLocation(), SourceLocation(), |
| 764 | &Context.Idents.get("cnt"), |
| 765 | Context.UnsignedLongTy, |
| 766 | /*TInfo=*/0, |
| 767 | SC_None, |
| 768 | SC_None, |
| 769 | 0); |
| 770 | Params.push_back(cnt); |
| 771 | DictionaryWithObjectsMethod->setMethodParams(Context, Params, |
| 772 | ArrayRef<SourceLocation>()); |
| 773 | } |
| 774 | |
| 775 | if (!DictionaryWithObjectsMethod) { |
| 776 | Diag(SR.getBegin(), diag::err_undeclared_dictwithobjects) << Sel; |
| 777 | return ExprError(); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | // Make sure the return type is reasonable. |
| 782 | if (!DictionaryWithObjectsMethod->getResultType()->isObjCObjectPointerType()){ |
| 783 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 784 | << DictionaryWithObjectsMethod->getSelector(); |
| 785 | Diag(DictionaryWithObjectsMethod->getLocation(), |
| 786 | diag::note_objc_literal_method_return) |
| 787 | << DictionaryWithObjectsMethod->getResultType(); |
| 788 | return ExprError(); |
| 789 | } |
| 790 | |
| 791 | // Dig out the type that all values should be converted to. |
| 792 | QualType ValueT = DictionaryWithObjectsMethod->param_begin()[0]->getType(); |
| 793 | const PointerType *PtrValue = ValueT->getAs<PointerType>(); |
| 794 | if (!PtrValue || |
| 795 | !Context.hasSameUnqualifiedType(PtrValue->getPointeeType(), IdT)) { |
| 796 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 797 | << DictionaryWithObjectsMethod->getSelector(); |
| 798 | Diag(DictionaryWithObjectsMethod->param_begin()[0]->getLocation(), |
| 799 | diag::note_objc_literal_method_param) |
| 800 | << 0 << ValueT |
| 801 | << Context.getPointerType(IdT.withConst()); |
| 802 | return ExprError(); |
| 803 | } |
| 804 | ValueT = PtrValue->getPointeeType(); |
| 805 | |
| 806 | // Dig out the type that all keys should be converted to. |
| 807 | QualType KeyT = DictionaryWithObjectsMethod->param_begin()[1]->getType(); |
| 808 | const PointerType *PtrKey = KeyT->getAs<PointerType>(); |
| 809 | if (!PtrKey || |
| 810 | !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), |
| 811 | IdT)) { |
| 812 | bool err = true; |
| 813 | if (PtrKey) { |
| 814 | if (QIDNSCopying.isNull()) { |
| 815 | // key argument of selector is id<NSCopying>? |
| 816 | if (ObjCProtocolDecl *NSCopyingPDecl = |
| 817 | LookupProtocol(&Context.Idents.get("NSCopying"), SR.getBegin())) { |
| 818 | ObjCProtocolDecl *PQ[] = {NSCopyingPDecl}; |
| 819 | QIDNSCopying = |
| 820 | Context.getObjCObjectType(Context.ObjCBuiltinIdTy, |
| 821 | (ObjCProtocolDecl**) PQ,1); |
| 822 | QIDNSCopying = Context.getObjCObjectPointerType(QIDNSCopying); |
| 823 | } |
| 824 | } |
| 825 | if (!QIDNSCopying.isNull()) |
| 826 | err = !Context.hasSameUnqualifiedType(PtrKey->getPointeeType(), |
| 827 | QIDNSCopying); |
| 828 | } |
| 829 | |
| 830 | if (err) { |
| 831 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 832 | << DictionaryWithObjectsMethod->getSelector(); |
| 833 | Diag(DictionaryWithObjectsMethod->param_begin()[1]->getLocation(), |
| 834 | diag::note_objc_literal_method_param) |
| 835 | << 1 << KeyT |
| 836 | << Context.getPointerType(IdT.withConst()); |
| 837 | return ExprError(); |
| 838 | } |
| 839 | } |
| 840 | KeyT = PtrKey->getPointeeType(); |
| 841 | |
| 842 | // Check that the 'count' parameter is integral. |
| 843 | if (!DictionaryWithObjectsMethod->param_begin()[2]->getType() |
| 844 | ->isIntegerType()) { |
| 845 | Diag(SR.getBegin(), diag::err_objc_literal_method_sig) |
| 846 | << DictionaryWithObjectsMethod->getSelector(); |
| 847 | Diag(DictionaryWithObjectsMethod->param_begin()[2]->getLocation(), |
| 848 | diag::note_objc_literal_method_param) |
| 849 | << 2 |
| 850 | << DictionaryWithObjectsMethod->param_begin()[2]->getType() |
| 851 | << "integral"; |
| 852 | return ExprError(); |
| 853 | } |
| 854 | |
| 855 | // Check that each of the keys and values provided is valid in a collection |
| 856 | // literal, performing conversions as necessary. |
| 857 | bool HasPackExpansions = false; |
| 858 | for (unsigned I = 0, N = NumElements; I != N; ++I) { |
| 859 | // Check the key. |
| 860 | ExprResult Key = CheckObjCCollectionLiteralElement(*this, Elements[I].Key, |
| 861 | KeyT); |
| 862 | if (Key.isInvalid()) |
| 863 | return ExprError(); |
| 864 | |
| 865 | // Check the value. |
| 866 | ExprResult Value |
| 867 | = CheckObjCCollectionLiteralElement(*this, Elements[I].Value, ValueT); |
| 868 | if (Value.isInvalid()) |
| 869 | return ExprError(); |
| 870 | |
| 871 | Elements[I].Key = Key.get(); |
| 872 | Elements[I].Value = Value.get(); |
| 873 | |
| 874 | if (Elements[I].EllipsisLoc.isInvalid()) |
| 875 | continue; |
| 876 | |
| 877 | if (!Elements[I].Key->containsUnexpandedParameterPack() && |
| 878 | !Elements[I].Value->containsUnexpandedParameterPack()) { |
| 879 | Diag(Elements[I].EllipsisLoc, |
| 880 | diag::err_pack_expansion_without_parameter_packs) |
| 881 | << SourceRange(Elements[I].Key->getLocStart(), |
| 882 | Elements[I].Value->getLocEnd()); |
| 883 | return ExprError(); |
| 884 | } |
| 885 | |
| 886 | HasPackExpansions = true; |
| 887 | } |
| 888 | |
| 889 | |
| 890 | QualType Ty |
| 891 | = Context.getObjCObjectPointerType( |
| 892 | Context.getObjCInterfaceType(NSDictionaryDecl)); |
| 893 | return MaybeBindToTemporary( |
| 894 | ObjCDictionaryLiteral::Create(Context, |
| 895 | llvm::makeArrayRef(Elements, |
| 896 | NumElements), |
| 897 | HasPackExpansions, |
| 898 | Ty, |
| 899 | DictionaryWithObjectsMethod, SR)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Argyrios Kyrtzidis | 3b5904b | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 902 | ExprResult Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 903 | TypeSourceInfo *EncodedTypeInfo, |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 904 | SourceLocation RParenLoc) { |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 905 | QualType EncodedType = EncodedTypeInfo->getType(); |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 906 | QualType StrTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 907 | if (EncodedType->isDependentType()) |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 908 | StrTy = Context.DependentTy; |
| 909 | else { |
Fariborz Jahanian | 6c91615 | 2011-06-16 22:34:44 +0000 | [diff] [blame] | 910 | if (!EncodedType->getAsArrayTypeUnsafe() && //// Incomplete array is handled. |
| 911 | !EncodedType->isVoidType()) // void is handled too. |
Argyrios Kyrtzidis | 3b5904b | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 912 | if (RequireCompleteType(AtLoc, EncodedType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 913 | diag::err_incomplete_type_objc_at_encode, |
| 914 | EncodedTypeInfo->getTypeLoc())) |
Argyrios Kyrtzidis | 3b5904b | 2011-05-14 20:32:39 +0000 | [diff] [blame] | 915 | return ExprError(); |
| 916 | |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 917 | std::string Str; |
| 918 | Context.getObjCEncodingForType(EncodedType, Str); |
| 919 | |
| 920 | // The type of @encode is the same as the type of the corresponding string, |
| 921 | // which is an array type. |
| 922 | StrTy = Context.CharTy; |
| 923 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 924 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 925 | StrTy.addConst(); |
| 926 | StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), |
| 927 | ArrayType::Normal, 0); |
| 928 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 929 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 930 | return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc); |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 931 | } |
| 932 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 933 | ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 934 | SourceLocation EncodeLoc, |
| 935 | SourceLocation LParenLoc, |
| 936 | ParsedType ty, |
| 937 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 938 | // FIXME: Preserve type source info ? |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 939 | TypeSourceInfo *TInfo; |
| 940 | QualType EncodedType = GetTypeFromParser(ty, &TInfo); |
| 941 | if (!TInfo) |
| 942 | TInfo = Context.getTrivialTypeSourceInfo(EncodedType, |
| 943 | PP.getLocForEndOfToken(LParenLoc)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 944 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 945 | return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 946 | } |
| 947 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 948 | ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 949 | SourceLocation AtLoc, |
| 950 | SourceLocation SelLoc, |
| 951 | SourceLocation LParenLoc, |
| 952 | SourceLocation RParenLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 953 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 954 | SourceRange(LParenLoc, RParenLoc), false, false); |
Fariborz Jahanian | 7ff22de | 2009-06-16 16:25:00 +0000 | [diff] [blame] | 955 | if (!Method) |
| 956 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 957 | SourceRange(LParenLoc, RParenLoc)); |
| 958 | if (!Method) |
| 959 | Diag(SelLoc, diag::warn_undeclared_selector) << Sel; |
Fariborz Jahanian | 4c91d89 | 2011-07-13 19:05:43 +0000 | [diff] [blame] | 960 | |
| 961 | if (!Method || |
| 962 | Method->getImplementationControl() != ObjCMethodDecl::Optional) { |
| 963 | llvm::DenseMap<Selector, SourceLocation>::iterator Pos |
| 964 | = ReferencedSelectors.find(Sel); |
| 965 | if (Pos == ReferencedSelectors.end()) |
| 966 | ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); |
| 967 | } |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 968 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 969 | // In ARC, forbid the user from using @selector for |
| 970 | // retain/release/autorelease/dealloc/retainCount. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 971 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 972 | switch (Sel.getMethodFamily()) { |
| 973 | case OMF_retain: |
| 974 | case OMF_release: |
| 975 | case OMF_autorelease: |
| 976 | case OMF_retainCount: |
| 977 | case OMF_dealloc: |
| 978 | Diag(AtLoc, diag::err_arc_illegal_selector) << |
| 979 | Sel << SourceRange(LParenLoc, RParenLoc); |
| 980 | break; |
| 981 | |
| 982 | case OMF_None: |
| 983 | case OMF_alloc: |
| 984 | case OMF_copy: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 985 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 986 | case OMF_init: |
| 987 | case OMF_mutableCopy: |
| 988 | case OMF_new: |
| 989 | case OMF_self: |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 990 | case OMF_performSelector: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 991 | break; |
| 992 | } |
| 993 | } |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 994 | QualType Ty = Context.getObjCSelType(); |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 995 | return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 996 | } |
| 997 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 998 | ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 999 | SourceLocation AtLoc, |
| 1000 | SourceLocation ProtoLoc, |
| 1001 | SourceLocation LParenLoc, |
| 1002 | SourceLocation RParenLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1003 | ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1004 | if (!PDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1005 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1006 | return true; |
| 1007 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1008 | |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 1009 | QualType Ty = Context.getObjCProtoType(); |
| 1010 | if (Ty.isNull()) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1011 | return true; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1012 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 1013 | return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1016 | /// Try to capture an implicit reference to 'self'. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 1017 | ObjCMethodDecl *Sema::tryCaptureObjCSelf(SourceLocation Loc) { |
| 1018 | DeclContext *DC = getFunctionLevelDeclContext(); |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1019 | |
| 1020 | // If we're not in an ObjC method, error out. Note that, unlike the |
| 1021 | // C++ case, we don't require an instance method --- class methods |
| 1022 | // still have a 'self', and we really do still need to capture it! |
| 1023 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(DC); |
| 1024 | if (!method) |
| 1025 | return 0; |
| 1026 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 1027 | tryCaptureVariable(method->getSelfDecl(), Loc); |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1028 | |
| 1029 | return method; |
| 1030 | } |
| 1031 | |
Douglas Gregor | 5c16d63 | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1032 | static QualType stripObjCInstanceType(ASTContext &Context, QualType T) { |
| 1033 | if (T == Context.getObjCInstanceType()) |
| 1034 | return Context.getObjCIdType(); |
| 1035 | |
| 1036 | return T; |
| 1037 | } |
| 1038 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1039 | QualType Sema::getMessageSendResultType(QualType ReceiverType, |
| 1040 | ObjCMethodDecl *Method, |
| 1041 | bool isClassMessage, bool isSuperMessage) { |
| 1042 | assert(Method && "Must have a method"); |
| 1043 | if (!Method->hasRelatedResultType()) |
| 1044 | return Method->getSendResultType(); |
| 1045 | |
| 1046 | // If a method has a related return type: |
| 1047 | // - if the method found is an instance method, but the message send |
| 1048 | // was a class message send, T is the declared return type of the method |
| 1049 | // found |
| 1050 | if (Method->isInstanceMethod() && isClassMessage) |
Douglas Gregor | 5c16d63 | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1051 | return stripObjCInstanceType(Context, Method->getSendResultType()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1052 | |
| 1053 | // - if the receiver is super, T is a pointer to the class of the |
| 1054 | // enclosing method definition |
| 1055 | if (isSuperMessage) { |
| 1056 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 1057 | if (ObjCInterfaceDecl *Class = CurMethod->getClassInterface()) |
| 1058 | return Context.getObjCObjectPointerType( |
| 1059 | Context.getObjCInterfaceType(Class)); |
| 1060 | } |
| 1061 | |
| 1062 | // - if the receiver is the name of a class U, T is a pointer to U |
| 1063 | if (ReceiverType->getAs<ObjCInterfaceType>() || |
| 1064 | ReceiverType->isObjCQualifiedInterfaceType()) |
| 1065 | return Context.getObjCObjectPointerType(ReceiverType); |
| 1066 | // - if the receiver is of type Class or qualified Class type, |
| 1067 | // T is the declared return type of the method. |
| 1068 | if (ReceiverType->isObjCClassType() || |
| 1069 | ReceiverType->isObjCQualifiedClassType()) |
Douglas Gregor | 5c16d63 | 2011-09-09 20:05:21 +0000 | [diff] [blame] | 1070 | return stripObjCInstanceType(Context, Method->getSendResultType()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1071 | |
| 1072 | // - if the receiver is id, qualified id, Class, or qualified Class, T |
| 1073 | // is the receiver type, otherwise |
| 1074 | // - T is the type of the receiver expression. |
| 1075 | return ReceiverType; |
| 1076 | } |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1077 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1078 | void Sema::EmitRelatedResultTypeNote(const Expr *E) { |
| 1079 | E = E->IgnoreParenImpCasts(); |
| 1080 | const ObjCMessageExpr *MsgSend = dyn_cast<ObjCMessageExpr>(E); |
| 1081 | if (!MsgSend) |
| 1082 | return; |
| 1083 | |
| 1084 | const ObjCMethodDecl *Method = MsgSend->getMethodDecl(); |
| 1085 | if (!Method) |
| 1086 | return; |
| 1087 | |
| 1088 | if (!Method->hasRelatedResultType()) |
| 1089 | return; |
| 1090 | |
| 1091 | if (Context.hasSameUnqualifiedType(Method->getResultType() |
| 1092 | .getNonReferenceType(), |
| 1093 | MsgSend->getType())) |
| 1094 | return; |
| 1095 | |
Douglas Gregor | e97179c | 2011-09-08 01:46:34 +0000 | [diff] [blame] | 1096 | if (!Context.hasSameUnqualifiedType(Method->getResultType(), |
| 1097 | Context.getObjCInstanceType())) |
| 1098 | return; |
| 1099 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1100 | Diag(Method->getLocation(), diag::note_related_result_type_inferred) |
| 1101 | << Method->isInstanceMethod() << Method->getSelector() |
| 1102 | << MsgSend->getType(); |
| 1103 | } |
| 1104 | |
| 1105 | bool Sema::CheckMessageArgumentTypes(QualType ReceiverType, |
| 1106 | Expr **Args, unsigned NumArgs, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1107 | Selector Sel, ObjCMethodDecl *Method, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1108 | bool isClassMessage, bool isSuperMessage, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1109 | SourceLocation lbrac, SourceLocation rbrac, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1110 | QualType &ReturnType, ExprValueKind &VK) { |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1111 | if (!Method) { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 1112 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1113 | for (unsigned i = 0; i != NumArgs; i++) { |
| 1114 | if (Args[i]->isTypeDependent()) |
| 1115 | continue; |
| 1116 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1117 | ExprResult Result = DefaultArgumentPromotion(Args[i]); |
| 1118 | if (Result.isInvalid()) |
| 1119 | return true; |
| 1120 | Args[i] = Result.take(); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1121 | } |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 1122 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1123 | unsigned DiagID; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1124 | if (getLangOpts().ObjCAutoRefCount) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1125 | DiagID = diag::err_arc_method_not_found; |
| 1126 | else |
| 1127 | DiagID = isClassMessage ? diag::warn_class_method_not_found |
| 1128 | : diag::warn_inst_method_not_found; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1129 | if (!getLangOpts().DebuggerSupport) |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 1130 | Diag(lbrac, DiagID) |
| 1131 | << Sel << isClassMessage << SourceRange(lbrac, rbrac); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 1132 | |
| 1133 | // In debuggers, we want to use __unknown_anytype for these |
| 1134 | // results so that clients can cast them. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1135 | if (getLangOpts().DebuggerSupport) { |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 1136 | ReturnType = Context.UnknownAnyTy; |
| 1137 | } else { |
| 1138 | ReturnType = Context.getObjCIdType(); |
| 1139 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1140 | VK = VK_RValue; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1141 | return false; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 1142 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1143 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1144 | ReturnType = getMessageSendResultType(ReceiverType, Method, isClassMessage, |
| 1145 | isSuperMessage); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1146 | VK = Expr::getValueKindForType(Method->getResultType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1148 | unsigned NumNamedArgs = Sel.getNumArgs(); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1149 | // Method might have more arguments than selector indicates. This is due |
| 1150 | // to addition of c-style arguments in method. |
| 1151 | if (Method->param_size() > Sel.getNumArgs()) |
| 1152 | NumNamedArgs = Method->param_size(); |
| 1153 | // FIXME. This need be cleaned up. |
| 1154 | if (NumArgs < NumNamedArgs) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1155 | Diag(lbrac, diag::err_typecheck_call_too_few_args) |
| 1156 | << 2 << NumNamedArgs << NumArgs; |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 1157 | return false; |
| 1158 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1159 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 1160 | bool IsError = false; |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1161 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1162 | // We can't do any type-checking on a type-dependent argument. |
| 1163 | if (Args[i]->isTypeDependent()) |
| 1164 | continue; |
| 1165 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1166 | Expr *argExpr = Args[i]; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1167 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1168 | ParmVarDecl *param = Method->param_begin()[i]; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1169 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1170 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1171 | // Strip the unbridged-cast placeholder expression off unless it's |
| 1172 | // a consumed argument. |
| 1173 | if (argExpr->hasPlaceholderType(BuiltinType::ARCUnbridgedCast) && |
| 1174 | !param->hasAttr<CFConsumedAttr>()) |
| 1175 | argExpr = stripARCUnbridgedCast(argExpr); |
| 1176 | |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1177 | if (RequireCompleteType(argExpr->getSourceRange().getBegin(), |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1178 | param->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 1179 | diag::err_call_incomplete_argument, argExpr)) |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1180 | return true; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1181 | |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 1182 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Context, |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 1183 | param); |
John McCall | 3fa5cae | 2010-10-26 07:05:15 +0000 | [diff] [blame] | 1184 | ExprResult ArgE = PerformCopyInitialization(Entity, lbrac, Owned(argExpr)); |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 1185 | if (ArgE.isInvalid()) |
| 1186 | IsError = true; |
| 1187 | else |
| 1188 | Args[i] = ArgE.takeAs<Expr>(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1189 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1190 | |
| 1191 | // Promote additional arguments to variadic methods. |
| 1192 | if (Method->isVariadic()) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1193 | for (unsigned i = NumNamedArgs; i < NumArgs; ++i) { |
| 1194 | if (Args[i]->isTypeDependent()) |
| 1195 | continue; |
| 1196 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1197 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
| 1198 | IsError |= Arg.isInvalid(); |
| 1199 | Args[i] = Arg.take(); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1200 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1201 | } else { |
| 1202 | // Check for extra arguments to non-variadic methods. |
| 1203 | if (NumArgs != NumNamedArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | Diag(Args[NumNamedArgs]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1205 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 1206 | << 2 /*method*/ << NumNamedArgs << NumArgs |
| 1207 | << Method->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1208 | << SourceRange(Args[NumNamedArgs]->getLocStart(), |
| 1209 | Args[NumArgs-1]->getLocEnd()); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1213 | DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs); |
Jean-Daniel Dupas | 29c3f81 | 2012-01-17 20:03:31 +0000 | [diff] [blame] | 1214 | |
| 1215 | // Do additional checkings on method. |
| 1216 | IsError |= CheckObjCMethodCall(Method, lbrac, Args, NumArgs); |
| 1217 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 1218 | return IsError; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 1221 | bool Sema::isSelfExpr(Expr *receiver) { |
Fariborz Jahanian | f2d74cc | 2011-03-27 19:53:47 +0000 | [diff] [blame] | 1222 | // 'self' is objc 'self' in an objc method only. |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1223 | ObjCMethodDecl *method = |
| 1224 | dyn_cast<ObjCMethodDecl>(CurContext->getNonClosureAncestor()); |
| 1225 | if (!method) return false; |
| 1226 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1227 | receiver = receiver->IgnoreParenLValueCasts(); |
| 1228 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(receiver)) |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1229 | if (DRE->getDecl() == method->getSelfDecl()) |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 1230 | return true; |
| 1231 | return false; |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 1234 | // Helper method for ActOnClassMethod/ActOnInstanceMethod. |
| 1235 | // Will search "local" class/category implementations for a method decl. |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 1236 | // 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] | 1237 | // Returns 0 if no method is found. |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1238 | ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel, |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 1239 | ObjCInterfaceDecl *ClassDecl) { |
| 1240 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1241 | // lookup in class and all superclasses |
| 1242 | while (ClassDecl && !Method) { |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1243 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1244 | Method = ImpDecl->getClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1245 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1246 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 1247 | if (!Method) |
| 1248 | Method = ClassDecl->getCategoryClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1249 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1250 | // Before we give up, check if the selector is an instance method. |
| 1251 | // But only in the root. This matches gcc's behaviour and what the |
| 1252 | // runtime expects. |
| 1253 | if (!Method && !ClassDecl->getSuperClass()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1254 | Method = ClassDecl->lookupInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | // Look through local category implementations associated |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1256 | // with the root class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | if (!Method) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1258 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 1259 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1260 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1261 | ClassDecl = ClassDecl->getSuperClass(); |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 1262 | } |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1263 | return Method; |
| 1264 | } |
| 1265 | |
| 1266 | ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, |
| 1267 | ObjCInterfaceDecl *ClassDecl) { |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 1268 | if (!ClassDecl->hasDefinition()) |
| 1269 | return 0; |
| 1270 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1271 | ObjCMethodDecl *Method = 0; |
| 1272 | while (ClassDecl && !Method) { |
| 1273 | // If we have implementations in scope, check "private" methods. |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1274 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1275 | Method = ImpDecl->getInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1276 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1277 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 1278 | if (!Method) |
| 1279 | Method = ClassDecl->getCategoryInstanceMethod(Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 1280 | ClassDecl = ClassDecl->getSuperClass(); |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 1281 | } |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 1282 | return Method; |
| 1283 | } |
| 1284 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1285 | /// LookupMethodInType - Look up a method in an ObjCObjectType. |
| 1286 | ObjCMethodDecl *Sema::LookupMethodInObjectType(Selector sel, QualType type, |
| 1287 | bool isInstance) { |
| 1288 | const ObjCObjectType *objType = type->castAs<ObjCObjectType>(); |
| 1289 | if (ObjCInterfaceDecl *iface = objType->getInterface()) { |
| 1290 | // Look it up in the main interface (and categories, etc.) |
| 1291 | if (ObjCMethodDecl *method = iface->lookupMethod(sel, isInstance)) |
| 1292 | return method; |
| 1293 | |
| 1294 | // Okay, look for "private" methods declared in any |
| 1295 | // @implementations we've seen. |
| 1296 | if (isInstance) { |
| 1297 | if (ObjCMethodDecl *method = LookupPrivateInstanceMethod(sel, iface)) |
| 1298 | return method; |
| 1299 | } else { |
| 1300 | if (ObjCMethodDecl *method = LookupPrivateClassMethod(sel, iface)) |
| 1301 | return method; |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | // Check qualifiers. |
| 1306 | for (ObjCObjectType::qual_iterator |
| 1307 | i = objType->qual_begin(), e = objType->qual_end(); i != e; ++i) |
| 1308 | if (ObjCMethodDecl *method = (*i)->lookupMethod(sel, isInstance)) |
| 1309 | return method; |
| 1310 | |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
Fariborz Jahanian | 6147806 | 2011-03-09 20:18:06 +0000 | [diff] [blame] | 1314 | /// LookupMethodInQualifiedType - Lookups up a method in protocol qualifier |
| 1315 | /// list of a qualified objective pointer type. |
| 1316 | ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel, |
| 1317 | const ObjCObjectPointerType *OPT, |
| 1318 | bool Instance) |
| 1319 | { |
| 1320 | ObjCMethodDecl *MD = 0; |
| 1321 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 1322 | E = OPT->qual_end(); I != E; ++I) { |
| 1323 | ObjCProtocolDecl *PROTO = (*I); |
| 1324 | if ((MD = PROTO->lookupMethod(Sel, Instance))) { |
| 1325 | return MD; |
| 1326 | } |
| 1327 | } |
| 1328 | return 0; |
| 1329 | } |
| 1330 | |
Fariborz Jahanian | 9879556 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1331 | static void DiagnoseARCUseOfWeakReceiver(Sema &S, Expr *Receiver) { |
| 1332 | if (!Receiver) |
Fariborz Jahanian | 289677d | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1333 | return; |
| 1334 | |
Fariborz Jahanian | 9879556 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1335 | Expr *RExpr = Receiver->IgnoreParenImpCasts(); |
| 1336 | SourceLocation Loc = RExpr->getLocStart(); |
| 1337 | QualType T = RExpr->getType(); |
| 1338 | ObjCPropertyDecl *PDecl = 0; |
| 1339 | ObjCMethodDecl *GDecl = 0; |
| 1340 | if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(RExpr)) { |
| 1341 | RExpr = POE->getSyntacticForm(); |
| 1342 | if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(RExpr)) { |
| 1343 | if (PRE->isImplicitProperty()) { |
| 1344 | GDecl = PRE->getImplicitPropertyGetter(); |
| 1345 | if (GDecl) { |
| 1346 | T = GDecl->getResultType(); |
| 1347 | } |
| 1348 | } |
| 1349 | else { |
| 1350 | PDecl = PRE->getExplicitProperty(); |
| 1351 | if (PDecl) { |
| 1352 | T = PDecl->getType(); |
| 1353 | } |
| 1354 | } |
Fariborz Jahanian | 289677d | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1355 | } |
Fariborz Jahanian | 9879556 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | if (T.getObjCLifetime() == Qualifiers::OCL_Weak) { |
| 1359 | S.Diag(Loc, diag::warn_receiver_is_weak) |
| 1360 | << ((!PDecl && !GDecl) ? 0 : (PDecl ? 1 : 2)); |
| 1361 | if (PDecl) |
| 1362 | S.Diag(PDecl->getLocation(), diag::note_property_declare); |
| 1363 | else if (GDecl) |
| 1364 | S.Diag(GDecl->getLocation(), diag::note_method_declared_at) << GDecl; |
Fariborz Jahanian | 289677d | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1365 | return; |
| 1366 | } |
| 1367 | |
Fariborz Jahanian | 9879556 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 1368 | if (PDecl && |
| 1369 | (PDecl->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak)) { |
| 1370 | S.Diag(Loc, diag::warn_receiver_is_weak) << 1; |
| 1371 | S.Diag(PDecl->getLocation(), diag::note_property_declare); |
| 1372 | } |
Fariborz Jahanian | 289677d | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1375 | /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an |
| 1376 | /// objective C interface. This is a property reference expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1377 | ExprResult Sema:: |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1378 | HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT, |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1379 | Expr *BaseExpr, SourceLocation OpLoc, |
| 1380 | DeclarationName MemberName, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1381 | SourceLocation MemberLoc, |
| 1382 | SourceLocation SuperLoc, QualType SuperType, |
| 1383 | bool Super) { |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1384 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 1385 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
Douglas Gregor | 109ec1b | 2011-04-20 18:19:55 +0000 | [diff] [blame] | 1386 | |
| 1387 | if (MemberName.getNameKind() != DeclarationName::Identifier) { |
| 1388 | Diag(MemberLoc, diag::err_invalid_property_name) |
| 1389 | << MemberName << QualType(OPT, 0); |
| 1390 | return ExprError(); |
| 1391 | } |
| 1392 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1393 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1394 | SourceRange BaseRange = Super? SourceRange(SuperLoc) |
| 1395 | : BaseExpr->getSourceRange(); |
| 1396 | if (RequireCompleteType(MemberLoc, OPT->getPointeeType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 1397 | diag::err_property_not_found_forward_class, |
| 1398 | MemberName, BaseRange)) |
Fariborz Jahanian | 8b1aba4 | 2010-12-16 00:56:28 +0000 | [diff] [blame] | 1399 | return ExprError(); |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1400 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1401 | // Search for a declared property first. |
| 1402 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
| 1403 | // Check whether we can reference this property. |
| 1404 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1405 | return ExprError(); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1406 | if (Super) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1407 | return Owned(new (Context) ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1408 | VK_LValue, OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1409 | MemberLoc, |
| 1410 | SuperLoc, SuperType)); |
| 1411 | else |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1412 | return Owned(new (Context) ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1413 | VK_LValue, OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1414 | MemberLoc, BaseExpr)); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1415 | } |
| 1416 | // Check protocols on qualified interfaces. |
| 1417 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 1418 | E = OPT->qual_end(); I != E; ++I) |
| 1419 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 1420 | // Check whether we can reference this property. |
| 1421 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 1422 | return ExprError(); |
Fariborz Jahanian | 289677d | 2012-04-19 21:44:57 +0000 | [diff] [blame] | 1423 | |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1424 | if (Super) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1425 | return Owned(new (Context) ObjCPropertyRefExpr(PD, |
| 1426 | Context.PseudoObjectTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1427 | VK_LValue, |
| 1428 | OK_ObjCProperty, |
| 1429 | MemberLoc, |
| 1430 | SuperLoc, SuperType)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1431 | else |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1432 | return Owned(new (Context) ObjCPropertyRefExpr(PD, |
| 1433 | Context.PseudoObjectTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1434 | VK_LValue, |
| 1435 | OK_ObjCProperty, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1436 | MemberLoc, |
| 1437 | BaseExpr)); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1438 | } |
| 1439 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 1440 | // selector is implemented. |
| 1441 | |
| 1442 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 1443 | // shared with the code in ActOnInstanceMessage. |
| 1444 | |
| 1445 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 1446 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1447 | |
| 1448 | // May be founf in property's qualified list. |
| 1449 | if (!Getter) |
| 1450 | Getter = LookupMethodInQualifiedType(Sel, OPT, true); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1451 | |
| 1452 | // If this reference is in an @implementation, check for 'private' methods. |
| 1453 | if (!Getter) |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 1454 | Getter = IFace->lookupPrivateMethod(Sel); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1455 | |
| 1456 | // Look through local category implementations associated with the class. |
| 1457 | if (!Getter) |
| 1458 | Getter = IFace->getCategoryInstanceMethod(Sel); |
| 1459 | if (Getter) { |
| 1460 | // Check if we can reference this property. |
| 1461 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 1462 | return ExprError(); |
| 1463 | } |
| 1464 | // If we found a getter then this may be a valid dot-reference, we |
| 1465 | // will look for the matching setter, in case it is needed. |
| 1466 | Selector SetterSel = |
| 1467 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 1468 | PP.getSelectorTable(), Member); |
| 1469 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1470 | |
| 1471 | // May be founf in property's qualified list. |
| 1472 | if (!Setter) |
| 1473 | Setter = LookupMethodInQualifiedType(SetterSel, OPT, true); |
| 1474 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1475 | if (!Setter) { |
| 1476 | // If this reference is in an @implementation, also check for 'private' |
| 1477 | // methods. |
Fariborz Jahanian | 74b2756 | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 1478 | Setter = IFace->lookupPrivateMethod(SetterSel); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1479 | } |
| 1480 | // Look through local category implementations associated with the class. |
| 1481 | if (!Setter) |
| 1482 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 1483 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1484 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 1485 | return ExprError(); |
| 1486 | |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1487 | if (Getter || Setter) { |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1488 | if (Super) |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1489 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1490 | Context.PseudoObjectTy, |
| 1491 | VK_LValue, OK_ObjCProperty, |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1492 | MemberLoc, |
| 1493 | SuperLoc, SuperType)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1494 | else |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1495 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1496 | Context.PseudoObjectTy, |
| 1497 | VK_LValue, OK_ObjCProperty, |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1498 | MemberLoc, BaseExpr)); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1499 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | // Attempt to correct for typos in property names. |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1503 | DeclFilterCCC<ObjCPropertyDecl> Validator; |
| 1504 | if (TypoCorrection Corrected = CorrectTypo( |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1505 | DeclarationNameInfo(MemberName, MemberLoc), LookupOrdinaryName, NULL, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1506 | NULL, Validator, IFace, false, OPT)) { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1507 | ObjCPropertyDecl *Property = |
| 1508 | Corrected.getCorrectionDeclAs<ObjCPropertyDecl>(); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1509 | DeclarationName TypoResult = Corrected.getCorrection(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1510 | Diag(MemberLoc, diag::err_property_not_found_suggest) |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 1511 | << MemberName << QualType(OPT, 0) << TypoResult |
| 1512 | << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString()); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1513 | Diag(Property->getLocation(), diag::note_previous_decl) |
| 1514 | << Property->getDeclName(); |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1515 | return HandleExprPropertyRefExpr(OPT, BaseExpr, OpLoc, |
| 1516 | TypoResult, MemberLoc, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1517 | SuperLoc, SuperType, Super); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1518 | } |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1519 | ObjCInterfaceDecl *ClassDeclared; |
| 1520 | if (ObjCIvarDecl *Ivar = |
| 1521 | IFace->lookupInstanceVariable(Member, ClassDeclared)) { |
| 1522 | QualType T = Ivar->getType(); |
| 1523 | if (const ObjCObjectPointerType * OBJPT = |
| 1524 | T->getAsObjCInterfacePointerType()) { |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1525 | if (RequireCompleteType(MemberLoc, OBJPT->getPointeeType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 1526 | diag::err_property_not_as_forward_class, |
| 1527 | MemberName, BaseExpr)) |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1528 | return ExprError(); |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1529 | } |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1530 | Diag(MemberLoc, |
| 1531 | diag::err_ivar_access_using_property_syntax_suggest) |
| 1532 | << MemberName << QualType(OPT, 0) << Ivar->getDeclName() |
| 1533 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| 1534 | return ExprError(); |
Fariborz Jahanian | 41aadbc | 2011-02-17 01:26:14 +0000 | [diff] [blame] | 1535 | } |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1537 | Diag(MemberLoc, diag::err_property_not_found) |
| 1538 | << MemberName << QualType(OPT, 0); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1539 | if (Setter) |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1540 | Diag(Setter->getLocation(), diag::note_getter_unavailable) |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 1541 | << MemberName << BaseExpr->getSourceRange(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1542 | return ExprError(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | |
| 1546 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1547 | ExprResult Sema:: |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1548 | ActOnClassPropertyRefExpr(IdentifierInfo &receiverName, |
| 1549 | IdentifierInfo &propertyName, |
| 1550 | SourceLocation receiverNameLoc, |
| 1551 | SourceLocation propertyNameLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1552 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1553 | IdentifierInfo *receiverNamePtr = &receiverName; |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1554 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr, |
| 1555 | receiverNameLoc); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1556 | |
| 1557 | bool IsSuper = false; |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1558 | if (IFace == 0) { |
| 1559 | // If the "receiver" is 'super' in a method, handle it as an expression-like |
| 1560 | // property reference. |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1561 | if (receiverNamePtr->isStr("super")) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1562 | IsSuper = true; |
| 1563 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 1564 | if (ObjCMethodDecl *CurMethod = tryCaptureObjCSelf(receiverNameLoc)) { |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1565 | if (CurMethod->isInstanceMethod()) { |
| 1566 | QualType T = |
| 1567 | Context.getObjCInterfaceType(CurMethod->getClassInterface()); |
| 1568 | T = Context.getObjCObjectPointerType(T); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1569 | |
| 1570 | return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(), |
Fariborz Jahanian | 6326e05 | 2011-06-28 00:00:52 +0000 | [diff] [blame] | 1571 | /*BaseExpr*/0, |
| 1572 | SourceLocation()/*OpLoc*/, |
| 1573 | &propertyName, |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 1574 | propertyNameLoc, |
| 1575 | receiverNameLoc, T, true); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1576 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1577 | |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1578 | // Otherwise, if this is a class method, try dispatching to our |
| 1579 | // superclass. |
| 1580 | IFace = CurMethod->getClassInterface()->getSuperClass(); |
| 1581 | } |
John McCall | 26743b2 | 2011-02-03 09:00:02 +0000 | [diff] [blame] | 1582 | } |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1583 | |
| 1584 | if (IFace == 0) { |
| 1585 | Diag(receiverNameLoc, diag::err_expected_ident_or_lparen); |
| 1586 | return ExprError(); |
| 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | // Search for a declared property first. |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1591 | Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1592 | ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1593 | |
| 1594 | // If this reference is in an @implementation, check for 'private' methods. |
| 1595 | if (!Getter) |
| 1596 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1597 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1598 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1599 | Getter = ImpDecl->getClassMethod(Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1600 | |
| 1601 | if (Getter) { |
| 1602 | // FIXME: refactor/share with ActOnMemberReference(). |
| 1603 | // Check if we can reference this property. |
| 1604 | if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) |
| 1605 | return ExprError(); |
| 1606 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1608 | // Look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1609 | Selector SetterSel = |
| 1610 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Steve Naroff | fdc92b7 | 2009-03-10 17:24:38 +0000 | [diff] [blame] | 1611 | PP.getSelectorTable(), &propertyName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1612 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1613 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1614 | if (!Setter) { |
| 1615 | // If this reference is in an @implementation, also check for 'private' |
| 1616 | // methods. |
| 1617 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 1618 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 1619 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1620 | Setter = ImpDecl->getClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1621 | } |
| 1622 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 1623 | if (!Setter) |
| 1624 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1625 | |
| 1626 | if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) |
| 1627 | return ExprError(); |
| 1628 | |
| 1629 | if (Getter || Setter) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1630 | if (IsSuper) |
| 1631 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1632 | Context.PseudoObjectTy, |
| 1633 | VK_LValue, OK_ObjCProperty, |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1634 | propertyNameLoc, |
| 1635 | receiverNameLoc, |
| 1636 | Context.getObjCInterfaceType(IFace))); |
| 1637 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1638 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 1639 | Context.PseudoObjectTy, |
| 1640 | VK_LValue, OK_ObjCProperty, |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1641 | propertyNameLoc, |
| 1642 | receiverNameLoc, IFace)); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1643 | } |
| 1644 | return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) |
| 1645 | << &propertyName << Context.getObjCInterfaceType(IFace)); |
| 1646 | } |
| 1647 | |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1648 | namespace { |
| 1649 | |
| 1650 | class ObjCInterfaceOrSuperCCC : public CorrectionCandidateCallback { |
| 1651 | public: |
| 1652 | ObjCInterfaceOrSuperCCC(ObjCMethodDecl *Method) { |
| 1653 | // Determine whether "super" is acceptable in the current context. |
| 1654 | if (Method && Method->getClassInterface()) |
| 1655 | WantObjCSuper = Method->getClassInterface()->getSuperClass(); |
| 1656 | } |
| 1657 | |
| 1658 | virtual bool ValidateCandidate(const TypoCorrection &candidate) { |
| 1659 | return candidate.getCorrectionDeclAs<ObjCInterfaceDecl>() || |
| 1660 | candidate.isKeyword("super"); |
| 1661 | } |
| 1662 | }; |
| 1663 | |
| 1664 | } |
| 1665 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1666 | Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1667 | IdentifierInfo *Name, |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1668 | SourceLocation NameLoc, |
| 1669 | bool IsSuper, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1670 | bool HasTrailingDot, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1671 | ParsedType &ReceiverType) { |
| 1672 | ReceiverType = ParsedType(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1673 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1674 | // 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] | 1675 | // messaging super. If the identifier is "super" and there is a |
| 1676 | // trailing dot, it's an instance message. |
| 1677 | if (IsSuper && S->isInObjcMethodScope()) |
| 1678 | return HasTrailingDot? ObjCInstanceMessage : ObjCSuperMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1679 | |
| 1680 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| 1681 | LookupName(Result, S); |
| 1682 | |
| 1683 | switch (Result.getResultKind()) { |
| 1684 | case LookupResult::NotFound: |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 1685 | // Normal name lookup didn't find anything. If we're in an |
| 1686 | // 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] | 1687 | // FIXME: This is a hack. Ivar lookup should be part of normal |
| 1688 | // lookup. |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 1689 | if (ObjCMethodDecl *Method = getCurMethodDecl()) { |
Argyrios Kyrtzidis | ccc9e76 | 2011-11-09 00:22:48 +0000 | [diff] [blame] | 1690 | if (!Method->getClassInterface()) { |
| 1691 | // Fall back: let the parser try to parse it as an instance message. |
| 1692 | return ObjCInstanceMessage; |
| 1693 | } |
| 1694 | |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 1695 | ObjCInterfaceDecl *ClassDeclared; |
| 1696 | if (Method->getClassInterface()->lookupInstanceVariable(Name, |
| 1697 | ClassDeclared)) |
| 1698 | return ObjCInstanceMessage; |
| 1699 | } |
Douglas Gregor | 95f4292 | 2010-10-14 22:11:03 +0000 | [diff] [blame] | 1700 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1701 | // Break out; we'll perform typo correction below. |
| 1702 | break; |
| 1703 | |
| 1704 | case LookupResult::NotFoundInCurrentInstantiation: |
| 1705 | case LookupResult::FoundOverloaded: |
| 1706 | case LookupResult::FoundUnresolvedValue: |
| 1707 | case LookupResult::Ambiguous: |
| 1708 | Result.suppressDiagnostics(); |
| 1709 | return ObjCInstanceMessage; |
| 1710 | |
| 1711 | case LookupResult::Found: { |
Fariborz Jahanian | 8348de3 | 2011-02-08 00:23:07 +0000 | [diff] [blame] | 1712 | // If the identifier is a class or not, and there is a trailing dot, |
| 1713 | // it's an instance message. |
| 1714 | if (HasTrailingDot) |
| 1715 | return ObjCInstanceMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1716 | // We found something. If it's a type, then we have a class |
| 1717 | // message. Otherwise, it's an instance message. |
| 1718 | NamedDecl *ND = Result.getFoundDecl(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1719 | QualType T; |
| 1720 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 1721 | T = Context.getObjCInterfaceType(Class); |
| 1722 | else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| 1723 | T = Context.getTypeDeclType(Type); |
| 1724 | else |
| 1725 | return ObjCInstanceMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1727 | // We have a class message, and T is the type we're |
| 1728 | // messaging. Build source-location information for it. |
| 1729 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1730 | ReceiverType = CreateParsedType(T, TSInfo); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 1731 | return ObjCClassMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1735 | ObjCInterfaceOrSuperCCC Validator(getCurMethodDecl()); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1736 | if (TypoCorrection Corrected = CorrectTypo(Result.getLookupNameInfo(), |
| 1737 | Result.getLookupKind(), S, NULL, |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1738 | Validator)) { |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1739 | if (Corrected.isKeyword()) { |
| 1740 | // If we've found the keyword "super" (the only keyword that would be |
| 1741 | // returned by CorrectTypo), this is a send to super. |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1742 | Diag(NameLoc, diag::err_unknown_receiver_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1743 | << Name << Corrected.getCorrection() |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1744 | << FixItHint::CreateReplacement(SourceRange(NameLoc), "super"); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1745 | return ObjCSuperMessage; |
Kaelyn Uhrain | 2f4d88f | 2012-01-13 01:32:50 +0000 | [diff] [blame] | 1746 | } else if (ObjCInterfaceDecl *Class = |
| 1747 | Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| 1748 | // If we found a declaration, correct when it refers to an Objective-C |
| 1749 | // class. |
| 1750 | Diag(NameLoc, diag::err_unknown_receiver_suggest) |
| 1751 | << Name << Corrected.getCorrection() |
| 1752 | << FixItHint::CreateReplacement(SourceRange(NameLoc), |
| 1753 | Class->getNameAsString()); |
| 1754 | Diag(Class->getLocation(), diag::note_previous_decl) |
| 1755 | << Corrected.getCorrection(); |
| 1756 | |
| 1757 | QualType T = Context.getObjCInterfaceType(Class); |
| 1758 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
| 1759 | ReceiverType = CreateParsedType(T, TSInfo); |
| 1760 | return ObjCClassMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | // Fall back: let the parser try to parse it as an instance message. |
| 1765 | return ObjCInstanceMessage; |
| 1766 | } |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 1767 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1768 | ExprResult Sema::ActOnSuperMessage(Scope *S, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1769 | SourceLocation SuperLoc, |
| 1770 | Selector Sel, |
| 1771 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1772 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1773 | SourceLocation RBracLoc, |
| 1774 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1775 | // Determine whether we are inside a method or not. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 1776 | ObjCMethodDecl *Method = tryCaptureObjCSelf(SuperLoc); |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1777 | if (!Method) { |
| 1778 | Diag(SuperLoc, diag::err_invalid_receiver_to_message_super); |
| 1779 | return ExprError(); |
| 1780 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1782 | ObjCInterfaceDecl *Class = Method->getClassInterface(); |
| 1783 | if (!Class) { |
| 1784 | Diag(SuperLoc, diag::error_no_super_class_message) |
| 1785 | << Method->getDeclName(); |
| 1786 | return ExprError(); |
| 1787 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1788 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1789 | ObjCInterfaceDecl *Super = Class->getSuperClass(); |
| 1790 | if (!Super) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1791 | // The current class does not have a superclass. |
Ted Kremenek | e00909a | 2011-01-23 17:21:34 +0000 | [diff] [blame] | 1792 | Diag(SuperLoc, diag::error_root_class_cannot_use_super) |
| 1793 | << Class->getIdentifier(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1794 | return ExprError(); |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 1795 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1796 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1797 | // We are in a method whose class has a superclass, so 'super' |
| 1798 | // is acting as a keyword. |
| 1799 | if (Method->isInstanceMethod()) { |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 1800 | if (Sel.getMethodFamily() == OMF_dealloc) |
| 1801 | ObjCShouldCallSuperDealloc = false; |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 1802 | if (Sel.getMethodFamily() == OMF_finalize) |
| 1803 | ObjCShouldCallSuperFinalize = false; |
Nico Weber | 9a1ecf0 | 2011-08-22 17:25:57 +0000 | [diff] [blame] | 1804 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1805 | // Since we are in an instance method, this is an instance |
| 1806 | // message to the superclass instance. |
| 1807 | QualType SuperTy = Context.getObjCInterfaceType(Super); |
| 1808 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1809 | return BuildInstanceMessage(0, SuperTy, SuperLoc, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1810 | Sel, /*Method=*/0, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1811 | LBracLoc, SelectorLocs, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1812 | } |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 1813 | |
| 1814 | // Since we are in a class method, this is a class message to |
| 1815 | // the superclass. |
| 1816 | return BuildClassMessage(/*ReceiverTypeInfo=*/0, |
| 1817 | Context.getObjCInterfaceType(Super), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 1818 | SuperLoc, Sel, /*Method=*/0, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1819 | LBracLoc, SelectorLocs, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1822 | |
| 1823 | ExprResult Sema::BuildClassMessageImplicit(QualType ReceiverType, |
| 1824 | bool isSuperReceiver, |
| 1825 | SourceLocation Loc, |
| 1826 | Selector Sel, |
| 1827 | ObjCMethodDecl *Method, |
| 1828 | MultiExprArg Args) { |
| 1829 | TypeSourceInfo *receiverTypeInfo = 0; |
| 1830 | if (!ReceiverType.isNull()) |
| 1831 | receiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType); |
| 1832 | |
| 1833 | return BuildClassMessage(receiverTypeInfo, ReceiverType, |
| 1834 | /*SuperLoc=*/isSuperReceiver ? Loc : SourceLocation(), |
| 1835 | Sel, Method, Loc, Loc, Loc, Args, |
| 1836 | /*isImplicit=*/true); |
| 1837 | |
| 1838 | } |
| 1839 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 1840 | static void applyCocoaAPICheck(Sema &S, const ObjCMessageExpr *Msg, |
| 1841 | unsigned DiagID, |
| 1842 | bool (*refactor)(const ObjCMessageExpr *, |
| 1843 | const NSAPI &, edit::Commit &)) { |
| 1844 | SourceLocation MsgLoc = Msg->getExprLoc(); |
| 1845 | if (S.Diags.getDiagnosticLevel(DiagID, MsgLoc) == DiagnosticsEngine::Ignored) |
| 1846 | return; |
| 1847 | |
| 1848 | SourceManager &SM = S.SourceMgr; |
| 1849 | edit::Commit ECommit(SM, S.LangOpts); |
| 1850 | if (refactor(Msg,*S.NSAPIObj, ECommit)) { |
| 1851 | DiagnosticBuilder Builder = S.Diag(MsgLoc, DiagID) |
| 1852 | << Msg->getSelector() << Msg->getSourceRange(); |
| 1853 | // FIXME: Don't emit diagnostic at all if fixits are non-commitable. |
| 1854 | if (!ECommit.isCommitable()) |
| 1855 | return; |
| 1856 | for (edit::Commit::edit_iterator |
| 1857 | I = ECommit.edit_begin(), E = ECommit.edit_end(); I != E; ++I) { |
| 1858 | const edit::Commit::Edit &Edit = *I; |
| 1859 | switch (Edit.Kind) { |
| 1860 | case edit::Commit::Act_Insert: |
| 1861 | Builder.AddFixItHint(FixItHint::CreateInsertion(Edit.OrigLoc, |
| 1862 | Edit.Text, |
| 1863 | Edit.BeforePrev)); |
| 1864 | break; |
| 1865 | case edit::Commit::Act_InsertFromRange: |
| 1866 | Builder.AddFixItHint( |
| 1867 | FixItHint::CreateInsertionFromRange(Edit.OrigLoc, |
| 1868 | Edit.getInsertFromRange(SM), |
| 1869 | Edit.BeforePrev)); |
| 1870 | break; |
| 1871 | case edit::Commit::Act_Remove: |
| 1872 | Builder.AddFixItHint(FixItHint::CreateRemoval(Edit.getFileRange(SM))); |
| 1873 | break; |
| 1874 | } |
| 1875 | } |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | static void checkCocoaAPI(Sema &S, const ObjCMessageExpr *Msg) { |
| 1880 | applyCocoaAPICheck(S, Msg, diag::warn_objc_redundant_literal_use, |
| 1881 | edit::rewriteObjCRedundantCallWithLiteral); |
| 1882 | } |
| 1883 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1884 | /// \brief Build an Objective-C class message expression. |
| 1885 | /// |
| 1886 | /// This routine takes care of both normal class messages and |
| 1887 | /// class messages to the superclass. |
| 1888 | /// |
| 1889 | /// \param ReceiverTypeInfo Type source information that describes the |
| 1890 | /// receiver of this message. This may be NULL, in which case we are |
| 1891 | /// sending to the superclass and \p SuperLoc must be a valid source |
| 1892 | /// location. |
| 1893 | |
| 1894 | /// \param ReceiverType The type of the object receiving the |
| 1895 | /// message. When \p ReceiverTypeInfo is non-NULL, this is the same |
| 1896 | /// type as that refers to. For a superclass send, this is the type of |
| 1897 | /// the superclass. |
| 1898 | /// |
| 1899 | /// \param SuperLoc The location of the "super" keyword in a |
| 1900 | /// superclass message. |
| 1901 | /// |
| 1902 | /// \param Sel The selector to which the message is being sent. |
| 1903 | /// |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1904 | /// \param Method The method that this class message is invoking, if |
| 1905 | /// already known. |
| 1906 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1907 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 1908 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1909 | /// \param RBrac The location of the closing square bracket ']'. |
| 1910 | /// |
| 1911 | /// \param Args The message arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1912 | ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1913 | QualType ReceiverType, |
| 1914 | SourceLocation SuperLoc, |
| 1915 | Selector Sel, |
| 1916 | ObjCMethodDecl *Method, |
| 1917 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1918 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1919 | SourceLocation RBracLoc, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1920 | MultiExprArg ArgsIn, |
| 1921 | bool isImplicit) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1922 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 1923 | : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin(); |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 1924 | if (LBracLoc.isInvalid()) { |
| 1925 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 1926 | << FixItHint::CreateInsertion(Loc, "["); |
| 1927 | LBracLoc = Loc; |
| 1928 | } |
| 1929 | |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1930 | if (ReceiverType->isDependentType()) { |
| 1931 | // If the receiver type is dependent, we can't type-check anything |
| 1932 | // at this point. Build a dependent expression. |
| 1933 | unsigned NumArgs = ArgsIn.size(); |
| 1934 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 1935 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1936 | return Owned(ObjCMessageExpr::Create(Context, ReceiverType, |
| 1937 | VK_RValue, LBracLoc, ReceiverTypeInfo, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 1938 | Sel, SelectorLocs, /*Method=*/0, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 1939 | makeArrayRef(Args, NumArgs),RBracLoc, |
| 1940 | isImplicit)); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 1941 | } |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 1942 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1943 | // Find the class to which we are sending this message. |
| 1944 | ObjCInterfaceDecl *Class = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 1945 | const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>(); |
| 1946 | if (!ClassType || !(Class = ClassType->getInterface())) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1947 | Diag(Loc, diag::err_invalid_receiver_class_message) |
| 1948 | << ReceiverType; |
| 1949 | return ExprError(); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 1950 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1951 | assert(Class && "We don't know which class we're messaging?"); |
Fariborz Jahanian | 43bcdb2 | 2011-10-15 19:18:36 +0000 | [diff] [blame] | 1952 | // objc++ diagnoses during typename annotation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1953 | if (!getLangOpts().CPlusPlus) |
Fariborz Jahanian | 43bcdb2 | 2011-10-15 19:18:36 +0000 | [diff] [blame] | 1954 | (void)DiagnoseUseOfDecl(Class, Loc); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1955 | // Find the method we are messaging. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1956 | if (!Method) { |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 1957 | SourceRange TypeRange |
| 1958 | = SuperLoc.isValid()? SourceRange(SuperLoc) |
| 1959 | : ReceiverTypeInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 1960 | if (RequireCompleteType(Loc, Context.getObjCInterfaceType(Class), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1961 | (getLangOpts().ObjCAutoRefCount |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 1962 | ? diag::err_arc_receiver_forward_class |
| 1963 | : diag::warn_receiver_forward_class), |
| 1964 | TypeRange)) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1965 | // A forward class used in messaging is treated as a 'Class' |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1966 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 1967 | SourceRange(LBracLoc, RBracLoc)); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1968 | if (Method && !getLangOpts().ObjCAutoRefCount) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1969 | Diag(Method->getLocation(), diag::note_method_sent_forward_class) |
| 1970 | << Method->getDeclName(); |
| 1971 | } |
| 1972 | if (!Method) |
| 1973 | Method = Class->lookupClassMethod(Sel); |
| 1974 | |
| 1975 | // If we have an implementation in scope, check "private" methods. |
| 1976 | if (!Method) |
| 1977 | Method = LookupPrivateClassMethod(Sel, Class); |
| 1978 | |
| 1979 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 1980 | return ExprError(); |
Fariborz Jahanian | 89bc314 | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 1981 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1983 | // Check the argument types and determine the result type. |
| 1984 | QualType ReturnType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1985 | ExprValueKind VK = VK_RValue; |
| 1986 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1987 | unsigned NumArgs = ArgsIn.size(); |
| 1988 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 1989 | if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, true, |
| 1990 | SuperLoc.isValid(), LBracLoc, RBracLoc, |
| 1991 | ReturnType, VK)) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1992 | return ExprError(); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1993 | |
Douglas Gregor | 483dd2f | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 1994 | if (Method && !Method->getResultType()->isVoidType() && |
| 1995 | RequireCompleteType(LBracLoc, Method->getResultType(), |
| 1996 | diag::err_illegal_message_expr_incomplete_type)) |
| 1997 | return ExprError(); |
| 1998 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1999 | // Construct the appropriate ObjCMessageExpr. |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2000 | ObjCMessageExpr *Result; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2001 | if (SuperLoc.isValid()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2002 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2003 | SuperLoc, /*IsInstanceSuper=*/false, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2004 | ReceiverType, Sel, SelectorLocs, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2005 | Method, makeArrayRef(Args, NumArgs), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2006 | RBracLoc, isImplicit); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2007 | else { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2008 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2009 | ReceiverTypeInfo, Sel, SelectorLocs, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2010 | Method, makeArrayRef(Args, NumArgs), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2011 | RBracLoc, isImplicit); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2012 | if (!isImplicit) |
| 2013 | checkCocoaAPI(*this, Result); |
| 2014 | } |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2015 | return MaybeBindToTemporary(Result); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2018 | // ActOnClassMessage - used for both unary and keyword messages. |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2019 | // ArgExprs is optional - if it is present, the number of expressions |
| 2020 | // is obtained from Sel.getNumArgs(). |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2021 | ExprResult Sema::ActOnClassMessage(Scope *S, |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 2022 | ParsedType Receiver, |
| 2023 | Selector Sel, |
| 2024 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2025 | ArrayRef<SourceLocation> SelectorLocs, |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 2026 | SourceLocation RBracLoc, |
| 2027 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2028 | TypeSourceInfo *ReceiverTypeInfo; |
| 2029 | QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo); |
| 2030 | if (ReceiverType.isNull()) |
| 2031 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2033 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2034 | if (!ReceiverTypeInfo) |
| 2035 | ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc); |
| 2036 | |
| 2037 | return BuildClassMessage(ReceiverTypeInfo, ReceiverType, |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2038 | /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2039 | LBracLoc, SelectorLocs, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2040 | } |
| 2041 | |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2042 | ExprResult Sema::BuildInstanceMessageImplicit(Expr *Receiver, |
| 2043 | QualType ReceiverType, |
| 2044 | SourceLocation Loc, |
| 2045 | Selector Sel, |
| 2046 | ObjCMethodDecl *Method, |
| 2047 | MultiExprArg Args) { |
| 2048 | return BuildInstanceMessage(Receiver, ReceiverType, |
| 2049 | /*SuperLoc=*/!Receiver ? Loc : SourceLocation(), |
| 2050 | Sel, Method, Loc, Loc, Loc, Args, |
| 2051 | /*isImplicit=*/true); |
| 2052 | } |
| 2053 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2054 | /// \brief Build an Objective-C instance message expression. |
| 2055 | /// |
| 2056 | /// This routine takes care of both normal instance messages and |
| 2057 | /// instance messages to the superclass instance. |
| 2058 | /// |
| 2059 | /// \param Receiver The expression that computes the object that will |
| 2060 | /// receive this message. This may be empty, in which case we are |
| 2061 | /// sending to the superclass instance and \p SuperLoc must be a valid |
| 2062 | /// source location. |
| 2063 | /// |
| 2064 | /// \param ReceiverType The (static) type of the object receiving the |
| 2065 | /// message. When a \p Receiver expression is provided, this is the |
| 2066 | /// same type as that expression. For a superclass instance send, this |
| 2067 | /// is a pointer to the type of the superclass. |
| 2068 | /// |
| 2069 | /// \param SuperLoc The location of the "super" keyword in a |
| 2070 | /// superclass instance message. |
| 2071 | /// |
| 2072 | /// \param Sel The selector to which the message is being sent. |
| 2073 | /// |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2074 | /// \param Method The method that this instance message is invoking, if |
| 2075 | /// already known. |
| 2076 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2077 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 2078 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2079 | /// \param RBrac The location of the closing square bracket ']'. |
| 2080 | /// |
| 2081 | /// \param Args The message arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2082 | ExprResult Sema::BuildInstanceMessage(Expr *Receiver, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2083 | QualType ReceiverType, |
| 2084 | SourceLocation SuperLoc, |
| 2085 | Selector Sel, |
| 2086 | ObjCMethodDecl *Method, |
| 2087 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2088 | ArrayRef<SourceLocation> SelectorLocs, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2089 | SourceLocation RBracLoc, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2090 | MultiExprArg ArgsIn, |
| 2091 | bool isImplicit) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 2092 | // The location of the receiver. |
| 2093 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart(); |
| 2094 | |
| 2095 | if (LBracLoc.isInvalid()) { |
| 2096 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 2097 | << FixItHint::CreateInsertion(Loc, "["); |
| 2098 | LBracLoc = Loc; |
| 2099 | } |
| 2100 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2101 | // If we have a receiver expression, perform appropriate promotions |
| 2102 | // and determine receiver type. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2103 | if (Receiver) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2104 | if (Receiver->hasPlaceholderType()) { |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 2105 | ExprResult Result; |
| 2106 | if (Receiver->getType() == Context.UnknownAnyTy) |
| 2107 | Result = forceUnknownAnyToType(Receiver, Context.getObjCIdType()); |
| 2108 | else |
| 2109 | Result = CheckPlaceholderExpr(Receiver); |
| 2110 | if (Result.isInvalid()) return ExprError(); |
| 2111 | Receiver = Result.take(); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2112 | } |
| 2113 | |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2114 | if (Receiver->isTypeDependent()) { |
| 2115 | // If the receiver is type-dependent, we can't type-check anything |
| 2116 | // at this point. Build a dependent expression. |
| 2117 | unsigned NumArgs = ArgsIn.size(); |
| 2118 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 2119 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
| 2120 | return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2121 | VK_RValue, LBracLoc, Receiver, Sel, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2122 | SelectorLocs, /*Method=*/0, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2123 | makeArrayRef(Args, NumArgs), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2124 | RBracLoc, isImplicit)); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2127 | // If necessary, apply function/array conversion to the receiver. |
| 2128 | // C99 6.7.5.3p[7,8]. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2129 | ExprResult Result = DefaultFunctionArrayLvalueConversion(Receiver); |
| 2130 | if (Result.isInvalid()) |
| 2131 | return ExprError(); |
| 2132 | Receiver = Result.take(); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2133 | ReceiverType = Receiver->getType(); |
| 2134 | } |
| 2135 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2136 | if (!Method) { |
| 2137 | // Handle messages to id. |
Fariborz Jahanian | ba55198 | 2010-08-10 18:10:50 +0000 | [diff] [blame] | 2138 | bool receiverIsId = ReceiverType->isObjCIdType(); |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2139 | if (receiverIsId || ReceiverType->isBlockPointerType() || |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2140 | (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) { |
| 2141 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2142 | SourceRange(LBracLoc, RBracLoc), |
| 2143 | receiverIsId); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2144 | if (!Method) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2145 | Method = LookupFactoryMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2146 | SourceRange(LBracLoc, RBracLoc), |
| 2147 | receiverIsId); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2148 | } else if (ReceiverType->isObjCClassType() || |
| 2149 | ReceiverType->isObjCQualifiedClassType()) { |
| 2150 | // Handle messages to Class. |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2151 | // We allow sending a message to a qualified Class ("Class<foo>"), which |
| 2152 | // is ok as long as one of the protocols implements the selector (if not, warn). |
| 2153 | if (const ObjCObjectPointerType *QClassTy |
| 2154 | = ReceiverType->getAsObjCQualifiedClassType()) { |
| 2155 | // Search protocols for class methods. |
| 2156 | Method = LookupMethodInQualifiedType(Sel, QClassTy, false); |
| 2157 | if (!Method) { |
| 2158 | Method = LookupMethodInQualifiedType(Sel, QClassTy, true); |
| 2159 | // warn if instance method found for a Class message. |
| 2160 | if (Method) { |
| 2161 | Diag(Loc, diag::warn_instance_method_on_class_found) |
| 2162 | << Method->getSelector() << Sel; |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2163 | Diag(Method->getLocation(), diag::note_method_declared_at) |
| 2164 | << Method->getDeclName(); |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2165 | } |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 2166 | } |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2167 | } else { |
| 2168 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 2169 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { |
| 2170 | // First check the public methods in the class interface. |
| 2171 | Method = ClassDecl->lookupClassMethod(Sel); |
| 2172 | |
| 2173 | if (!Method) |
| 2174 | Method = LookupPrivateClassMethod(Sel, ClassDecl); |
| 2175 | } |
| 2176 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 2177 | return ExprError(); |
| 2178 | } |
| 2179 | if (!Method) { |
| 2180 | // If not messaging 'self', look for any factory method named 'Sel'. |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 2181 | if (!Receiver || !isSelfExpr(Receiver)) { |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2182 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 2183 | SourceRange(LBracLoc, RBracLoc), |
| 2184 | true); |
| 2185 | if (!Method) { |
| 2186 | // If no class (factory) method was found, check if an _instance_ |
| 2187 | // method of the same name exists in the root class only. |
| 2188 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 2189 | SourceRange(LBracLoc, RBracLoc), |
Fariborz Jahanian | 759abb4 | 2011-04-06 18:40:08 +0000 | [diff] [blame] | 2190 | true); |
| 2191 | if (Method) |
| 2192 | if (const ObjCInterfaceDecl *ID = |
| 2193 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { |
| 2194 | if (ID->getSuperClass()) |
| 2195 | Diag(Loc, diag::warn_root_inst_method_not_found) |
| 2196 | << Sel << SourceRange(LBracLoc, RBracLoc); |
| 2197 | } |
| 2198 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2199 | } |
| 2200 | } |
| 2201 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2202 | } else { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2203 | ObjCInterfaceDecl* ClassDecl = 0; |
| 2204 | |
| 2205 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 2206 | // long as one of the protocols implements the selector (if not, warn). |
| 2207 | if (const ObjCObjectPointerType *QIdTy |
| 2208 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 2209 | // Search protocols for instance methods. |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2210 | Method = LookupMethodInQualifiedType(Sel, QIdTy, true); |
| 2211 | if (!Method) |
| 2212 | Method = LookupMethodInQualifiedType(Sel, QIdTy, false); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2213 | } else if (const ObjCObjectPointerType *OCIType |
| 2214 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 2215 | // We allow sending a message to a pointer to an interface (an object). |
| 2216 | ClassDecl = OCIType->getInterfaceDecl(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2217 | |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2218 | // Try to complete the type. Under ARC, this is a hard error from which |
| 2219 | // we don't try to recover. |
| 2220 | const ObjCInterfaceDecl *forwardClass = 0; |
| 2221 | if (RequireCompleteType(Loc, OCIType->getPointeeType(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2222 | getLangOpts().ObjCAutoRefCount |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame^] | 2223 | ? diag::err_arc_receiver_forward_instance |
| 2224 | : diag::warn_receiver_forward_instance, |
| 2225 | Receiver? Receiver->getSourceRange() |
| 2226 | : SourceRange(SuperLoc))) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2227 | if (getLangOpts().ObjCAutoRefCount) |
Douglas Gregor | b302996 | 2011-11-14 22:10:01 +0000 | [diff] [blame] | 2228 | return ExprError(); |
| 2229 | |
| 2230 | forwardClass = OCIType->getInterfaceDecl(); |
Fariborz Jahanian | 4cc9b10 | 2012-02-03 01:02:44 +0000 | [diff] [blame] | 2231 | Diag(Receiver ? Receiver->getLocStart() |
| 2232 | : SuperLoc, diag::note_receiver_is_id); |
Douglas Gregor | 2e5c15b | 2011-12-15 05:27:12 +0000 | [diff] [blame] | 2233 | Method = 0; |
| 2234 | } else { |
| 2235 | Method = ClassDecl->lookupInstanceMethod(Sel); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2236 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2237 | |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2238 | if (!Method) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2239 | // Search protocol qualifiers. |
Fariborz Jahanian | 27569b0 | 2011-03-09 22:17:12 +0000 | [diff] [blame] | 2240 | Method = LookupMethodInQualifiedType(Sel, OCIType, true); |
| 2241 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2242 | if (!Method) { |
| 2243 | // If we have implementations in scope, check "private" methods. |
| 2244 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 2245 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2246 | if (!Method && getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2247 | Diag(Loc, diag::err_arc_may_not_respond) |
| 2248 | << OCIType->getPointeeType() << Sel; |
| 2249 | return ExprError(); |
| 2250 | } |
| 2251 | |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 2252 | if (!Method && (!Receiver || !isSelfExpr(Receiver))) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2253 | // If we still haven't found a method, look in the global pool. This |
| 2254 | // behavior isn't very desirable, however we need it for GCC |
| 2255 | // compatibility. FIXME: should we deviate?? |
| 2256 | if (OCIType->qual_empty()) { |
| 2257 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 2258 | SourceRange(LBracLoc, RBracLoc)); |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 2259 | if (Method && !forwardClass) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2260 | Diag(Loc, diag::warn_maynot_respond) |
| 2261 | << OCIType->getInterfaceDecl()->getIdentifier() << Sel; |
| 2262 | } |
| 2263 | } |
| 2264 | } |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 2265 | if (Method && DiagnoseUseOfDecl(Method, Loc, forwardClass)) |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2266 | return ExprError(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2267 | } else if (!getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2268 | !Context.getObjCIdType().isNull() && |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 2269 | (ReceiverType->isPointerType() || |
| 2270 | ReceiverType->isIntegerType())) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2271 | // Implicitly convert integers and pointers to 'id' but emit a warning. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2272 | // But not in ARC. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2273 | Diag(Loc, diag::warn_bad_receiver_type) |
| 2274 | << ReceiverType |
| 2275 | << Receiver->getSourceRange(); |
| 2276 | if (ReceiverType->isPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2277 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2278 | CK_CPointerToObjCPointerCast).take(); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 2279 | else { |
| 2280 | // TODO: specialized warning on null receivers? |
| 2281 | bool IsNull = Receiver->isNullPointerConstant(Context, |
| 2282 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2283 | Receiver = ImpCastExprToType(Receiver, Context.getObjCIdType(), |
| 2284 | IsNull ? CK_NullToPointer : CK_IntegralToPointer).take(); |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 2285 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2286 | ReceiverType = Receiver->getType(); |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 2287 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2288 | ExprResult ReceiverRes; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2289 | if (getLangOpts().CPlusPlus) |
John McCall | 0bcc9bc | 2011-09-09 06:11:02 +0000 | [diff] [blame] | 2290 | ReceiverRes = PerformContextuallyConvertToObjCPointer(Receiver); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2291 | if (ReceiverRes.isUsable()) { |
| 2292 | Receiver = ReceiverRes.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2293 | return BuildInstanceMessage(Receiver, |
| 2294 | ReceiverType, |
| 2295 | SuperLoc, |
| 2296 | Sel, |
| 2297 | Method, |
| 2298 | LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2299 | SelectorLocs, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2300 | RBracLoc, |
| 2301 | move(ArgsIn)); |
| 2302 | } else { |
| 2303 | // Reject other random receiver types (e.g. structs). |
| 2304 | Diag(Loc, diag::err_bad_receiver_type) |
| 2305 | << ReceiverType << Receiver->getSourceRange(); |
| 2306 | return ExprError(); |
Fariborz Jahanian | 3ba6061 | 2010-05-13 17:19:25 +0000 | [diff] [blame] | 2307 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2308 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2309 | } |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 2310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2311 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2312 | // Check the message arguments. |
| 2313 | unsigned NumArgs = ArgsIn.size(); |
| 2314 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 2315 | QualType ReturnType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2316 | ExprValueKind VK = VK_RValue; |
Fariborz Jahanian | 2600503 | 2010-12-01 01:07:24 +0000 | [diff] [blame] | 2317 | bool ClassMessage = (ReceiverType->isObjCClassType() || |
| 2318 | ReceiverType->isObjCQualifiedClassType()); |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 2319 | if (CheckMessageArgumentTypes(ReceiverType, Args, NumArgs, Sel, Method, |
| 2320 | ClassMessage, SuperLoc.isValid(), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2321 | LBracLoc, RBracLoc, ReturnType, VK)) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2322 | return ExprError(); |
Fariborz Jahanian | da59e09 | 2010-06-16 19:56:08 +0000 | [diff] [blame] | 2323 | |
Douglas Gregor | 483dd2f | 2011-01-11 03:23:19 +0000 | [diff] [blame] | 2324 | if (Method && !Method->getResultType()->isVoidType() && |
| 2325 | RequireCompleteType(LBracLoc, Method->getResultType(), |
| 2326 | diag::err_illegal_message_expr_incomplete_type)) |
| 2327 | return ExprError(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2328 | |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2329 | SourceLocation SelLoc = SelectorLocs.front(); |
| 2330 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2331 | // In ARC, forbid the user from sending messages to |
| 2332 | // retain/release/autorelease/dealloc/retainCount explicitly. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2333 | if (getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2334 | ObjCMethodFamily family = |
| 2335 | (Method ? Method->getMethodFamily() : Sel.getMethodFamily()); |
| 2336 | switch (family) { |
| 2337 | case OMF_init: |
| 2338 | if (Method) |
| 2339 | checkInitMethod(Method, ReceiverType); |
| 2340 | |
| 2341 | case OMF_None: |
| 2342 | case OMF_alloc: |
| 2343 | case OMF_copy: |
Nico Weber | 80cb6e6 | 2011-08-28 22:35:17 +0000 | [diff] [blame] | 2344 | case OMF_finalize: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2345 | case OMF_mutableCopy: |
| 2346 | case OMF_new: |
| 2347 | case OMF_self: |
| 2348 | break; |
| 2349 | |
| 2350 | case OMF_dealloc: |
| 2351 | case OMF_retain: |
| 2352 | case OMF_release: |
| 2353 | case OMF_autorelease: |
| 2354 | case OMF_retainCount: |
| 2355 | Diag(Loc, diag::err_arc_illegal_explicit_message) |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2356 | << Sel << SelLoc; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2357 | break; |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2358 | |
| 2359 | case OMF_performSelector: |
| 2360 | if (Method && NumArgs >= 1) { |
| 2361 | if (ObjCSelectorExpr *SelExp = dyn_cast<ObjCSelectorExpr>(Args[0])) { |
| 2362 | Selector ArgSel = SelExp->getSelector(); |
| 2363 | ObjCMethodDecl *SelMethod = |
| 2364 | LookupInstanceMethodInGlobalPool(ArgSel, |
| 2365 | SelExp->getSourceRange()); |
| 2366 | if (!SelMethod) |
| 2367 | SelMethod = |
| 2368 | LookupFactoryMethodInGlobalPool(ArgSel, |
| 2369 | SelExp->getSourceRange()); |
| 2370 | if (SelMethod) { |
| 2371 | ObjCMethodFamily SelFamily = SelMethod->getMethodFamily(); |
| 2372 | switch (SelFamily) { |
| 2373 | case OMF_alloc: |
| 2374 | case OMF_copy: |
| 2375 | case OMF_mutableCopy: |
| 2376 | case OMF_new: |
| 2377 | case OMF_self: |
| 2378 | case OMF_init: |
| 2379 | // Issue error, unless ns_returns_not_retained. |
| 2380 | if (!SelMethod->hasAttr<NSReturnsNotRetainedAttr>()) { |
| 2381 | // selector names a +1 method |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2382 | Diag(SelLoc, |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2383 | diag::err_arc_perform_selector_retains); |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2384 | Diag(SelMethod->getLocation(), diag::note_method_declared_at) |
| 2385 | << SelMethod->getDeclName(); |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2386 | } |
| 2387 | break; |
| 2388 | default: |
| 2389 | // +0 call. OK. unless ns_returns_retained. |
| 2390 | if (SelMethod->hasAttr<NSReturnsRetainedAttr>()) { |
| 2391 | // selector names a +1 method |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2392 | Diag(SelLoc, |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2393 | diag::err_arc_perform_selector_retains); |
Ted Kremenek | 3306ec1 | 2012-02-27 22:55:11 +0000 | [diff] [blame] | 2394 | Diag(SelMethod->getLocation(), diag::note_method_declared_at) |
| 2395 | << SelMethod->getDeclName(); |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2396 | } |
| 2397 | break; |
| 2398 | } |
| 2399 | } |
| 2400 | } else { |
| 2401 | // error (may leak). |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2402 | Diag(SelLoc, diag::warn_arc_perform_selector_leaks); |
Fariborz Jahanian | 9670e17 | 2011-07-05 22:38:59 +0000 | [diff] [blame] | 2403 | Diag(Args[0]->getExprLoc(), diag::note_used_here); |
| 2404 | } |
| 2405 | } |
| 2406 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2407 | } |
| 2408 | } |
| 2409 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2410 | // Construct the appropriate ObjCMessageExpr instance. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2411 | ObjCMessageExpr *Result; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2412 | if (SuperLoc.isValid()) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2413 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2414 | SuperLoc, /*IsInstanceSuper=*/true, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2415 | ReceiverType, Sel, SelectorLocs, Method, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2416 | makeArrayRef(Args, NumArgs), RBracLoc, |
| 2417 | isImplicit); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2418 | else { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2419 | Result = ObjCMessageExpr::Create(Context, ReturnType, VK, LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2420 | Receiver, Sel, SelectorLocs, Method, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2421 | makeArrayRef(Args, NumArgs), RBracLoc, |
| 2422 | isImplicit); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2423 | if (!isImplicit) |
| 2424 | checkCocoaAPI(*this, Result); |
| 2425 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2426 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2427 | if (getLangOpts().ObjCAutoRefCount) { |
Fariborz Jahanian | 9879556 | 2012-04-19 23:49:39 +0000 | [diff] [blame] | 2428 | DiagnoseARCUseOfWeakReceiver(*this, Receiver); |
Fariborz Jahanian | 878f850 | 2012-04-04 20:05:25 +0000 | [diff] [blame] | 2429 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2430 | // In ARC, annotate delegate init calls. |
| 2431 | if (Result->getMethodFamily() == OMF_init && |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 2432 | (SuperLoc.isValid() || isSelfExpr(Receiver))) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2433 | // Only consider init calls *directly* in init implementations, |
| 2434 | // not within blocks. |
| 2435 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(CurContext); |
| 2436 | if (method && method->getMethodFamily() == OMF_init) { |
| 2437 | // The implicit assignment to self means we also don't want to |
| 2438 | // consume the result. |
| 2439 | Result->setDelegateInitCall(true); |
| 2440 | return Owned(Result); |
| 2441 | } |
| 2442 | } |
| 2443 | |
| 2444 | // In ARC, check for message sends which are likely to introduce |
| 2445 | // retain cycles. |
| 2446 | checkRetainCycles(Result); |
| 2447 | } |
| 2448 | |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 2449 | return MaybeBindToTemporary(Result); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 2453 | // ArgExprs is optional - if it is present, the number of expressions |
| 2454 | // is obtained from Sel.getNumArgs(). |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2455 | ExprResult Sema::ActOnInstanceMessage(Scope *S, |
| 2456 | Expr *Receiver, |
| 2457 | Selector Sel, |
| 2458 | SourceLocation LBracLoc, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2459 | ArrayRef<SourceLocation> SelectorLocs, |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2460 | SourceLocation RBracLoc, |
| 2461 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 2462 | if (!Receiver) |
| 2463 | return ExprError(); |
| 2464 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2465 | return BuildInstanceMessage(Receiver, Receiver->getType(), |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 2466 | /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2467 | LBracLoc, SelectorLocs, RBracLoc, move(Args)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 2468 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 2469 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2470 | enum ARCConversionTypeClass { |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2471 | /// int, void, struct A |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2472 | ACTC_none, |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2473 | |
| 2474 | /// id, void (^)() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2475 | ACTC_retainable, |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2476 | |
| 2477 | /// id*, id***, void (^*)(), |
| 2478 | ACTC_indirectRetainable, |
| 2479 | |
| 2480 | /// void* might be a normal C type, or it might a CF type. |
| 2481 | ACTC_voidPtr, |
| 2482 | |
| 2483 | /// struct A* |
| 2484 | ACTC_coreFoundation |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2485 | }; |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2486 | static bool isAnyRetainable(ARCConversionTypeClass ACTC) { |
| 2487 | return (ACTC == ACTC_retainable || |
| 2488 | ACTC == ACTC_coreFoundation || |
| 2489 | ACTC == ACTC_voidPtr); |
| 2490 | } |
| 2491 | static bool isAnyCLike(ARCConversionTypeClass ACTC) { |
| 2492 | return ACTC == ACTC_none || |
| 2493 | ACTC == ACTC_voidPtr || |
| 2494 | ACTC == ACTC_coreFoundation; |
| 2495 | } |
| 2496 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2497 | static ARCConversionTypeClass classifyTypeForARCConversion(QualType type) { |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2498 | bool isIndirect = false; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2499 | |
| 2500 | // Ignore an outermost reference type. |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2501 | if (const ReferenceType *ref = type->getAs<ReferenceType>()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2502 | type = ref->getPointeeType(); |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2503 | isIndirect = true; |
| 2504 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2505 | |
| 2506 | // Drill through pointers and arrays recursively. |
| 2507 | while (true) { |
| 2508 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 2509 | type = ptr->getPointeeType(); |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2510 | |
| 2511 | // The first level of pointer may be the innermost pointer on a CF type. |
| 2512 | if (!isIndirect) { |
| 2513 | if (type->isVoidType()) return ACTC_voidPtr; |
| 2514 | if (type->isRecordType()) return ACTC_coreFoundation; |
| 2515 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2516 | } else if (const ArrayType *array = type->getAsArrayTypeUnsafe()) { |
| 2517 | type = QualType(array->getElementType()->getBaseElementTypeUnsafe(), 0); |
| 2518 | } else { |
| 2519 | break; |
| 2520 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2521 | isIndirect = true; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2524 | if (isIndirect) { |
| 2525 | if (type->isObjCARCBridgableType()) |
| 2526 | return ACTC_indirectRetainable; |
| 2527 | return ACTC_none; |
| 2528 | } |
| 2529 | |
| 2530 | if (type->isObjCARCBridgableType()) |
| 2531 | return ACTC_retainable; |
| 2532 | |
| 2533 | return ACTC_none; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2534 | } |
| 2535 | |
| 2536 | namespace { |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2537 | /// A result from the cast checker. |
| 2538 | enum ACCResult { |
| 2539 | /// Cannot be casted. |
| 2540 | ACC_invalid, |
| 2541 | |
| 2542 | /// Can be safely retained or not retained. |
| 2543 | ACC_bottom, |
| 2544 | |
| 2545 | /// Can be casted at +0. |
| 2546 | ACC_plusZero, |
| 2547 | |
| 2548 | /// Can be casted at +1. |
| 2549 | ACC_plusOne |
| 2550 | }; |
| 2551 | ACCResult merge(ACCResult left, ACCResult right) { |
| 2552 | if (left == right) return left; |
| 2553 | if (left == ACC_bottom) return right; |
| 2554 | if (right == ACC_bottom) return left; |
| 2555 | return ACC_invalid; |
| 2556 | } |
| 2557 | |
| 2558 | /// A checker which white-lists certain expressions whose conversion |
| 2559 | /// to or from retainable type would otherwise be forbidden in ARC. |
| 2560 | class ARCCastChecker : public StmtVisitor<ARCCastChecker, ACCResult> { |
| 2561 | typedef StmtVisitor<ARCCastChecker, ACCResult> super; |
| 2562 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2563 | ASTContext &Context; |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2564 | ARCConversionTypeClass SourceClass; |
| 2565 | ARCConversionTypeClass TargetClass; |
| 2566 | |
| 2567 | static bool isCFType(QualType type) { |
| 2568 | // Someday this can use ns_bridged. For now, it has to do this. |
| 2569 | return type->isCARCBridgableType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2570 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2571 | |
| 2572 | public: |
| 2573 | ARCCastChecker(ASTContext &Context, ARCConversionTypeClass source, |
| 2574 | ARCConversionTypeClass target) |
| 2575 | : Context(Context), SourceClass(source), TargetClass(target) {} |
| 2576 | |
| 2577 | using super::Visit; |
| 2578 | ACCResult Visit(Expr *e) { |
| 2579 | return super::Visit(e->IgnoreParens()); |
| 2580 | } |
| 2581 | |
| 2582 | ACCResult VisitStmt(Stmt *s) { |
| 2583 | return ACC_invalid; |
| 2584 | } |
| 2585 | |
| 2586 | /// Null pointer constants can be casted however you please. |
| 2587 | ACCResult VisitExpr(Expr *e) { |
| 2588 | if (e->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 2589 | return ACC_bottom; |
| 2590 | return ACC_invalid; |
| 2591 | } |
| 2592 | |
| 2593 | /// Objective-C string literals can be safely casted. |
| 2594 | ACCResult VisitObjCStringLiteral(ObjCStringLiteral *e) { |
| 2595 | // If we're casting to any retainable type, go ahead. Global |
| 2596 | // strings are immune to retains, so this is bottom. |
| 2597 | if (isAnyRetainable(TargetClass)) return ACC_bottom; |
| 2598 | |
| 2599 | return ACC_invalid; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2602 | /// Look through certain implicit and explicit casts. |
| 2603 | ACCResult VisitCastExpr(CastExpr *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2604 | switch (e->getCastKind()) { |
| 2605 | case CK_NullToPointer: |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2606 | return ACC_bottom; |
| 2607 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2608 | case CK_NoOp: |
| 2609 | case CK_LValueToRValue: |
| 2610 | case CK_BitCast: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 2611 | case CK_CPointerToObjCPointerCast: |
| 2612 | case CK_BlockPointerToObjCPointerCast: |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2613 | case CK_AnyPointerToBlockPointerCast: |
| 2614 | return Visit(e->getSubExpr()); |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2615 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2616 | default: |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2617 | return ACC_invalid; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2618 | } |
| 2619 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2620 | |
| 2621 | /// Look through unary extension. |
| 2622 | ACCResult VisitUnaryExtension(UnaryOperator *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2623 | return Visit(e->getSubExpr()); |
| 2624 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2625 | |
| 2626 | /// Ignore the LHS of a comma operator. |
| 2627 | ACCResult VisitBinComma(BinaryOperator *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2628 | return Visit(e->getRHS()); |
| 2629 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2630 | |
| 2631 | /// Conditional operators are okay if both sides are okay. |
| 2632 | ACCResult VisitConditionalOperator(ConditionalOperator *e) { |
| 2633 | ACCResult left = Visit(e->getTrueExpr()); |
| 2634 | if (left == ACC_invalid) return ACC_invalid; |
| 2635 | return merge(left, Visit(e->getFalseExpr())); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2636 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2637 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2638 | /// Look through pseudo-objects. |
| 2639 | ACCResult VisitPseudoObjectExpr(PseudoObjectExpr *e) { |
| 2640 | // If we're getting here, we should always have a result. |
| 2641 | return Visit(e->getResultExpr()); |
| 2642 | } |
| 2643 | |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2644 | /// Statement expressions are okay if their result expression is okay. |
| 2645 | ACCResult VisitStmtExpr(StmtExpr *e) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2646 | return Visit(e->getSubStmt()->body_back()); |
| 2647 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2648 | |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2649 | /// Some declaration references are okay. |
| 2650 | ACCResult VisitDeclRefExpr(DeclRefExpr *e) { |
| 2651 | // References to global constants from system headers are okay. |
| 2652 | // These are things like 'kCFStringTransformToLatin'. They are |
| 2653 | // can also be assumed to be immune to retains. |
| 2654 | VarDecl *var = dyn_cast<VarDecl>(e->getDecl()); |
| 2655 | if (isAnyRetainable(TargetClass) && |
| 2656 | isAnyRetainable(SourceClass) && |
| 2657 | var && |
| 2658 | var->getStorageClass() == SC_Extern && |
| 2659 | var->getType().isConstQualified() && |
| 2660 | Context.getSourceManager().isInSystemHeader(var->getLocation())) { |
| 2661 | return ACC_bottom; |
| 2662 | } |
| 2663 | |
| 2664 | // Nothing else. |
| 2665 | return ACC_invalid; |
Fariborz Jahanian | af97517 | 2011-06-21 17:38:29 +0000 | [diff] [blame] | 2666 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2667 | |
| 2668 | /// Some calls are okay. |
| 2669 | ACCResult VisitCallExpr(CallExpr *e) { |
| 2670 | if (FunctionDecl *fn = e->getDirectCallee()) |
| 2671 | if (ACCResult result = checkCallToFunction(fn)) |
| 2672 | return result; |
| 2673 | |
| 2674 | return super::VisitCallExpr(e); |
| 2675 | } |
| 2676 | |
| 2677 | ACCResult checkCallToFunction(FunctionDecl *fn) { |
| 2678 | // Require a CF*Ref return type. |
| 2679 | if (!isCFType(fn->getResultType())) |
| 2680 | return ACC_invalid; |
| 2681 | |
| 2682 | if (!isAnyRetainable(TargetClass)) |
| 2683 | return ACC_invalid; |
| 2684 | |
| 2685 | // Honor an explicit 'not retained' attribute. |
| 2686 | if (fn->hasAttr<CFReturnsNotRetainedAttr>()) |
| 2687 | return ACC_plusZero; |
| 2688 | |
| 2689 | // Honor an explicit 'retained' attribute, except that for |
| 2690 | // now we're not going to permit implicit handling of +1 results, |
| 2691 | // because it's a bit frightening. |
| 2692 | if (fn->hasAttr<CFReturnsRetainedAttr>()) |
| 2693 | return ACC_invalid; // ACC_plusOne if we start accepting this |
| 2694 | |
| 2695 | // Recognize this specific builtin function, which is used by CFSTR. |
| 2696 | unsigned builtinID = fn->getBuiltinID(); |
| 2697 | if (builtinID == Builtin::BI__builtin___CFStringMakeConstantString) |
| 2698 | return ACC_bottom; |
| 2699 | |
| 2700 | // Otherwise, don't do anything implicit with an unaudited function. |
| 2701 | if (!fn->hasAttr<CFAuditedTransferAttr>()) |
| 2702 | return ACC_invalid; |
| 2703 | |
| 2704 | // Otherwise, it's +0 unless it follows the create convention. |
| 2705 | if (ento::coreFoundation::followsCreateRule(fn)) |
| 2706 | return ACC_invalid; // ACC_plusOne if we start accepting this |
| 2707 | |
| 2708 | return ACC_plusZero; |
| 2709 | } |
| 2710 | |
| 2711 | ACCResult VisitObjCMessageExpr(ObjCMessageExpr *e) { |
| 2712 | return checkCallToMethod(e->getMethodDecl()); |
| 2713 | } |
| 2714 | |
| 2715 | ACCResult VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *e) { |
| 2716 | ObjCMethodDecl *method; |
| 2717 | if (e->isExplicitProperty()) |
| 2718 | method = e->getExplicitProperty()->getGetterMethodDecl(); |
| 2719 | else |
| 2720 | method = e->getImplicitPropertyGetter(); |
| 2721 | return checkCallToMethod(method); |
| 2722 | } |
| 2723 | |
| 2724 | ACCResult checkCallToMethod(ObjCMethodDecl *method) { |
| 2725 | if (!method) return ACC_invalid; |
| 2726 | |
| 2727 | // Check for message sends to functions returning CF types. We |
| 2728 | // just obey the Cocoa conventions with these, even though the |
| 2729 | // return type is CF. |
| 2730 | if (!isAnyRetainable(TargetClass) || !isCFType(method->getResultType())) |
| 2731 | return ACC_invalid; |
| 2732 | |
| 2733 | // If the method is explicitly marked not-retained, it's +0. |
| 2734 | if (method->hasAttr<CFReturnsNotRetainedAttr>()) |
| 2735 | return ACC_plusZero; |
| 2736 | |
| 2737 | // If the method is explicitly marked as returning retained, or its |
| 2738 | // selector follows a +1 Cocoa convention, treat it as +1. |
| 2739 | if (method->hasAttr<CFReturnsRetainedAttr>()) |
| 2740 | return ACC_plusOne; |
| 2741 | |
| 2742 | switch (method->getSelector().getMethodFamily()) { |
| 2743 | case OMF_alloc: |
| 2744 | case OMF_copy: |
| 2745 | case OMF_mutableCopy: |
| 2746 | case OMF_new: |
| 2747 | return ACC_plusOne; |
| 2748 | |
| 2749 | default: |
| 2750 | // Otherwise, treat it as +0. |
| 2751 | return ACC_plusZero; |
Fariborz Jahanian | c8505ad | 2011-06-21 19:42:38 +0000 | [diff] [blame] | 2752 | } |
| 2753 | } |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2754 | }; |
Fariborz Jahanian | 1522a7c | 2011-06-20 20:54:42 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2757 | static bool |
| 2758 | KnownName(Sema &S, const char *name) { |
| 2759 | LookupResult R(S, &S.Context.Idents.get(name), SourceLocation(), |
| 2760 | Sema::LookupOrdinaryName); |
| 2761 | return S.LookupName(R, S.TUScope, false); |
| 2762 | } |
| 2763 | |
Argyrios Kyrtzidis | ae1b4af | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 2764 | static void addFixitForObjCARCConversion(Sema &S, |
| 2765 | DiagnosticBuilder &DiagB, |
| 2766 | Sema::CheckedConversionKind CCK, |
| 2767 | SourceLocation afterLParen, |
| 2768 | QualType castType, |
| 2769 | Expr *castExpr, |
| 2770 | const char *bridgeKeyword, |
| 2771 | const char *CFBridgeName) { |
| 2772 | // We handle C-style and implicit casts here. |
| 2773 | switch (CCK) { |
| 2774 | case Sema::CCK_ImplicitConversion: |
| 2775 | case Sema::CCK_CStyleCast: |
| 2776 | break; |
| 2777 | case Sema::CCK_FunctionalCast: |
| 2778 | case Sema::CCK_OtherCast: |
| 2779 | return; |
| 2780 | } |
| 2781 | |
| 2782 | if (CFBridgeName) { |
| 2783 | Expr *castedE = castExpr; |
| 2784 | if (CStyleCastExpr *CCE = dyn_cast<CStyleCastExpr>(castedE)) |
| 2785 | castedE = CCE->getSubExpr(); |
| 2786 | castedE = castedE->IgnoreImpCasts(); |
| 2787 | SourceRange range = castedE->getSourceRange(); |
| 2788 | if (isa<ParenExpr>(castedE)) { |
| 2789 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 2790 | CFBridgeName)); |
| 2791 | } else { |
| 2792 | std::string namePlusParen = CFBridgeName; |
| 2793 | namePlusParen += "("; |
| 2794 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 2795 | namePlusParen)); |
| 2796 | DiagB.AddFixItHint(FixItHint::CreateInsertion( |
| 2797 | S.PP.getLocForEndOfToken(range.getEnd()), |
| 2798 | ")")); |
| 2799 | } |
| 2800 | return; |
| 2801 | } |
| 2802 | |
| 2803 | if (CCK == Sema::CCK_CStyleCast) { |
| 2804 | DiagB.AddFixItHint(FixItHint::CreateInsertion(afterLParen, bridgeKeyword)); |
| 2805 | } else { |
| 2806 | std::string castCode = "("; |
| 2807 | castCode += bridgeKeyword; |
| 2808 | castCode += castType.getAsString(); |
| 2809 | castCode += ")"; |
| 2810 | Expr *castedE = castExpr->IgnoreImpCasts(); |
| 2811 | SourceRange range = castedE->getSourceRange(); |
| 2812 | if (isa<ParenExpr>(castedE)) { |
| 2813 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 2814 | castCode)); |
| 2815 | } else { |
| 2816 | castCode += "("; |
| 2817 | DiagB.AddFixItHint(FixItHint::CreateInsertion(range.getBegin(), |
| 2818 | castCode)); |
| 2819 | DiagB.AddFixItHint(FixItHint::CreateInsertion( |
| 2820 | S.PP.getLocForEndOfToken(range.getEnd()), |
| 2821 | ")")); |
| 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2826 | static void |
| 2827 | diagnoseObjCARCConversion(Sema &S, SourceRange castRange, |
| 2828 | QualType castType, ARCConversionTypeClass castACTC, |
| 2829 | Expr *castExpr, ARCConversionTypeClass exprACTC, |
| 2830 | Sema::CheckedConversionKind CCK) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2831 | SourceLocation loc = |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2832 | (castRange.isValid() ? castRange.getBegin() : castExpr->getExprLoc()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2833 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2834 | if (S.makeUnavailableInSystemHeader(loc, |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2835 | "converts between Objective-C and C pointers in -fobjc-arc")) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2836 | return; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2837 | |
| 2838 | QualType castExprType = castExpr->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2839 | |
John McCall | 71c482c | 2011-06-17 06:50:50 +0000 | [diff] [blame] | 2840 | unsigned srcKind = 0; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2841 | switch (exprACTC) { |
John McCall | 2cf031d | 2011-10-01 01:01:08 +0000 | [diff] [blame] | 2842 | case ACTC_none: |
| 2843 | case ACTC_coreFoundation: |
| 2844 | case ACTC_voidPtr: |
| 2845 | srcKind = (castExprType->isPointerType() ? 1 : 0); |
| 2846 | break; |
| 2847 | case ACTC_retainable: |
| 2848 | srcKind = (castExprType->isBlockPointerType() ? 2 : 3); |
| 2849 | break; |
| 2850 | case ACTC_indirectRetainable: |
| 2851 | srcKind = 4; |
| 2852 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2855 | // Check whether this could be fixed with a bridge cast. |
| 2856 | SourceLocation afterLParen = S.PP.getLocForEndOfToken(castRange.getBegin()); |
| 2857 | SourceLocation noteLoc = afterLParen.isValid() ? afterLParen : loc; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2858 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2859 | // Bridge from an ARC type to a CF type. |
| 2860 | if (castACTC == ACTC_retainable && isAnyRetainable(exprACTC)) { |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2861 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2862 | S.Diag(loc, diag::err_arc_cast_requires_bridge) |
| 2863 | << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit |
| 2864 | << 2 // of C pointer type |
| 2865 | << castExprType |
| 2866 | << unsigned(castType->isBlockPointerType()) // to ObjC|block type |
| 2867 | << castType |
| 2868 | << castRange |
| 2869 | << castExpr->getSourceRange(); |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2870 | bool br = KnownName(S, "CFBridgingRelease"); |
Argyrios Kyrtzidis | ae1b4af | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 2871 | { |
| 2872 | DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge); |
| 2873 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| 2874 | castType, castExpr, "__bridge ", 0); |
| 2875 | } |
| 2876 | { |
| 2877 | DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge_transfer) |
| 2878 | << castExprType << br; |
| 2879 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| 2880 | castType, castExpr, "__bridge_transfer ", |
| 2881 | br ? "CFBridgingRelease" : 0); |
| 2882 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2883 | |
| 2884 | return; |
| 2885 | } |
| 2886 | |
| 2887 | // Bridge from a CF type to an ARC type. |
| 2888 | if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC)) { |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 2889 | bool br = KnownName(S, "CFBridgingRetain"); |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2890 | S.Diag(loc, diag::err_arc_cast_requires_bridge) |
| 2891 | << unsigned(CCK == Sema::CCK_ImplicitConversion) // cast|implicit |
| 2892 | << unsigned(castExprType->isBlockPointerType()) // of ObjC|block type |
| 2893 | << castExprType |
| 2894 | << 2 // to C pointer type |
| 2895 | << castType |
| 2896 | << castRange |
| 2897 | << castExpr->getSourceRange(); |
| 2898 | |
Argyrios Kyrtzidis | ae1b4af | 2012-02-16 17:31:07 +0000 | [diff] [blame] | 2899 | { |
| 2900 | DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge); |
| 2901 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| 2902 | castType, castExpr, "__bridge ", 0); |
| 2903 | } |
| 2904 | { |
| 2905 | DiagnosticBuilder DiagB = S.Diag(noteLoc, diag::note_arc_bridge_retained) |
| 2906 | << castType << br; |
| 2907 | addFixitForObjCARCConversion(S, DiagB, CCK, afterLParen, |
| 2908 | castType, castExpr, "__bridge_retained ", |
| 2909 | br ? "CFBridgingRetain" : 0); |
| 2910 | } |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2911 | |
| 2912 | return; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2915 | S.Diag(loc, diag::err_arc_mismatched_cast) |
| 2916 | << (CCK != Sema::CCK_ImplicitConversion) |
| 2917 | << srcKind << castExprType << castType |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2918 | << castRange << castExpr->getSourceRange(); |
| 2919 | } |
| 2920 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2921 | Sema::ARCConversionResult |
| 2922 | Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType, |
| 2923 | Expr *&castExpr, CheckedConversionKind CCK) { |
| 2924 | QualType castExprType = castExpr->getType(); |
| 2925 | |
| 2926 | // For the purposes of the classification, we assume reference types |
| 2927 | // will bind to temporaries. |
| 2928 | QualType effCastType = castType; |
| 2929 | if (const ReferenceType *ref = castType->getAs<ReferenceType>()) |
| 2930 | effCastType = ref->getPointeeType(); |
| 2931 | |
| 2932 | ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExprType); |
| 2933 | ARCConversionTypeClass castACTC = classifyTypeForARCConversion(effCastType); |
Fariborz Jahanian | 6d09f01 | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 2934 | if (exprACTC == castACTC) { |
| 2935 | // check for viablity and report error if casting an rvalue to a |
| 2936 | // life-time qualifier. |
Fariborz Jahanian | fc2eff5 | 2011-10-29 00:06:10 +0000 | [diff] [blame] | 2937 | if ((castACTC == ACTC_retainable) && |
Fariborz Jahanian | 6d09f01 | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 2938 | (CCK == CCK_CStyleCast || CCK == CCK_OtherCast) && |
Fariborz Jahanian | fc2eff5 | 2011-10-29 00:06:10 +0000 | [diff] [blame] | 2939 | (castType != castExprType)) { |
| 2940 | const Type *DT = castType.getTypePtr(); |
| 2941 | QualType QDT = castType; |
| 2942 | // We desugar some types but not others. We ignore those |
| 2943 | // that cannot happen in a cast; i.e. auto, and those which |
| 2944 | // should not be de-sugared; i.e typedef. |
| 2945 | if (const ParenType *PT = dyn_cast<ParenType>(DT)) |
| 2946 | QDT = PT->desugar(); |
| 2947 | else if (const TypeOfType *TP = dyn_cast<TypeOfType>(DT)) |
| 2948 | QDT = TP->desugar(); |
| 2949 | else if (const AttributedType *AT = dyn_cast<AttributedType>(DT)) |
| 2950 | QDT = AT->desugar(); |
| 2951 | if (QDT != castType && |
| 2952 | QDT.getObjCLifetime() != Qualifiers::OCL_None) { |
| 2953 | SourceLocation loc = |
| 2954 | (castRange.isValid() ? castRange.getBegin() |
| 2955 | : castExpr->getExprLoc()); |
| 2956 | Diag(loc, diag::err_arc_nolifetime_behavior); |
| 2957 | } |
Fariborz Jahanian | 6d09f01 | 2011-10-28 20:06:07 +0000 | [diff] [blame] | 2958 | } |
| 2959 | return ACR_okay; |
| 2960 | } |
| 2961 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 2962 | if (isAnyCLike(exprACTC) && isAnyCLike(castACTC)) return ACR_okay; |
| 2963 | |
| 2964 | // Allow all of these types to be cast to integer types (but not |
| 2965 | // vice-versa). |
| 2966 | if (castACTC == ACTC_none && castType->isIntegralType(Context)) |
| 2967 | return ACR_okay; |
| 2968 | |
| 2969 | // Allow casts between pointers to lifetime types (e.g., __strong id*) |
| 2970 | // and pointers to void (e.g., cv void *). Casting from void* to lifetime* |
| 2971 | // must be explicit. |
| 2972 | if (exprACTC == ACTC_indirectRetainable && castACTC == ACTC_voidPtr) |
| 2973 | return ACR_okay; |
| 2974 | if (castACTC == ACTC_indirectRetainable && exprACTC == ACTC_voidPtr && |
| 2975 | CCK != CCK_ImplicitConversion) |
| 2976 | return ACR_okay; |
| 2977 | |
| 2978 | switch (ARCCastChecker(Context, exprACTC, castACTC).Visit(castExpr)) { |
| 2979 | // For invalid casts, fall through. |
| 2980 | case ACC_invalid: |
| 2981 | break; |
| 2982 | |
| 2983 | // Do nothing for both bottom and +0. |
| 2984 | case ACC_bottom: |
| 2985 | case ACC_plusZero: |
| 2986 | return ACR_okay; |
| 2987 | |
| 2988 | // If the result is +1, consume it here. |
| 2989 | case ACC_plusOne: |
| 2990 | castExpr = ImplicitCastExpr::Create(Context, castExpr->getType(), |
| 2991 | CK_ARCConsumeObject, castExpr, |
| 2992 | 0, VK_RValue); |
| 2993 | ExprNeedsCleanups = true; |
| 2994 | return ACR_okay; |
| 2995 | } |
| 2996 | |
| 2997 | // If this is a non-implicit cast from id or block type to a |
| 2998 | // CoreFoundation type, delay complaining in case the cast is used |
| 2999 | // in an acceptable context. |
| 3000 | if (exprACTC == ACTC_retainable && isAnyRetainable(castACTC) && |
| 3001 | CCK != CCK_ImplicitConversion) |
| 3002 | return ACR_unbridged; |
| 3003 | |
| 3004 | diagnoseObjCARCConversion(*this, castRange, castType, castACTC, |
| 3005 | castExpr, exprACTC, CCK); |
| 3006 | return ACR_okay; |
| 3007 | } |
| 3008 | |
| 3009 | /// Given that we saw an expression with the ARCUnbridgedCastTy |
| 3010 | /// placeholder type, complain bitterly. |
| 3011 | void Sema::diagnoseARCUnbridgedCast(Expr *e) { |
| 3012 | // We expect the spurious ImplicitCastExpr to already have been stripped. |
| 3013 | assert(!e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 3014 | CastExpr *realCast = cast<CastExpr>(e->IgnoreParens()); |
| 3015 | |
| 3016 | SourceRange castRange; |
| 3017 | QualType castType; |
| 3018 | CheckedConversionKind CCK; |
| 3019 | |
| 3020 | if (CStyleCastExpr *cast = dyn_cast<CStyleCastExpr>(realCast)) { |
| 3021 | castRange = SourceRange(cast->getLParenLoc(), cast->getRParenLoc()); |
| 3022 | castType = cast->getTypeAsWritten(); |
| 3023 | CCK = CCK_CStyleCast; |
| 3024 | } else if (ExplicitCastExpr *cast = dyn_cast<ExplicitCastExpr>(realCast)) { |
| 3025 | castRange = cast->getTypeInfoAsWritten()->getTypeLoc().getSourceRange(); |
| 3026 | castType = cast->getTypeAsWritten(); |
| 3027 | CCK = CCK_OtherCast; |
| 3028 | } else { |
| 3029 | castType = cast->getType(); |
| 3030 | CCK = CCK_ImplicitConversion; |
| 3031 | } |
| 3032 | |
| 3033 | ARCConversionTypeClass castACTC = |
| 3034 | classifyTypeForARCConversion(castType.getNonReferenceType()); |
| 3035 | |
| 3036 | Expr *castExpr = realCast->getSubExpr(); |
| 3037 | assert(classifyTypeForARCConversion(castExpr->getType()) == ACTC_retainable); |
| 3038 | |
| 3039 | diagnoseObjCARCConversion(*this, castRange, castType, castACTC, |
| 3040 | castExpr, ACTC_retainable, CCK); |
| 3041 | } |
| 3042 | |
| 3043 | /// stripARCUnbridgedCast - Given an expression of ARCUnbridgedCast |
| 3044 | /// type, remove the placeholder cast. |
| 3045 | Expr *Sema::stripARCUnbridgedCast(Expr *e) { |
| 3046 | assert(e->hasPlaceholderType(BuiltinType::ARCUnbridgedCast)); |
| 3047 | |
| 3048 | if (ParenExpr *pe = dyn_cast<ParenExpr>(e)) { |
| 3049 | Expr *sub = stripARCUnbridgedCast(pe->getSubExpr()); |
| 3050 | return new (Context) ParenExpr(pe->getLParen(), pe->getRParen(), sub); |
| 3051 | } else if (UnaryOperator *uo = dyn_cast<UnaryOperator>(e)) { |
| 3052 | assert(uo->getOpcode() == UO_Extension); |
| 3053 | Expr *sub = stripARCUnbridgedCast(uo->getSubExpr()); |
| 3054 | return new (Context) UnaryOperator(sub, UO_Extension, sub->getType(), |
| 3055 | sub->getValueKind(), sub->getObjectKind(), |
| 3056 | uo->getOperatorLoc()); |
| 3057 | } else if (GenericSelectionExpr *gse = dyn_cast<GenericSelectionExpr>(e)) { |
| 3058 | assert(!gse->isResultDependent()); |
| 3059 | |
| 3060 | unsigned n = gse->getNumAssocs(); |
| 3061 | SmallVector<Expr*, 4> subExprs(n); |
| 3062 | SmallVector<TypeSourceInfo*, 4> subTypes(n); |
| 3063 | for (unsigned i = 0; i != n; ++i) { |
| 3064 | subTypes[i] = gse->getAssocTypeSourceInfo(i); |
| 3065 | Expr *sub = gse->getAssocExpr(i); |
| 3066 | if (i == gse->getResultIndex()) |
| 3067 | sub = stripARCUnbridgedCast(sub); |
| 3068 | subExprs[i] = sub; |
| 3069 | } |
| 3070 | |
| 3071 | return new (Context) GenericSelectionExpr(Context, gse->getGenericLoc(), |
| 3072 | gse->getControllingExpr(), |
| 3073 | subTypes.data(), subExprs.data(), |
| 3074 | n, gse->getDefaultLoc(), |
| 3075 | gse->getRParenLoc(), |
| 3076 | gse->containsUnexpandedParameterPack(), |
| 3077 | gse->getResultIndex()); |
| 3078 | } else { |
| 3079 | assert(isa<ImplicitCastExpr>(e) && "bad form of unbridged cast!"); |
| 3080 | return cast<ImplicitCastExpr>(e)->getSubExpr(); |
| 3081 | } |
| 3082 | } |
| 3083 | |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 3084 | bool Sema::CheckObjCARCUnavailableWeakConversion(QualType castType, |
| 3085 | QualType exprType) { |
| 3086 | QualType canCastType = |
| 3087 | Context.getCanonicalType(castType).getUnqualifiedType(); |
| 3088 | QualType canExprType = |
| 3089 | Context.getCanonicalType(exprType).getUnqualifiedType(); |
| 3090 | if (isa<ObjCObjectPointerType>(canCastType) && |
| 3091 | castType.getObjCLifetime() == Qualifiers::OCL_Weak && |
| 3092 | canExprType->isObjCObjectPointerType()) { |
| 3093 | if (const ObjCObjectPointerType *ObjT = |
| 3094 | canExprType->getAs<ObjCObjectPointerType>()) |
| 3095 | if (ObjT->getInterfaceDecl()->isArcWeakrefUnavailable()) |
| 3096 | return false; |
| 3097 | } |
| 3098 | return true; |
| 3099 | } |
| 3100 | |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 3101 | /// Look for an ObjCReclaimReturnedObject cast and destroy it. |
| 3102 | static Expr *maybeUndoReclaimObject(Expr *e) { |
| 3103 | // For now, we just undo operands that are *immediately* reclaim |
| 3104 | // expressions, which prevents the vast majority of potential |
| 3105 | // problems here. To catch them all, we'd need to rebuild arbitrary |
| 3106 | // value-propagating subexpressions --- we can't reliably rebuild |
| 3107 | // in-place because of expression sharing. |
| 3108 | if (ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3109 | if (ice->getCastKind() == CK_ARCReclaimReturnedObject) |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 3110 | return ice->getSubExpr(); |
| 3111 | |
| 3112 | return e; |
| 3113 | } |
| 3114 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3115 | ExprResult Sema::BuildObjCBridgedCast(SourceLocation LParenLoc, |
| 3116 | ObjCBridgeCastKind Kind, |
| 3117 | SourceLocation BridgeKeywordLoc, |
| 3118 | TypeSourceInfo *TSInfo, |
| 3119 | Expr *SubExpr) { |
John McCall | 4906cf9 | 2011-08-26 00:48:42 +0000 | [diff] [blame] | 3120 | ExprResult SubResult = UsualUnaryConversions(SubExpr); |
| 3121 | if (SubResult.isInvalid()) return ExprError(); |
| 3122 | SubExpr = SubResult.take(); |
| 3123 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3124 | QualType T = TSInfo->getType(); |
| 3125 | QualType FromType = SubExpr->getType(); |
| 3126 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3127 | CastKind CK; |
| 3128 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3129 | bool MustConsume = false; |
| 3130 | if (T->isDependentType() || SubExpr->isTypeDependent()) { |
| 3131 | // Okay: we'll build a dependent expression type. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3132 | CK = CK_Dependent; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3133 | } else if (T->isObjCARCBridgableType() && FromType->isCARCBridgableType()) { |
| 3134 | // Casting CF -> id |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3135 | CK = (T->isBlockPointerType() ? CK_AnyPointerToBlockPointerCast |
| 3136 | : CK_CPointerToObjCPointerCast); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3137 | switch (Kind) { |
| 3138 | case OBC_Bridge: |
| 3139 | break; |
| 3140 | |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3141 | case OBC_BridgeRetained: { |
| 3142 | bool br = KnownName(*this, "CFBridgingRelease"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3143 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 3144 | << 2 |
| 3145 | << FromType |
| 3146 | << (T->isBlockPointerType()? 1 : 0) |
| 3147 | << T |
| 3148 | << SubExpr->getSourceRange() |
| 3149 | << Kind; |
| 3150 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 3151 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge"); |
| 3152 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_transfer) |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3153 | << FromType << br |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3154 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3155 | br ? "CFBridgingRelease " |
| 3156 | : "__bridge_transfer "); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3157 | |
| 3158 | Kind = OBC_Bridge; |
| 3159 | break; |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3160 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3161 | |
| 3162 | case OBC_BridgeTransfer: |
| 3163 | // We must consume the Objective-C object produced by the cast. |
| 3164 | MustConsume = true; |
| 3165 | break; |
| 3166 | } |
| 3167 | } else if (T->isCARCBridgableType() && FromType->isObjCARCBridgableType()) { |
| 3168 | // Okay: id -> CF |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3169 | CK = CK_BitCast; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3170 | switch (Kind) { |
| 3171 | case OBC_Bridge: |
John McCall | 7e5e5f4 | 2011-07-07 06:58:02 +0000 | [diff] [blame] | 3172 | // Reclaiming a value that's going to be __bridge-casted to CF |
| 3173 | // is very dangerous, so we don't do it. |
| 3174 | SubExpr = maybeUndoReclaimObject(SubExpr); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3175 | break; |
| 3176 | |
| 3177 | case OBC_BridgeRetained: |
| 3178 | // Produce the object before casting it. |
| 3179 | SubExpr = ImplicitCastExpr::Create(Context, FromType, |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3180 | CK_ARCProduceObject, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3181 | SubExpr, 0, VK_RValue); |
| 3182 | break; |
| 3183 | |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3184 | case OBC_BridgeTransfer: { |
| 3185 | bool br = KnownName(*this, "CFBridgingRetain"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3186 | Diag(BridgeKeywordLoc, diag::err_arc_bridge_cast_wrong_kind) |
| 3187 | << (FromType->isBlockPointerType()? 1 : 0) |
| 3188 | << FromType |
| 3189 | << 2 |
| 3190 | << T |
| 3191 | << SubExpr->getSourceRange() |
| 3192 | << Kind; |
| 3193 | |
| 3194 | Diag(BridgeKeywordLoc, diag::note_arc_bridge) |
| 3195 | << FixItHint::CreateReplacement(BridgeKeywordLoc, "__bridge "); |
| 3196 | Diag(BridgeKeywordLoc, diag::note_arc_bridge_retained) |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3197 | << T << br |
| 3198 | << FixItHint::CreateReplacement(BridgeKeywordLoc, |
| 3199 | br ? "CFBridgingRetain " : "__bridge_retained"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3200 | |
| 3201 | Kind = OBC_Bridge; |
| 3202 | break; |
| 3203 | } |
Fariborz Jahanian | 52b6236 | 2012-02-01 22:56:20 +0000 | [diff] [blame] | 3204 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3205 | } else { |
| 3206 | Diag(LParenLoc, diag::err_arc_bridge_cast_incompatible) |
| 3207 | << FromType << T << Kind |
| 3208 | << SubExpr->getSourceRange() |
| 3209 | << TSInfo->getTypeLoc().getSourceRange(); |
| 3210 | return ExprError(); |
| 3211 | } |
| 3212 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 3213 | Expr *Result = new (Context) ObjCBridgedCastExpr(LParenLoc, Kind, CK, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3214 | BridgeKeywordLoc, |
| 3215 | TSInfo, SubExpr); |
| 3216 | |
| 3217 | if (MustConsume) { |
| 3218 | ExprNeedsCleanups = true; |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 3219 | Result = ImplicitCastExpr::Create(Context, T, CK_ARCConsumeObject, Result, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3220 | 0, VK_RValue); |
| 3221 | } |
| 3222 | |
| 3223 | return Result; |
| 3224 | } |
| 3225 | |
| 3226 | ExprResult Sema::ActOnObjCBridgedCast(Scope *S, |
| 3227 | SourceLocation LParenLoc, |
| 3228 | ObjCBridgeCastKind Kind, |
| 3229 | SourceLocation BridgeKeywordLoc, |
| 3230 | ParsedType Type, |
| 3231 | SourceLocation RParenLoc, |
| 3232 | Expr *SubExpr) { |
| 3233 | TypeSourceInfo *TSInfo = 0; |
| 3234 | QualType T = GetTypeFromParser(Type, &TSInfo); |
| 3235 | if (!TSInfo) |
| 3236 | TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc); |
| 3237 | return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo, |
| 3238 | SubExpr); |
| 3239 | } |