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