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