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