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