SemaExprObjC.cpp revision f36e02d4aff98bf2e52e342e0038d4172fbb5e64
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" 17#include "clang/AST/ExprObjC.h" 18#include "llvm/ADT/SmallString.h" 19#include "clang/Lex/Preprocessor.h" 20 21using namespace clang; 22 23Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, 24 ExprTy **strings, 25 unsigned NumStrings) { 26 StringLiteral **Strings = reinterpret_cast<StringLiteral**>(strings); 27 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. 32 StringLiteral *S = Strings[0]; 33 34 // If we have a multi-part string, merge it all together. 35 if (NumStrings != 1) { 36 // Concatenate objc strings. 37 llvm::SmallString<128> StrBuf; 38 llvm::SmallVector<SourceLocation, 8> StrLocs; 39 40 for (unsigned i = 0; i != NumStrings; ++i) { 41 S = Strings[i]; 42 43 // ObjC strings can't be wide. 44 if (S->isWide()) { 45 Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) 46 << S->getSourceRange(); 47 return true; 48 } 49 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. 57 S->Destroy(Context); 58 } 59 60 // Create the aggregate string with the appropriate content and location 61 // information. 62 S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false, 63 Context.getPointerType(Context.CharTy), 64 &StrLocs[0], StrLocs.size()); 65 } 66 67 // Verify that this composite string is acceptable for ObjC strings. 68 if (CheckObjCString(S)) 69 return true; 70 71 // Initialize the constant string interface lazily. This assumes 72 // the NSString interface is seen in this translation unit. Note: We 73 // don't use NSConstantString, since the runtime team considers this 74 // interface private (even though it appears in the header files). 75 QualType Ty = Context.getObjCConstantStringInterface(); 76 if (!Ty.isNull()) { 77 Ty = Context.getObjCObjectPointerType(Ty); 78 } else { 79 IdentifierInfo *NSIdent = &Context.Idents.get("NSString"); 80 NamedDecl *IF = LookupSingleName(TUScope, NSIdent, LookupOrdinaryName); 81 if (ObjCInterfaceDecl *StrIF = dyn_cast_or_null<ObjCInterfaceDecl>(IF)) { 82 Context.setObjCConstantStringInterface(StrIF); 83 Ty = Context.getObjCConstantStringInterface(); 84 Ty = Context.getObjCObjectPointerType(Ty); 85 } else { 86 // If there is no NSString interface defined then treat constant 87 // strings as untyped objects and let the runtime figure it out later. 88 Ty = Context.getObjCIdType(); 89 } 90 } 91 92 return new (Context) ObjCStringLiteral(S, Ty, AtLocs[0]); 93} 94 95Expr *Sema::BuildObjCEncodeExpression(SourceLocation AtLoc, 96 QualType EncodedType, 97 SourceLocation RParenLoc) { 98 QualType StrTy; 99 if (EncodedType->isDependentType()) 100 StrTy = Context.DependentTy; 101 else { 102 std::string Str; 103 Context.getObjCEncodingForType(EncodedType, Str); 104 105 // The type of @encode is the same as the type of the corresponding string, 106 // which is an array type. 107 StrTy = Context.CharTy; 108 // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). 109 if (getLangOptions().CPlusPlus) 110 StrTy.addConst(); 111 StrTy = Context.getConstantArrayType(StrTy, llvm::APInt(32, Str.size()+1), 112 ArrayType::Normal, 0); 113 } 114 115 return new (Context) ObjCEncodeExpr(StrTy, EncodedType, AtLoc, RParenLoc); 116} 117 118Sema::ExprResult Sema::ParseObjCEncodeExpression(SourceLocation AtLoc, 119 SourceLocation EncodeLoc, 120 SourceLocation LParenLoc, 121 TypeTy *ty, 122 SourceLocation RParenLoc) { 123 // FIXME: Preserve type source info ? 124 QualType EncodedType = GetTypeFromParser(ty); 125 126 return BuildObjCEncodeExpression(AtLoc, EncodedType, RParenLoc); 127} 128 129Sema::ExprResult Sema::ParseObjCSelectorExpression(Selector Sel, 130 SourceLocation AtLoc, 131 SourceLocation SelLoc, 132 SourceLocation LParenLoc, 133 SourceLocation RParenLoc) { 134 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool(Sel, 135 SourceRange(LParenLoc, RParenLoc), false); 136 if (!Method) 137 Method = LookupFactoryMethodInGlobalPool(Sel, 138 SourceRange(LParenLoc, RParenLoc)); 139 if (!Method) 140 Diag(SelLoc, diag::warn_undeclared_selector) << Sel; 141 142 QualType Ty = Context.getObjCSelType(); 143 return new (Context) ObjCSelectorExpr(Ty, Sel, AtLoc, RParenLoc); 144} 145 146Sema::ExprResult Sema::ParseObjCProtocolExpression(IdentifierInfo *ProtocolId, 147 SourceLocation AtLoc, 148 SourceLocation ProtoLoc, 149 SourceLocation LParenLoc, 150 SourceLocation RParenLoc) { 151 ObjCProtocolDecl* PDecl = LookupProtocol(ProtocolId); 152 if (!PDecl) { 153 Diag(ProtoLoc, diag::err_undeclared_protocol) << ProtocolId; 154 return true; 155 } 156 157 QualType Ty = Context.getObjCProtoType(); 158 if (Ty.isNull()) 159 return true; 160 Ty = Context.getObjCObjectPointerType(Ty); 161 return new (Context) ObjCProtocolExpr(Ty, PDecl, AtLoc, RParenLoc); 162} 163 164bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, 165 Selector Sel, ObjCMethodDecl *Method, 166 bool isClassMessage, 167 SourceLocation lbrac, SourceLocation rbrac, 168 QualType &ReturnType) { 169 if (!Method) { 170 // Apply default argument promotion as for (C99 6.5.2.2p6). 171 for (unsigned i = 0; i != NumArgs; i++) 172 DefaultArgumentPromotion(Args[i]); 173 174 unsigned DiagID = isClassMessage ? diag::warn_class_method_not_found : 175 diag::warn_inst_method_not_found; 176 Diag(lbrac, DiagID) 177 << Sel << isClassMessage << SourceRange(lbrac, rbrac); 178 ReturnType = Context.getObjCIdType(); 179 return false; 180 } 181 182 ReturnType = Method->getResultType(); 183 184 unsigned NumNamedArgs = Sel.getNumArgs(); 185 assert(NumArgs >= NumNamedArgs && "Too few arguments for selector!"); 186 187 bool IsError = false; 188 for (unsigned i = 0; i < NumNamedArgs; i++) { 189 Expr *argExpr = Args[i]; 190 assert(argExpr && "CheckMessageArgumentTypes(): missing expression"); 191 192 QualType lhsType = Method->param_begin()[i]->getType(); 193 QualType rhsType = argExpr->getType(); 194 195 // If necessary, apply function/array conversion. C99 6.7.5.3p[7,8]. 196 if (lhsType->isArrayType()) 197 lhsType = Context.getArrayDecayedType(lhsType); 198 else if (lhsType->isFunctionType()) 199 lhsType = Context.getPointerType(lhsType); 200 201 AssignConvertType Result = 202 CheckSingleAssignmentConstraints(lhsType, argExpr); 203 if (Args[i] != argExpr) // The expression was converted. 204 Args[i] = argExpr; // Make sure we store the converted expression. 205 206 IsError |= 207 DiagnoseAssignmentResult(Result, argExpr->getLocStart(), lhsType, rhsType, 208 argExpr, "sending"); 209 } 210 211 // Promote additional arguments to variadic methods. 212 if (Method->isVariadic()) { 213 for (unsigned i = NumNamedArgs; i < NumArgs; ++i) 214 IsError |= DefaultVariadicArgumentPromotion(Args[i], VariadicMethod); 215 } else { 216 // Check for extra arguments to non-variadic methods. 217 if (NumArgs != NumNamedArgs) { 218 Diag(Args[NumNamedArgs]->getLocStart(), 219 diag::err_typecheck_call_too_many_args) 220 << 2 /*method*/ << Method->getSourceRange() 221 << SourceRange(Args[NumNamedArgs]->getLocStart(), 222 Args[NumArgs-1]->getLocEnd()); 223 } 224 } 225 226 return IsError; 227} 228 229bool Sema::isSelfExpr(Expr *RExpr) { 230 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(RExpr)) 231 if (DRE->getDecl()->getIdentifier() == &Context.Idents.get("self")) 232 return true; 233 return false; 234} 235 236// Helper method for ActOnClassMethod/ActOnInstanceMethod. 237// Will search "local" class/category implementations for a method decl. 238// If failed, then we search in class's root for an instance method. 239// Returns 0 if no method is found. 240ObjCMethodDecl *Sema::LookupPrivateClassMethod(Selector Sel, 241 ObjCInterfaceDecl *ClassDecl) { 242 ObjCMethodDecl *Method = 0; 243 // lookup in class and all superclasses 244 while (ClassDecl && !Method) { 245 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) 246 Method = ImpDecl->getClassMethod(Sel); 247 248 // Look through local category implementations associated with the class. 249 if (!Method) 250 Method = ClassDecl->getCategoryClassMethod(Sel); 251 252 // Before we give up, check if the selector is an instance method. 253 // But only in the root. This matches gcc's behaviour and what the 254 // runtime expects. 255 if (!Method && !ClassDecl->getSuperClass()) { 256 Method = ClassDecl->lookupInstanceMethod(Sel); 257 // Look through local category implementations associated 258 // with the root class. 259 if (!Method) 260 Method = LookupPrivateInstanceMethod(Sel, ClassDecl); 261 } 262 263 ClassDecl = ClassDecl->getSuperClass(); 264 } 265 return Method; 266} 267 268ObjCMethodDecl *Sema::LookupPrivateInstanceMethod(Selector Sel, 269 ObjCInterfaceDecl *ClassDecl) { 270 ObjCMethodDecl *Method = 0; 271 while (ClassDecl && !Method) { 272 // If we have implementations in scope, check "private" methods. 273 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) 274 Method = ImpDecl->getInstanceMethod(Sel); 275 276 // Look through local category implementations associated with the class. 277 if (!Method) 278 Method = ClassDecl->getCategoryInstanceMethod(Sel); 279 ClassDecl = ClassDecl->getSuperClass(); 280 } 281 return Method; 282} 283 284Action::OwningExprResult Sema::ActOnClassPropertyRefExpr( 285 IdentifierInfo &receiverName, 286 IdentifierInfo &propertyName, 287 SourceLocation &receiverNameLoc, 288 SourceLocation &propertyNameLoc) { 289 290 ObjCInterfaceDecl *IFace = getObjCInterfaceDecl(&receiverName); 291 292 // Search for a declared property first. 293 294 Selector Sel = PP.getSelectorTable().getNullarySelector(&propertyName); 295 ObjCMethodDecl *Getter = IFace->lookupClassMethod(Sel); 296 297 // If this reference is in an @implementation, check for 'private' methods. 298 if (!Getter) 299 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) 300 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) 301 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) 302 Getter = ImpDecl->getClassMethod(Sel); 303 304 if (Getter) { 305 // FIXME: refactor/share with ActOnMemberReference(). 306 // Check if we can reference this property. 307 if (DiagnoseUseOfDecl(Getter, propertyNameLoc)) 308 return ExprError(); 309 } 310 311 // Look for the matching setter, in case it is needed. 312 Selector SetterSel = 313 SelectorTable::constructSetterName(PP.getIdentifierTable(), 314 PP.getSelectorTable(), &propertyName); 315 316 ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); 317 if (!Setter) { 318 // If this reference is in an @implementation, also check for 'private' 319 // methods. 320 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) 321 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) 322 if (ObjCImplementationDecl *ImpDecl = ClassDecl->getImplementation()) 323 Setter = ImpDecl->getClassMethod(SetterSel); 324 } 325 // Look through local category implementations associated with the class. 326 if (!Setter) 327 Setter = IFace->getCategoryClassMethod(SetterSel); 328 329 if (Setter && DiagnoseUseOfDecl(Setter, propertyNameLoc)) 330 return ExprError(); 331 332 if (Getter || Setter) { 333 QualType PType; 334 335 if (Getter) 336 PType = Getter->getResultType(); 337 else { 338 for (ObjCMethodDecl::param_iterator PI = Setter->param_begin(), 339 E = Setter->param_end(); PI != E; ++PI) 340 PType = (*PI)->getType(); 341 } 342 return Owned(new (Context) ObjCImplicitSetterGetterRefExpr( 343 Getter, PType, Setter, 344 propertyNameLoc, IFace, receiverNameLoc)); 345 } 346 return ExprError(Diag(propertyNameLoc, diag::err_property_not_found) 347 << &propertyName << Context.getObjCInterfaceType(IFace)); 348} 349 350 351// ActOnClassMessage - used for both unary and keyword messages. 352// ArgExprs is optional - if it is present, the number of expressions 353// is obtained from Sel.getNumArgs(). 354Sema::ExprResult Sema::ActOnClassMessage( 355 Scope *S, 356 IdentifierInfo *receiverName, Selector Sel, 357 SourceLocation lbrac, SourceLocation receiverLoc, 358 SourceLocation selectorLoc, SourceLocation rbrac, 359 ExprTy **Args, unsigned NumArgs) { 360 assert(receiverName && "missing receiver class name"); 361 362 Expr **ArgExprs = reinterpret_cast<Expr **>(Args); 363 ObjCInterfaceDecl* ClassDecl = 0; 364 bool isSuper = false; 365 366 if (receiverName->isStr("super")) { 367 if (getCurMethodDecl()) { 368 isSuper = true; 369 ObjCInterfaceDecl *OID = getCurMethodDecl()->getClassInterface(); 370 if (!OID) 371 return Diag(lbrac, diag::error_no_super_class_message) 372 << getCurMethodDecl()->getDeclName(); 373 ClassDecl = OID->getSuperClass(); 374 if (!ClassDecl) 375 return Diag(lbrac, diag::error_no_super_class) << OID->getDeclName(); 376 if (getCurMethodDecl()->isInstanceMethod()) { 377 QualType superTy = Context.getObjCInterfaceType(ClassDecl); 378 superTy = Context.getObjCObjectPointerType(superTy); 379 ExprResult ReceiverExpr = new (Context) ObjCSuperExpr(SourceLocation(), 380 superTy); 381 // We are really in an instance method, redirect. 382 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, 383 selectorLoc, rbrac, Args, NumArgs); 384 } 385 // We are sending a message to 'super' within a class method. Do nothing, 386 // the receiver will pass through as 'super' (how convenient:-). 387 } else { 388 // 'super' has been used outside a method context. If a variable named 389 // 'super' has been declared, redirect. If not, produce a diagnostic. 390 NamedDecl *SuperDecl 391 = LookupSingleName(S, receiverName, LookupOrdinaryName); 392 ValueDecl *VD = dyn_cast_or_null<ValueDecl>(SuperDecl); 393 if (VD) { 394 ExprResult ReceiverExpr = new (Context) DeclRefExpr(VD, VD->getType(), 395 receiverLoc); 396 // We are really in an instance method, redirect. 397 return ActOnInstanceMessage(ReceiverExpr.get(), Sel, lbrac, 398 selectorLoc, rbrac, Args, NumArgs); 399 } 400 return Diag(receiverLoc, diag::err_undeclared_var_use) << receiverName; 401 } 402 } else 403 ClassDecl = getObjCInterfaceDecl(receiverName); 404 405 // The following code allows for the following GCC-ism: 406 // 407 // typedef XCElementDisplayRect XCElementGraphicsRect; 408 // 409 // @implementation XCRASlice 410 // - whatever { // Note that XCElementGraphicsRect is a typedef name. 411 // _sGraphicsDelegate =[[XCElementGraphicsRect alloc] init]; 412 // } 413 // 414 // If necessary, the following lookup could move to getObjCInterfaceDecl(). 415 if (!ClassDecl) { 416 NamedDecl *IDecl 417 = LookupSingleName(TUScope, receiverName, LookupOrdinaryName); 418 if (TypedefDecl *OCTD = dyn_cast_or_null<TypedefDecl>(IDecl)) { 419 const ObjCInterfaceType *OCIT; 420 OCIT = OCTD->getUnderlyingType()->getAs<ObjCInterfaceType>(); 421 if (!OCIT) { 422 Diag(receiverLoc, diag::err_invalid_receiver_to_message); 423 return true; 424 } 425 ClassDecl = OCIT->getDecl(); 426 } 427 } 428 assert(ClassDecl && "missing interface declaration"); 429 ObjCMethodDecl *Method = 0; 430 QualType returnType; 431 if (ClassDecl->isForwardDecl()) { 432 // A forward class used in messaging is tread as a 'Class' 433 Diag(lbrac, diag::warn_receiver_forward_class) << ClassDecl->getDeclName(); 434 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac)); 435 if (Method) 436 Diag(Method->getLocation(), diag::note_method_sent_forward_class) 437 << Method->getDeclName(); 438 } 439 if (!Method) 440 Method = ClassDecl->lookupClassMethod(Sel); 441 442 // If we have an implementation in scope, check "private" methods. 443 if (!Method) 444 Method = LookupPrivateClassMethod(Sel, ClassDecl); 445 446 if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) 447 return true; 448 449 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, true, 450 lbrac, rbrac, returnType)) 451 return true; 452 453 returnType = returnType.getNonReferenceType(); 454 455 // If we have the ObjCInterfaceDecl* for the class that is receiving the 456 // message, use that to construct the ObjCMessageExpr. Otherwise pass on the 457 // IdentifierInfo* for the class. 458 // FIXME: need to do a better job handling 'super' usage within a class. For 459 // now, we simply pass the "super" identifier through (which isn't consistent 460 // with instance methods. 461 if (isSuper) 462 return new (Context) ObjCMessageExpr(receiverName, Sel, returnType, Method, 463 lbrac, rbrac, ArgExprs, NumArgs); 464 else 465 return new (Context) ObjCMessageExpr(ClassDecl, Sel, returnType, Method, 466 lbrac, rbrac, ArgExprs, NumArgs); 467} 468 469// ActOnInstanceMessage - used for both unary and keyword messages. 470// ArgExprs is optional - if it is present, the number of expressions 471// is obtained from Sel.getNumArgs(). 472Sema::ExprResult Sema::ActOnInstanceMessage(ExprTy *receiver, Selector Sel, 473 SourceLocation lbrac, 474 SourceLocation receiverLoc, 475 SourceLocation rbrac, 476 ExprTy **Args, unsigned NumArgs) { 477 assert(receiver && "missing receiver expression"); 478 479 Expr **ArgExprs = reinterpret_cast<Expr **>(Args); 480 Expr *RExpr = static_cast<Expr *>(receiver); 481 482 // If necessary, apply function/array conversion to the receiver. 483 // C99 6.7.5.3p[7,8]. 484 DefaultFunctionArrayConversion(RExpr); 485 486 QualType returnType; 487 QualType ReceiverCType = 488 Context.getCanonicalType(RExpr->getType()).getUnqualifiedType(); 489 490 // Handle messages to 'super'. 491 if (isa<ObjCSuperExpr>(RExpr)) { 492 ObjCMethodDecl *Method = 0; 493 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { 494 // If we have an interface in scope, check 'super' methods. 495 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) 496 if (ObjCInterfaceDecl *SuperDecl = ClassDecl->getSuperClass()) { 497 Method = SuperDecl->lookupInstanceMethod(Sel); 498 499 if (!Method) 500 // If we have implementations in scope, check "private" methods. 501 Method = LookupPrivateInstanceMethod(Sel, SuperDecl); 502 } 503 } 504 505 if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) 506 return true; 507 508 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, 509 lbrac, rbrac, returnType)) 510 return true; 511 512 returnType = returnType.getNonReferenceType(); 513 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, 514 rbrac, ArgExprs, NumArgs); 515 } 516 517 // Handle messages to id. 518 if (ReceiverCType->isObjCIdType() || ReceiverCType->isBlockPointerType() || 519 Context.isObjCNSObjectType(RExpr->getType())) { 520 ObjCMethodDecl *Method = LookupInstanceMethodInGlobalPool( 521 Sel, SourceRange(lbrac,rbrac)); 522 if (!Method) 523 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac, rbrac)); 524 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, 525 lbrac, rbrac, returnType)) 526 return true; 527 returnType = returnType.getNonReferenceType(); 528 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, 529 rbrac, ArgExprs, NumArgs); 530 } 531 532 // Handle messages to Class. 533 if (ReceiverCType->isObjCClassType() || 534 ReceiverCType->isObjCQualifiedClassType()) { 535 ObjCMethodDecl *Method = 0; 536 537 if (ObjCMethodDecl *CurMeth = getCurMethodDecl()) { 538 if (ObjCInterfaceDecl *ClassDecl = CurMeth->getClassInterface()) { 539 // First check the public methods in the class interface. 540 Method = ClassDecl->lookupClassMethod(Sel); 541 542 if (!Method) 543 Method = LookupPrivateClassMethod(Sel, ClassDecl); 544 545 // FIXME: if we still haven't found a method, we need to look in 546 // protocols (if we have qualifiers). 547 } 548 if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) 549 return true; 550 } 551 if (!Method) { 552 // If not messaging 'self', look for any factory method named 'Sel'. 553 if (!isSelfExpr(RExpr)) { 554 Method = LookupFactoryMethodInGlobalPool(Sel, SourceRange(lbrac,rbrac)); 555 if (!Method) { 556 // If no class (factory) method was found, check if an _instance_ 557 // method of the same name exists in the root class only. 558 Method = LookupInstanceMethodInGlobalPool( 559 Sel, SourceRange(lbrac,rbrac)); 560 if (Method) 561 if (const ObjCInterfaceDecl *ID = 562 dyn_cast<ObjCInterfaceDecl>(Method->getDeclContext())) { 563 if (ID->getSuperClass()) 564 Diag(lbrac, diag::warn_root_inst_method_not_found) 565 << Sel << SourceRange(lbrac, rbrac); 566 } 567 } 568 } 569 } 570 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, 571 lbrac, rbrac, returnType)) 572 return true; 573 returnType = returnType.getNonReferenceType(); 574 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, 575 rbrac, ArgExprs, NumArgs); 576 } 577 578 ObjCMethodDecl *Method = 0; 579 ObjCInterfaceDecl* ClassDecl = 0; 580 581 // We allow sending a message to a qualified ID ("id<foo>"), which is ok as 582 // long as one of the protocols implements the selector (if not, warn). 583 if (const ObjCObjectPointerType *QIdTy = 584 ReceiverCType->getAsObjCQualifiedIdType()) { 585 // Search protocols for instance methods. 586 for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), 587 E = QIdTy->qual_end(); I != E; ++I) { 588 ObjCProtocolDecl *PDecl = *I; 589 if (PDecl && (Method = PDecl->lookupInstanceMethod(Sel))) 590 break; 591 // Since we aren't supporting "Class<foo>", look for a class method. 592 if (PDecl && (Method = PDecl->lookupClassMethod(Sel))) 593 break; 594 } 595 } else if (const ObjCObjectPointerType *OCIType = 596 ReceiverCType->getAsObjCInterfacePointerType()) { 597 // We allow sending a message to a pointer to an interface (an object). 598 599 ClassDecl = OCIType->getInterfaceDecl(); 600 // FIXME: consider using LookupInstanceMethodInGlobalPool, since it will be 601 // faster than the following method (which can do *many* linear searches). 602 // The idea is to add class info to InstanceMethodPool. 603 Method = ClassDecl->lookupInstanceMethod(Sel); 604 605 if (!Method) { 606 // Search protocol qualifiers. 607 for (ObjCObjectPointerType::qual_iterator QI = OCIType->qual_begin(), 608 E = OCIType->qual_end(); QI != E; ++QI) { 609 if ((Method = (*QI)->lookupInstanceMethod(Sel))) 610 break; 611 } 612 } 613 if (!Method) { 614 // If we have implementations in scope, check "private" methods. 615 Method = LookupPrivateInstanceMethod(Sel, ClassDecl); 616 617 if (!Method && !isSelfExpr(RExpr)) { 618 // If we still haven't found a method, look in the global pool. This 619 // behavior isn't very desirable, however we need it for GCC 620 // compatibility. FIXME: should we deviate?? 621 if (OCIType->qual_empty()) { 622 Method = LookupInstanceMethodInGlobalPool( 623 Sel, SourceRange(lbrac,rbrac)); 624 if (Method && !OCIType->getInterfaceDecl()->isForwardDecl()) 625 Diag(lbrac, diag::warn_maynot_respond) 626 << OCIType->getInterfaceDecl()->getIdentifier()->getName() << Sel; 627 } 628 } 629 } 630 if (Method && DiagnoseUseOfDecl(Method, receiverLoc)) 631 return true; 632 } else if (!Context.getObjCIdType().isNull() && 633 (ReceiverCType->isPointerType() || 634 (ReceiverCType->isIntegerType() && 635 ReceiverCType->isScalarType()))) { 636 // Implicitly convert integers and pointers to 'id' but emit a warning. 637 Diag(lbrac, diag::warn_bad_receiver_type) 638 << RExpr->getType() << RExpr->getSourceRange(); 639 ImpCastExprToType(RExpr, Context.getObjCIdType()); 640 } else { 641 // Reject other random receiver types (e.g. structs). 642 Diag(lbrac, diag::err_bad_receiver_type) 643 << RExpr->getType() << RExpr->getSourceRange(); 644 return true; 645 } 646 647 if (Method) 648 DiagnoseSentinelCalls(Method, receiverLoc, ArgExprs, NumArgs); 649 if (CheckMessageArgumentTypes(ArgExprs, NumArgs, Sel, Method, false, 650 lbrac, rbrac, returnType)) 651 return true; 652 returnType = returnType.getNonReferenceType(); 653 return new (Context) ObjCMessageExpr(RExpr, Sel, returnType, Method, lbrac, 654 rbrac, ArgExprs, NumArgs); 655} 656 657