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 | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 18 | using namespace clang; |
| 19 | |
| 20 | Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, |
| 21 | ExprTy **Strings, |
| 22 | unsigned NumStrings) { |
| 23 | SourceLocation AtLoc = AtLocs[0]; |
| 24 | StringLiteral* S = static_cast<StringLiteral *>(Strings[0]); |
| 25 | if (NumStrings > 1) { |
| 26 | // Concatenate objc strings. |
| 27 | StringLiteral* ES = static_cast<StringLiteral *>(Strings[NumStrings-1]); |
| 28 | SourceLocation EndLoc = ES->getSourceRange().getEnd(); |
| 29 | unsigned Length = 0; |
| 30 | for (unsigned i = 0; i < NumStrings; i++) |
| 31 | Length += static_cast<StringLiteral *>(Strings[i])->getByteLength(); |
| 32 | char *strBuf = new char [Length]; |
| 33 | char *p = strBuf; |
| 34 | bool isWide = false; |
| 35 | for (unsigned i = 0; i < NumStrings; i++) { |
| 36 | S = static_cast<StringLiteral *>(Strings[i]); |
| 37 | if (S->isWide()) |
| 38 | isWide = true; |
| 39 | memcpy(p, S->getStrData(), S->getByteLength()); |
| 40 | p += S->getByteLength(); |
| 41 | delete S; |
| 42 | } |
| 43 | S = new StringLiteral(strBuf, Length, |
| 44 | isWide, Context.getPointerType(Context.CharTy), |
| 45 | AtLoc, EndLoc); |
| 46 | } |
| 47 | |
| 48 | if (CheckBuiltinCFStringArgument(S)) |
| 49 | return true; |
| 50 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 51 | if (Context.getObjCConstantStringInterface().isNull()) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 52 | // Initialize the constant string interface lazily. This assumes |
| 53 | // the NSConstantString interface is seen in this translation unit. |
| 54 | IdentifierInfo *NSIdent = &Context.Idents.get("NSConstantString"); |
Steve Naroff | b327ce0 | 2008-04-02 14:35:35 +0000 | [diff] [blame] | 55 | Decl *IFace = LookupDecl(NSIdent, Decl::IDNS_Ordinary, TUScope); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 56 | ObjCInterfaceDecl *strIFace = dyn_cast_or_null<ObjCInterfaceDecl>(IFace); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 57 | if (!strIFace) |
| 58 | return Diag(S->getLocStart(), diag::err_undef_interface, |
| 59 | NSIdent->getName()); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 60 | Context.setObjCConstantStringInterface(strIFace); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 61 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 62 | QualType t = Context.getObjCConstantStringInterface(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 63 | t = Context.getPointerType(t); |
| 64 | return new ObjCStringLiteral(S, t, AtLoc); |
| 65 | } |
| 66 | |
| 67 | Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, |
| 68 | SourceLocation EncodeLoc, |
| 69 | SourceLocation LParenLoc, |
| 70 | TypeTy *Ty, |
| 71 | SourceLocation RParenLoc) { |
| 72 | QualType EncodedType = QualType::getFromOpaquePtr(Ty); |
| 73 | |
| 74 | QualType t = Context.getPointerType(Context.CharTy); |
| 75 | return new ObjCEncodeExpr(t, EncodedType, AtLoc, RParenLoc); |
| 76 | } |
| 77 | |
| 78 | Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, |
| 79 | SourceLocation AtLoc, |
| 80 | SourceLocation SelLoc, |
| 81 | SourceLocation LParenLoc, |
| 82 | SourceLocation RParenLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 83 | QualType t = Context.getObjCSelType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 84 | return new ObjCSelectorExpr(t, Sel, AtLoc, RParenLoc); |
| 85 | } |
| 86 | |
| 87 | Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, |
| 88 | SourceLocation AtLoc, |
| 89 | SourceLocation ProtoLoc, |
| 90 | SourceLocation LParenLoc, |
| 91 | SourceLocation RParenLoc) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 92 | ObjCProtocolDecl* PDecl = ObjCProtocols[ProtocolId]; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 93 | if (!PDecl) { |
| 94 | Diag(ProtoLoc, diag::err_undeclared_protocol, ProtocolId->getName()); |
| 95 | return true; |
| 96 | } |
| 97 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 98 | QualType t = Context.getObjCProtoType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 99 | if (t.isNull()) |
| 100 | return true; |
| 101 | t = Context.getPointerType(t); |
| 102 | return new ObjCProtocolExpr(t, PDecl, AtLoc, RParenLoc); |
| 103 | } |
| 104 | |
| 105 | bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 106 | ObjCMethodDecl *Method) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 107 | bool anyIncompatibleArgs = false; |
| 108 | |
| 109 | for (unsigned i = 0; i < NumArgs; i++) { |
| 110 | Expr *argExpr = Args[i]; |
| 111 | assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); |
| 112 | |
| 113 | QualType lhsType = Method->getParamDecl(i)->getType(); |
| 114 | QualType rhsType = argExpr->getType(); |
| 115 | |
| 116 | // 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] | 117 | if (lhsType->isArrayType()) |
| 118 | lhsType = Context.getArrayDecayedType(lhsType); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 119 | else if (lhsType->isFunctionType()) |
| 120 | lhsType = Context.getPointerType(lhsType); |
| 121 | |
Chris Lattner | 987798a | 2008-04-02 17:17:33 +0000 | [diff] [blame] | 122 | AssignConvertType Result = |
| 123 | CheckSingleAssignmentConstraints(lhsType, argExpr); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 124 | if (Args[i] != argExpr) // The expression was converted. |
| 125 | Args[i] = argExpr; // Make sure we store the converted expression. |
| 126 | |
| 127 | anyIncompatibleArgs |= |
| 128 | DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType, |
| 129 | argExpr, "sending"); |
| 130 | } |
| 131 | return anyIncompatibleArgs; |
| 132 | } |
| 133 | |
| 134 | // ActOnClassMessage - used for both unary and keyword messages. |
| 135 | // ArgExprs is optional - if it is present, the number of expressions |
| 136 | // is obtained from Sel.getNumArgs(). |
| 137 | Sema::ExprResult Sema::ActOnClassMessage( |
| 138 | Scope *S, |
| 139 | IdentifierInfo *receiverName, Selector Sel, |
| 140 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
| 141 | { |
| 142 | assert(receiverName && "missing receiver class name"); |
| 143 | |
| 144 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 145 | ObjCInterfaceDecl* ClassDecl = 0; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 146 | if (!strcmp(receiverName->getName(), "super") && CurMethodDecl) { |
| 147 | ClassDecl = CurMethodDecl->getClassInterface()->getSuperClass(); |
Steve Naroff | 9511096 | 2008-03-28 21:37:05 +0000 | [diff] [blame] | 148 | if (!ClassDecl) |
| 149 | return Diag(lbrac, diag::error_no_super_class, |
| 150 | CurMethodDecl->getClassInterface()->getName()); |
| 151 | if (CurMethodDecl->isInstance()) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 152 | // Synthesize a cast to the super class. This hack allows us to loosely |
| 153 | // represent super without creating a special expression node. |
| 154 | IdentifierInfo &II = Context.Idents.get("self"); |
| 155 | ExprResult ReceiverExpr = ActOnIdentifierExpr(S, lbrac, II, false); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 156 | QualType superTy = Context.getObjCInterfaceType(ClassDecl); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 157 | superTy = Context.getPointerType(superTy); |
| 158 | ReceiverExpr = ActOnCastExpr(SourceLocation(), superTy.getAsOpaquePtr(), |
| 159 | SourceLocation(), ReceiverExpr.Val); |
| 160 | // We are really in an instance method, redirect. |
| 161 | return ActOnInstanceMessage(ReceiverExpr.Val, Sel, lbrac, rbrac, |
| 162 | Args, NumArgs); |
| 163 | } |
| 164 | // We are sending a message to 'super' within a class method. Do nothing, |
| 165 | // the receiver will pass through as 'super' (how convenient:-). |
| 166 | } else |
| 167 | ClassDecl = getObjCInterfaceDecl(receiverName); |
| 168 | |
| 169 | // FIXME: can ClassDecl ever be null? |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 170 | ObjCMethodDecl *Method = ClassDecl->lookupClassMethod(Sel); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 171 | QualType returnType; |
| 172 | |
| 173 | // Before we give up, check if the selector is an instance method. |
| 174 | if (!Method) |
| 175 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 176 | if (!Method) { |
| 177 | Diag(lbrac, diag::warn_method_not_found, std::string("+"), Sel.getName(), |
| 178 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 179 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 180 | } else { |
| 181 | returnType = Method->getResultType(); |
| 182 | if (Sel.getNumArgs()) { |
| 183 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 184 | return true; |
| 185 | } |
| 186 | } |
| 187 | return new ObjCMessageExpr(receiverName, Sel, returnType, Method, |
| 188 | lbrac, rbrac, ArgExprs, NumArgs); |
| 189 | } |
| 190 | |
| 191 | // ActOnInstanceMessage - used for both unary and keyword messages. |
| 192 | // ArgExprs is optional - if it is present, the number of expressions |
| 193 | // is obtained from Sel.getNumArgs(). |
| 194 | Sema::ExprResult Sema::ActOnInstanceMessage( |
| 195 | ExprTy *receiver, Selector Sel, |
| 196 | SourceLocation lbrac, SourceLocation rbrac, ExprTy **Args, unsigned NumArgs) |
| 197 | { |
| 198 | assert(receiver && "missing receiver expression"); |
| 199 | |
| 200 | Expr **ArgExprs = reinterpret_cast<Expr **>(Args); |
| 201 | Expr *RExpr = static_cast<Expr *>(receiver); |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 202 | QualType receiverType = RExpr->getType().getCanonicalType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 203 | QualType returnType; |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 204 | ObjCMethodDecl *Method = 0; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 205 | |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 206 | // FIXME: This code is not stripping off type qualifiers! Should it? |
| 207 | if (receiverType == Context.getObjCIdType().getCanonicalType() || |
| 208 | receiverType == Context.getObjCClassType().getCanonicalType()) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 209 | Method = InstanceMethodPool[Sel].Method; |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 210 | if (!Method) |
| 211 | Method = FactoryMethodPool[Sel].Method; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 212 | if (!Method) { |
| 213 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 214 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 215 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 216 | } else { |
| 217 | returnType = Method->getResultType(); |
| 218 | if (Sel.getNumArgs()) |
| 219 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 220 | return true; |
| 221 | } |
| 222 | } else { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 223 | bool receiverIsQualId = isa<ObjCQualifiedIdType>(receiverType); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 224 | // FIXME (snaroff): checking in this code from Patrick. Needs to be |
| 225 | // revisited. how do we get the ClassDecl from the receiver expression? |
| 226 | if (!receiverIsQualId) |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 227 | while (const PointerType *PTy = receiverType->getAsPointerType()) |
| 228 | receiverType = PTy->getPointeeType(); |
| 229 | |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 230 | ObjCInterfaceDecl* ClassDecl = 0; |
| 231 | if (ObjCQualifiedInterfaceType *QIT = |
| 232 | dyn_cast<ObjCQualifiedInterfaceType>(receiverType)) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 233 | ClassDecl = QIT->getDecl(); |
| 234 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 235 | if (!Method) { |
| 236 | // search protocols |
| 237 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 238 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 239 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | if (!Method) |
| 244 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 245 | std::string("-"), Sel.getName(), |
| 246 | SourceRange(lbrac, rbrac)); |
| 247 | } |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 248 | else if (ObjCQualifiedIdType *QIT = |
| 249 | dyn_cast<ObjCQualifiedIdType>(receiverType)) { |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 250 | // search protocols |
| 251 | for (unsigned i = 0; i < QIT->getNumProtocols(); i++) { |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 252 | ObjCProtocolDecl *PDecl = QIT->getProtocols(i); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 253 | if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) |
| 254 | break; |
| 255 | } |
| 256 | if (!Method) |
| 257 | Diag(lbrac, diag::warn_method_not_found_in_protocol, |
| 258 | std::string("-"), Sel.getName(), |
| 259 | SourceRange(lbrac, rbrac)); |
| 260 | } |
| 261 | else { |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 262 | ObjCInterfaceType *OCIReceiver =dyn_cast<ObjCInterfaceType>(receiverType); |
| 263 | if (OCIReceiver == 0) { |
Chris Lattner | 6e10a08 | 2008-02-01 06:57:39 +0000 | [diff] [blame] | 264 | Diag(lbrac, diag::error_bad_receiver_type, |
| 265 | RExpr->getType().getAsString()); |
Fariborz Jahanian | 1dad5b2 | 2008-01-25 17:43:39 +0000 | [diff] [blame] | 266 | return true; |
| 267 | } |
Chris Lattner | fb8cc1d | 2008-02-01 06:43:02 +0000 | [diff] [blame] | 268 | ClassDecl = OCIReceiver->getDecl(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 269 | // FIXME: consider using InstanceMethodPool, since it will be faster |
| 270 | // than the following method (which can do *many* linear searches). The |
| 271 | // idea is to add class info to InstanceMethodPool... |
| 272 | Method = ClassDecl->lookupInstanceMethod(Sel); |
| 273 | } |
| 274 | if (!Method) { |
| 275 | // If we have an implementation in scope, check "private" methods. |
| 276 | if (ClassDecl) |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 277 | if (ObjCImplementationDecl *ImpDecl = |
| 278 | ObjCImplementations[ClassDecl->getIdentifier()]) |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 279 | Method = ImpDecl->getInstanceMethod(Sel); |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 280 | // If we still haven't found a method, look in the global pool. This |
| 281 | // behavior isn't very desirable, however we need it for GCC |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 282 | // compatibility. |
Chris Lattner | c81c814 | 2008-02-25 21:04:36 +0000 | [diff] [blame] | 283 | if (!Method) |
| 284 | Method = InstanceMethodPool[Sel].Method; |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 285 | } |
| 286 | if (!Method) { |
| 287 | Diag(lbrac, diag::warn_method_not_found, std::string("-"), Sel.getName(), |
| 288 | SourceRange(lbrac, rbrac)); |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 289 | returnType = Context.getObjCIdType(); |
Chris Lattner | 85a932e | 2008-01-04 22:32:30 +0000 | [diff] [blame] | 290 | } else { |
| 291 | returnType = Method->getResultType(); |
| 292 | if (Sel.getNumArgs()) |
| 293 | if (CheckMessageArgumentTypes(ArgExprs, Sel.getNumArgs(), Method)) |
| 294 | return true; |
| 295 | } |
| 296 | } |
| 297 | return new ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, rbrac, |
| 298 | ArgExprs, NumArgs); |
| 299 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 300 | |
| 301 | //===----------------------------------------------------------------------===// |
| 302 | // ObjCQualifiedIdTypesAreCompatible - Compatibility testing for qualified id's. |
| 303 | //===----------------------------------------------------------------------===// |
| 304 | |
| 305 | /// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the |
| 306 | /// inheritance hierarchy of 'rProto'. |
| 307 | static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto, |
| 308 | ObjCProtocolDecl *rProto) { |
| 309 | if (lProto == rProto) |
| 310 | return true; |
| 311 | ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols(); |
| 312 | for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++) |
| 313 | if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i])) |
| 314 | return true; |
| 315 | return false; |
| 316 | } |
| 317 | |
| 318 | /// ClassImplementsProtocol - Checks that 'lProto' protocol |
| 319 | /// has been implemented in IDecl class, its super class or categories (if |
| 320 | /// lookupCategory is true). |
| 321 | static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto, |
| 322 | ObjCInterfaceDecl *IDecl, |
| 323 | bool lookupCategory) { |
| 324 | |
| 325 | // 1st, look up the class. |
| 326 | ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols(); |
| 327 | for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) { |
| 328 | if (ProtocolCompatibleWithProtocol(lProto, protoList[i])) |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | // 2nd, look up the category. |
| 333 | if (lookupCategory) |
| 334 | for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl; |
| 335 | CDecl = CDecl->getNextClassCategory()) { |
| 336 | protoList = CDecl->getReferencedProtocols(); |
| 337 | for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) { |
| 338 | if (ProtocolCompatibleWithProtocol(lProto, protoList[i])) |
| 339 | return true; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | // 3rd, look up the super class(s) |
| 344 | if (IDecl->getSuperClass()) |
| 345 | return |
| 346 | ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory); |
| 347 | |
| 348 | return false; |
| 349 | } |
| 350 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 351 | /// ObjCQualifiedIdTypesAreCompatible - We know that one of lhs/rhs is an |
| 352 | /// ObjCQualifiedIDType. |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 353 | bool Sema::ObjCQualifiedIdTypesAreCompatible(QualType lhs, QualType rhs, |
| 354 | bool compare) { |
| 355 | // Allow id<P..> and an 'id' or void* type in all cases. |
| 356 | if (const PointerType *PT = lhs->getAsPointerType()) { |
| 357 | QualType PointeeTy = PT->getPointeeType(); |
| 358 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 359 | return true; |
| 360 | } else if (const PointerType *PT = rhs->getAsPointerType()) { |
| 361 | QualType PointeeTy = PT->getPointeeType(); |
| 362 | if (Context.isObjCIdType(PointeeTy) || PointeeTy->isVoidType()) |
| 363 | return true; |
| 364 | } |
| 365 | |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 366 | if (const ObjCQualifiedIdType *lhsQID = lhs->getAsObjCQualifiedIdType()) { |
| 367 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 368 | const ObjCQualifiedInterfaceType *rhsQI = 0; |
| 369 | if (!rhsQID) { |
| 370 | // Not comparing two ObjCQualifiedIdType's? |
| 371 | if (!rhs->isPointerType()) return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 372 | QualType rtype = rhs->getAsPointerType()->getPointeeType(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 373 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 374 | rhsQI = rtype->getAsObjCQualifiedInterfaceType(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 375 | if (rhsQI == 0) { |
| 376 | // If the RHS is an interface pointer ('NSString*'), handle it. |
| 377 | if (const ObjCInterfaceType *IT = rtype->getAsObjCInterfaceType()) { |
| 378 | ObjCInterfaceDecl *rhsID = IT->getDecl(); |
| 379 | for (unsigned i = 0; i != lhsQID->getNumProtocols(); ++i) { |
| 380 | // when comparing an id<P> on lhs with a static type on rhs, |
| 381 | // see if static class implements all of id's protocols, directly or |
| 382 | // through its super class and categories. |
| 383 | if (!ClassImplementsProtocol(lhsQID->getProtocols(i), rhsID, true)) |
| 384 | return false; |
| 385 | } |
| 386 | return true; |
| 387 | } |
| 388 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 389 | } |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 390 | |
| 391 | ObjCQualifiedIdType::qual_iterator RHSProtoI, RHSProtoE; |
| 392 | if (rhsQI) { |
| 393 | RHSProtoI = rhsQI->qual_begin(); |
| 394 | RHSProtoE = rhsQI->qual_end(); |
| 395 | } else if (rhsQID) { |
| 396 | RHSProtoI = rhsQID->qual_begin(); |
| 397 | RHSProtoE = rhsQID->qual_end(); |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 398 | } else { |
| 399 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) { |
| 403 | ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i); |
| 404 | bool match = false; |
| 405 | |
| 406 | // when comparing an id<P> on lhs with a static type on rhs, |
| 407 | // see if static class implements all of id's protocols, directly or |
| 408 | // through its super class and categories. |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 409 | for (; RHSProtoI != RHSProtoE; ++RHSProtoI) { |
| 410 | ObjCProtocolDecl *rhsProto = *RHSProtoI; |
| 411 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
| 412 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 413 | match = true; |
| 414 | break; |
| 415 | } |
| 416 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 417 | if (!match) |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | return true; |
| 422 | } |
| 423 | |
| 424 | const ObjCQualifiedIdType *rhsQID = rhs->getAsObjCQualifiedIdType(); |
| 425 | assert(rhsQID && "One of the LHS/RHS should be id<x>"); |
| 426 | |
| 427 | if (!lhs->isPointerType()) |
| 428 | return false; |
| 429 | |
| 430 | QualType ltype = lhs->getAsPointerType()->getPointeeType(); |
| 431 | if (const ObjCQualifiedInterfaceType *lhsQI = |
| 432 | ltype->getAsObjCQualifiedInterfaceType()) { |
| 433 | ObjCQualifiedIdType::qual_iterator LHSProtoI = lhsQI->qual_begin(); |
| 434 | ObjCQualifiedIdType::qual_iterator LHSProtoE = lhsQI->qual_end(); |
| 435 | for (; LHSProtoI != LHSProtoE; ++LHSProtoI) { |
| 436 | bool match = false; |
| 437 | ObjCProtocolDecl *lhsProto = *LHSProtoI; |
| 438 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 439 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 440 | if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) || |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 441 | compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) { |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 442 | match = true; |
| 443 | break; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 446 | if (!match) |
| 447 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 448 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 449 | return true; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 450 | } |
Chris Lattner | b1698cf | 2008-04-20 02:09:31 +0000 | [diff] [blame] | 451 | |
| 452 | if (const ObjCInterfaceType *IT = ltype->getAsObjCInterfaceType()) { |
| 453 | // for static type vs. qualified 'id' type, check that class implements |
| 454 | // all of 'id's protocols. |
| 455 | ObjCInterfaceDecl *lhsID = IT->getDecl(); |
| 456 | for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) { |
| 457 | ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j); |
| 458 | if (!ClassImplementsProtocol(rhsProto, lhsID, compare)) |
| 459 | return false; |
| 460 | } |
| 461 | return true; |
| 462 | } |
| 463 | return false; |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 464 | } |
| 465 | |