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" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Initialization.h" |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 20 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 21 | #include "clang/AST/TypeLoc.h" |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
| 24 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 27 | ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
| 28 | Expr **strings, |
| 29 | unsigned NumStrings) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 30 | StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); |
| 31 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 32 | // Most ObjC strings are formed out of a single piece. However, we *can* |
| 33 | // have strings formed out of multiple @ strings with multiple pptokens in |
| 34 | // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one |
| 35 | // StringLiteral for ObjCStringLiteral to hold onto. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 36 | StringLiteral *S = Strings[0]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 37 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 38 | // If we have a multi-part string, merge it all together. |
| 39 | if (NumStrings != 1) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 40 | // Concatenate objc strings. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 41 | llvm::SmallString<128> StrBuf; |
| 42 | llvm::SmallVector<SourceLocation, 8> StrLocs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 44 | for (unsigned i = 0; i != NumStrings; ++i) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 45 | S = Strings[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 47 | // ObjC strings can't be wide. |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 48 | if (S->isWide()) { |
| 49 | Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 50 | << S->getSourceRange(); |
| 51 | return true; |
| 52 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 53 | |
Benjamin Kramer | 2f4eaef | 2010-08-17 12:54:38 +0000 | [diff] [blame] | 54 | // Append the string. |
| 55 | StrBuf += S->getString(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 57 | // Get the locations of the string tokens. |
| 58 | StrLocs.append(S->tokloc_begin(), S->tokloc_end()); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 59 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 61 | // Create the aggregate string with the appropriate content and location |
| 62 | // information. |
| 63 | S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false, |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 64 | Context.getPointerType(Context.CharTy), |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 65 | &StrLocs[0], StrLocs.size()); |
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 | 6903981 | 2009-02-18 06:01:06 +0000 | [diff] [blame] | 68 | // Verify that this composite string is acceptable for ObjC strings. |
| 69 | if (CheckObjCString(S)) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 70 | return true; |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 71 | |
| 72 | // Initialize the constant string interface lazily. This assumes |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 73 | // the NSString interface is seen in this translation unit. Note: We |
| 74 | // don't use NSConstantString, since the runtime team considers this |
| 75 | // interface private (even though it appears in the header files). |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 76 | QualType Ty = Context.getObjCConstantStringInterface(); |
| 77 | if (!Ty.isNull()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 78 | Ty = Context.getObjCObjectPointerType(Ty); |
Fariborz Jahanian | 8a43776 | 2010-04-23 23:19:04 +0000 | [diff] [blame] | 79 | } else if (getLangOptions().NoConstantCFStrings) { |
| 80 | IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString"); |
| 81 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0], |
| 82 | LookupOrdinaryName); |
| 83 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 84 | Context.setObjCConstantStringInterface(StrIF); |
| 85 | Ty = Context.getObjCConstantStringInterface(); |
| 86 | Ty = Context.getObjCObjectPointerType(Ty); |
| 87 | } else { |
| 88 | // If there is no NSConstantString interface defined then treat this |
| 89 | // as error and recover from it. |
| 90 | Diag(S->getLocStart(), diag::err_no_nsconstant_string_class) << NSIdent |
| 91 | << S->getSourceRange(); |
| 92 | Ty = Context.getObjCIdType(); |
| 93 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 94 | } else { |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 95 | IdentifierInfo *NSIdent = &Context.Idents.get("NSString"); |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 96 | NamedDecl *IF = LookupSingleName(TUScope, NSIdent, AtLocs[0], |
| 97 | LookupOrdinaryName); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 98 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 99 | Context.setObjCConstantStringInterface(StrIF); |
| 100 | Ty = Context.getObjCConstantStringInterface(); |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 101 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 102 | } else { |
Steve Naroff | d9fd764 | 2009-04-07 14:18:33 +0000 | [diff] [blame] | 103 | // If there is no NSString interface defined then treat constant |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 104 | // strings as untyped objects and let the runtime figure it out later. |
| 105 | Ty = Context.getObjCIdType(); |
| 106 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 107 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 108 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 109 | return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 113 | TypeSourceInfo *EncodedTypeInfo, |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 114 | SourceLocation RParenLoc) { |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 115 | QualType EncodedType = EncodedTypeInfo->getType(); |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 116 | QualType StrTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 117 | if (EncodedType->isDependentType()) |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 118 | StrTy = Context.DependentTy; |
| 119 | else { |
| 120 | std::string Str; |
| 121 | Context.getObjCEncodingForType(EncodedType, Str); |
| 122 | |
| 123 | // The type of @encode is the same as the type of the corresponding string, |
| 124 | // which is an array type. |
| 125 | StrTy = Context.CharTy; |
| 126 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
John McCall | 4b7a834 | 2010-03-15 10:54:44 +0000 | [diff] [blame] | 127 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 128 | StrTy.addConst(); |
| 129 | StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), |
| 130 | ArrayType::Normal, 0); |
| 131 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 133 | return new (Context) ObjCEncodeExpr(StrTy, EncodedTypeInfo, AtLoc, RParenLoc); |
Anders Carlsson | fc0f021 | 2009-06-07 18:45:35 +0000 | [diff] [blame] | 134 | } |
| 135 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 136 | ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 137 | SourceLocation EncodeLoc, |
| 138 | SourceLocation LParenLoc, |
| 139 | ParsedType ty, |
| 140 | SourceLocation RParenLoc) { |
Argyrios Kyrtzidis | e866190 | 2009-08-19 01:28:28 +0000 | [diff] [blame] | 141 | // FIXME: Preserve type source info ? |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 142 | TypeSourceInfo *TInfo; |
| 143 | QualType EncodedType = GetTypeFromParser(ty, &TInfo); |
| 144 | if (!TInfo) |
| 145 | TInfo = Context.getTrivialTypeSourceInfo(EncodedType, |
| 146 | PP.getLocForEndOfToken(LParenLoc)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 147 | |
Douglas Gregor | 81d3466 | 2010-04-20 15:39:42 +0000 | [diff] [blame] | 148 | return BuildObjCEncodeExpression(AtLoc, TInfo, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 149 | } |
| 150 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 151 | ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 152 | SourceLocation AtLoc, |
| 153 | SourceLocation SelLoc, |
| 154 | SourceLocation LParenLoc, |
| 155 | SourceLocation RParenLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 157 | SourceRange(LParenLoc, RParenLoc), false, false); |
Fariborz Jahanian | 7ff22de | 2009-06-16 16:25:00 +0000 | [diff] [blame] | 158 | if (!Method) |
| 159 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 160 | SourceRange(LParenLoc, RParenLoc)); |
| 161 | if (!Method) |
| 162 | Diag(SelLoc, diag::warn_undeclared_selector) << Sel; |
| 163 | |
Fariborz Jahanian | 3fe1041 | 2010-07-22 18:24:20 +0000 | [diff] [blame] | 164 | llvm::DenseMap<Selector, SourceLocation>::iterator Pos |
| 165 | = ReferencedSelectors.find(Sel); |
| 166 | if (Pos == ReferencedSelectors.end()) |
| 167 | ReferencedSelectors.insert(std::make_pair(Sel, SelLoc)); |
| 168 | |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 169 | QualType Ty = Context.getObjCSelType(); |
Daniel Dunbar | 6d5a1c2 | 2010-02-03 20:11:42 +0000 | [diff] [blame] | 170 | return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 171 | } |
| 172 | |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 173 | ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 174 | SourceLocation AtLoc, |
| 175 | SourceLocation ProtoLoc, |
| 176 | SourceLocation LParenLoc, |
| 177 | SourceLocation RParenLoc) { |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 178 | ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId, ProtoLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 179 | if (!PDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 180 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 181 | return true; |
| 182 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 184 | QualType Ty = Context.getObjCProtoType(); |
| 185 | if (Ty.isNull()) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 186 | return true; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 187 | Ty = Context.getObjCObjectPointerType(Ty); |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 188 | return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, |
| 192 | Selector Sel, ObjCMethodDecl *Method, |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 193 | bool isClassMessage, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 194 | SourceLocation lbrac, SourceLocation rbrac, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | QualType &ReturnType) { |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 196 | if (!Method) { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 197 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 198 | for (unsigned i = 0; i != NumArgs; i++) { |
| 199 | if (Args[i]->isTypeDependent()) |
| 200 | continue; |
| 201 | |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 202 | DefaultArgumentPromotion(Args[i]); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 203 | } |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 204 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 205 | unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found : |
| 206 | diag::warn_inst_method_not_found; |
| 207 | Diag(lbrac, DiagID) |
| 208 | << Sel << isClassMessage << SourceRange(lbrac, rbrac); |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 209 | ReturnType = Context.getObjCIdType(); |
| 210 | return false; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 213 | ReturnType = Method->getSendResultType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 215 | unsigned NumNamedArgs = Sel.getNumArgs(); |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 216 | // Method might have more arguments than selector indicates. This is due |
| 217 | // to addition of c-style arguments in method. |
| 218 | if (Method->param_size() > Sel.getNumArgs()) |
| 219 | NumNamedArgs = Method->param_size(); |
| 220 | // FIXME. This need be cleaned up. |
| 221 | if (NumArgs < NumNamedArgs) { |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 222 | Diag(lbrac, diag::err_typecheck_call_too_few_args) << 2 |
| 223 | << NumNamedArgs << NumArgs; |
Fariborz Jahanian | 4f4fd92 | 2010-04-08 00:30:06 +0000 | [diff] [blame] | 224 | return false; |
| 225 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 226 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 227 | bool IsError = false; |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 228 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 229 | // We can't do any type-checking on a type-dependent argument. |
| 230 | if (Args[i]->isTypeDependent()) |
| 231 | continue; |
| 232 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 233 | Expr *argExpr = Args[i]; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 234 | |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 235 | ParmVarDecl *Param = Method->param_begin()[i]; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 236 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 237 | |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 238 | if (RequireCompleteType(argExpr->getSourceRange().getBegin(), |
| 239 | Param->getType(), |
| 240 | PDiag(diag::err_call_incomplete_argument) |
| 241 | << argExpr->getSourceRange())) |
| 242 | return true; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 243 | |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 244 | InitializedEntity Entity = InitializedEntity::InitializeParameter(Param); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 245 | ExprResult ArgE = PerformCopyInitialization(Entity, |
Douglas Gregor | 688fc9b | 2010-04-21 23:24:10 +0000 | [diff] [blame] | 246 | SourceLocation(), |
| 247 | Owned(argExpr->Retain())); |
| 248 | if (ArgE.isInvalid()) |
| 249 | IsError = true; |
| 250 | else |
| 251 | Args[i] = ArgE.takeAs<Expr>(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 252 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 253 | |
| 254 | // Promote additional arguments to variadic methods. |
| 255 | if (Method->isVariadic()) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 256 | for (unsigned i = NumNamedArgs; i < NumArgs; ++i) { |
| 257 | if (Args[i]->isTypeDependent()) |
| 258 | continue; |
| 259 | |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 260 | IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod, 0); |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 261 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 262 | } else { |
| 263 | // Check for extra arguments to non-variadic methods. |
| 264 | if (NumArgs != NumNamedArgs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 265 | Diag(Args[NumNamedArgs]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 266 | diag::err_typecheck_call_too_many_args) |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 267 | << 2 /*method*/ << NumNamedArgs << NumArgs |
| 268 | << Method->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 269 | << SourceRange(Args[NumNamedArgs]->getLocStart(), |
| 270 | Args[NumArgs-1]->getLocEnd()); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 274 | DiagnoseSentinelCalls(Method, lbrac, Args, NumArgs); |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 275 | return IsError; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 278 | bool Sema::isSelfExpr(Expr *RExpr) { |
| 279 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr)) |
| 280 | if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self")) |
| 281 | return true; |
| 282 | return false; |
| 283 | } |
| 284 | |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 285 | // Helper method for ActOnClassMethod/ActOnInstanceMethod. |
| 286 | // Will search "local" class/category implementations for a method decl. |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 287 | // 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] | 288 | // Returns 0 if no method is found. |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 289 | ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel, |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 290 | ObjCInterfaceDecl *ClassDecl) { |
| 291 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 292 | // lookup in class and all superclasses |
| 293 | while (ClassDecl && !Method) { |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 294 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 295 | Method = ImpDecl->getClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 297 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 298 | if (!Method) |
| 299 | Method = ClassDecl->getCategoryClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 301 | // Before we give up, check if the selector is an instance method. |
| 302 | // But only in the root. This matches gcc's behaviour and what the |
| 303 | // runtime expects. |
| 304 | if (!Method && !ClassDecl->getSuperClass()) { |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 305 | Method = ClassDecl->lookupInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 306 | // Look through local category implementations associated |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 307 | // with the root class. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | if (!Method) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 309 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 310 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 311 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 312 | ClassDecl = ClassDecl->getSuperClass(); |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 313 | } |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 314 | return Method; |
| 315 | } |
| 316 | |
| 317 | ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, |
| 318 | ObjCInterfaceDecl *ClassDecl) { |
| 319 | ObjCMethodDecl *Method = 0; |
| 320 | while (ClassDecl && !Method) { |
| 321 | // If we have implementations in scope, check "private" methods. |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 322 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 323 | Method = ImpDecl->getInstanceMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 325 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 326 | if (!Method) |
| 327 | Method = ClassDecl->getCategoryInstanceMethod(Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 328 | ClassDecl = ClassDecl->getSuperClass(); |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 329 | } |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 330 | return Method; |
| 331 | } |
| 332 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 333 | /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an |
| 334 | /// objective C interface. This is a property reference expression. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 335 | ExprResult Sema:: |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 336 | HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT, |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 337 | Expr *BaseExpr, DeclarationName MemberName, |
| 338 | SourceLocation MemberLoc) { |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 339 | const ObjCInterfaceType *IFaceT = OPT->getInterfaceType(); |
| 340 | ObjCInterfaceDecl *IFace = IFaceT->getDecl(); |
| 341 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 342 | |
| 343 | // Search for a declared property first. |
| 344 | if (ObjCPropertyDecl *PD = IFace->FindPropertyDeclaration(Member)) { |
| 345 | // Check whether we can reference this property. |
| 346 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 347 | return ExprError(); |
| 348 | QualType ResTy = PD->getType(); |
| 349 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 350 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
| 351 | if (DiagnosePropertyAccessorMismatch(PD, Getter, MemberLoc)) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 352 | ResTy = Getter->getSendResultType(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 353 | return Owned(new (Context) ObjCPropertyRefExpr(PD, ResTy, |
| 354 | MemberLoc, BaseExpr)); |
| 355 | } |
| 356 | // Check protocols on qualified interfaces. |
| 357 | for (ObjCObjectPointerType::qual_iterator I = OPT->qual_begin(), |
| 358 | E = OPT->qual_end(); I != E; ++I) |
| 359 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 360 | // Check whether we can reference this property. |
| 361 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 362 | return ExprError(); |
| 363 | |
| 364 | return Owned(new (Context) ObjCPropertyRefExpr(PD, PD->getType(), |
| 365 | MemberLoc, BaseExpr)); |
| 366 | } |
| 367 | // If that failed, look for an "implicit" property by seeing if the nullary |
| 368 | // selector is implemented. |
| 369 | |
| 370 | // FIXME: The logic for looking up nullary and unary selectors should be |
| 371 | // shared with the code in ActOnInstanceMessage. |
| 372 | |
| 373 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 374 | ObjCMethodDecl *Getter = IFace->lookupInstanceMethod(Sel); |
| 375 | |
| 376 | // If this reference is in an @implementation, check for 'private' methods. |
| 377 | if (!Getter) |
| 378 | Getter = IFace->lookupPrivateInstanceMethod(Sel); |
| 379 | |
| 380 | // Look through local category implementations associated with the class. |
| 381 | if (!Getter) |
| 382 | Getter = IFace->getCategoryInstanceMethod(Sel); |
| 383 | if (Getter) { |
| 384 | // Check if we can reference this property. |
| 385 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 386 | return ExprError(); |
| 387 | } |
| 388 | // If we found a getter then this may be a valid dot-reference, we |
| 389 | // will look for the matching setter, in case it is needed. |
| 390 | Selector SetterSel = |
| 391 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 392 | PP.getSelectorTable(), Member); |
| 393 | ObjCMethodDecl *Setter = IFace->lookupInstanceMethod(SetterSel); |
| 394 | if (!Setter) { |
| 395 | // If this reference is in an @implementation, also check for 'private' |
| 396 | // methods. |
| 397 | Setter = IFace->lookupPrivateInstanceMethod(SetterSel); |
| 398 | } |
| 399 | // Look through local category implementations associated with the class. |
| 400 | if (!Setter) |
| 401 | Setter = IFace->getCategoryInstanceMethod(SetterSel); |
| 402 | |
| 403 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 404 | return ExprError(); |
| 405 | |
| 406 | if (Getter) { |
| 407 | QualType PType; |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 408 | PType = Getter->getSendResultType(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 409 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr(Getter, PType, |
| 410 | Setter, MemberLoc, BaseExpr)); |
| 411 | } |
| 412 | |
| 413 | // Attempt to correct for typos in property names. |
| 414 | LookupResult Res(*this, MemberName, MemberLoc, LookupOrdinaryName); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 415 | if (CorrectTypo(Res, 0, 0, IFace, false, CTC_NoKeywords, OPT) && |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 416 | Res.getAsSingle<ObjCPropertyDecl>()) { |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 417 | DeclarationName TypoResult = Res.getLookupName(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 418 | Diag(MemberLoc, diag::err_property_not_found_suggest) |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 419 | << MemberName << QualType(OPT, 0) << TypoResult |
| 420 | << FixItHint::CreateReplacement(MemberLoc, TypoResult.getAsString()); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 421 | ObjCPropertyDecl *Property = Res.getAsSingle<ObjCPropertyDecl>(); |
| 422 | Diag(Property->getLocation(), diag::note_previous_decl) |
| 423 | << Property->getDeclName(); |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 424 | return HandleExprPropertyRefExpr(OPT, BaseExpr, TypoResult, MemberLoc); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 425 | } |
Chris Lattner | b9d4fc1 | 2010-04-11 07:51:10 +0000 | [diff] [blame] | 426 | |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 427 | Diag(MemberLoc, diag::err_property_not_found) |
| 428 | << MemberName << QualType(OPT, 0); |
| 429 | if (Setter && !Getter) |
| 430 | Diag(Setter->getLocation(), diag::note_getter_unavailable) |
| 431 | << MemberName << BaseExpr->getSourceRange(); |
| 432 | return ExprError(); |
Chris Lattner | 7f81652 | 2010-04-11 07:45:24 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | |
| 436 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 437 | ExprResult Sema:: |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 438 | ActOnClassPropertyRefExpr(IdentifierInfo &receiverName, |
| 439 | IdentifierInfo &propertyName, |
| 440 | SourceLocation receiverNameLoc, |
| 441 | SourceLocation propertyNameLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 443 | IdentifierInfo *receiverNamePtr = &receiverName; |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 444 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(receiverNamePtr, |
| 445 | receiverNameLoc); |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 446 | if (IFace == 0) { |
| 447 | // If the "receiver" is 'super' in a method, handle it as an expression-like |
| 448 | // property reference. |
| 449 | if (ObjCMethodDecl *CurMethod = getCurMethodDecl()) |
| 450 | if (receiverNamePtr->isStr("super")) { |
| 451 | if (CurMethod->isInstanceMethod()) { |
| 452 | QualType T = |
| 453 | Context.getObjCInterfaceType(CurMethod->getClassInterface()); |
| 454 | T = Context.getObjCObjectPointerType(T); |
| 455 | Expr *SuperExpr = new (Context) ObjCSuperExpr(receiverNameLoc, T); |
| 456 | |
| 457 | return HandleExprPropertyRefExpr(T->getAsObjCInterfacePointerType(), |
| 458 | SuperExpr, &propertyName, |
| 459 | propertyNameLoc); |
| 460 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 461 | |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 462 | // Otherwise, if this is a class method, try dispatching to our |
| 463 | // superclass. |
| 464 | IFace = CurMethod->getClassInterface()->getSuperClass(); |
| 465 | } |
| 466 | |
| 467 | if (IFace == 0) { |
| 468 | Diag(receiverNameLoc, diag::err_expected_ident_or_lparen); |
| 469 | return ExprError(); |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | // Search for a declared property first. |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 474 | Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 475 | ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 476 | |
| 477 | // If this reference is in an @implementation, check for 'private' methods. |
| 478 | if (!Getter) |
| 479 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 480 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 481 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 482 | Getter = ImpDecl->getClassMethod(Sel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 483 | |
| 484 | if (Getter) { |
| 485 | // FIXME: refactor/share with ActOnMemberReference(). |
| 486 | // Check if we can reference this property. |
| 487 | if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) |
| 488 | return ExprError(); |
| 489 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 490 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 491 | // Look for the matching setter, in case it is needed. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 492 | Selector SetterSel = |
| 493 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
Steve Naroff | fdc92b7 | 2009-03-10 17:24:38 +0000 | [diff] [blame] | 494 | PP.getSelectorTable(), &propertyName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 495 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 496 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 497 | if (!Setter) { |
| 498 | // If this reference is in an @implementation, also check for 'private' |
| 499 | // methods. |
| 500 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 501 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Argyrios Kyrtzidis | 8701877 | 2009-07-21 00:06:04 +0000 | [diff] [blame] | 502 | if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 503 | Setter = ImpDecl->getClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 504 | } |
| 505 | // Look through local category implementations associated with the class. |
Argyrios Kyrtzidis | 1cb35dd | 2009-07-21 00:06:20 +0000 | [diff] [blame] | 506 | if (!Setter) |
| 507 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 508 | |
| 509 | if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) |
| 510 | return ExprError(); |
| 511 | |
| 512 | if (Getter || Setter) { |
| 513 | QualType PType; |
| 514 | |
| 515 | if (Getter) |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 516 | PType = Getter->getSendResultType(); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 517 | else { |
| 518 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 519 | E = Setter->param_end(); PI != E; ++PI) |
| 520 | PType = (*PI)->getType(); |
| 521 | } |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 522 | return Owned(new (Context) ObjCImplicitSetterGetterRefExpr( |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 523 | Getter, PType, Setter, |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 524 | propertyNameLoc, IFace, receiverNameLoc)); |
| 525 | } |
| 526 | return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) |
| 527 | << &propertyName << Context.getObjCInterfaceType(IFace)); |
| 528 | } |
| 529 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 530 | Sema::ObjCMessageKind Sema::getObjCMessageKind(Scope *S, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 531 | IdentifierInfo *Name, |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 532 | SourceLocation NameLoc, |
| 533 | bool IsSuper, |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 534 | bool HasTrailingDot, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 535 | ParsedType &ReceiverType) { |
| 536 | ReceiverType = ParsedType(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 537 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 538 | // If the identifier is "super" and there is no trailing dot, we're |
| 539 | // messaging super. |
| 540 | if (IsSuper && !HasTrailingDot && S->isInObjcMethodScope()) |
| 541 | return ObjCSuperMessage; |
| 542 | |
| 543 | LookupResult Result(*this, Name, NameLoc, LookupOrdinaryName); |
| 544 | LookupName(Result, S); |
| 545 | |
| 546 | switch (Result.getResultKind()) { |
| 547 | case LookupResult::NotFound: |
Douglas Gregor | ed46442 | 2010-04-19 20:09:36 +0000 | [diff] [blame] | 548 | // Normal name lookup didn't find anything. If we're in an |
| 549 | // Objective-C method, look for ivars. If we find one, we're done! |
| 550 | // FIXME: This is a hack. Ivar lookup should be part of normal lookup. |
| 551 | if (ObjCMethodDecl *Method = getCurMethodDecl()) { |
| 552 | ObjCInterfaceDecl *ClassDeclared; |
| 553 | if (Method->getClassInterface()->lookupInstanceVariable(Name, |
| 554 | ClassDeclared)) |
| 555 | return ObjCInstanceMessage; |
| 556 | } |
| 557 | |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 558 | // Break out; we'll perform typo correction below. |
| 559 | break; |
| 560 | |
| 561 | case LookupResult::NotFoundInCurrentInstantiation: |
| 562 | case LookupResult::FoundOverloaded: |
| 563 | case LookupResult::FoundUnresolvedValue: |
| 564 | case LookupResult::Ambiguous: |
| 565 | Result.suppressDiagnostics(); |
| 566 | return ObjCInstanceMessage; |
| 567 | |
| 568 | case LookupResult::Found: { |
| 569 | // We found something. If it's a type, then we have a class |
| 570 | // message. Otherwise, it's an instance message. |
| 571 | NamedDecl *ND = Result.getFoundDecl(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 572 | QualType T; |
| 573 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) |
| 574 | T = Context.getObjCInterfaceType(Class); |
| 575 | else if (TypeDecl *Type = dyn_cast<TypeDecl>(ND)) |
| 576 | T = Context.getTypeDeclType(Type); |
| 577 | else |
| 578 | return ObjCInstanceMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 579 | |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 580 | // We have a class message, and T is the type we're |
| 581 | // messaging. Build source-location information for it. |
| 582 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 583 | ReceiverType = CreateParsedType(T, TSInfo); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 584 | return ObjCClassMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 588 | // Determine our typo-correction context. |
| 589 | CorrectTypoContext CTC = CTC_Expression; |
| 590 | if (ObjCMethodDecl *Method = getCurMethodDecl()) |
| 591 | if (Method->getClassInterface() && |
| 592 | Method->getClassInterface()->getSuperClass()) |
| 593 | CTC = CTC_ObjCMessageReceiver; |
| 594 | |
| 595 | if (DeclarationName Corrected = CorrectTypo(Result, S, 0, 0, false, CTC)) { |
| 596 | if (Result.isSingleResult()) { |
| 597 | // If we found a declaration, correct when it refers to an Objective-C |
| 598 | // class. |
| 599 | NamedDecl *ND = Result.getFoundDecl(); |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 600 | if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 601 | Diag(NameLoc, diag::err_unknown_receiver_suggest) |
| 602 | << Name << Result.getLookupName() |
| 603 | << FixItHint::CreateReplacement(SourceRange(NameLoc), |
| 604 | ND->getNameAsString()); |
| 605 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 606 | << Corrected; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 607 | |
Douglas Gregor | 1569f95 | 2010-04-21 20:38:13 +0000 | [diff] [blame] | 608 | QualType T = Context.getObjCInterfaceType(Class); |
| 609 | TypeSourceInfo *TSInfo = Context.getTrivialTypeSourceInfo(T, NameLoc); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 610 | ReceiverType = CreateParsedType(T, TSInfo); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 611 | return ObjCClassMessage; |
| 612 | } |
| 613 | } else if (Result.empty() && Corrected.getAsIdentifierInfo() && |
| 614 | Corrected.getAsIdentifierInfo()->isStr("super")) { |
| 615 | // If we've found the keyword "super", this is a send to super. |
| 616 | Diag(NameLoc, diag::err_unknown_receiver_suggest) |
| 617 | << Name << Corrected |
| 618 | << FixItHint::CreateReplacement(SourceRange(NameLoc), "super"); |
| 619 | Name = Corrected.getAsIdentifierInfo(); |
| 620 | return ObjCSuperMessage; |
Douglas Gregor | 47bd543 | 2010-04-14 02:46:37 +0000 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
| 624 | // Fall back: let the parser try to parse it as an instance message. |
| 625 | return ObjCInstanceMessage; |
| 626 | } |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 627 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 628 | ExprResult Sema::ActOnSuperMessage(Scope *S, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 629 | SourceLocation SuperLoc, |
| 630 | Selector Sel, |
| 631 | SourceLocation LBracLoc, |
| 632 | SourceLocation SelectorLoc, |
| 633 | SourceLocation RBracLoc, |
| 634 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 635 | // Determine whether we are inside a method or not. |
| 636 | ObjCMethodDecl *Method = getCurMethodDecl(); |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 637 | if (!Method) { |
| 638 | Diag(SuperLoc, diag::err_invalid_receiver_to_message_super); |
| 639 | return ExprError(); |
| 640 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 641 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 642 | ObjCInterfaceDecl *Class = Method->getClassInterface(); |
| 643 | if (!Class) { |
| 644 | Diag(SuperLoc, diag::error_no_super_class_message) |
| 645 | << Method->getDeclName(); |
| 646 | return ExprError(); |
| 647 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 648 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 649 | ObjCInterfaceDecl *Super = Class->getSuperClass(); |
| 650 | if (!Super) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 651 | // The current class does not have a superclass. |
| 652 | Diag(SuperLoc, diag::error_no_super_class) << Class->getIdentifier(); |
| 653 | return ExprError(); |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 654 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 655 | |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 656 | // We are in a method whose class has a superclass, so 'super' |
| 657 | // is acting as a keyword. |
| 658 | if (Method->isInstanceMethod()) { |
| 659 | // Since we are in an instance method, this is an instance |
| 660 | // message to the superclass instance. |
| 661 | QualType SuperTy = Context.getObjCInterfaceType(Super); |
| 662 | SuperTy = Context.getObjCObjectPointerType(SuperTy); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 663 | return BuildInstanceMessage(0, SuperTy, SuperLoc, |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 664 | Sel, /*Method=*/0, LBracLoc, RBracLoc, |
| 665 | move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 666 | } |
Douglas Gregor | f95861a | 2010-04-21 20:01:04 +0000 | [diff] [blame] | 667 | |
| 668 | // Since we are in a class method, this is a class message to |
| 669 | // the superclass. |
| 670 | return BuildClassMessage(/*ReceiverTypeInfo=*/0, |
| 671 | Context.getObjCInterfaceType(Super), |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 672 | SuperLoc, Sel, /*Method=*/0, LBracLoc, RBracLoc, |
| 673 | move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /// \brief Build an Objective-C class message expression. |
| 677 | /// |
| 678 | /// This routine takes care of both normal class messages and |
| 679 | /// class messages to the superclass. |
| 680 | /// |
| 681 | /// \param ReceiverTypeInfo Type source information that describes the |
| 682 | /// receiver of this message. This may be NULL, in which case we are |
| 683 | /// sending to the superclass and \p SuperLoc must be a valid source |
| 684 | /// location. |
| 685 | |
| 686 | /// \param ReceiverType The type of the object receiving the |
| 687 | /// message. When \p ReceiverTypeInfo is non-NULL, this is the same |
| 688 | /// type as that refers to. For a superclass send, this is the type of |
| 689 | /// the superclass. |
| 690 | /// |
| 691 | /// \param SuperLoc The location of the "super" keyword in a |
| 692 | /// superclass message. |
| 693 | /// |
| 694 | /// \param Sel The selector to which the message is being sent. |
| 695 | /// |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 696 | /// \param Method The method that this class message is invoking, if |
| 697 | /// already known. |
| 698 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 699 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 700 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 701 | /// \param RBrac The location of the closing square bracket ']'. |
| 702 | /// |
| 703 | /// \param Args The message arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 704 | ExprResult Sema::BuildClassMessage(TypeSourceInfo *ReceiverTypeInfo, |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 705 | QualType ReceiverType, |
| 706 | SourceLocation SuperLoc, |
| 707 | Selector Sel, |
| 708 | ObjCMethodDecl *Method, |
| 709 | SourceLocation LBracLoc, |
| 710 | SourceLocation RBracLoc, |
| 711 | MultiExprArg ArgsIn) { |
| 712 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 713 | : ReceiverTypeInfo->getTypeLoc().getSourceRange().getBegin(); |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 714 | if (LBracLoc.isInvalid()) { |
| 715 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 716 | << FixItHint::CreateInsertion(Loc, "["); |
| 717 | LBracLoc = Loc; |
| 718 | } |
| 719 | |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 720 | if (ReceiverType->isDependentType()) { |
| 721 | // If the receiver type is dependent, we can't type-check anything |
| 722 | // at this point. Build a dependent expression. |
| 723 | unsigned NumArgs = ArgsIn.size(); |
| 724 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 725 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
| 726 | return Owned(ObjCMessageExpr::Create(Context, ReceiverType, LBracLoc, |
| 727 | ReceiverTypeInfo, Sel, /*Method=*/0, |
| 728 | Args, NumArgs, RBracLoc)); |
| 729 | } |
Chris Lattner | 15faee1 | 2010-04-12 05:38:43 +0000 | [diff] [blame] | 730 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 731 | // Find the class to which we are sending this message. |
| 732 | ObjCInterfaceDecl *Class = 0; |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 733 | const ObjCObjectType *ClassType = ReceiverType->getAs<ObjCObjectType>(); |
| 734 | if (!ClassType || !(Class = ClassType->getInterface())) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 735 | Diag(Loc, diag::err_invalid_receiver_class_message) |
| 736 | << ReceiverType; |
| 737 | return ExprError(); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 738 | } |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 739 | assert(Class && "We don't know which class we're messaging?"); |
| 740 | |
| 741 | // Find the method we are messaging. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 742 | if (!Method) { |
| 743 | if (Class->isForwardDecl()) { |
| 744 | // A forward class used in messaging is treated as a 'Class' |
| 745 | Diag(Loc, diag::warn_receiver_forward_class) << Class->getDeclName(); |
| 746 | Method = LookupFactoryMethodInGlobalPool(Sel, |
| 747 | SourceRange(LBracLoc, RBracLoc)); |
| 748 | if (Method) |
| 749 | Diag(Method->getLocation(), diag::note_method_sent_forward_class) |
| 750 | << Method->getDeclName(); |
| 751 | } |
| 752 | if (!Method) |
| 753 | Method = Class->lookupClassMethod(Sel); |
| 754 | |
| 755 | // If we have an implementation in scope, check "private" methods. |
| 756 | if (!Method) |
| 757 | Method = LookupPrivateClassMethod(Sel, Class); |
| 758 | |
| 759 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 760 | return ExprError(); |
Fariborz Jahanian | 89bc314 | 2009-05-08 23:02:36 +0000 | [diff] [blame] | 761 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 763 | // Check the argument types and determine the result type. |
| 764 | QualType ReturnType; |
| 765 | unsigned NumArgs = ArgsIn.size(); |
| 766 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 767 | if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, true, |
Douglas Gregor | ff331c1 | 2010-07-25 18:17:45 +0000 | [diff] [blame] | 768 | LBracLoc, RBracLoc, ReturnType)) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 769 | return ExprError(); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 770 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 771 | // Construct the appropriate ObjCMessageExpr. |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 772 | Expr *Result; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 773 | if (SuperLoc.isValid()) |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 774 | Result = ObjCMessageExpr::Create(Context, ReturnType, LBracLoc, |
| 775 | SuperLoc, /*IsInstanceSuper=*/false, |
| 776 | ReceiverType, Sel, Method, Args, |
| 777 | NumArgs, RBracLoc); |
| 778 | else |
| 779 | Result = ObjCMessageExpr::Create(Context, ReturnType, LBracLoc, |
| 780 | ReceiverTypeInfo, Sel, Method, Args, |
| 781 | NumArgs, RBracLoc); |
| 782 | return MaybeBindToTemporary(Result); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 785 | // ActOnClassMessage - used for both unary and keyword messages. |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 786 | // ArgExprs is optional - if it is present, the number of expressions |
| 787 | // is obtained from Sel.getNumArgs(). |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 788 | ExprResult Sema::ActOnClassMessage(Scope *S, |
Douglas Gregor | 77328d1 | 2010-09-15 23:19:31 +0000 | [diff] [blame] | 789 | ParsedType Receiver, |
| 790 | Selector Sel, |
| 791 | SourceLocation LBracLoc, |
| 792 | SourceLocation SelectorLoc, |
| 793 | SourceLocation RBracLoc, |
| 794 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 795 | TypeSourceInfo *ReceiverTypeInfo; |
| 796 | QualType ReceiverType = GetTypeFromParser(Receiver, &ReceiverTypeInfo); |
| 797 | if (ReceiverType.isNull()) |
| 798 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 799 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 800 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 801 | if (!ReceiverTypeInfo) |
| 802 | ReceiverTypeInfo = Context.getTrivialTypeSourceInfo(ReceiverType, LBracLoc); |
| 803 | |
| 804 | return BuildClassMessage(ReceiverTypeInfo, ReceiverType, |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 805 | /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, |
Douglas Gregor | 39968ad | 2010-04-22 16:50:51 +0000 | [diff] [blame] | 806 | LBracLoc, RBracLoc, move(Args)); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /// \brief Build an Objective-C instance message expression. |
| 810 | /// |
| 811 | /// This routine takes care of both normal instance messages and |
| 812 | /// instance messages to the superclass instance. |
| 813 | /// |
| 814 | /// \param Receiver The expression that computes the object that will |
| 815 | /// receive this message. This may be empty, in which case we are |
| 816 | /// sending to the superclass instance and \p SuperLoc must be a valid |
| 817 | /// source location. |
| 818 | /// |
| 819 | /// \param ReceiverType The (static) type of the object receiving the |
| 820 | /// message. When a \p Receiver expression is provided, this is the |
| 821 | /// same type as that expression. For a superclass instance send, this |
| 822 | /// is a pointer to the type of the superclass. |
| 823 | /// |
| 824 | /// \param SuperLoc The location of the "super" keyword in a |
| 825 | /// superclass instance message. |
| 826 | /// |
| 827 | /// \param Sel The selector to which the message is being sent. |
| 828 | /// |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 829 | /// \param Method The method that this instance message is invoking, if |
| 830 | /// already known. |
| 831 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 832 | /// \param LBracLoc The location of the opening square bracket ']'. |
| 833 | /// |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 834 | /// \param RBrac The location of the closing square bracket ']'. |
| 835 | /// |
| 836 | /// \param Args The message arguments. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 837 | ExprResult Sema::BuildInstanceMessage(Expr *Receiver, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 838 | QualType ReceiverType, |
| 839 | SourceLocation SuperLoc, |
| 840 | Selector Sel, |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 841 | ObjCMethodDecl *Method, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 842 | SourceLocation LBracLoc, |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 843 | SourceLocation RBracLoc, |
| 844 | MultiExprArg ArgsIn) { |
Douglas Gregor | 0fbda68 | 2010-09-15 14:51:05 +0000 | [diff] [blame] | 845 | // The location of the receiver. |
| 846 | SourceLocation Loc = SuperLoc.isValid()? SuperLoc : Receiver->getLocStart(); |
| 847 | |
| 848 | if (LBracLoc.isInvalid()) { |
| 849 | Diag(Loc, diag::err_missing_open_square_message_send) |
| 850 | << FixItHint::CreateInsertion(Loc, "["); |
| 851 | LBracLoc = Loc; |
| 852 | } |
| 853 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 854 | // If we have a receiver expression, perform appropriate promotions |
| 855 | // and determine receiver type. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 856 | if (Receiver) { |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 857 | if (Receiver->isTypeDependent()) { |
| 858 | // If the receiver is type-dependent, we can't type-check anything |
| 859 | // at this point. Build a dependent expression. |
| 860 | unsigned NumArgs = ArgsIn.size(); |
| 861 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 862 | assert(SuperLoc.isInvalid() && "Message to super with dependent type"); |
| 863 | return Owned(ObjCMessageExpr::Create(Context, Context.DependentTy, |
| 864 | LBracLoc, Receiver, Sel, |
| 865 | /*Method=*/0, Args, NumArgs, |
| 866 | RBracLoc)); |
| 867 | } |
| 868 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 869 | // If necessary, apply function/array conversion to the receiver. |
| 870 | // C99 6.7.5.3p[7,8]. |
| 871 | DefaultFunctionArrayLvalueConversion(Receiver); |
| 872 | ReceiverType = Receiver->getType(); |
| 873 | } |
| 874 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 875 | if (!Method) { |
| 876 | // Handle messages to id. |
Fariborz Jahanian | ba55198 | 2010-08-10 18:10:50 +0000 | [diff] [blame] | 877 | bool receiverIsId = ReceiverType->isObjCIdType(); |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 878 | if (receiverIsId || ReceiverType->isBlockPointerType() || |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 879 | (Receiver && Context.isObjCNSObjectType(Receiver->getType()))) { |
| 880 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 881 | SourceRange(LBracLoc, RBracLoc), |
| 882 | receiverIsId); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 883 | if (!Method) |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 884 | Method = LookupFactoryMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 885 | SourceRange(LBracLoc, RBracLoc), |
| 886 | receiverIsId); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 887 | } else if (ReceiverType->isObjCClassType() || |
| 888 | ReceiverType->isObjCQualifiedClassType()) { |
| 889 | // Handle messages to Class. |
| 890 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 891 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { |
| 892 | // First check the public methods in the class interface. |
| 893 | Method = ClassDecl->lookupClassMethod(Sel); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 895 | if (!Method) |
| 896 | Method = LookupPrivateClassMethod(Sel, ClassDecl); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 898 | // FIXME: if we still haven't found a method, we need to look in |
| 899 | // protocols (if we have qualifiers). |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 900 | } |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 901 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 902 | return ExprError(); |
Fariborz Jahanian | 268bc8c | 2009-03-03 22:19:15 +0000 | [diff] [blame] | 903 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 904 | if (!Method) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 905 | // If not messaging 'self', look for any factory method named 'Sel'. |
| 906 | if (!Receiver || !isSelfExpr(Receiver)) { |
| 907 | Method = LookupFactoryMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 908 | SourceRange(LBracLoc, RBracLoc), |
| 909 | true); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 910 | if (!Method) { |
| 911 | // If no class (factory) method was found, check if an _instance_ |
| 912 | // method of the same name exists in the root class only. |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 913 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 914 | SourceRange(LBracLoc, RBracLoc), |
| 915 | true); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 916 | if (Method) |
| 917 | if (const ObjCInterfaceDecl *ID = |
| 918 | dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { |
| 919 | if (ID->getSuperClass()) |
| 920 | Diag(Loc, diag::warn_root_inst_method_not_found) |
| 921 | << Sel << SourceRange(LBracLoc, RBracLoc); |
| 922 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 926 | } else { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 927 | ObjCInterfaceDecl* ClassDecl = 0; |
| 928 | |
| 929 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 930 | // long as one of the protocols implements the selector (if not, warn). |
| 931 | if (const ObjCObjectPointerType *QIdTy |
| 932 | = ReceiverType->getAsObjCQualifiedIdType()) { |
| 933 | // Search protocols for instance methods. |
| 934 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
| 935 | E = QIdTy->qual_end(); I != E; ++I) { |
| 936 | ObjCProtocolDecl *PDecl = *I; |
| 937 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 938 | break; |
| 939 | // Since we aren't supporting "Class<foo>", look for a class method. |
| 940 | if (PDecl && (Method = PDecl->lookupClassMethod(Sel))) |
| 941 | break; |
| 942 | } |
| 943 | } else if (const ObjCObjectPointerType *OCIType |
| 944 | = ReceiverType->getAsObjCInterfacePointerType()) { |
| 945 | // We allow sending a message to a pointer to an interface (an object). |
| 946 | ClassDecl = OCIType->getInterfaceDecl(); |
| 947 | // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be |
| 948 | // faster than the following method (which can do *many* linear searches). |
Sebastian Redl | db9d214 | 2010-08-02 23:18:59 +0000 | [diff] [blame] | 949 | // The idea is to add class info to MethodPool. |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 950 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 951 | |
| 952 | if (!Method) { |
| 953 | // Search protocol qualifiers. |
| 954 | for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(), |
| 955 | E = OCIType->qual_end(); QI != E; ++QI) { |
| 956 | if ((Method = (*QI)->lookupInstanceMethod(Sel))) |
| 957 | break; |
| 958 | } |
| 959 | } |
| 960 | if (!Method) { |
| 961 | // If we have implementations in scope, check "private" methods. |
| 962 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 963 | |
| 964 | if (!Method && (!Receiver || !isSelfExpr(Receiver))) { |
| 965 | // If we still haven't found a method, look in the global pool. This |
| 966 | // behavior isn't very desirable, however we need it for GCC |
| 967 | // compatibility. FIXME: should we deviate?? |
| 968 | if (OCIType->qual_empty()) { |
| 969 | Method = LookupInstanceMethodInGlobalPool(Sel, |
Fariborz Jahanian | 6b308f6 | 2010-08-09 23:27:58 +0000 | [diff] [blame] | 970 | SourceRange(LBracLoc, RBracLoc)); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 971 | if (Method && !OCIType->getInterfaceDecl()->isForwardDecl()) |
| 972 | Diag(Loc, diag::warn_maynot_respond) |
| 973 | << OCIType->getInterfaceDecl()->getIdentifier() << Sel; |
| 974 | } |
| 975 | } |
| 976 | } |
| 977 | if (Method && DiagnoseUseOfDecl(Method, Loc)) |
| 978 | return ExprError(); |
| 979 | } else if (!Context.getObjCIdType().isNull() && |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 980 | (ReceiverType->isPointerType() || |
| 981 | ReceiverType->isIntegerType())) { |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 982 | // Implicitly convert integers and pointers to 'id' but emit a warning. |
| 983 | Diag(Loc, diag::warn_bad_receiver_type) |
| 984 | << ReceiverType |
| 985 | << Receiver->getSourceRange(); |
| 986 | if (ReceiverType->isPointerType()) |
| 987 | ImpCastExprToType(Receiver, Context.getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 988 | CK_BitCast); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 989 | else |
| 990 | ImpCastExprToType(Receiver, Context.getObjCIdType(), |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 991 | CK_IntegralToPointer); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 992 | ReceiverType = Receiver->getType(); |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 993 | } |
Fariborz Jahanian | 3ba6061 | 2010-05-13 17:19:25 +0000 | [diff] [blame] | 994 | else if (getLangOptions().CPlusPlus && |
| 995 | !PerformContextuallyConvertToObjCId(Receiver)) { |
| 996 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(Receiver)) { |
| 997 | Receiver = ICE->getSubExpr(); |
| 998 | ReceiverType = Receiver->getType(); |
| 999 | } |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1000 | return BuildInstanceMessage(Receiver, |
Fariborz Jahanian | 79d3f04 | 2010-05-12 23:29:11 +0000 | [diff] [blame] | 1001 | ReceiverType, |
| 1002 | SuperLoc, |
| 1003 | Sel, |
| 1004 | Method, |
| 1005 | LBracLoc, |
| 1006 | RBracLoc, |
| 1007 | move(ArgsIn)); |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1008 | } else { |
| 1009 | // Reject other random receiver types (e.g. structs). |
| 1010 | Diag(Loc, diag::err_bad_receiver_type) |
| 1011 | << ReceiverType << Receiver->getSourceRange(); |
| 1012 | return ExprError(); |
| 1013 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1014 | } |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 1015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1016 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1017 | // Check the message arguments. |
| 1018 | unsigned NumArgs = ArgsIn.size(); |
| 1019 | Expr **Args = reinterpret_cast<Expr **>(ArgsIn.release()); |
| 1020 | QualType ReturnType; |
| 1021 | if (CheckMessageArgumentTypes(Args, NumArgs, Sel, Method, false, |
| 1022 | LBracLoc, RBracLoc, ReturnType)) |
| 1023 | return ExprError(); |
Fariborz Jahanian | da59e09 | 2010-06-16 19:56:08 +0000 | [diff] [blame] | 1024 | |
| 1025 | if (!ReturnType->isVoidType()) { |
| 1026 | if (RequireCompleteType(LBracLoc, ReturnType, |
| 1027 | diag::err_illegal_message_expr_incomplete_type)) |
| 1028 | return ExprError(); |
| 1029 | } |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 1030 | |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1031 | // Construct the appropriate ObjCMessageExpr instance. |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1032 | Expr *Result; |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1033 | if (SuperLoc.isValid()) |
Douglas Gregor | 2d6b0e9 | 2010-05-22 05:17:18 +0000 | [diff] [blame] | 1034 | Result = ObjCMessageExpr::Create(Context, ReturnType, LBracLoc, |
| 1035 | SuperLoc, /*IsInstanceSuper=*/true, |
| 1036 | ReceiverType, Sel, Method, |
| 1037 | Args, NumArgs, RBracLoc); |
| 1038 | else |
| 1039 | Result = ObjCMessageExpr::Create(Context, ReturnType, LBracLoc, Receiver, |
| 1040 | Sel, Method, Args, NumArgs, RBracLoc); |
| 1041 | return MaybeBindToTemporary(Result); |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 1045 | // ArgExprs is optional - if it is present, the number of expressions |
| 1046 | // is obtained from Sel.getNumArgs(). |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1047 | ExprResult Sema::ActOnInstanceMessage(Scope *S, |
| 1048 | Expr *Receiver, |
| 1049 | Selector Sel, |
| 1050 | SourceLocation LBracLoc, |
| 1051 | SourceLocation SelectorLoc, |
| 1052 | SourceLocation RBracLoc, |
| 1053 | MultiExprArg Args) { |
Douglas Gregor | 2725ca8 | 2010-04-21 19:57:20 +0000 | [diff] [blame] | 1054 | if (!Receiver) |
| 1055 | return ExprError(); |
| 1056 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 1057 | return BuildInstanceMessage(Receiver, Receiver->getType(), |
Douglas Gregor | f49bb08 | 2010-04-22 17:01:48 +0000 | [diff] [blame] | 1058 | /*SuperLoc=*/SourceLocation(), Sel, /*Method=*/0, |
| 1059 | LBracLoc, RBracLoc, move(Args)); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1060 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 1061 | |