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