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