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