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