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