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