Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 1 | //===--- SemaExprObjC.cpp - Semantic Analysis for ObjC Expressions --------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for Objective-C expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "Sema.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| 16 | #include "clang/AST/DeclObjC.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 17 | #include "clang/AST/ExprObjC.h" |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 19 | #include "clang/Lex/Preprocessor.h" |
| 20 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
| 23 | Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 24 | ExprTy **strings, |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 25 | unsigned NumStrings) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 26 | StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); |
| 27 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 28 | // Most ObjC strings are formed out of a single piece. However, we *can* |
| 29 | // have strings formed out of multiple @ strings with multiple pptokens in |
| 30 | // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one |
| 31 | // StringLiteral for ObjCStringLiteral to hold onto. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 32 | StringLiteral *S = Strings[0]; |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 33 | |
| 34 | // If we have a multi-part string, merge it all together. |
| 35 | if (NumStrings != 1) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 36 | // Concatenate objc strings. |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 37 | llvm::SmallString<128> StrBuf; |
| 38 | llvm::SmallVector<SourceLocation, 8> StrLocs; |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 40 | for (unsigned i = 0; i != NumStrings; ++i) { |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 41 | S = Strings[i]; |
| 42 | |
| 43 | // ObjC strings can't be wide. |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 44 | if (S->isWide()) { |
| 45 | Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) |
| 46 | << S->getSourceRange(); |
| 47 | return true; |
| 48 | } |
| 49 | |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 50 | // Get the string data. |
| 51 | StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength()); |
| 52 | |
| 53 | // Get the locations of the string tokens. |
| 54 | StrLocs.append(S->tokloc_begin(), S->tokloc_end()); |
| 55 | |
| 56 | // Free the temporary string. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 57 | S->Destroy(Context); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 58 | } |
Chris Lattner | 39c28bb | 2009-02-18 06:48:40 +0000 | [diff] [blame] | 59 | |
| 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 | } |
| 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 |
| 72 | // the NSConstantString interface is seen in this translation unit. |
| 73 | QualType Ty = Context.getObjCConstantStringInterface(); |
| 74 | if (!Ty.isNull()) { |
| 75 | Ty = Context.getPointerType(Ty); |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 76 | } else { |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 77 | IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString"); |
| 78 | NamedDecl *IF = LookupName(TUScope, NSIdent, LookupOrdinaryName); |
| 79 | if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { |
| 80 | Context.setObjCConstantStringInterface(StrIF); |
| 81 | Ty = Context.getObjCConstantStringInterface(); |
| 82 | Ty = Context.getPointerType(Ty); |
| 83 | } else { |
| 84 | // If there is no NSConstantString interface defined then treat constant |
| 85 | // strings as untyped objects and let the runtime figure it out later. |
| 86 | Ty = Context.getObjCIdType(); |
| 87 | } |
Chris Lattner | 13fd7e5 | 2008-06-21 21:44:18 +0000 | [diff] [blame] | 88 | } |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 89 | |
Chris Lattner | f4b136f | 2009-02-18 06:13:04 +0000 | [diff] [blame] | 90 | return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 94 | SourceLocation EncodeLoc, |
| 95 | SourceLocation LParenLoc, |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 96 | TypeTy *ty, |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 97 | SourceLocation RParenLoc) { |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 98 | QualType EncodedType = QualType::getFromOpaquePtr(ty); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 99 | |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 100 | std::string Str; |
| 101 | Context.getObjCEncodingForType(EncodedType, Str); |
| 102 | |
| 103 | // The type of @encode is the same as the type of the corresponding string, |
| 104 | // which is an array type. |
| 105 | QualType StrTy = Context.CharTy; |
| 106 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| 107 | if (getLangOptions().CPlusPlus) |
| 108 | StrTy.addConst(); |
| 109 | StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), |
| 110 | ArrayType::Normal, 0); |
| 111 | |
| 112 | return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 116 | SourceLocation AtLoc, |
| 117 | SourceLocation SelLoc, |
| 118 | SourceLocation LParenLoc, |
| 119 | SourceLocation RParenLoc) { |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 120 | QualType Ty = Context.getObjCSelType(); |
| 121 | return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 125 | SourceLocation AtLoc, |
| 126 | SourceLocation ProtoLoc, |
| 127 | SourceLocation LParenLoc, |
| 128 | SourceLocation RParenLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 129 | ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId]; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 130 | if (!PDecl) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 131 | Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 132 | return true; |
| 133 | } |
| 134 | |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 135 | QualType Ty = Context.getObjCProtoType(); |
| 136 | if (Ty.isNull()) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 137 | return true; |
Chris Lattner | a0af1fe | 2009-02-18 06:06:56 +0000 | [diff] [blame] | 138 | Ty = Context.getPointerType(Ty); |
| 139 | return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 142 | bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, |
| 143 | Selector Sel, ObjCMethodDecl *Method, |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 144 | bool isClassMessage, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 145 | SourceLocation lbrac, SourceLocation rbrac, |
| 146 | QualType &ReturnType) { |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 147 | if (!Method) { |
Daniel Dunbar | 6660c8a | 2008-09-11 00:04:36 +0000 | [diff] [blame] | 148 | // Apply default argument promotion as for (C99 6.5.2.2p6). |
| 149 | for (unsigned i = 0; i != NumArgs; i++) |
| 150 | DefaultArgumentPromotion(Args[i]); |
| 151 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 152 | unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found : |
| 153 | diag::warn_inst_method_not_found; |
| 154 | Diag(lbrac, DiagID) |
| 155 | << Sel << isClassMessage << SourceRange(lbrac, rbrac); |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 156 | ReturnType = Context.getObjCIdType(); |
| 157 | return false; |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 158 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 159 | |
| 160 | ReturnType = Method->getResultType(); |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 161 | |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 162 | unsigned NumNamedArgs = Sel.getNumArgs(); |
| 163 | assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!"); |
| 164 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 165 | bool anyIncompatibleArgs = false; |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 166 | for (unsigned i = 0; i < NumNamedArgs; i++) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 167 | Expr *argExpr = Args[i]; |
| 168 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
| 169 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 170 | QualType lhsType = Method->param_begin()[i]->getType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 171 | QualType rhsType = argExpr->getType(); |
| 172 | |
| 173 | // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8]. |
Chris Lattner | 987798a | 2008-04-02 17:17:33 +0000 | [diff] [blame] | 174 | if (lhsType->isArrayType()) |
| 175 | lhsType = Context.getArrayDecayedType(lhsType); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 176 | else if (lhsType->isFunctionType()) |
| 177 | lhsType = Context.getPointerType(lhsType); |
| 178 | |
Chris Lattner | 987798a | 2008-04-02 17:17:33 +0000 | [diff] [blame] | 179 | AssignConvertType Result = |
| 180 | CheckSingleAssignmentConstraints(lhsType, argExpr); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 181 | if (Args[i] != argExpr) // The expression was converted. |
| 182 | Args[i] = argExpr; // Make sure we store the converted expression. |
| 183 | |
| 184 | anyIncompatibleArgs |= |
| 185 | DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType, |
| 186 | argExpr, "sending"); |
| 187 | } |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 188 | |
| 189 | // Promote additional arguments to variadic methods. |
| 190 | if (Method->isVariadic()) { |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 191 | for (unsigned i = NumNamedArgs; i < NumArgs; ++i) |
| 192 | DefaultVariadicArgumentPromotion(Args[i], VariadicMethod); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 193 | } else { |
| 194 | // Check for extra arguments to non-variadic methods. |
| 195 | if (NumArgs != NumNamedArgs) { |
| 196 | Diag(Args[NumNamedArgs]->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 197 | diag::err_typecheck_call_too_many_args) |
Chris Lattner | 2c21a07 | 2008-11-21 18:44:24 +0000 | [diff] [blame] | 198 | << 2 /*method*/ << Method->getSourceRange() |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 199 | << SourceRange(Args[NumNamedArgs]->getLocStart(), |
| 200 | Args[NumArgs-1]->getLocEnd()); |
Daniel Dunbar | 91e19b2 | 2008-09-11 00:50:25 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 204 | return anyIncompatibleArgs; |
| 205 | } |
| 206 | |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 207 | bool Sema::isSelfExpr(Expr *RExpr) { |
| 208 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr)) |
| 209 | if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self")) |
| 210 | return true; |
| 211 | return false; |
| 212 | } |
| 213 | |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 214 | // Helper method for ActOnClassMethod/ActOnInstanceMethod. |
| 215 | // Will search "local" class/category implementations for a method decl. |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 216 | // 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] | 217 | // Returns 0 if no method is found. |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 218 | ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel, |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 219 | ObjCInterfaceDecl *ClassDecl) { |
| 220 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 221 | // lookup in class and all superclasses |
| 222 | while (ClassDecl && !Method) { |
| 223 | if (ObjCImplementationDecl *ImpDecl = |
| 224 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 225 | Method = ImpDecl->getClassMethod(Sel); |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 226 | |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 227 | // Look through local category implementations associated with the class. |
| 228 | if (!Method) { |
| 229 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) { |
| 230 | if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl) |
| 231 | Method = ObjCCategoryImpls[i]->getClassMethod(Sel); |
| 232 | } |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 233 | } |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 234 | |
| 235 | // Before we give up, check if the selector is an instance method. |
| 236 | // But only in the root. This matches gcc's behaviour and what the |
| 237 | // runtime expects. |
| 238 | if (!Method && !ClassDecl->getSuperClass()) { |
| 239 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 240 | // Look through local category implementations associated |
| 241 | // with the root class. |
| 242 | if (!Method) |
| 243 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 244 | } |
| 245 | |
| 246 | ClassDecl = ClassDecl->getSuperClass(); |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 247 | } |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 248 | return Method; |
| 249 | } |
| 250 | |
| 251 | ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, |
| 252 | ObjCInterfaceDecl *ClassDecl) { |
| 253 | ObjCMethodDecl *Method = 0; |
| 254 | while (ClassDecl && !Method) { |
| 255 | // If we have implementations in scope, check "private" methods. |
| 256 | if (ObjCImplementationDecl *ImpDecl = |
| 257 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 258 | Method = ImpDecl->getInstanceMethod(Sel); |
| 259 | |
| 260 | // Look through local category implementations associated with the class. |
| 261 | if (!Method) { |
| 262 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Method; i++) { |
| 263 | if (ObjCCategoryImpls[i]->getClassInterface() == ClassDecl) |
| 264 | Method = ObjCCategoryImpls[i]->getInstanceMethod(Sel); |
| 265 | } |
| 266 | } |
| 267 | ClassDecl = ClassDecl->getSuperClass(); |
Fariborz Jahanian | 175ba1e | 2009-03-04 18:15:57 +0000 | [diff] [blame] | 268 | } |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 269 | return Method; |
| 270 | } |
| 271 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 272 | Action::OwningExprResult Sema::ActOnClassPropertyRefExpr( |
| 273 | IdentifierInfo &receiverName, |
| 274 | IdentifierInfo &propertyName, |
| 275 | SourceLocation &receiverNameLoc, |
| 276 | SourceLocation &propertyNameLoc) { |
| 277 | |
| 278 | ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName); |
| 279 | |
| 280 | // Search for a declared property first. |
| 281 | |
| 282 | Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); |
| 283 | ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); |
| 284 | |
| 285 | // If this reference is in an @implementation, check for 'private' methods. |
| 286 | if (!Getter) |
| 287 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 288 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 289 | if (ObjCImplementationDecl *ImpDecl = |
| 290 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 291 | Getter = ImpDecl->getClassMethod(Sel); |
| 292 | |
| 293 | if (Getter) { |
| 294 | // FIXME: refactor/share with ActOnMemberReference(). |
| 295 | // Check if we can reference this property. |
| 296 | if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) |
| 297 | return ExprError(); |
| 298 | } |
| 299 | |
| 300 | // Look for the matching setter, in case it is needed. |
Steve Naroff | fdc92b7 | 2009-03-10 17:24:38 +0000 | [diff] [blame] | 301 | Selector SetterSel = |
| 302 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 303 | PP.getSelectorTable(), &propertyName); |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 304 | |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 305 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 306 | if (!Setter) { |
| 307 | // If this reference is in an @implementation, also check for 'private' |
| 308 | // methods. |
| 309 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) |
| 310 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
| 311 | if (ObjCImplementationDecl *ImpDecl = |
| 312 | ObjCImplementations[ClassDecl->getIdentifier()]) |
| 313 | Setter = ImpDecl->getClassMethod(SetterSel); |
| 314 | } |
| 315 | // Look through local category implementations associated with the class. |
| 316 | if (!Setter) { |
| 317 | for (unsigned i = 0; i < ObjCCategoryImpls.size() && !Setter; i++) { |
| 318 | if (ObjCCategoryImpls[i]->getClassInterface() == IFace) |
| 319 | Setter = ObjCCategoryImpls[i]->getClassMethod(SetterSel); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) |
| 324 | return ExprError(); |
| 325 | |
| 326 | if (Getter || Setter) { |
| 327 | QualType PType; |
| 328 | |
| 329 | if (Getter) |
| 330 | PType = Getter->getResultType(); |
| 331 | else { |
| 332 | for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), |
| 333 | E = Setter->param_end(); PI != E; ++PI) |
| 334 | PType = (*PI)->getType(); |
| 335 | } |
| 336 | return Owned(new (Context) ObjCKVCRefExpr(Getter, PType, Setter, |
| 337 | propertyNameLoc, IFace, receiverNameLoc)); |
| 338 | } |
| 339 | return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) |
| 340 | << &propertyName << Context.getObjCInterfaceType(IFace)); |
| 341 | } |
| 342 | |
| 343 | |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 344 | // ActOnClassMessage - used for both unary and keyword messages. |
| 345 | // ArgExprs is optional - if it is present, the number of expressions |
| 346 | // is obtained from Sel.getNumArgs(). |
| 347 | Sema::ExprResult Sema::ActOnClassMessage( |
| 348 | Scope *S, |
| 349 | IdentifierInfo *receiverName, Selector Sel, |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 350 | SourceLocation lbrac, SourceLocation receiverLoc, |
| 351 | SourceLocation selectorLoc, SourceLocation rbrac, |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 352 | ExprTy **Args, unsigned NumArgs) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 353 | { |
| 354 | assert(receiverName && "missing receiver class name"); |
| 355 | |
| 356 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 357 | ObjCInterfaceDecl* ClassDecl = 0; |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 358 | bool isSuper = false; |
| 359 | |
Chris Lattner | 8469265 | 2008-11-20 05:35:30 +0000 | [diff] [blame] | 360 | if (receiverName->isStr("super")) { |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 361 | if (getCurMethodDecl()) { |
| 362 | isSuper = true; |
Fariborz Jahanian | 4b1e275 | 2009-01-07 21:01:41 +0000 | [diff] [blame] | 363 | ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface(); |
| 364 | if (!OID) |
| 365 | return Diag(lbrac, diag::error_no_super_class_message) |
| 366 | << getCurMethodDecl()->getDeclName(); |
| 367 | ClassDecl = OID->getSuperClass(); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 368 | if (!ClassDecl) |
Fariborz Jahanian | 4b1e275 | 2009-01-07 21:01:41 +0000 | [diff] [blame] | 369 | return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName(); |
Douglas Gregor | f8d49f6 | 2009-01-09 17:18:27 +0000 | [diff] [blame] | 370 | if (getCurMethodDecl()->isInstanceMethod()) { |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 371 | QualType superTy = Context.getObjCInterfaceType(ClassDecl); |
| 372 | superTy = Context.getPointerType(superTy); |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 373 | ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(), |
| 374 | superTy); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 375 | // We are really in an instance method, redirect. |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 376 | return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, |
| 377 | selectorLoc, rbrac, Args, NumArgs); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 378 | } |
| 379 | // We are sending a message to 'super' within a class method. Do nothing, |
| 380 | // the receiver will pass through as 'super' (how convenient:-). |
| 381 | } else { |
| 382 | // 'super' has been used outside a method context. If a variable named |
| 383 | // 'super' has been declared, redirect. If not, produce a diagnostic. |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 384 | NamedDecl *SuperDecl = LookupName(S, receiverName, LookupOrdinaryName); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 385 | ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl); |
| 386 | if (VD) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 387 | ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(), |
| 388 | receiverLoc); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 389 | // We are really in an instance method, redirect. |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 390 | return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, |
| 391 | selectorLoc, rbrac, Args, NumArgs); |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 392 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 393 | return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName; |
Steve Naroff | 5cb93b8 | 2008-11-19 15:54:23 +0000 | [diff] [blame] | 394 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 395 | } else |
| 396 | ClassDecl = getObjCInterfaceDecl(receiverName); |
| 397 | |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 398 | // The following code allows for the following GCC-ism: |
Steve Naroff | cb28be6 | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 399 | // |
| 400 | // typedef XCElementDisplayRect XCElementGraphicsRect; |
| 401 | // |
| 402 | // @implementation XCRASlice |
| 403 | // - whatever { // Note that XCElementGraphicsRect is a typedef name. |
| 404 | // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init]; |
| 405 | // } |
| 406 | // |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 407 | // If necessary, the following lookup could move to getObjCInterfaceDecl(). |
| 408 | if (!ClassDecl) { |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 409 | NamedDecl *IDecl = LookupName(TUScope, receiverName, LookupOrdinaryName); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 410 | if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) { |
| 411 | const ObjCInterfaceType *OCIT; |
| 412 | OCIT = OCTD->getUnderlyingType()->getAsObjCInterfaceType(); |
Chris Lattner | 64540d7 | 2009-03-29 05:01:10 +0000 | [diff] [blame^] | 413 | if (!OCIT) { |
| 414 | Diag(receiverLoc, diag::err_invalid_receiver_to_message); |
| 415 | return true; |
| 416 | } |
Fariborz Jahanian | ebff1fe | 2009-01-16 20:35:09 +0000 | [diff] [blame] | 417 | ClassDecl = OCIT->getDecl(); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | assert(ClassDecl && "missing interface declaration"); |
Steve Naroff | cb28be6 | 2008-06-04 23:08:38 +0000 | [diff] [blame] | 421 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 422 | QualType returnType; |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 423 | Method = ClassDecl->lookupClassMethod(Sel); |
| 424 | |
| 425 | // If we have an implementation in scope, check "private" methods. |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 426 | if (!Method) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 427 | Method = LookupPrivateClassMethod(Sel, ClassDecl); |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 428 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 429 | if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) |
| 430 | return true; |
Anders Carlsson | 59843ad | 2009-02-14 19:08:58 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 432 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 433 | lbrac, rbrac, returnType)) |
| 434 | return true; |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 435 | |
| 436 | // If we have the ObjCInterfaceDecl* for the class that is receiving |
| 437 | // the message, use that to construct the ObjCMessageExpr. Otherwise |
| 438 | // pass on the IdentifierInfo* for the class. |
Steve Naroff | fc93d52 | 2008-07-24 19:44:33 +0000 | [diff] [blame] | 439 | // FIXME: need to do a better job handling 'super' usage within a class |
| 440 | // For now, we simply pass the "super" identifier through (which isn't |
| 441 | // consistent with instance methods. |
Steve Naroff | 7c778f1 | 2008-07-25 19:39:00 +0000 | [diff] [blame] | 442 | if (isSuper) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 443 | return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method, |
| 444 | lbrac, rbrac, ArgExprs, NumArgs); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 445 | else |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 446 | return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method, |
| 447 | lbrac, rbrac, ArgExprs, NumArgs); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 451 | // ArgExprs is optional - if it is present, the number of expressions |
| 452 | // is obtained from Sel.getNumArgs(). |
Chris Lattner | 1565e03 | 2008-07-21 06:31:05 +0000 | [diff] [blame] | 453 | Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 454 | SourceLocation lbrac, |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 455 | SourceLocation receiverLoc, |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 456 | SourceLocation rbrac, |
| 457 | ExprTy **Args, unsigned NumArgs) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 458 | assert(receiver && "missing receiver expression"); |
| 459 | |
| 460 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
| 461 | Expr *RExpr = static_cast<Expr *>(receiver); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 462 | QualType returnType; |
Steve Naroff | 94a82c9 | 2008-05-31 02:19:15 +0000 | [diff] [blame] | 463 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 464 | QualType ReceiverCType = |
| 465 | Context.getCanonicalType(RExpr->getType()).getUnqualifiedType(); |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 466 | |
| 467 | // Handle messages to 'super'. |
Steve Naroff | 279d896 | 2009-02-23 15:40:48 +0000 | [diff] [blame] | 468 | if (isa<ObjCSuperExpr>(RExpr)) { |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 469 | ObjCMethodDecl *Method = 0; |
| 470 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
| 471 | // If we have an interface in scope, check 'super' methods. |
| 472 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 473 | if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) { |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 474 | Method = SuperDecl->lookupInstanceMethod(Sel); |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 475 | |
| 476 | if (!Method) |
| 477 | // If we have implementations in scope, check "private" methods. |
| 478 | Method = LookupPrivateInstanceMethod(Sel, SuperDecl); |
| 479 | } |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 480 | } |
Anders Carlsson | ff975cf | 2009-02-14 18:21:46 +0000 | [diff] [blame] | 481 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 482 | if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) |
| 483 | return true; |
Anders Carlsson | 59843ad | 2009-02-14 19:08:58 +0000 | [diff] [blame] | 484 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 485 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 486 | lbrac, rbrac, returnType)) |
| 487 | return true; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 488 | return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, |
| 489 | rbrac, ArgExprs, NumArgs); |
Steve Naroff | 87d3ef0 | 2008-11-17 22:29:32 +0000 | [diff] [blame] | 490 | } |
| 491 | |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 492 | // Handle messages to id. |
Steve Naroff | 6c4088e | 2008-09-29 16:51:41 +0000 | [diff] [blame] | 493 | if (ReceiverCType == Context.getCanonicalType(Context.getObjCIdType()) || |
Chris Lattner | 0c73f37 | 2009-03-09 21:19:16 +0000 | [diff] [blame] | 494 | ReceiverCType->isBlockPointerType()) { |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 495 | ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool( |
| 496 | Sel, SourceRange(lbrac,rbrac)); |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 497 | if (!Method) |
| 498 | Method = FactoryMethodPool[Sel].Method; |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 499 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 500 | lbrac, rbrac, returnType)) |
| 501 | return true; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 502 | return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, |
| 503 | rbrac, ArgExprs, NumArgs); |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | // Handle messages to Class. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 507 | if (ReceiverCType == Context.getCanonicalType(Context.getObjCClassType())) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 508 | ObjCMethodDecl *Method = 0; |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 509 | |
Chris Lattner | 6562fda | 2008-07-21 06:44:27 +0000 | [diff] [blame] | 510 | if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { |
Steve Naroff | d526c2f | 2009-02-23 02:25:40 +0000 | [diff] [blame] | 511 | if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { |
| 512 | // First check the public methods in the class interface. |
| 513 | Method = ClassDecl->lookupClassMethod(Sel); |
| 514 | |
Steve Naroff | f1afaf6 | 2009-02-26 15:55:06 +0000 | [diff] [blame] | 515 | if (!Method) |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 516 | Method = LookupPrivateClassMethod(Sel, ClassDecl); |
Steve Naroff | d526c2f | 2009-02-23 02:25:40 +0000 | [diff] [blame] | 517 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 518 | if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) |
| 519 | return true; |
Steve Naroff | d526c2f | 2009-02-23 02:25:40 +0000 | [diff] [blame] | 520 | } |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 521 | if (!Method) { |
| 522 | // If not messaging 'self', look for any factory method named 'Sel'. |
| 523 | if (!isSelfExpr(RExpr)) { |
| 524 | Method = FactoryMethodPool[Sel].Method; |
Fariborz Jahanian | b1006c7 | 2009-03-04 17:50:39 +0000 | [diff] [blame] | 525 | if (!Method) { |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 526 | Method = LookupInstanceMethodInGlobalPool( |
| 527 | Sel, SourceRange(lbrac,rbrac)); |
Fariborz Jahanian | b1006c7 | 2009-03-04 17:50:39 +0000 | [diff] [blame] | 528 | } |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 531 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 532 | lbrac, rbrac, returnType)) |
| 533 | return true; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 534 | return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, |
| 535 | rbrac, ArgExprs, NumArgs); |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 538 | ObjCMethodDecl *Method = 0; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 539 | ObjCInterfaceDecl* ClassDecl = 0; |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 540 | |
| 541 | // We allow sending a message to a qualified ID ("id<foo>"), which is ok as |
| 542 | // long as one of the protocols implements the selector (if not, warn). |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 543 | if (ObjCQualifiedIdType *QIT = dyn_cast<ObjCQualifiedIdType>(ReceiverCType)) { |
Steve Naroff | f7f52e7 | 2009-02-21 21:17:01 +0000 | [diff] [blame] | 544 | // Search protocols for instance methods. |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 545 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
| 546 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
| 547 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 548 | break; |
| 549 | } |
Steve Naroff | 279d896 | 2009-02-23 15:40:48 +0000 | [diff] [blame] | 550 | } else if (const ObjCInterfaceType *OCIType = |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 551 | ReceiverCType->getAsPointerToObjCInterfaceType()) { |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 552 | // We allow sending a message to a pointer to an interface (an object). |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 553 | |
Steve Naroff | 279d896 | 2009-02-23 15:40:48 +0000 | [diff] [blame] | 554 | ClassDecl = OCIType->getDecl(); |
Steve Naroff | 037cda5 | 2008-09-30 14:38:43 +0000 | [diff] [blame] | 555 | // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be |
| 556 | // faster than the following method (which can do *many* linear searches). |
| 557 | // The idea is to add class info to InstanceMethodPool. |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 558 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 559 | |
| 560 | if (!Method) { |
| 561 | // Search protocol qualifiers. |
Steve Naroff | 279d896 | 2009-02-23 15:40:48 +0000 | [diff] [blame] | 562 | for (ObjCQualifiedInterfaceType::qual_iterator QI = OCIType->qual_begin(), |
| 563 | E = OCIType->qual_end(); QI != E; ++QI) { |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 564 | if ((Method = (*QI)->lookupInstanceMethod(Sel))) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 565 | break; |
| 566 | } |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 567 | } |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 568 | if (!Method) { |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 569 | // If we have implementations in scope, check "private" methods. |
| 570 | Method = LookupPrivateInstanceMethod(Sel, ClassDecl); |
| 571 | |
| 572 | if (!Method && !isSelfExpr(RExpr)) { |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 573 | // If we still haven't found a method, look in the global pool. This |
| 574 | // behavior isn't very desirable, however we need it for GCC |
| 575 | // compatibility. FIXME: should we deviate?? |
Steve Naroff | 5609ec0 | 2009-03-08 18:56:13 +0000 | [diff] [blame] | 576 | if (OCIType->qual_empty()) { |
Steve Naroff | 6b9dfd4 | 2009-03-04 15:11:40 +0000 | [diff] [blame] | 577 | Method = LookupInstanceMethodInGlobalPool( |
| 578 | Sel, SourceRange(lbrac,rbrac)); |
| 579 | if (Method && !OCIType->getDecl()->isForwardDecl()) |
| 580 | Diag(lbrac, diag::warn_maynot_respond) |
| 581 | << OCIType->getDecl()->getIdentifier()->getName() << Sel; |
| 582 | } |
Fariborz Jahanian | 268bc8c | 2009-03-03 22:19:15 +0000 | [diff] [blame] | 583 | } |
Steve Naroff | 0de21fd | 2009-02-22 19:35:57 +0000 | [diff] [blame] | 584 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 585 | if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) |
| 586 | return true; |
Chris Lattner | 0c73f37 | 2009-03-09 21:19:16 +0000 | [diff] [blame] | 587 | } else if (!Context.getObjCIdType().isNull() && |
| 588 | (ReceiverCType->isPointerType() || |
| 589 | (ReceiverCType->isIntegerType() && |
| 590 | ReceiverCType->isScalarType()))) { |
| 591 | // Implicitly convert integers and pointers to 'id' but emit a warning. |
Steve Naroff | 8e2945a | 2009-03-01 17:14:31 +0000 | [diff] [blame] | 592 | Diag(lbrac, diag::warn_bad_receiver_type) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 593 | << RExpr->getType() << RExpr->getSourceRange(); |
Chris Lattner | 0c73f37 | 2009-03-09 21:19:16 +0000 | [diff] [blame] | 594 | ImpCastExprToType(RExpr, Context.getObjCIdType()); |
| 595 | } else { |
| 596 | // Reject other random receiver types (e.g. structs). |
| 597 | Diag(lbrac, diag::err_bad_receiver_type) |
| 598 | << RExpr->getType() << RExpr->getSourceRange(); |
Chris Lattner | 2b1cc8b | 2008-07-21 06:12:56 +0000 | [diff] [blame] | 599 | return true; |
Chris Lattner | fe1a553 | 2008-07-21 05:57:44 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Chris Lattner | 077bf5e | 2008-11-24 03:33:13 +0000 | [diff] [blame] | 602 | if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, |
Daniel Dunbar | 637cebb | 2008-09-11 00:01:56 +0000 | [diff] [blame] | 603 | lbrac, rbrac, returnType)) |
| 604 | return true; |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 605 | return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, |
| 606 | rbrac, ArgExprs, NumArgs); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 607 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 608 | |
| 609 | //===----------------------------------------------------------------------===// |
| 610 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 611 | //===----------------------------------------------------------------------===// |
| 612 | |
| 613 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 614 | /// inheritance hierarchy of 'rProto'. |
| 615 | static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 616 | ObjCProtocolDecl *rProto) { |
| 617 | if (lProto == rProto) |
| 618 | return true; |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 619 | for (ObjCProtocolDecl::protocol_iterator PI = rProto->protocol_begin(), |
| 620 | E = rProto->protocol_end(); PI != E; ++PI) |
| 621 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 622 | return true; |
| 623 | return false; |
| 624 | } |
| 625 | |
| 626 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 627 | /// has been implemented in IDecl class, its super class or categories (if |
| 628 | /// lookupCategory is true). |
| 629 | static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 630 | ObjCInterfaceDecl *IDecl, |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 631 | bool lookupCategory, |
| 632 | bool RHSIsQualifiedID = false) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 633 | |
| 634 | // 1st, look up the class. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 635 | const ObjCList<ObjCProtocolDecl> &Protocols = |
| 636 | IDecl->getReferencedProtocols(); |
| 637 | |
| 638 | for (ObjCList<ObjCProtocolDecl>::iterator PI = Protocols.begin(), |
| 639 | E = Protocols.end(); PI != E; ++PI) { |
| 640 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 641 | return true; |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 642 | // This is dubious and is added to be compatible with gcc. |
| 643 | // In gcc, it is also allowed assigning a protocol-qualified 'id' |
| 644 | // type to a LHS object when protocol in qualified LHS is in list |
| 645 | // of protocols in the rhs 'id' object. This IMO, should be a bug. |
Ted Kremenek | fd5b2ce | 2008-06-04 20:48:08 +0000 | [diff] [blame] | 646 | // FIXME: Treat this as an extension, and flag this as an error when |
| 647 | // GCC extensions are not enabled. |
Chris Lattner | 3db6cae | 2008-07-21 18:19:38 +0000 | [diff] [blame] | 648 | if (RHSIsQualifiedID && ProtocolCompatibleWithProtocol(*PI, lProto)) |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 649 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // 2nd, look up the category. |
| 653 | if (lookupCategory) |
| 654 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 655 | CDecl = CDecl->getNextClassCategory()) { |
Chris Lattner | 780f329 | 2008-07-21 21:32:27 +0000 | [diff] [blame] | 656 | for (ObjCCategoryDecl::protocol_iterator PI = CDecl->protocol_begin(), |
| 657 | E = CDecl->protocol_end(); PI != E; ++PI) |
| 658 | if (ProtocolCompatibleWithProtocol(lProto, *PI)) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 659 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | // 3rd, look up the super class(s) |
| 663 | if (IDecl->getSuperClass()) |
| 664 | return |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 665 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory, |
| 666 | RHSIsQualifiedID); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 667 | |
| 668 | return false; |
| 669 | } |
| 670 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 671 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 672 | /// ObjCQualifiedIDType. |
Steve Naroff | 15edf0d | 2009-03-03 15:43:24 +0000 | [diff] [blame] | 673 | /// FIXME: Move to ASTContext::typesAreCompatible() and friends. |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 674 | bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 675 | bool compare) { |
| 676 | // Allow id<P..> and an 'id' or void* type in all cases. |
| 677 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 678 | QualType PointeeTy = PT->getPointeeType(); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 679 | if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType()) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 680 | return true; |
| 681 | } else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 682 | QualType PointeeTy = PT->getPointeeType(); |
Steve Naroff | 389bf46 | 2009-02-12 17:52:19 +0000 | [diff] [blame] | 683 | if (Context.isObjCIdStructType(PointeeTy) || PointeeTy->isVoidType()) |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 684 | return true; |
| 685 | } |
| 686 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 687 | if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
| 688 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 689 | const ObjCQualifiedInterfaceType *rhsQI = 0; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 690 | QualType rtype; |
| 691 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 692 | if (!rhsQID) { |
| 693 | // Not comparing two ObjCQualifiedIdType's? |
| 694 | if (!rhs->isPointerType()) return false; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 695 | |
| 696 | rtype = rhs->getAsPointerType()->getPointeeType(); |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 697 | rhsQI = rtype->getAsObjCQualifiedInterfaceType(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 698 | if (rhsQI == 0) { |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 699 | // If the RHS is a unqualified interface pointer "NSString*", |
| 700 | // make sure we check the class hierarchy. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 701 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 702 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 703 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 704 | // when comparing an id<P> on lhs with a static type on rhs, |
| 705 | // see if static class implements all of id's protocols, directly or |
| 706 | // through its super class and categories. |
| 707 | if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) |
| 708 | return false; |
| 709 | } |
| 710 | return true; |
| 711 | } |
| 712 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 713 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 714 | |
| 715 | ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE; |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 716 | if (rhsQI) { // We have a qualified interface (e.g. "NSObject<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 717 | RHSProtoI = rhsQI->qual_begin(); |
| 718 | RHSProtoE = rhsQI->qual_end(); |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 719 | } else if (rhsQID) { // We have a qualified id (e.g. "id<Proto> *"). |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 720 | RHSProtoI = rhsQID->qual_begin(); |
| 721 | RHSProtoE = rhsQID->qual_end(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 722 | } else { |
| 723 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
| 727 | ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
| 728 | bool match = false; |
| 729 | |
| 730 | // when comparing an id<P> on lhs with a static type on rhs, |
| 731 | // see if static class implements all of id's protocols, directly or |
| 732 | // through its super class and categories. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 733 | for (; RHSProtoI != RHSProtoE; ++RHSProtoI) { |
| 734 | ObjCProtocolDecl *rhsProto = *RHSProtoI; |
| 735 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 736 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 737 | match = true; |
| 738 | break; |
| 739 | } |
| 740 | } |
Steve Naroff | 289d9f2 | 2008-06-01 02:43:50 +0000 | [diff] [blame] | 741 | if (rhsQI) { |
| 742 | // If the RHS is a qualified interface pointer "NSString<P>*", |
| 743 | // make sure we check the class hierarchy. |
| 744 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 745 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 746 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 747 | // when comparing an id<P> on lhs with a static type on rhs, |
| 748 | // see if static class implements all of id's protocols, directly or |
| 749 | // through its super class and categories. |
| 750 | if (ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) { |
| 751 | match = true; |
| 752 | break; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 757 | if (!match) |
| 758 | return false; |
| 759 | } |
| 760 | |
| 761 | return true; |
| 762 | } |
| 763 | |
| 764 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 765 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 766 | |
| 767 | if (!lhs->isPointerType()) |
| 768 | return false; |
| 769 | |
| 770 | QualType ltype = lhs->getAsPointerType()->getPointeeType(); |
| 771 | if (const ObjCQualifiedInterfaceType *lhsQI = |
| 772 | ltype->getAsObjCQualifiedInterfaceType()) { |
| 773 | ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin(); |
| 774 | ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end(); |
| 775 | for (; LHSProtoI != LHSProtoE; ++LHSProtoI) { |
| 776 | bool match = false; |
| 777 | ObjCProtocolDecl *lhsProto = *LHSProtoI; |
| 778 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 779 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 780 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Eli Friedman | 82b4e76 | 2008-12-16 20:15:50 +0000 | [diff] [blame] | 781 | (compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto))) { |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 782 | match = true; |
| 783 | break; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 786 | if (!match) |
| 787 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 788 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 789 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 790 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 791 | |
| 792 | if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) { |
| 793 | // for static type vs. qualified 'id' type, check that class implements |
| 794 | // all of 'id's protocols. |
| 795 | ObjCInterfaceDecl *lhsID = IT->getDecl(); |
| 796 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 797 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
Fariborz Jahanian | 2663170 | 2008-06-04 19:00:03 +0000 | [diff] [blame] | 798 | if (!ClassImplementsProtocol(rhsProto, lhsID, compare, true)) |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 799 | return false; |
| 800 | } |
| 801 | return true; |
| 802 | } |
| 803 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 804 | } |
| 805 | |