SemaExprCXX.cpp revision 199990
1193326Sed//===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements semantic analysis for C++ expressions. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#include "Sema.h" 15199482Srdivacky#include "Lookup.h" 16198092Srdivacky#include "clang/AST/ASTContext.h" 17198092Srdivacky#include "clang/AST/CXXInheritance.h" 18193326Sed#include "clang/AST/ExprCXX.h" 19198092Srdivacky#include "clang/Basic/PartialDiagnostic.h" 20198092Srdivacky#include "clang/Basic/TargetInfo.h" 21198092Srdivacky#include "clang/Lex/Preprocessor.h" 22193326Sed#include "clang/Parse/DeclSpec.h" 23193326Sed#include "llvm/ADT/STLExtras.h" 24193326Sedusing namespace clang; 25193326Sed 26193326Sed/// ActOnCXXTypeidOfType - Parse typeid( type-id ). 27193326SedAction::OwningExprResult 28193326SedSema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, 29193326Sed bool isType, void *TyOrExpr, SourceLocation RParenLoc) { 30198092Srdivacky if (!StdNamespace) 31193326Sed return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); 32198092Srdivacky 33198092Srdivacky if (isType) 34198092Srdivacky // FIXME: Preserve type source info. 35198092Srdivacky TyOrExpr = GetTypeFromParser(TyOrExpr).getAsOpaquePtr(); 36198092Srdivacky 37193326Sed IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); 38199482Srdivacky LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); 39199482Srdivacky LookupQualifiedName(R, StdNamespace); 40198092Srdivacky Decl *TypeInfoDecl = R.getAsSingleDecl(Context); 41193326Sed RecordDecl *TypeInfoRecordDecl = dyn_cast_or_null<RecordDecl>(TypeInfoDecl); 42193326Sed if (!TypeInfoRecordDecl) 43193326Sed return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); 44193326Sed 45193326Sed QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); 46193326Sed 47194711Sed if (!isType) { 48194711Sed // C++0x [expr.typeid]p3: 49198092Srdivacky // When typeid is applied to an expression other than an lvalue of a 50198092Srdivacky // polymorphic class type [...] [the] expression is an unevaluated 51194711Sed // operand. 52198092Srdivacky 53194711Sed // FIXME: if the type of the expression is a class type, the class 54194711Sed // shall be completely defined. 55194711Sed bool isUnevaluatedOperand = true; 56194711Sed Expr *E = static_cast<Expr *>(TyOrExpr); 57194711Sed if (E && !E->isTypeDependent() && E->isLvalue(Context) == Expr::LV_Valid) { 58194711Sed QualType T = E->getType(); 59198092Srdivacky if (const RecordType *RecordT = T->getAs<RecordType>()) { 60194711Sed CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); 61194711Sed if (RecordD->isPolymorphic()) 62194711Sed isUnevaluatedOperand = false; 63194711Sed } 64194711Sed } 65198092Srdivacky 66199990Srdivacky // If this is an unevaluated operand, clear out the set of 67199990Srdivacky // declaration references we have been computing and eliminate any 68199990Srdivacky // temporaries introduced in its computation. 69194711Sed if (isUnevaluatedOperand) 70199990Srdivacky ExprEvalContexts.back().Context = Unevaluated; 71194711Sed } 72198092Srdivacky 73193326Sed return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr, 74193326Sed TypeInfoType.withConst(), 75193326Sed SourceRange(OpLoc, RParenLoc))); 76193326Sed} 77193326Sed 78193326Sed/// ActOnCXXBoolLiteral - Parse {true,false} literals. 79193326SedAction::OwningExprResult 80193326SedSema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { 81193326Sed assert((Kind == tok::kw_true || Kind == tok::kw_false) && 82193326Sed "Unknown C++ Boolean value!"); 83193326Sed return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, 84193326Sed Context.BoolTy, OpLoc)); 85193326Sed} 86193326Sed 87193326Sed/// ActOnCXXNullPtrLiteral - Parse 'nullptr'. 88193326SedAction::OwningExprResult 89193326SedSema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { 90193326Sed return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); 91193326Sed} 92193326Sed 93193326Sed/// ActOnCXXThrow - Parse throw expressions. 94193326SedAction::OwningExprResult 95193326SedSema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { 96193326Sed Expr *Ex = E.takeAs<Expr>(); 97193326Sed if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) 98193326Sed return ExprError(); 99193326Sed return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); 100193326Sed} 101193326Sed 102193326Sed/// CheckCXXThrowOperand - Validate the operand of a throw. 103193326Sedbool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { 104193326Sed // C++ [except.throw]p3: 105193326Sed // [...] adjusting the type from "array of T" or "function returning T" 106193326Sed // to "pointer to T" or "pointer to function returning T", [...] 107193326Sed DefaultFunctionArrayConversion(E); 108193326Sed 109193326Sed // If the type of the exception would be an incomplete type or a pointer 110193326Sed // to an incomplete type other than (cv) void the program is ill-formed. 111193326Sed QualType Ty = E->getType(); 112193326Sed int isPointer = 0; 113198092Srdivacky if (const PointerType* Ptr = Ty->getAs<PointerType>()) { 114193326Sed Ty = Ptr->getPointeeType(); 115193326Sed isPointer = 1; 116193326Sed } 117193326Sed if (!isPointer || !Ty->isVoidType()) { 118193326Sed if (RequireCompleteType(ThrowLoc, Ty, 119198092Srdivacky PDiag(isPointer ? diag::err_throw_incomplete_ptr 120198092Srdivacky : diag::err_throw_incomplete) 121198092Srdivacky << E->getSourceRange())) 122193326Sed return true; 123193326Sed } 124193326Sed 125193326Sed // FIXME: Construct a temporary here. 126193326Sed return false; 127193326Sed} 128193326Sed 129193326SedAction::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { 130193326Sed /// C++ 9.3.2: In the body of a non-static member function, the keyword this 131193326Sed /// is a non-lvalue expression whose value is the address of the object for 132193326Sed /// which the function is called. 133193326Sed 134193326Sed if (!isa<FunctionDecl>(CurContext)) 135193326Sed return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); 136193326Sed 137193326Sed if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) 138193326Sed if (MD->isInstance()) 139193326Sed return Owned(new (Context) CXXThisExpr(ThisLoc, 140193326Sed MD->getThisType(Context))); 141193326Sed 142193326Sed return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); 143193326Sed} 144193326Sed 145193326Sed/// ActOnCXXTypeConstructExpr - Parse construction of a specified type. 146193326Sed/// Can be interpreted either as function-style casting ("int(x)") 147193326Sed/// or class type construction ("ClassType(x,y,z)") 148193326Sed/// or creation of a value-initialized type ("int()"). 149193326SedAction::OwningExprResult 150193326SedSema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, 151193326Sed SourceLocation LParenLoc, 152193326Sed MultiExprArg exprs, 153193326Sed SourceLocation *CommaLocs, 154193326Sed SourceLocation RParenLoc) { 155193326Sed assert(TypeRep && "Missing type!"); 156198092Srdivacky // FIXME: Preserve type source info. 157198092Srdivacky QualType Ty = GetTypeFromParser(TypeRep); 158193326Sed unsigned NumExprs = exprs.size(); 159193326Sed Expr **Exprs = (Expr**)exprs.get(); 160193326Sed SourceLocation TyBeginLoc = TypeRange.getBegin(); 161193326Sed SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); 162193326Sed 163193326Sed if (Ty->isDependentType() || 164193326Sed CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { 165193326Sed exprs.release(); 166198092Srdivacky 167198092Srdivacky return Owned(CXXUnresolvedConstructExpr::Create(Context, 168198092Srdivacky TypeRange.getBegin(), Ty, 169193326Sed LParenLoc, 170193326Sed Exprs, NumExprs, 171193326Sed RParenLoc)); 172193326Sed } 173193326Sed 174198092Srdivacky if (Ty->isArrayType()) 175198092Srdivacky return ExprError(Diag(TyBeginLoc, 176198092Srdivacky diag::err_value_init_for_array_type) << FullRange); 177198092Srdivacky if (!Ty->isVoidType() && 178198092Srdivacky RequireCompleteType(TyBeginLoc, Ty, 179198092Srdivacky PDiag(diag::err_invalid_incomplete_type_use) 180198092Srdivacky << FullRange)) 181198092Srdivacky return ExprError(); 182198893Srdivacky 183198092Srdivacky if (RequireNonAbstractType(TyBeginLoc, Ty, 184198092Srdivacky diag::err_allocation_of_abstract_type)) 185198092Srdivacky return ExprError(); 186198092Srdivacky 187198092Srdivacky 188193326Sed // C++ [expr.type.conv]p1: 189193326Sed // If the expression list is a single expression, the type conversion 190193326Sed // expression is equivalent (in definedness, and if defined in meaning) to the 191193326Sed // corresponding cast expression. 192193326Sed // 193193326Sed if (NumExprs == 1) { 194198092Srdivacky CastExpr::CastKind Kind = CastExpr::CK_Unknown; 195198092Srdivacky CXXMethodDecl *Method = 0; 196198092Srdivacky if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, Method, 197198092Srdivacky /*FunctionalStyle=*/true)) 198193326Sed return ExprError(); 199198092Srdivacky 200193326Sed exprs.release(); 201198092Srdivacky if (Method) { 202198092Srdivacky OwningExprResult CastArg 203198092Srdivacky = BuildCXXCastArgument(TypeRange.getBegin(), Ty.getNonReferenceType(), 204198092Srdivacky Kind, Method, Owned(Exprs[0])); 205198092Srdivacky if (CastArg.isInvalid()) 206198092Srdivacky return ExprError(); 207198092Srdivacky 208198092Srdivacky Exprs[0] = CastArg.takeAs<Expr>(); 209198092Srdivacky } 210198092Srdivacky 211193326Sed return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), 212198092Srdivacky Ty, TyBeginLoc, Kind, 213198092Srdivacky Exprs[0], RParenLoc)); 214193326Sed } 215193326Sed 216198092Srdivacky if (const RecordType *RT = Ty->getAs<RecordType>()) { 217193326Sed CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 218193326Sed 219198092Srdivacky if (NumExprs > 1 || !Record->hasTrivialConstructor() || 220198092Srdivacky !Record->hasTrivialDestructor()) { 221198092Srdivacky ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); 222198092Srdivacky 223193326Sed CXXConstructorDecl *Constructor 224198092Srdivacky = PerformInitializationByConstructor(Ty, move(exprs), 225193326Sed TypeRange.getBegin(), 226193326Sed SourceRange(TypeRange.getBegin(), 227193326Sed RParenLoc), 228193326Sed DeclarationName(), 229198092Srdivacky IK_Direct, 230198092Srdivacky ConstructorArgs); 231193326Sed 232193326Sed if (!Constructor) 233193326Sed return ExprError(); 234193326Sed 235198092Srdivacky OwningExprResult Result = 236198092Srdivacky BuildCXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc, 237198092Srdivacky move_arg(ConstructorArgs), RParenLoc); 238198092Srdivacky if (Result.isInvalid()) 239198092Srdivacky return ExprError(); 240198092Srdivacky 241198092Srdivacky return MaybeBindToTemporary(Result.takeAs<Expr>()); 242193326Sed } 243193326Sed 244193326Sed // Fall through to value-initialize an object of class type that 245193326Sed // doesn't have a user-declared default constructor. 246193326Sed } 247193326Sed 248193326Sed // C++ [expr.type.conv]p1: 249193326Sed // If the expression list specifies more than a single value, the type shall 250193326Sed // be a class with a suitably declared constructor. 251193326Sed // 252193326Sed if (NumExprs > 1) 253193326Sed return ExprError(Diag(CommaLocs[0], 254193326Sed diag::err_builtin_func_cast_more_than_one_arg) 255193326Sed << FullRange); 256193326Sed 257193326Sed assert(NumExprs == 0 && "Expected 0 expressions"); 258193326Sed // C++ [expr.type.conv]p2: 259193326Sed // The expression T(), where T is a simple-type-specifier for a non-array 260193326Sed // complete object type or the (possibly cv-qualified) void type, creates an 261193326Sed // rvalue of the specified type, which is value-initialized. 262193326Sed // 263193326Sed exprs.release(); 264193326Sed return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); 265193326Sed} 266193326Sed 267193326Sed 268193326Sed/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: 269193326Sed/// @code new (memory) int[size][4] @endcode 270193326Sed/// or 271193326Sed/// @code ::new Foo(23, "hello") @endcode 272193326Sed/// For the interpretation of this heap of arguments, consult the base version. 273193326SedAction::OwningExprResult 274193326SedSema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, 275193326Sed SourceLocation PlacementLParen, MultiExprArg PlacementArgs, 276193326Sed SourceLocation PlacementRParen, bool ParenTypeId, 277193326Sed Declarator &D, SourceLocation ConstructorLParen, 278193326Sed MultiExprArg ConstructorArgs, 279198092Srdivacky SourceLocation ConstructorRParen) { 280193326Sed Expr *ArraySize = 0; 281193326Sed // If the specified type is an array, unwrap it and save the expression. 282193326Sed if (D.getNumTypeObjects() > 0 && 283193326Sed D.getTypeObject(0).Kind == DeclaratorChunk::Array) { 284193326Sed DeclaratorChunk &Chunk = D.getTypeObject(0); 285193326Sed if (Chunk.Arr.hasStatic) 286193326Sed return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) 287193326Sed << D.getSourceRange()); 288193326Sed if (!Chunk.Arr.NumElts) 289193326Sed return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) 290193326Sed << D.getSourceRange()); 291198893Srdivacky 292198893Srdivacky if (ParenTypeId) { 293198893Srdivacky // Can't have dynamic array size when the type-id is in parentheses. 294198893Srdivacky Expr *NumElts = (Expr *)Chunk.Arr.NumElts; 295198893Srdivacky if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && 296198893Srdivacky !NumElts->isIntegerConstantExpr(Context)) { 297198893Srdivacky Diag(D.getTypeObject(0).Loc, diag::err_new_paren_array_nonconst) 298198893Srdivacky << NumElts->getSourceRange(); 299198893Srdivacky return ExprError(); 300198893Srdivacky } 301198893Srdivacky } 302198893Srdivacky 303193326Sed ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); 304198893Srdivacky D.DropFirstTypeObject(); 305193326Sed } 306193326Sed 307198092Srdivacky // Every dimension shall be of constant size. 308198893Srdivacky if (ArraySize) { 309198893Srdivacky for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { 310198092Srdivacky if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) 311198092Srdivacky break; 312193326Sed 313198092Srdivacky DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; 314198092Srdivacky if (Expr *NumElts = (Expr *)Array.NumElts) { 315198092Srdivacky if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && 316198092Srdivacky !NumElts->isIntegerConstantExpr(Context)) { 317198092Srdivacky Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) 318198092Srdivacky << NumElts->getSourceRange(); 319198092Srdivacky return ExprError(); 320198092Srdivacky } 321198092Srdivacky } 322193326Sed } 323193326Sed } 324198893Srdivacky 325198092Srdivacky //FIXME: Store DeclaratorInfo in CXXNew expression. 326198092Srdivacky DeclaratorInfo *DInfo = 0; 327198893Srdivacky QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, &DInfo); 328198092Srdivacky if (D.isInvalidType()) 329198092Srdivacky return ExprError(); 330199990Srdivacky 331198092Srdivacky return BuildCXXNew(StartLoc, UseGlobal, 332193326Sed PlacementLParen, 333198092Srdivacky move(PlacementArgs), 334193326Sed PlacementRParen, 335193326Sed ParenTypeId, 336198092Srdivacky AllocType, 337193326Sed D.getSourceRange().getBegin(), 338193326Sed D.getSourceRange(), 339193326Sed Owned(ArraySize), 340193326Sed ConstructorLParen, 341193326Sed move(ConstructorArgs), 342193326Sed ConstructorRParen); 343193326Sed} 344193326Sed 345198092SrdivackySema::OwningExprResult 346193326SedSema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, 347193326Sed SourceLocation PlacementLParen, 348193326Sed MultiExprArg PlacementArgs, 349193326Sed SourceLocation PlacementRParen, 350198092Srdivacky bool ParenTypeId, 351193326Sed QualType AllocType, 352193326Sed SourceLocation TypeLoc, 353193326Sed SourceRange TypeRange, 354193326Sed ExprArg ArraySizeE, 355193326Sed SourceLocation ConstructorLParen, 356193326Sed MultiExprArg ConstructorArgs, 357193326Sed SourceLocation ConstructorRParen) { 358193326Sed if (CheckAllocatedType(AllocType, TypeLoc, TypeRange)) 359193326Sed return ExprError(); 360193326Sed 361193326Sed QualType ResultType = Context.getPointerType(AllocType); 362193326Sed 363193326Sed // That every array dimension except the first is constant was already 364193326Sed // checked by the type check above. 365193326Sed 366193326Sed // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral 367193326Sed // or enumeration type with a non-negative value." 368193326Sed Expr *ArraySize = (Expr *)ArraySizeE.get(); 369193326Sed if (ArraySize && !ArraySize->isTypeDependent()) { 370193326Sed QualType SizeType = ArraySize->getType(); 371193326Sed if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) 372193326Sed return ExprError(Diag(ArraySize->getSourceRange().getBegin(), 373193326Sed diag::err_array_size_not_integral) 374193326Sed << SizeType << ArraySize->getSourceRange()); 375193326Sed // Let's see if this is a constant < 0. If so, we reject it out of hand. 376193326Sed // We don't care about special rules, so we tell the machinery it's not 377193326Sed // evaluated - it gives us a result in more cases. 378193326Sed if (!ArraySize->isValueDependent()) { 379193326Sed llvm::APSInt Value; 380193326Sed if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { 381193326Sed if (Value < llvm::APSInt( 382198092Srdivacky llvm::APInt::getNullValue(Value.getBitWidth()), 383198092Srdivacky Value.isUnsigned())) 384193326Sed return ExprError(Diag(ArraySize->getSourceRange().getBegin(), 385193326Sed diag::err_typecheck_negative_array_size) 386193326Sed << ArraySize->getSourceRange()); 387193326Sed } 388193326Sed } 389198092Srdivacky 390198398Srdivacky ImpCastExprToType(ArraySize, Context.getSizeType(), 391198398Srdivacky CastExpr::CK_IntegralCast); 392193326Sed } 393193326Sed 394193326Sed FunctionDecl *OperatorNew = 0; 395193326Sed FunctionDecl *OperatorDelete = 0; 396193326Sed Expr **PlaceArgs = (Expr**)PlacementArgs.get(); 397193326Sed unsigned NumPlaceArgs = PlacementArgs.size(); 398199990Srdivacky 399193326Sed if (!AllocType->isDependentType() && 400193326Sed !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && 401193326Sed FindAllocationFunctions(StartLoc, 402193326Sed SourceRange(PlacementLParen, PlacementRParen), 403193326Sed UseGlobal, AllocType, ArraySize, PlaceArgs, 404193326Sed NumPlaceArgs, OperatorNew, OperatorDelete)) 405193326Sed return ExprError(); 406199990Srdivacky llvm::SmallVector<Expr *, 8> AllPlaceArgs; 407199990Srdivacky if (OperatorNew) { 408199990Srdivacky // Add default arguments, if any. 409199990Srdivacky const FunctionProtoType *Proto = 410199990Srdivacky OperatorNew->getType()->getAs<FunctionProtoType>(); 411199990Srdivacky VariadicCallType CallType = 412199990Srdivacky Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; 413199990Srdivacky bool Invalid = GatherArgumentsForCall(PlacementLParen, OperatorNew, 414199990Srdivacky Proto, 1, PlaceArgs, NumPlaceArgs, 415199990Srdivacky AllPlaceArgs, CallType); 416199990Srdivacky if (Invalid) 417199990Srdivacky return ExprError(); 418199990Srdivacky 419199990Srdivacky NumPlaceArgs = AllPlaceArgs.size(); 420199990Srdivacky if (NumPlaceArgs > 0) 421199990Srdivacky PlaceArgs = &AllPlaceArgs[0]; 422199990Srdivacky } 423199990Srdivacky 424193326Sed bool Init = ConstructorLParen.isValid(); 425193326Sed // --- Choosing a constructor --- 426193326Sed // C++ 5.3.4p15 427193326Sed // 1) If T is a POD and there's no initializer (ConstructorLParen is invalid) 428193326Sed // the object is not initialized. If the object, or any part of it, is 429193326Sed // const-qualified, it's an error. 430193326Sed // 2) If T is a POD and there's an empty initializer, the object is value- 431193326Sed // initialized. 432193326Sed // 3) If T is a POD and there's one initializer argument, the object is copy- 433193326Sed // constructed. 434193326Sed // 4) If T is a POD and there's more initializer arguments, it's an error. 435193326Sed // 5) If T is not a POD, the initializer arguments are used as constructor 436193326Sed // arguments. 437193326Sed // 438193326Sed // Or by the C++0x formulation: 439193326Sed // 1) If there's no initializer, the object is default-initialized according 440193326Sed // to C++0x rules. 441193326Sed // 2) Otherwise, the object is direct-initialized. 442193326Sed CXXConstructorDecl *Constructor = 0; 443193326Sed Expr **ConsArgs = (Expr**)ConstructorArgs.get(); 444193326Sed const RecordType *RT; 445193326Sed unsigned NumConsArgs = ConstructorArgs.size(); 446199482Srdivacky ASTOwningVector<&ActionBase::DeleteExpr> ConvertedConstructorArgs(*this); 447199482Srdivacky 448198398Srdivacky if (AllocType->isDependentType() || 449198398Srdivacky Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { 450193326Sed // Skip all the checks. 451198092Srdivacky } else if ((RT = AllocType->getAs<RecordType>()) && 452198092Srdivacky !AllocType->isAggregateType()) { 453193326Sed Constructor = PerformInitializationByConstructor( 454198092Srdivacky AllocType, move(ConstructorArgs), 455193326Sed TypeLoc, 456193326Sed SourceRange(TypeLoc, ConstructorRParen), 457193326Sed RT->getDecl()->getDeclName(), 458198092Srdivacky NumConsArgs != 0 ? IK_Direct : IK_Default, 459198092Srdivacky ConvertedConstructorArgs); 460193326Sed if (!Constructor) 461193326Sed return ExprError(); 462198092Srdivacky 463198092Srdivacky // Take the converted constructor arguments and use them for the new 464198092Srdivacky // expression. 465198092Srdivacky NumConsArgs = ConvertedConstructorArgs.size(); 466198092Srdivacky ConsArgs = (Expr **)ConvertedConstructorArgs.take(); 467193326Sed } else { 468193326Sed if (!Init) { 469193326Sed // FIXME: Check that no subpart is const. 470193326Sed if (AllocType.isConstQualified()) 471193326Sed return ExprError(Diag(StartLoc, diag::err_new_uninitialized_const) 472193326Sed << TypeRange); 473193326Sed } else if (NumConsArgs == 0) { 474193326Sed // Object is value-initialized. Do nothing. 475193326Sed } else if (NumConsArgs == 1) { 476193326Sed // Object is direct-initialized. 477193326Sed // FIXME: What DeclarationName do we pass in here? 478193326Sed if (CheckInitializerTypes(ConsArgs[0], AllocType, StartLoc, 479193326Sed DeclarationName() /*AllocType.getAsString()*/, 480193326Sed /*DirectInit=*/true)) 481193326Sed return ExprError(); 482193326Sed } else { 483193326Sed return ExprError(Diag(StartLoc, 484193326Sed diag::err_builtin_direct_init_more_than_one_arg) 485193326Sed << SourceRange(ConstructorLParen, ConstructorRParen)); 486193326Sed } 487193326Sed } 488193326Sed 489193326Sed // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) 490198398Srdivacky 491193326Sed PlacementArgs.release(); 492193326Sed ConstructorArgs.release(); 493193326Sed ArraySizeE.release(); 494193326Sed return Owned(new (Context) CXXNewExpr(UseGlobal, OperatorNew, PlaceArgs, 495193326Sed NumPlaceArgs, ParenTypeId, ArraySize, Constructor, Init, 496193326Sed ConsArgs, NumConsArgs, OperatorDelete, ResultType, 497198092Srdivacky StartLoc, Init ? ConstructorRParen : SourceLocation())); 498193326Sed} 499193326Sed 500193326Sed/// CheckAllocatedType - Checks that a type is suitable as the allocated type 501193326Sed/// in a new-expression. 502193326Sed/// dimension off and stores the size expression in ArraySize. 503193326Sedbool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, 504198092Srdivacky SourceRange R) { 505193326Sed // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an 506193326Sed // abstract class type or array thereof. 507193326Sed if (AllocType->isFunctionType()) 508193326Sed return Diag(Loc, diag::err_bad_new_type) 509193326Sed << AllocType << 0 << R; 510193326Sed else if (AllocType->isReferenceType()) 511193326Sed return Diag(Loc, diag::err_bad_new_type) 512193326Sed << AllocType << 1 << R; 513193326Sed else if (!AllocType->isDependentType() && 514193326Sed RequireCompleteType(Loc, AllocType, 515198092Srdivacky PDiag(diag::err_new_incomplete_type) 516198092Srdivacky << R)) 517193326Sed return true; 518193326Sed else if (RequireNonAbstractType(Loc, AllocType, 519193326Sed diag::err_allocation_of_abstract_type)) 520193326Sed return true; 521193326Sed 522193326Sed return false; 523193326Sed} 524193326Sed 525193326Sed/// FindAllocationFunctions - Finds the overloads of operator new and delete 526193326Sed/// that are appropriate for the allocation. 527193326Sedbool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, 528193326Sed bool UseGlobal, QualType AllocType, 529193326Sed bool IsArray, Expr **PlaceArgs, 530193326Sed unsigned NumPlaceArgs, 531193326Sed FunctionDecl *&OperatorNew, 532198092Srdivacky FunctionDecl *&OperatorDelete) { 533193326Sed // --- Choosing an allocation function --- 534193326Sed // C++ 5.3.4p8 - 14 & 18 535193326Sed // 1) If UseGlobal is true, only look in the global scope. Else, also look 536193326Sed // in the scope of the allocated class. 537193326Sed // 2) If an array size is given, look for operator new[], else look for 538193326Sed // operator new. 539193326Sed // 3) The first argument is always size_t. Append the arguments from the 540193326Sed // placement form. 541193326Sed // FIXME: Also find the appropriate delete operator. 542193326Sed 543193326Sed llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); 544193326Sed // We don't care about the actual value of this argument. 545193326Sed // FIXME: Should the Sema create the expression and embed it in the syntax 546193326Sed // tree? Or should the consumer just recalculate the value? 547198092Srdivacky IntegerLiteral Size(llvm::APInt::getNullValue( 548198092Srdivacky Context.Target.getPointerWidth(0)), 549198092Srdivacky Context.getSizeType(), 550198092Srdivacky SourceLocation()); 551198092Srdivacky AllocArgs[0] = &Size; 552193326Sed std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); 553193326Sed 554193326Sed DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( 555193326Sed IsArray ? OO_Array_New : OO_New); 556193326Sed if (AllocType->isRecordType() && !UseGlobal) { 557198092Srdivacky CXXRecordDecl *Record 558198092Srdivacky = cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl()); 559193326Sed // FIXME: We fail to find inherited overloads. 560193326Sed if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], 561193326Sed AllocArgs.size(), Record, /*AllowMissing=*/true, 562193326Sed OperatorNew)) 563193326Sed return true; 564193326Sed } 565193326Sed if (!OperatorNew) { 566193326Sed // Didn't find a member overload. Look for a global one. 567193326Sed DeclareGlobalNewDelete(); 568193326Sed DeclContext *TUDecl = Context.getTranslationUnitDecl(); 569193326Sed if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], 570193326Sed AllocArgs.size(), TUDecl, /*AllowMissing=*/false, 571193326Sed OperatorNew)) 572193326Sed return true; 573193326Sed } 574193326Sed 575193326Sed // FindAllocationOverload can change the passed in arguments, so we need to 576193326Sed // copy them back. 577193326Sed if (NumPlaceArgs > 0) 578193326Sed std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); 579198092Srdivacky 580193326Sed return false; 581193326Sed} 582193326Sed 583193326Sed/// FindAllocationOverload - Find an fitting overload for the allocation 584193326Sed/// function in the specified scope. 585193326Sedbool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, 586193326Sed DeclarationName Name, Expr** Args, 587193326Sed unsigned NumArgs, DeclContext *Ctx, 588198092Srdivacky bool AllowMissing, FunctionDecl *&Operator) { 589199482Srdivacky LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); 590199482Srdivacky LookupQualifiedName(R, Ctx); 591198092Srdivacky if (R.empty()) { 592193326Sed if (AllowMissing) 593193326Sed return false; 594193326Sed return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) 595193326Sed << Name << Range; 596193326Sed } 597193326Sed 598198092Srdivacky // FIXME: handle ambiguity 599198092Srdivacky 600193326Sed OverloadCandidateSet Candidates; 601198092Srdivacky for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); 602198092Srdivacky Alloc != AllocEnd; ++Alloc) { 603193326Sed // Even member operator new/delete are implicitly treated as 604193326Sed // static, so don't use AddMemberCandidate. 605198092Srdivacky if (FunctionDecl *Fn = dyn_cast<FunctionDecl>(*Alloc)) { 606193326Sed AddOverloadCandidate(Fn, Args, NumArgs, Candidates, 607193326Sed /*SuppressUserConversions=*/false); 608198092Srdivacky continue; 609198092Srdivacky } 610198092Srdivacky 611198092Srdivacky // FIXME: Handle function templates 612193326Sed } 613193326Sed 614193326Sed // Do the resolution. 615193326Sed OverloadCandidateSet::iterator Best; 616194613Sed switch(BestViableFunction(Candidates, StartLoc, Best)) { 617193326Sed case OR_Success: { 618193326Sed // Got one! 619193326Sed FunctionDecl *FnDecl = Best->Function; 620193326Sed // The first argument is size_t, and the first parameter must be size_t, 621193326Sed // too. This is checked on declaration and can be assumed. (It can't be 622193326Sed // asserted on, though, since invalid decls are left in there.) 623199990Srdivacky // Whatch out for variadic allocator function. 624199990Srdivacky unsigned NumArgsInFnDecl = FnDecl->getNumParams(); 625199990Srdivacky for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { 626193326Sed // FIXME: Passing word to diagnostic. 627193326Sed if (PerformCopyInitialization(Args[i], 628193326Sed FnDecl->getParamDecl(i)->getType(), 629193326Sed "passing")) 630193326Sed return true; 631193326Sed } 632193326Sed Operator = FnDecl; 633193326Sed return false; 634193326Sed } 635193326Sed 636193326Sed case OR_No_Viable_Function: 637193326Sed Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) 638193326Sed << Name << Range; 639193326Sed PrintOverloadCandidates(Candidates, /*OnlyViable=*/false); 640193326Sed return true; 641193326Sed 642193326Sed case OR_Ambiguous: 643193326Sed Diag(StartLoc, diag::err_ovl_ambiguous_call) 644193326Sed << Name << Range; 645193326Sed PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); 646193326Sed return true; 647193326Sed 648193326Sed case OR_Deleted: 649193326Sed Diag(StartLoc, diag::err_ovl_deleted_call) 650193326Sed << Best->Function->isDeleted() 651193326Sed << Name << Range; 652193326Sed PrintOverloadCandidates(Candidates, /*OnlyViable=*/true); 653193326Sed return true; 654193326Sed } 655193326Sed assert(false && "Unreachable, bad result from BestViableFunction"); 656193326Sed return true; 657193326Sed} 658193326Sed 659193326Sed 660193326Sed/// DeclareGlobalNewDelete - Declare the global forms of operator new and 661193326Sed/// delete. These are: 662193326Sed/// @code 663193326Sed/// void* operator new(std::size_t) throw(std::bad_alloc); 664193326Sed/// void* operator new[](std::size_t) throw(std::bad_alloc); 665193326Sed/// void operator delete(void *) throw(); 666193326Sed/// void operator delete[](void *) throw(); 667193326Sed/// @endcode 668193326Sed/// Note that the placement and nothrow forms of new are *not* implicitly 669193326Sed/// declared. Their use requires including \<new\>. 670198092Srdivackyvoid Sema::DeclareGlobalNewDelete() { 671193326Sed if (GlobalNewDeleteDeclared) 672193326Sed return; 673198092Srdivacky 674198092Srdivacky // C++ [basic.std.dynamic]p2: 675198092Srdivacky // [...] The following allocation and deallocation functions (18.4) are 676198092Srdivacky // implicitly declared in global scope in each translation unit of a 677198092Srdivacky // program 678198092Srdivacky // 679198092Srdivacky // void* operator new(std::size_t) throw(std::bad_alloc); 680198092Srdivacky // void* operator new[](std::size_t) throw(std::bad_alloc); 681198092Srdivacky // void operator delete(void*) throw(); 682198092Srdivacky // void operator delete[](void*) throw(); 683198092Srdivacky // 684198092Srdivacky // These implicit declarations introduce only the function names operator 685198092Srdivacky // new, operator new[], operator delete, operator delete[]. 686198092Srdivacky // 687198092Srdivacky // Here, we need to refer to std::bad_alloc, so we will implicitly declare 688198092Srdivacky // "std" or "bad_alloc" as necessary to form the exception specification. 689198092Srdivacky // However, we do not make these implicit declarations visible to name 690198092Srdivacky // lookup. 691198092Srdivacky if (!StdNamespace) { 692198092Srdivacky // The "std" namespace has not yet been defined, so build one implicitly. 693198092Srdivacky StdNamespace = NamespaceDecl::Create(Context, 694198092Srdivacky Context.getTranslationUnitDecl(), 695198092Srdivacky SourceLocation(), 696198092Srdivacky &PP.getIdentifierTable().get("std")); 697198092Srdivacky StdNamespace->setImplicit(true); 698198092Srdivacky } 699198092Srdivacky 700198092Srdivacky if (!StdBadAlloc) { 701198092Srdivacky // The "std::bad_alloc" class has not yet been declared, so build it 702198092Srdivacky // implicitly. 703198092Srdivacky StdBadAlloc = CXXRecordDecl::Create(Context, TagDecl::TK_class, 704198092Srdivacky StdNamespace, 705198092Srdivacky SourceLocation(), 706198092Srdivacky &PP.getIdentifierTable().get("bad_alloc"), 707198092Srdivacky SourceLocation(), 0); 708198092Srdivacky StdBadAlloc->setImplicit(true); 709198092Srdivacky } 710198092Srdivacky 711193326Sed GlobalNewDeleteDeclared = true; 712193326Sed 713193326Sed QualType VoidPtr = Context.getPointerType(Context.VoidTy); 714193326Sed QualType SizeT = Context.getSizeType(); 715193326Sed 716193326Sed DeclareGlobalAllocationFunction( 717193326Sed Context.DeclarationNames.getCXXOperatorName(OO_New), 718193326Sed VoidPtr, SizeT); 719193326Sed DeclareGlobalAllocationFunction( 720193326Sed Context.DeclarationNames.getCXXOperatorName(OO_Array_New), 721193326Sed VoidPtr, SizeT); 722193326Sed DeclareGlobalAllocationFunction( 723193326Sed Context.DeclarationNames.getCXXOperatorName(OO_Delete), 724193326Sed Context.VoidTy, VoidPtr); 725193326Sed DeclareGlobalAllocationFunction( 726193326Sed Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), 727193326Sed Context.VoidTy, VoidPtr); 728193326Sed} 729193326Sed 730193326Sed/// DeclareGlobalAllocationFunction - Declares a single implicit global 731193326Sed/// allocation function if it doesn't already exist. 732193326Sedvoid Sema::DeclareGlobalAllocationFunction(DeclarationName Name, 733198092Srdivacky QualType Return, QualType Argument) { 734193326Sed DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); 735193326Sed 736193326Sed // Check if this function is already declared. 737193326Sed { 738193326Sed DeclContext::lookup_iterator Alloc, AllocEnd; 739195341Sed for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); 740193326Sed Alloc != AllocEnd; ++Alloc) { 741193326Sed // FIXME: Do we need to check for default arguments here? 742193326Sed FunctionDecl *Func = cast<FunctionDecl>(*Alloc); 743193326Sed if (Func->getNumParams() == 1 && 744193326Sed Context.getCanonicalType(Func->getParamDecl(0)->getType())==Argument) 745193326Sed return; 746193326Sed } 747193326Sed } 748193326Sed 749198092Srdivacky QualType BadAllocType; 750198092Srdivacky bool HasBadAllocExceptionSpec 751198092Srdivacky = (Name.getCXXOverloadedOperator() == OO_New || 752198092Srdivacky Name.getCXXOverloadedOperator() == OO_Array_New); 753198092Srdivacky if (HasBadAllocExceptionSpec) { 754198092Srdivacky assert(StdBadAlloc && "Must have std::bad_alloc declared"); 755198092Srdivacky BadAllocType = Context.getTypeDeclType(StdBadAlloc); 756198092Srdivacky } 757198092Srdivacky 758198092Srdivacky QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0, 759198092Srdivacky true, false, 760198092Srdivacky HasBadAllocExceptionSpec? 1 : 0, 761198092Srdivacky &BadAllocType); 762193326Sed FunctionDecl *Alloc = 763193326Sed FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, 764198092Srdivacky FnType, /*DInfo=*/0, FunctionDecl::None, false, true); 765193326Sed Alloc->setImplicit(); 766193326Sed ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), 767198092Srdivacky 0, Argument, /*DInfo=*/0, 768198092Srdivacky VarDecl::None, 0); 769193326Sed Alloc->setParams(Context, &Param, 1); 770193326Sed 771193326Sed // FIXME: Also add this declaration to the IdentifierResolver, but 772193326Sed // make sure it is at the end of the chain to coincide with the 773193326Sed // global scope. 774195341Sed ((DeclContext *)TUScope->getEntity())->addDecl(Alloc); 775193326Sed} 776193326Sed 777199482Srdivackybool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, 778199482Srdivacky DeclarationName Name, 779199482Srdivacky FunctionDecl* &Operator) { 780199482Srdivacky LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); 781199482Srdivacky // Try to find operator delete/operator delete[] in class scope. 782199482Srdivacky LookupQualifiedName(Found, RD); 783199482Srdivacky 784199482Srdivacky if (Found.isAmbiguous()) 785199482Srdivacky return true; 786199482Srdivacky 787199482Srdivacky for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); 788199482Srdivacky F != FEnd; ++F) { 789199482Srdivacky if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F)) 790199482Srdivacky if (Delete->isUsualDeallocationFunction()) { 791199482Srdivacky Operator = Delete; 792199482Srdivacky return false; 793199482Srdivacky } 794199482Srdivacky } 795199482Srdivacky 796199482Srdivacky // We did find operator delete/operator delete[] declarations, but 797199482Srdivacky // none of them were suitable. 798199482Srdivacky if (!Found.empty()) { 799199482Srdivacky Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) 800199482Srdivacky << Name << RD; 801199482Srdivacky 802199482Srdivacky for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); 803199482Srdivacky F != FEnd; ++F) { 804199482Srdivacky Diag((*F)->getLocation(), 805199482Srdivacky diag::note_delete_member_function_declared_here) 806199482Srdivacky << Name; 807199482Srdivacky } 808199482Srdivacky 809199482Srdivacky return true; 810199482Srdivacky } 811199482Srdivacky 812199482Srdivacky // Look for a global declaration. 813199482Srdivacky DeclareGlobalNewDelete(); 814199482Srdivacky DeclContext *TUDecl = Context.getTranslationUnitDecl(); 815199482Srdivacky 816199482Srdivacky CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); 817199482Srdivacky Expr* DeallocArgs[1]; 818199482Srdivacky DeallocArgs[0] = &Null; 819199482Srdivacky if (FindAllocationOverload(StartLoc, SourceRange(), Name, 820199482Srdivacky DeallocArgs, 1, TUDecl, /*AllowMissing=*/false, 821199482Srdivacky Operator)) 822199482Srdivacky return true; 823199482Srdivacky 824199482Srdivacky assert(Operator && "Did not find a deallocation function!"); 825199482Srdivacky return false; 826199482Srdivacky} 827199482Srdivacky 828193326Sed/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: 829193326Sed/// @code ::delete ptr; @endcode 830193326Sed/// or 831193326Sed/// @code delete [] ptr; @endcode 832193326SedAction::OwningExprResult 833193326SedSema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, 834198092Srdivacky bool ArrayForm, ExprArg Operand) { 835198092Srdivacky // C++ [expr.delete]p1: 836198092Srdivacky // The operand shall have a pointer type, or a class type having a single 837198092Srdivacky // conversion function to a pointer type. The result has type void. 838198092Srdivacky // 839193326Sed // DR599 amends "pointer type" to "pointer to object type" in both cases. 840193326Sed 841198092Srdivacky FunctionDecl *OperatorDelete = 0; 842198092Srdivacky 843193326Sed Expr *Ex = (Expr *)Operand.get(); 844193326Sed if (!Ex->isTypeDependent()) { 845193326Sed QualType Type = Ex->getType(); 846193326Sed 847198092Srdivacky if (const RecordType *Record = Type->getAs<RecordType>()) { 848198092Srdivacky llvm::SmallVector<CXXConversionDecl *, 4> ObjectPtrConversions; 849198092Srdivacky CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); 850199990Srdivacky const UnresolvedSet *Conversions = RD->getVisibleConversionFunctions(); 851198092Srdivacky 852199990Srdivacky for (UnresolvedSet::iterator I = Conversions->begin(), 853199990Srdivacky E = Conversions->end(); I != E; ++I) { 854198092Srdivacky // Skip over templated conversion functions; they aren't considered. 855199990Srdivacky if (isa<FunctionTemplateDecl>(*I)) 856198092Srdivacky continue; 857198092Srdivacky 858199990Srdivacky CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); 859198092Srdivacky 860198092Srdivacky QualType ConvType = Conv->getConversionType().getNonReferenceType(); 861198092Srdivacky if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) 862198092Srdivacky if (ConvPtrType->getPointeeType()->isObjectType()) 863198092Srdivacky ObjectPtrConversions.push_back(Conv); 864198092Srdivacky } 865198092Srdivacky if (ObjectPtrConversions.size() == 1) { 866198092Srdivacky // We have a single conversion to a pointer-to-object type. Perform 867198092Srdivacky // that conversion. 868198092Srdivacky Operand.release(); 869198092Srdivacky if (!PerformImplicitConversion(Ex, 870198092Srdivacky ObjectPtrConversions.front()->getConversionType(), 871198092Srdivacky "converting")) { 872198092Srdivacky Operand = Owned(Ex); 873198092Srdivacky Type = Ex->getType(); 874198092Srdivacky } 875198092Srdivacky } 876198092Srdivacky else if (ObjectPtrConversions.size() > 1) { 877198092Srdivacky Diag(StartLoc, diag::err_ambiguous_delete_operand) 878198092Srdivacky << Type << Ex->getSourceRange(); 879198092Srdivacky for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) { 880198092Srdivacky CXXConversionDecl *Conv = ObjectPtrConversions[i]; 881198092Srdivacky Diag(Conv->getLocation(), diag::err_ovl_candidate); 882198092Srdivacky } 883198092Srdivacky return ExprError(); 884198092Srdivacky } 885193326Sed } 886193326Sed 887193326Sed if (!Type->isPointerType()) 888193326Sed return ExprError(Diag(StartLoc, diag::err_delete_operand) 889193326Sed << Type << Ex->getSourceRange()); 890193326Sed 891198092Srdivacky QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); 892193326Sed if (Pointee->isFunctionType() || Pointee->isVoidType()) 893193326Sed return ExprError(Diag(StartLoc, diag::err_delete_operand) 894193326Sed << Type << Ex->getSourceRange()); 895193326Sed else if (!Pointee->isDependentType() && 896198092Srdivacky RequireCompleteType(StartLoc, Pointee, 897198092Srdivacky PDiag(diag::warn_delete_incomplete) 898198092Srdivacky << Ex->getSourceRange())) 899193326Sed return ExprError(); 900193326Sed 901198092Srdivacky // C++ [expr.delete]p2: 902198092Srdivacky // [Note: a pointer to a const type can be the operand of a 903198092Srdivacky // delete-expression; it is not necessary to cast away the constness 904198092Srdivacky // (5.2.11) of the pointer expression before it is used as the operand 905198092Srdivacky // of the delete-expression. ] 906198092Srdivacky ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy), 907198092Srdivacky CastExpr::CK_NoOp); 908198092Srdivacky 909198092Srdivacky // Update the operand. 910198092Srdivacky Operand.take(); 911198092Srdivacky Operand = ExprArg(*this, Ex); 912198092Srdivacky 913198092Srdivacky DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( 914198092Srdivacky ArrayForm ? OO_Array_Delete : OO_Delete); 915198092Srdivacky 916199482Srdivacky if (const RecordType *RT = Pointee->getAs<RecordType>()) { 917199482Srdivacky CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 918199482Srdivacky 919199482Srdivacky if (!UseGlobal && 920199482Srdivacky FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete)) 921199482Srdivacky return ExprError(); 922198092Srdivacky 923199482Srdivacky if (!RD->hasTrivialDestructor()) 924199482Srdivacky if (const CXXDestructorDecl *Dtor = RD->getDestructor(Context)) 925198092Srdivacky MarkDeclarationReferenced(StartLoc, 926198092Srdivacky const_cast<CXXDestructorDecl*>(Dtor)); 927198092Srdivacky } 928199482Srdivacky 929198092Srdivacky if (!OperatorDelete) { 930199482Srdivacky // Look for a global declaration. 931198092Srdivacky DeclareGlobalNewDelete(); 932198092Srdivacky DeclContext *TUDecl = Context.getTranslationUnitDecl(); 933198092Srdivacky if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, 934198092Srdivacky &Ex, 1, TUDecl, /*AllowMissing=*/false, 935198092Srdivacky OperatorDelete)) 936198092Srdivacky return ExprError(); 937198092Srdivacky } 938198092Srdivacky 939193326Sed // FIXME: Check access and ambiguity of operator delete and destructor. 940193326Sed } 941193326Sed 942193326Sed Operand.release(); 943193326Sed return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 944198092Srdivacky OperatorDelete, Ex, StartLoc)); 945193326Sed} 946193326Sed 947199990Srdivacky/// \brief Check the use of the given variable as a C++ condition in an if, 948199990Srdivacky/// while, do-while, or switch statement. 949199990SrdivackyAction::OwningExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar) { 950199990Srdivacky QualType T = ConditionVar->getType(); 951199990Srdivacky 952199990Srdivacky // C++ [stmt.select]p2: 953199990Srdivacky // The declarator shall not specify a function or an array. 954199990Srdivacky if (T->isFunctionType()) 955199990Srdivacky return ExprError(Diag(ConditionVar->getLocation(), 956199990Srdivacky diag::err_invalid_use_of_function_type) 957199990Srdivacky << ConditionVar->getSourceRange()); 958199990Srdivacky else if (T->isArrayType()) 959199990Srdivacky return ExprError(Diag(ConditionVar->getLocation(), 960199990Srdivacky diag::err_invalid_use_of_array_type) 961199990Srdivacky << ConditionVar->getSourceRange()); 962193326Sed 963199990Srdivacky return Owned(DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar, 964199990Srdivacky ConditionVar->getLocation(), 965199990Srdivacky ConditionVar->getType().getNonReferenceType())); 966193326Sed} 967193326Sed 968193326Sed/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. 969193326Sedbool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { 970193326Sed // C++ 6.4p4: 971193326Sed // The value of a condition that is an initialized declaration in a statement 972193326Sed // other than a switch statement is the value of the declared variable 973193326Sed // implicitly converted to type bool. If that conversion is ill-formed, the 974193326Sed // program is ill-formed. 975193326Sed // The value of a condition that is an expression is the value of the 976193326Sed // expression, implicitly converted to bool. 977193326Sed // 978193326Sed return PerformContextuallyConvertToBool(CondExpr); 979193326Sed} 980193326Sed 981193326Sed/// Helper function to determine whether this is the (deprecated) C++ 982193326Sed/// conversion from a string literal to a pointer to non-const char or 983193326Sed/// non-const wchar_t (for narrow and wide string literals, 984193326Sed/// respectively). 985198092Srdivackybool 986193326SedSema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { 987193326Sed // Look inside the implicit cast, if it exists. 988193326Sed if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) 989193326Sed From = Cast->getSubExpr(); 990193326Sed 991193326Sed // A string literal (2.13.4) that is not a wide string literal can 992193326Sed // be converted to an rvalue of type "pointer to char"; a wide 993193326Sed // string literal can be converted to an rvalue of type "pointer 994193326Sed // to wchar_t" (C++ 4.2p2). 995193326Sed if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) 996198092Srdivacky if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) 997198092Srdivacky if (const BuiltinType *ToPointeeType 998198092Srdivacky = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { 999193326Sed // This conversion is considered only when there is an 1000193326Sed // explicit appropriate pointer target type (C++ 4.2p2). 1001198092Srdivacky if (!ToPtrType->getPointeeType().hasQualifiers() && 1002193326Sed ((StrLit->isWide() && ToPointeeType->isWideCharType()) || 1003193326Sed (!StrLit->isWide() && 1004193326Sed (ToPointeeType->getKind() == BuiltinType::Char_U || 1005193326Sed ToPointeeType->getKind() == BuiltinType::Char_S)))) 1006193326Sed return true; 1007193326Sed } 1008193326Sed 1009193326Sed return false; 1010193326Sed} 1011193326Sed 1012193326Sed/// PerformImplicitConversion - Perform an implicit conversion of the 1013193326Sed/// expression From to the type ToType. Returns true if there was an 1014193326Sed/// error, false otherwise. The expression From is replaced with the 1015193326Sed/// converted expression. Flavor is the kind of conversion we're 1016193326Sed/// performing, used in the error message. If @p AllowExplicit, 1017193326Sed/// explicit user-defined conversions are permitted. @p Elidable should be true 1018193326Sed/// when called for copies which may be elided (C++ 12.8p15). C++0x overload 1019193326Sed/// resolution works differently in that case. 1020193326Sedbool 1021193326SedSema::PerformImplicitConversion(Expr *&From, QualType ToType, 1022193326Sed const char *Flavor, bool AllowExplicit, 1023198092Srdivacky bool Elidable) { 1024193326Sed ImplicitConversionSequence ICS; 1025198092Srdivacky return PerformImplicitConversion(From, ToType, Flavor, AllowExplicit, 1026198092Srdivacky Elidable, ICS); 1027198092Srdivacky} 1028198092Srdivacky 1029198092Srdivackybool 1030198092SrdivackySema::PerformImplicitConversion(Expr *&From, QualType ToType, 1031198092Srdivacky const char *Flavor, bool AllowExplicit, 1032198092Srdivacky bool Elidable, 1033198092Srdivacky ImplicitConversionSequence& ICS) { 1034193326Sed ICS.ConversionKind = ImplicitConversionSequence::BadConversion; 1035193326Sed if (Elidable && getLangOptions().CPlusPlus0x) { 1036198092Srdivacky ICS = TryImplicitConversion(From, ToType, 1037198092Srdivacky /*SuppressUserConversions=*/false, 1038198092Srdivacky AllowExplicit, 1039198092Srdivacky /*ForceRValue=*/true, 1040198092Srdivacky /*InOverloadResolution=*/false); 1041193326Sed } 1042193326Sed if (ICS.ConversionKind == ImplicitConversionSequence::BadConversion) { 1043198092Srdivacky ICS = TryImplicitConversion(From, ToType, 1044198092Srdivacky /*SuppressUserConversions=*/false, 1045198092Srdivacky AllowExplicit, 1046198092Srdivacky /*ForceRValue=*/false, 1047198092Srdivacky /*InOverloadResolution=*/false); 1048193326Sed } 1049193326Sed return PerformImplicitConversion(From, ToType, ICS, Flavor); 1050193326Sed} 1051193326Sed 1052198398Srdivacky/// BuildCXXDerivedToBaseExpr - This routine generates the suitable AST 1053198398Srdivacky/// for the derived to base conversion of the expression 'From'. All 1054198398Srdivacky/// necessary information is passed in ICS. 1055198398Srdivackybool 1056198398SrdivackySema::BuildCXXDerivedToBaseExpr(Expr *&From, CastExpr::CastKind CastKind, 1057198398Srdivacky const ImplicitConversionSequence& ICS, 1058198398Srdivacky const char *Flavor) { 1059198398Srdivacky QualType BaseType = 1060198398Srdivacky QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr); 1061198398Srdivacky // Must do additional defined to base conversion. 1062198398Srdivacky QualType DerivedType = 1063198398Srdivacky QualType::getFromOpaquePtr(ICS.UserDefined.After.FromTypePtr); 1064198398Srdivacky 1065198398Srdivacky From = new (Context) ImplicitCastExpr( 1066198398Srdivacky DerivedType.getNonReferenceType(), 1067198398Srdivacky CastKind, 1068198398Srdivacky From, 1069198398Srdivacky DerivedType->isLValueReferenceType()); 1070198398Srdivacky From = new (Context) ImplicitCastExpr(BaseType.getNonReferenceType(), 1071198398Srdivacky CastExpr::CK_DerivedToBase, From, 1072198398Srdivacky BaseType->isLValueReferenceType()); 1073198398Srdivacky ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); 1074198398Srdivacky OwningExprResult FromResult = 1075198398Srdivacky BuildCXXConstructExpr( 1076198398Srdivacky ICS.UserDefined.After.CopyConstructor->getLocation(), 1077198398Srdivacky BaseType, 1078198398Srdivacky ICS.UserDefined.After.CopyConstructor, 1079198398Srdivacky MultiExprArg(*this, (void **)&From, 1)); 1080198398Srdivacky if (FromResult.isInvalid()) 1081198398Srdivacky return true; 1082198398Srdivacky From = FromResult.takeAs<Expr>(); 1083198398Srdivacky return false; 1084198398Srdivacky} 1085198398Srdivacky 1086193326Sed/// PerformImplicitConversion - Perform an implicit conversion of the 1087193326Sed/// expression From to the type ToType using the pre-computed implicit 1088193326Sed/// conversion sequence ICS. Returns true if there was an error, false 1089193326Sed/// otherwise. The expression From is replaced with the converted 1090193326Sed/// expression. Flavor is the kind of conversion we're performing, 1091193326Sed/// used in the error message. 1092193326Sedbool 1093193326SedSema::PerformImplicitConversion(Expr *&From, QualType ToType, 1094193326Sed const ImplicitConversionSequence &ICS, 1095199482Srdivacky const char* Flavor, bool IgnoreBaseAccess) { 1096193326Sed switch (ICS.ConversionKind) { 1097193326Sed case ImplicitConversionSequence::StandardConversion: 1098199482Srdivacky if (PerformImplicitConversion(From, ToType, ICS.Standard, Flavor, 1099199482Srdivacky IgnoreBaseAccess)) 1100193326Sed return true; 1101193326Sed break; 1102193326Sed 1103198092Srdivacky case ImplicitConversionSequence::UserDefinedConversion: { 1104198092Srdivacky 1105198092Srdivacky FunctionDecl *FD = ICS.UserDefined.ConversionFunction; 1106198092Srdivacky CastExpr::CastKind CastKind = CastExpr::CK_Unknown; 1107198092Srdivacky QualType BeforeToType; 1108198092Srdivacky if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { 1109198092Srdivacky CastKind = CastExpr::CK_UserDefinedConversion; 1110198092Srdivacky 1111198092Srdivacky // If the user-defined conversion is specified by a conversion function, 1112198092Srdivacky // the initial standard conversion sequence converts the source type to 1113198092Srdivacky // the implicit object parameter of the conversion function. 1114198092Srdivacky BeforeToType = Context.getTagDeclType(Conv->getParent()); 1115198092Srdivacky } else if (const CXXConstructorDecl *Ctor = 1116198092Srdivacky dyn_cast<CXXConstructorDecl>(FD)) { 1117198092Srdivacky CastKind = CastExpr::CK_ConstructorConversion; 1118199482Srdivacky // Do no conversion if dealing with ... for the first conversion. 1119199990Srdivacky if (!ICS.UserDefined.EllipsisConversion) { 1120199482Srdivacky // If the user-defined conversion is specified by a constructor, the 1121199482Srdivacky // initial standard conversion sequence converts the source type to the 1122199482Srdivacky // type required by the argument of the constructor 1123199990Srdivacky BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); 1124199990Srdivacky } 1125198092Srdivacky } 1126198092Srdivacky else 1127198092Srdivacky assert(0 && "Unknown conversion function kind!"); 1128199482Srdivacky // Whatch out for elipsis conversion. 1129199482Srdivacky if (!ICS.UserDefined.EllipsisConversion) { 1130199482Srdivacky if (PerformImplicitConversion(From, BeforeToType, 1131199482Srdivacky ICS.UserDefined.Before, "converting", 1132199482Srdivacky IgnoreBaseAccess)) 1133199482Srdivacky return true; 1134199482Srdivacky } 1135198092Srdivacky 1136198092Srdivacky OwningExprResult CastArg 1137198092Srdivacky = BuildCXXCastArgument(From->getLocStart(), 1138198092Srdivacky ToType.getNonReferenceType(), 1139198092Srdivacky CastKind, cast<CXXMethodDecl>(FD), 1140198092Srdivacky Owned(From)); 1141198092Srdivacky 1142198092Srdivacky if (CastArg.isInvalid()) 1143198092Srdivacky return true; 1144199990Srdivacky 1145199990Srdivacky From = CastArg.takeAs<Expr>(); 1146199990Srdivacky 1147199990Srdivacky // FIXME: This and the following if statement shouldn't be necessary, but 1148199990Srdivacky // there's some nasty stuff involving MaybeBindToTemporary going on here. 1149198398Srdivacky if (ICS.UserDefined.After.Second == ICK_Derived_To_Base && 1150198398Srdivacky ICS.UserDefined.After.CopyConstructor) { 1151198398Srdivacky return BuildCXXDerivedToBaseExpr(From, CastKind, ICS, Flavor); 1152198398Srdivacky } 1153199990Srdivacky 1154199990Srdivacky if (ICS.UserDefined.After.CopyConstructor) { 1155199990Srdivacky From = new (Context) ImplicitCastExpr(ToType.getNonReferenceType(), 1156199990Srdivacky CastKind, From, 1157199990Srdivacky ToType->isLValueReferenceType()); 1158199990Srdivacky return false; 1159199990Srdivacky } 1160199990Srdivacky 1161199990Srdivacky return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, 1162199990Srdivacky "converting", IgnoreBaseAccess); 1163198398Srdivacky } 1164198398Srdivacky 1165193326Sed case ImplicitConversionSequence::EllipsisConversion: 1166193326Sed assert(false && "Cannot perform an ellipsis conversion"); 1167193326Sed return false; 1168193326Sed 1169193326Sed case ImplicitConversionSequence::BadConversion: 1170193326Sed return true; 1171193326Sed } 1172193326Sed 1173193326Sed // Everything went well. 1174193326Sed return false; 1175193326Sed} 1176193326Sed 1177193326Sed/// PerformImplicitConversion - Perform an implicit conversion of the 1178193326Sed/// expression From to the type ToType by following the standard 1179193326Sed/// conversion sequence SCS. Returns true if there was an error, false 1180193326Sed/// otherwise. The expression From is replaced with the converted 1181193326Sed/// expression. Flavor is the context in which we're performing this 1182193326Sed/// conversion, for use in error messages. 1183198092Srdivackybool 1184193326SedSema::PerformImplicitConversion(Expr *&From, QualType ToType, 1185193326Sed const StandardConversionSequence& SCS, 1186199482Srdivacky const char *Flavor, bool IgnoreBaseAccess) { 1187193326Sed // Overall FIXME: we are recomputing too many types here and doing far too 1188193326Sed // much extra work. What this means is that we need to keep track of more 1189193326Sed // information that is computed when we try the implicit conversion initially, 1190193326Sed // so that we don't need to recompute anything here. 1191193326Sed QualType FromType = From->getType(); 1192193326Sed 1193193326Sed if (SCS.CopyConstructor) { 1194193326Sed // FIXME: When can ToType be a reference type? 1195193326Sed assert(!ToType->isReferenceType()); 1196198092Srdivacky if (SCS.Second == ICK_Derived_To_Base) { 1197198092Srdivacky ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); 1198198092Srdivacky if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), 1199198092Srdivacky MultiExprArg(*this, (void **)&From, 1), 1200198092Srdivacky /*FIXME:ConstructLoc*/SourceLocation(), 1201198092Srdivacky ConstructorArgs)) 1202198092Srdivacky return true; 1203198092Srdivacky OwningExprResult FromResult = 1204198092Srdivacky BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), 1205198092Srdivacky ToType, SCS.CopyConstructor, 1206198092Srdivacky move_arg(ConstructorArgs)); 1207198092Srdivacky if (FromResult.isInvalid()) 1208198092Srdivacky return true; 1209198092Srdivacky From = FromResult.takeAs<Expr>(); 1210198092Srdivacky return false; 1211198092Srdivacky } 1212198092Srdivacky OwningExprResult FromResult = 1213198092Srdivacky BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), 1214198092Srdivacky ToType, SCS.CopyConstructor, 1215198092Srdivacky MultiExprArg(*this, (void**)&From, 1)); 1216198092Srdivacky 1217198092Srdivacky if (FromResult.isInvalid()) 1218198092Srdivacky return true; 1219198092Srdivacky 1220198092Srdivacky From = FromResult.takeAs<Expr>(); 1221193326Sed return false; 1222193326Sed } 1223193326Sed 1224193326Sed // Perform the first implicit conversion. 1225193326Sed switch (SCS.First) { 1226193326Sed case ICK_Identity: 1227193326Sed case ICK_Lvalue_To_Rvalue: 1228193326Sed // Nothing to do. 1229193326Sed break; 1230193326Sed 1231193326Sed case ICK_Array_To_Pointer: 1232193326Sed FromType = Context.getArrayDecayedType(FromType); 1233198092Srdivacky ImpCastExprToType(From, FromType, CastExpr::CK_ArrayToPointerDecay); 1234193326Sed break; 1235193326Sed 1236193326Sed case ICK_Function_To_Pointer: 1237193326Sed if (Context.getCanonicalType(FromType) == Context.OverloadTy) { 1238193326Sed FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); 1239193326Sed if (!Fn) 1240193326Sed return true; 1241193326Sed 1242193326Sed if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) 1243193326Sed return true; 1244193326Sed 1245198398Srdivacky From = FixOverloadedFunctionReference(From, Fn); 1246193326Sed FromType = From->getType(); 1247198398Srdivacky 1248198398Srdivacky // If there's already an address-of operator in the expression, we have 1249198398Srdivacky // the right type already, and the code below would just introduce an 1250198398Srdivacky // invalid additional pointer level. 1251198398Srdivacky if (FromType->isPointerType() || FromType->isMemberFunctionPointerType()) 1252198398Srdivacky break; 1253193326Sed } 1254193326Sed FromType = Context.getPointerType(FromType); 1255198092Srdivacky ImpCastExprToType(From, FromType, CastExpr::CK_FunctionToPointerDecay); 1256193326Sed break; 1257193326Sed 1258193326Sed default: 1259193326Sed assert(false && "Improper first standard conversion"); 1260193326Sed break; 1261193326Sed } 1262193326Sed 1263193326Sed // Perform the second implicit conversion 1264193326Sed switch (SCS.Second) { 1265193326Sed case ICK_Identity: 1266198092Srdivacky // If both sides are functions (or pointers/references to them), there could 1267198092Srdivacky // be incompatible exception declarations. 1268198092Srdivacky if (CheckExceptionSpecCompatibility(From, ToType)) 1269198092Srdivacky return true; 1270198092Srdivacky // Nothing else to do. 1271193326Sed break; 1272193326Sed 1273193326Sed case ICK_Integral_Promotion: 1274198398Srdivacky case ICK_Integral_Conversion: 1275198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_IntegralCast); 1276198398Srdivacky break; 1277198398Srdivacky 1278193326Sed case ICK_Floating_Promotion: 1279198398Srdivacky case ICK_Floating_Conversion: 1280198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_FloatingCast); 1281198398Srdivacky break; 1282198398Srdivacky 1283193326Sed case ICK_Complex_Promotion: 1284193326Sed case ICK_Complex_Conversion: 1285198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); 1286198398Srdivacky break; 1287198398Srdivacky 1288193326Sed case ICK_Floating_Integral: 1289198398Srdivacky if (ToType->isFloatingType()) 1290198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_IntegralToFloating); 1291198398Srdivacky else 1292198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_FloatingToIntegral); 1293198398Srdivacky break; 1294198398Srdivacky 1295193326Sed case ICK_Complex_Real: 1296198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); 1297198398Srdivacky break; 1298198398Srdivacky 1299193326Sed case ICK_Compatible_Conversion: 1300198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_NoOp); 1301193326Sed break; 1302193326Sed 1303198092Srdivacky case ICK_Pointer_Conversion: { 1304193326Sed if (SCS.IncompatibleObjC) { 1305193326Sed // Diagnose incompatible Objective-C conversions 1306198092Srdivacky Diag(From->getSourceRange().getBegin(), 1307193326Sed diag::ext_typecheck_convert_incompatible_pointer) 1308193326Sed << From->getType() << ToType << Flavor 1309193326Sed << From->getSourceRange(); 1310193326Sed } 1311193326Sed 1312198092Srdivacky 1313198092Srdivacky CastExpr::CastKind Kind = CastExpr::CK_Unknown; 1314199482Srdivacky if (CheckPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) 1315193326Sed return true; 1316198092Srdivacky ImpCastExprToType(From, ToType, Kind); 1317193326Sed break; 1318198092Srdivacky } 1319198092Srdivacky 1320198092Srdivacky case ICK_Pointer_Member: { 1321198092Srdivacky CastExpr::CastKind Kind = CastExpr::CK_Unknown; 1322199482Srdivacky if (CheckMemberPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) 1323193326Sed return true; 1324198092Srdivacky if (CheckExceptionSpecCompatibility(From, ToType)) 1325198092Srdivacky return true; 1326198092Srdivacky ImpCastExprToType(From, ToType, Kind); 1327193326Sed break; 1328198092Srdivacky } 1329199990Srdivacky case ICK_Boolean_Conversion: { 1330199990Srdivacky CastExpr::CastKind Kind = CastExpr::CK_Unknown; 1331199990Srdivacky if (FromType->isMemberPointerType()) 1332199990Srdivacky Kind = CastExpr::CK_MemberPointerToBoolean; 1333199990Srdivacky 1334199990Srdivacky ImpCastExprToType(From, Context.BoolTy, Kind); 1335193326Sed break; 1336199990Srdivacky } 1337193326Sed 1338199482Srdivacky case ICK_Derived_To_Base: 1339199482Srdivacky if (CheckDerivedToBaseConversion(From->getType(), 1340199482Srdivacky ToType.getNonReferenceType(), 1341199482Srdivacky From->getLocStart(), 1342199482Srdivacky From->getSourceRange(), 1343199482Srdivacky IgnoreBaseAccess)) 1344199482Srdivacky return true; 1345199482Srdivacky ImpCastExprToType(From, ToType.getNonReferenceType(), 1346199482Srdivacky CastExpr::CK_DerivedToBase); 1347199482Srdivacky break; 1348199482Srdivacky 1349193326Sed default: 1350193326Sed assert(false && "Improper second standard conversion"); 1351193326Sed break; 1352193326Sed } 1353193326Sed 1354193326Sed switch (SCS.Third) { 1355193326Sed case ICK_Identity: 1356193326Sed // Nothing to do. 1357193326Sed break; 1358193326Sed 1359193326Sed case ICK_Qualification: 1360193326Sed // FIXME: Not sure about lvalue vs rvalue here in the presence of rvalue 1361193326Sed // references. 1362198092Srdivacky ImpCastExprToType(From, ToType.getNonReferenceType(), 1363198398Srdivacky CastExpr::CK_NoOp, 1364193326Sed ToType->isLValueReferenceType()); 1365193326Sed break; 1366199482Srdivacky 1367193326Sed default: 1368193326Sed assert(false && "Improper second standard conversion"); 1369193326Sed break; 1370193326Sed } 1371193326Sed 1372193326Sed return false; 1373193326Sed} 1374193326Sed 1375193326SedSema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, 1376193326Sed SourceLocation KWLoc, 1377193326Sed SourceLocation LParen, 1378193326Sed TypeTy *Ty, 1379193326Sed SourceLocation RParen) { 1380198092Srdivacky QualType T = GetTypeFromParser(Ty); 1381193326Sed 1382198092Srdivacky // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html 1383198092Srdivacky // all traits except __is_class, __is_enum and __is_union require a the type 1384198092Srdivacky // to be complete. 1385198092Srdivacky if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) { 1386198092Srdivacky if (RequireCompleteType(KWLoc, T, 1387198092Srdivacky diag::err_incomplete_type_used_in_type_trait_expr)) 1388198092Srdivacky return ExprError(); 1389198092Srdivacky } 1390198092Srdivacky 1391193326Sed // There is no point in eagerly computing the value. The traits are designed 1392193326Sed // to be used from type trait templates, so Ty will be a template parameter 1393193326Sed // 99% of the time. 1394198092Srdivacky return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T, 1395198092Srdivacky RParen, Context.BoolTy)); 1396193326Sed} 1397193326Sed 1398193326SedQualType Sema::CheckPointerToMemberOperands( 1399198092Srdivacky Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) { 1400193326Sed const char *OpSpelling = isIndirect ? "->*" : ".*"; 1401193326Sed // C++ 5.5p2 1402193326Sed // The binary operator .* [p3: ->*] binds its second operand, which shall 1403193326Sed // be of type "pointer to member of T" (where T is a completely-defined 1404193326Sed // class type) [...] 1405193326Sed QualType RType = rex->getType(); 1406198092Srdivacky const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); 1407193326Sed if (!MemPtr) { 1408193326Sed Diag(Loc, diag::err_bad_memptr_rhs) 1409193326Sed << OpSpelling << RType << rex->getSourceRange(); 1410193326Sed return QualType(); 1411198092Srdivacky } 1412193326Sed 1413193326Sed QualType Class(MemPtr->getClass(), 0); 1414193326Sed 1415193326Sed // C++ 5.5p2 1416193326Sed // [...] to its first operand, which shall be of class T or of a class of 1417193326Sed // which T is an unambiguous and accessible base class. [p3: a pointer to 1418193326Sed // such a class] 1419193326Sed QualType LType = lex->getType(); 1420193326Sed if (isIndirect) { 1421198092Srdivacky if (const PointerType *Ptr = LType->getAs<PointerType>()) 1422193326Sed LType = Ptr->getPointeeType().getNonReferenceType(); 1423193326Sed else { 1424193326Sed Diag(Loc, diag::err_bad_memptr_lhs) 1425198893Srdivacky << OpSpelling << 1 << LType 1426198893Srdivacky << CodeModificationHint::CreateReplacement(SourceRange(Loc), ".*"); 1427193326Sed return QualType(); 1428193326Sed } 1429193326Sed } 1430193326Sed 1431199482Srdivacky if (!Context.hasSameUnqualifiedType(Class, LType)) { 1432198092Srdivacky CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, 1433198092Srdivacky /*DetectVirtual=*/false); 1434193326Sed // FIXME: Would it be useful to print full ambiguity paths, or is that 1435193326Sed // overkill? 1436193326Sed if (!IsDerivedFrom(LType, Class, Paths) || 1437193326Sed Paths.isAmbiguous(Context.getCanonicalType(Class))) { 1438198893Srdivacky const char *ReplaceStr = isIndirect ? ".*" : "->*"; 1439193326Sed Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling 1440198893Srdivacky << (int)isIndirect << lex->getType() << 1441198893Srdivacky CodeModificationHint::CreateReplacement(SourceRange(Loc), ReplaceStr); 1442193326Sed return QualType(); 1443193326Sed } 1444193326Sed } 1445193326Sed 1446199512Srdivacky if (isa<CXXZeroInitValueExpr>(rex->IgnoreParens())) { 1447199512Srdivacky // Diagnose use of pointer-to-member type which when used as 1448199512Srdivacky // the functional cast in a pointer-to-member expression. 1449199512Srdivacky Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; 1450199512Srdivacky return QualType(); 1451199512Srdivacky } 1452193326Sed // C++ 5.5p2 1453193326Sed // The result is an object or a function of the type specified by the 1454193326Sed // second operand. 1455193326Sed // The cv qualifiers are the union of those in the pointer and the left side, 1456193326Sed // in accordance with 5.5p5 and 5.2.5. 1457193326Sed // FIXME: This returns a dereferenced member function pointer as a normal 1458193326Sed // function type. However, the only operation valid on such functions is 1459193326Sed // calling them. There's also a GCC extension to get a function pointer to the 1460193326Sed // thing, which is another complication, because this type - unlike the type 1461193326Sed // that is the result of this expression - takes the class as the first 1462193326Sed // argument. 1463193326Sed // We probably need a "MemberFunctionClosureType" or something like that. 1464193326Sed QualType Result = MemPtr->getPointeeType(); 1465198092Srdivacky Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers()); 1466193326Sed return Result; 1467193326Sed} 1468193326Sed 1469193326Sed/// \brief Get the target type of a standard or user-defined conversion. 1470193326Sedstatic QualType TargetType(const ImplicitConversionSequence &ICS) { 1471193326Sed assert((ICS.ConversionKind == 1472193326Sed ImplicitConversionSequence::StandardConversion || 1473193326Sed ICS.ConversionKind == 1474193326Sed ImplicitConversionSequence::UserDefinedConversion) && 1475193326Sed "function only valid for standard or user-defined conversions"); 1476193326Sed if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion) 1477193326Sed return QualType::getFromOpaquePtr(ICS.Standard.ToTypePtr); 1478193326Sed return QualType::getFromOpaquePtr(ICS.UserDefined.After.ToTypePtr); 1479193326Sed} 1480193326Sed 1481193326Sed/// \brief Try to convert a type to another according to C++0x 5.16p3. 1482193326Sed/// 1483193326Sed/// This is part of the parameter validation for the ? operator. If either 1484193326Sed/// value operand is a class type, the two operands are attempted to be 1485193326Sed/// converted to each other. This function does the conversion in one direction. 1486193326Sed/// It emits a diagnostic and returns true only if it finds an ambiguous 1487193326Sed/// conversion. 1488193326Sedstatic bool TryClassUnification(Sema &Self, Expr *From, Expr *To, 1489193326Sed SourceLocation QuestionLoc, 1490198092Srdivacky ImplicitConversionSequence &ICS) { 1491193326Sed // C++0x 5.16p3 1492193326Sed // The process for determining whether an operand expression E1 of type T1 1493193326Sed // can be converted to match an operand expression E2 of type T2 is defined 1494193326Sed // as follows: 1495193326Sed // -- If E2 is an lvalue: 1496193326Sed if (To->isLvalue(Self.Context) == Expr::LV_Valid) { 1497193326Sed // E1 can be converted to match E2 if E1 can be implicitly converted to 1498193326Sed // type "lvalue reference to T2", subject to the constraint that in the 1499193326Sed // conversion the reference must bind directly to E1. 1500193326Sed if (!Self.CheckReferenceInit(From, 1501193326Sed Self.Context.getLValueReferenceType(To->getType()), 1502198092Srdivacky To->getLocStart(), 1503198092Srdivacky /*SuppressUserConversions=*/false, 1504198092Srdivacky /*AllowExplicit=*/false, 1505198092Srdivacky /*ForceRValue=*/false, 1506198092Srdivacky &ICS)) 1507193326Sed { 1508193326Sed assert((ICS.ConversionKind == 1509193326Sed ImplicitConversionSequence::StandardConversion || 1510193326Sed ICS.ConversionKind == 1511193326Sed ImplicitConversionSequence::UserDefinedConversion) && 1512193326Sed "expected a definite conversion"); 1513193326Sed bool DirectBinding = 1514193326Sed ICS.ConversionKind == ImplicitConversionSequence::StandardConversion ? 1515193326Sed ICS.Standard.DirectBinding : ICS.UserDefined.After.DirectBinding; 1516193326Sed if (DirectBinding) 1517193326Sed return false; 1518193326Sed } 1519193326Sed } 1520193326Sed ICS.ConversionKind = ImplicitConversionSequence::BadConversion; 1521193326Sed // -- If E2 is an rvalue, or if the conversion above cannot be done: 1522193326Sed // -- if E1 and E2 have class type, and the underlying class types are 1523193326Sed // the same or one is a base class of the other: 1524193326Sed QualType FTy = From->getType(); 1525193326Sed QualType TTy = To->getType(); 1526198092Srdivacky const RecordType *FRec = FTy->getAs<RecordType>(); 1527198092Srdivacky const RecordType *TRec = TTy->getAs<RecordType>(); 1528193326Sed bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy); 1529193326Sed if (FRec && TRec && (FRec == TRec || 1530193326Sed FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { 1531193326Sed // E1 can be converted to match E2 if the class of T2 is the 1532193326Sed // same type as, or a base class of, the class of T1, and 1533193326Sed // [cv2 > cv1]. 1534193326Sed if ((FRec == TRec || FDerivedFromT) && TTy.isAtLeastAsQualifiedAs(FTy)) { 1535193326Sed // Could still fail if there's no copy constructor. 1536193326Sed // FIXME: Is this a hard error then, or just a conversion failure? The 1537193326Sed // standard doesn't say. 1538198092Srdivacky ICS = Self.TryCopyInitialization(From, TTy, 1539198092Srdivacky /*SuppressUserConversions=*/false, 1540198092Srdivacky /*ForceRValue=*/false, 1541198092Srdivacky /*InOverloadResolution=*/false); 1542193326Sed } 1543193326Sed } else { 1544193326Sed // -- Otherwise: E1 can be converted to match E2 if E1 can be 1545193326Sed // implicitly converted to the type that expression E2 would have 1546193326Sed // if E2 were converted to an rvalue. 1547193326Sed // First find the decayed type. 1548193326Sed if (TTy->isFunctionType()) 1549193326Sed TTy = Self.Context.getPointerType(TTy); 1550198092Srdivacky else if (TTy->isArrayType()) 1551193326Sed TTy = Self.Context.getArrayDecayedType(TTy); 1552193326Sed 1553193326Sed // Now try the implicit conversion. 1554193326Sed // FIXME: This doesn't detect ambiguities. 1555198092Srdivacky ICS = Self.TryImplicitConversion(From, TTy, 1556198092Srdivacky /*SuppressUserConversions=*/false, 1557198092Srdivacky /*AllowExplicit=*/false, 1558198092Srdivacky /*ForceRValue=*/false, 1559198092Srdivacky /*InOverloadResolution=*/false); 1560193326Sed } 1561193326Sed return false; 1562193326Sed} 1563193326Sed 1564193326Sed/// \brief Try to find a common type for two according to C++0x 5.16p5. 1565193326Sed/// 1566193326Sed/// This is part of the parameter validation for the ? operator. If either 1567193326Sed/// value operand is a class type, overload resolution is used to find a 1568193326Sed/// conversion to a common type. 1569193326Sedstatic bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, 1570193326Sed SourceLocation Loc) { 1571193326Sed Expr *Args[2] = { LHS, RHS }; 1572193326Sed OverloadCandidateSet CandidateSet; 1573198398Srdivacky Self.AddBuiltinOperatorCandidates(OO_Conditional, Loc, Args, 2, CandidateSet); 1574193326Sed 1575193326Sed OverloadCandidateSet::iterator Best; 1576194613Sed switch (Self.BestViableFunction(CandidateSet, Loc, Best)) { 1577193326Sed case Sema::OR_Success: 1578193326Sed // We found a match. Perform the conversions on the arguments and move on. 1579193326Sed if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], 1580193326Sed Best->Conversions[0], "converting") || 1581193326Sed Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], 1582193326Sed Best->Conversions[1], "converting")) 1583193326Sed break; 1584193326Sed return false; 1585193326Sed 1586193326Sed case Sema::OR_No_Viable_Function: 1587193326Sed Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) 1588193326Sed << LHS->getType() << RHS->getType() 1589193326Sed << LHS->getSourceRange() << RHS->getSourceRange(); 1590193326Sed return true; 1591193326Sed 1592193326Sed case Sema::OR_Ambiguous: 1593193326Sed Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) 1594193326Sed << LHS->getType() << RHS->getType() 1595193326Sed << LHS->getSourceRange() << RHS->getSourceRange(); 1596193326Sed // FIXME: Print the possible common types by printing the return types of 1597193326Sed // the viable candidates. 1598193326Sed break; 1599193326Sed 1600193326Sed case Sema::OR_Deleted: 1601193326Sed assert(false && "Conditional operator has only built-in overloads"); 1602193326Sed break; 1603193326Sed } 1604193326Sed return true; 1605193326Sed} 1606193326Sed 1607193326Sed/// \brief Perform an "extended" implicit conversion as returned by 1608193326Sed/// TryClassUnification. 1609193326Sed/// 1610193326Sed/// TryClassUnification generates ICSs that include reference bindings. 1611193326Sed/// PerformImplicitConversion is not suitable for this; it chokes if the 1612193326Sed/// second part of a standard conversion is ICK_DerivedToBase. This function 1613193326Sed/// handles the reference binding specially. 1614193326Sedstatic bool ConvertForConditional(Sema &Self, Expr *&E, 1615198092Srdivacky const ImplicitConversionSequence &ICS) { 1616193326Sed if (ICS.ConversionKind == ImplicitConversionSequence::StandardConversion && 1617193326Sed ICS.Standard.ReferenceBinding) { 1618193326Sed assert(ICS.Standard.DirectBinding && 1619193326Sed "TryClassUnification should never generate indirect ref bindings"); 1620193326Sed // FIXME: CheckReferenceInit should be able to reuse the ICS instead of 1621193326Sed // redoing all the work. 1622193326Sed return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( 1623198092Srdivacky TargetType(ICS)), 1624198092Srdivacky /*FIXME:*/E->getLocStart(), 1625198092Srdivacky /*SuppressUserConversions=*/false, 1626198092Srdivacky /*AllowExplicit=*/false, 1627198092Srdivacky /*ForceRValue=*/false); 1628193326Sed } 1629193326Sed if (ICS.ConversionKind == ImplicitConversionSequence::UserDefinedConversion && 1630193326Sed ICS.UserDefined.After.ReferenceBinding) { 1631193326Sed assert(ICS.UserDefined.After.DirectBinding && 1632193326Sed "TryClassUnification should never generate indirect ref bindings"); 1633193326Sed return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( 1634198092Srdivacky TargetType(ICS)), 1635198092Srdivacky /*FIXME:*/E->getLocStart(), 1636198092Srdivacky /*SuppressUserConversions=*/false, 1637198092Srdivacky /*AllowExplicit=*/false, 1638198092Srdivacky /*ForceRValue=*/false); 1639193326Sed } 1640193326Sed if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, "converting")) 1641193326Sed return true; 1642193326Sed return false; 1643193326Sed} 1644193326Sed 1645193326Sed/// \brief Check the operands of ?: under C++ semantics. 1646193326Sed/// 1647193326Sed/// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y 1648193326Sed/// extension. In this case, LHS == Cond. (But they're not aliases.) 1649193326SedQualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, 1650193326Sed SourceLocation QuestionLoc) { 1651193326Sed // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ 1652193326Sed // interface pointers. 1653193326Sed 1654193326Sed // C++0x 5.16p1 1655193326Sed // The first expression is contextually converted to bool. 1656193326Sed if (!Cond->isTypeDependent()) { 1657193326Sed if (CheckCXXBooleanCondition(Cond)) 1658193326Sed return QualType(); 1659193326Sed } 1660193326Sed 1661193326Sed // Either of the arguments dependent? 1662193326Sed if (LHS->isTypeDependent() || RHS->isTypeDependent()) 1663193326Sed return Context.DependentTy; 1664193326Sed 1665198954Srdivacky CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); 1666198954Srdivacky 1667193326Sed // C++0x 5.16p2 1668193326Sed // If either the second or the third operand has type (cv) void, ... 1669193326Sed QualType LTy = LHS->getType(); 1670193326Sed QualType RTy = RHS->getType(); 1671193326Sed bool LVoid = LTy->isVoidType(); 1672193326Sed bool RVoid = RTy->isVoidType(); 1673193326Sed if (LVoid || RVoid) { 1674193326Sed // ... then the [l2r] conversions are performed on the second and third 1675193326Sed // operands ... 1676193326Sed DefaultFunctionArrayConversion(LHS); 1677193326Sed DefaultFunctionArrayConversion(RHS); 1678193326Sed LTy = LHS->getType(); 1679193326Sed RTy = RHS->getType(); 1680193326Sed 1681193326Sed // ... and one of the following shall hold: 1682193326Sed // -- The second or the third operand (but not both) is a throw- 1683193326Sed // expression; the result is of the type of the other and is an rvalue. 1684193326Sed bool LThrow = isa<CXXThrowExpr>(LHS); 1685193326Sed bool RThrow = isa<CXXThrowExpr>(RHS); 1686193326Sed if (LThrow && !RThrow) 1687193326Sed return RTy; 1688193326Sed if (RThrow && !LThrow) 1689193326Sed return LTy; 1690193326Sed 1691193326Sed // -- Both the second and third operands have type void; the result is of 1692193326Sed // type void and is an rvalue. 1693193326Sed if (LVoid && RVoid) 1694193326Sed return Context.VoidTy; 1695193326Sed 1696193326Sed // Neither holds, error. 1697193326Sed Diag(QuestionLoc, diag::err_conditional_void_nonvoid) 1698193326Sed << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) 1699193326Sed << LHS->getSourceRange() << RHS->getSourceRange(); 1700193326Sed return QualType(); 1701193326Sed } 1702193326Sed 1703193326Sed // Neither is void. 1704193326Sed 1705193326Sed // C++0x 5.16p3 1706193326Sed // Otherwise, if the second and third operand have different types, and 1707193326Sed // either has (cv) class type, and attempt is made to convert each of those 1708193326Sed // operands to the other. 1709193326Sed if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) && 1710193326Sed (LTy->isRecordType() || RTy->isRecordType())) { 1711193326Sed ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; 1712193326Sed // These return true if a single direction is already ambiguous. 1713193326Sed if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight)) 1714193326Sed return QualType(); 1715193326Sed if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft)) 1716193326Sed return QualType(); 1717193326Sed 1718193326Sed bool HaveL2R = ICSLeftToRight.ConversionKind != 1719193326Sed ImplicitConversionSequence::BadConversion; 1720193326Sed bool HaveR2L = ICSRightToLeft.ConversionKind != 1721193326Sed ImplicitConversionSequence::BadConversion; 1722193326Sed // If both can be converted, [...] the program is ill-formed. 1723193326Sed if (HaveL2R && HaveR2L) { 1724193326Sed Diag(QuestionLoc, diag::err_conditional_ambiguous) 1725193326Sed << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); 1726193326Sed return QualType(); 1727193326Sed } 1728193326Sed 1729193326Sed // If exactly one conversion is possible, that conversion is applied to 1730193326Sed // the chosen operand and the converted operands are used in place of the 1731193326Sed // original operands for the remainder of this section. 1732193326Sed if (HaveL2R) { 1733193326Sed if (ConvertForConditional(*this, LHS, ICSLeftToRight)) 1734193326Sed return QualType(); 1735193326Sed LTy = LHS->getType(); 1736193326Sed } else if (HaveR2L) { 1737193326Sed if (ConvertForConditional(*this, RHS, ICSRightToLeft)) 1738193326Sed return QualType(); 1739193326Sed RTy = RHS->getType(); 1740193326Sed } 1741193326Sed } 1742193326Sed 1743193326Sed // C++0x 5.16p4 1744193326Sed // If the second and third operands are lvalues and have the same type, 1745193326Sed // the result is of that type [...] 1746193326Sed bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy); 1747193326Sed if (Same && LHS->isLvalue(Context) == Expr::LV_Valid && 1748193326Sed RHS->isLvalue(Context) == Expr::LV_Valid) 1749193326Sed return LTy; 1750193326Sed 1751193326Sed // C++0x 5.16p5 1752193326Sed // Otherwise, the result is an rvalue. If the second and third operands 1753193326Sed // do not have the same type, and either has (cv) class type, ... 1754193326Sed if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { 1755193326Sed // ... overload resolution is used to determine the conversions (if any) 1756193326Sed // to be applied to the operands. If the overload resolution fails, the 1757193326Sed // program is ill-formed. 1758193326Sed if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) 1759193326Sed return QualType(); 1760193326Sed } 1761193326Sed 1762193326Sed // C++0x 5.16p6 1763193326Sed // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard 1764193326Sed // conversions are performed on the second and third operands. 1765193326Sed DefaultFunctionArrayConversion(LHS); 1766193326Sed DefaultFunctionArrayConversion(RHS); 1767193326Sed LTy = LHS->getType(); 1768193326Sed RTy = RHS->getType(); 1769193326Sed 1770193326Sed // After those conversions, one of the following shall hold: 1771193326Sed // -- The second and third operands have the same type; the result 1772193326Sed // is of that type. 1773193326Sed if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) 1774193326Sed return LTy; 1775193326Sed 1776193326Sed // -- The second and third operands have arithmetic or enumeration type; 1777193326Sed // the usual arithmetic conversions are performed to bring them to a 1778193326Sed // common type, and the result is of that type. 1779193326Sed if (LTy->isArithmeticType() && RTy->isArithmeticType()) { 1780193326Sed UsualArithmeticConversions(LHS, RHS); 1781193326Sed return LHS->getType(); 1782193326Sed } 1783193326Sed 1784193326Sed // -- The second and third operands have pointer type, or one has pointer 1785193326Sed // type and the other is a null pointer constant; pointer conversions 1786193326Sed // and qualification conversions are performed to bring them to their 1787193326Sed // composite pointer type. The result is of the composite pointer type. 1788193326Sed QualType Composite = FindCompositePointerType(LHS, RHS); 1789193326Sed if (!Composite.isNull()) 1790193326Sed return Composite; 1791193326Sed 1792193326Sed // Fourth bullet is same for pointers-to-member. However, the possible 1793193326Sed // conversions are far more limited: we have null-to-pointer, upcast of 1794193326Sed // containing class, and second-level cv-ness. 1795193326Sed // cv-ness is not a union, but must match one of the two operands. (Which, 1796193326Sed // frankly, is stupid.) 1797198092Srdivacky const MemberPointerType *LMemPtr = LTy->getAs<MemberPointerType>(); 1798198092Srdivacky const MemberPointerType *RMemPtr = RTy->getAs<MemberPointerType>(); 1799198092Srdivacky if (LMemPtr && 1800198092Srdivacky RHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { 1801198398Srdivacky ImpCastExprToType(RHS, LTy, CastExpr::CK_NullToMemberPointer); 1802193326Sed return LTy; 1803193326Sed } 1804198092Srdivacky if (RMemPtr && 1805198092Srdivacky LHS->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { 1806198398Srdivacky ImpCastExprToType(LHS, RTy, CastExpr::CK_NullToMemberPointer); 1807193326Sed return RTy; 1808193326Sed } 1809193326Sed if (LMemPtr && RMemPtr) { 1810193326Sed QualType LPointee = LMemPtr->getPointeeType(); 1811193326Sed QualType RPointee = RMemPtr->getPointeeType(); 1812198092Srdivacky 1813198092Srdivacky QualifierCollector LPQuals, RPQuals; 1814198092Srdivacky const Type *LPCan = LPQuals.strip(Context.getCanonicalType(LPointee)); 1815198092Srdivacky const Type *RPCan = RPQuals.strip(Context.getCanonicalType(RPointee)); 1816198092Srdivacky 1817193326Sed // First, we check that the unqualified pointee type is the same. If it's 1818193326Sed // not, there's no conversion that will unify the two pointers. 1819198092Srdivacky if (LPCan == RPCan) { 1820198092Srdivacky 1821198092Srdivacky // Second, we take the greater of the two qualifications. If neither 1822193326Sed // is greater than the other, the conversion is not possible. 1823198092Srdivacky 1824198092Srdivacky Qualifiers MergedQuals = LPQuals + RPQuals; 1825198092Srdivacky 1826198092Srdivacky bool CompatibleQuals = true; 1827198092Srdivacky if (MergedQuals.getCVRQualifiers() != LPQuals.getCVRQualifiers() && 1828198092Srdivacky MergedQuals.getCVRQualifiers() != RPQuals.getCVRQualifiers()) 1829198092Srdivacky CompatibleQuals = false; 1830198092Srdivacky else if (LPQuals.getAddressSpace() != RPQuals.getAddressSpace()) 1831198092Srdivacky // FIXME: 1832198092Srdivacky // C99 6.5.15 as modified by TR 18037: 1833198092Srdivacky // If the second and third operands are pointers into different 1834198092Srdivacky // address spaces, the address spaces must overlap. 1835198092Srdivacky CompatibleQuals = false; 1836198092Srdivacky // FIXME: GC qualifiers? 1837198092Srdivacky 1838198092Srdivacky if (CompatibleQuals) { 1839193326Sed // Third, we check if either of the container classes is derived from 1840193326Sed // the other. 1841193326Sed QualType LContainer(LMemPtr->getClass(), 0); 1842193326Sed QualType RContainer(RMemPtr->getClass(), 0); 1843193326Sed QualType MoreDerived; 1844193326Sed if (Context.getCanonicalType(LContainer) == 1845193326Sed Context.getCanonicalType(RContainer)) 1846193326Sed MoreDerived = LContainer; 1847193326Sed else if (IsDerivedFrom(LContainer, RContainer)) 1848193326Sed MoreDerived = LContainer; 1849193326Sed else if (IsDerivedFrom(RContainer, LContainer)) 1850193326Sed MoreDerived = RContainer; 1851193326Sed 1852193326Sed if (!MoreDerived.isNull()) { 1853193326Sed // The type 'Q Pointee (MoreDerived::*)' is the common type. 1854193326Sed // We don't use ImpCastExprToType here because this could still fail 1855193326Sed // for ambiguous or inaccessible conversions. 1856198092Srdivacky LPointee = Context.getQualifiedType(LPointee, MergedQuals); 1857198092Srdivacky QualType Common 1858198092Srdivacky = Context.getMemberPointerType(LPointee, MoreDerived.getTypePtr()); 1859193326Sed if (PerformImplicitConversion(LHS, Common, "converting")) 1860193326Sed return QualType(); 1861193326Sed if (PerformImplicitConversion(RHS, Common, "converting")) 1862193326Sed return QualType(); 1863193326Sed return Common; 1864193326Sed } 1865193326Sed } 1866193326Sed } 1867193326Sed } 1868193326Sed 1869193326Sed Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) 1870193326Sed << LHS->getType() << RHS->getType() 1871193326Sed << LHS->getSourceRange() << RHS->getSourceRange(); 1872193326Sed return QualType(); 1873193326Sed} 1874193326Sed 1875193326Sed/// \brief Find a merged pointer type and convert the two expressions to it. 1876193326Sed/// 1877198092Srdivacky/// This finds the composite pointer type (or member pointer type) for @p E1 1878198092Srdivacky/// and @p E2 according to C++0x 5.9p2. It converts both expressions to this 1879198092Srdivacky/// type and returns it. 1880193326Sed/// It does not emit diagnostics. 1881193326SedQualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) { 1882193326Sed assert(getLangOptions().CPlusPlus && "This function assumes C++"); 1883193326Sed QualType T1 = E1->getType(), T2 = E2->getType(); 1884193326Sed 1885198092Srdivacky if (!T1->isPointerType() && !T1->isMemberPointerType() && 1886198092Srdivacky !T2->isPointerType() && !T2->isMemberPointerType()) 1887198092Srdivacky return QualType(); 1888198092Srdivacky 1889193326Sed // C++0x 5.9p2 1890193326Sed // Pointer conversions and qualification conversions are performed on 1891193326Sed // pointer operands to bring them to their composite pointer type. If 1892193326Sed // one operand is a null pointer constant, the composite pointer type is 1893193326Sed // the type of the other operand. 1894198092Srdivacky if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { 1895198398Srdivacky if (T2->isMemberPointerType()) 1896198398Srdivacky ImpCastExprToType(E1, T2, CastExpr::CK_NullToMemberPointer); 1897198398Srdivacky else 1898198398Srdivacky ImpCastExprToType(E1, T2, CastExpr::CK_IntegralToPointer); 1899193326Sed return T2; 1900193326Sed } 1901198092Srdivacky if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { 1902198398Srdivacky if (T1->isMemberPointerType()) 1903198398Srdivacky ImpCastExprToType(E2, T1, CastExpr::CK_NullToMemberPointer); 1904198398Srdivacky else 1905198398Srdivacky ImpCastExprToType(E2, T1, CastExpr::CK_IntegralToPointer); 1906193326Sed return T1; 1907193326Sed } 1908198092Srdivacky 1909198092Srdivacky // Now both have to be pointers or member pointers. 1910199482Srdivacky if ((!T1->isPointerType() && !T1->isMemberPointerType()) || 1911199482Srdivacky (!T2->isPointerType() && !T2->isMemberPointerType())) 1912193326Sed return QualType(); 1913193326Sed 1914193326Sed // Otherwise, of one of the operands has type "pointer to cv1 void," then 1915193326Sed // the other has type "pointer to cv2 T" and the composite pointer type is 1916193326Sed // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. 1917193326Sed // Otherwise, the composite pointer type is a pointer type similar to the 1918193326Sed // type of one of the operands, with a cv-qualification signature that is 1919193326Sed // the union of the cv-qualification signatures of the operand types. 1920193326Sed // In practice, the first part here is redundant; it's subsumed by the second. 1921193326Sed // What we do here is, we build the two possible composite types, and try the 1922193326Sed // conversions in both directions. If only one works, or if the two composite 1923193326Sed // types are the same, we have succeeded. 1924198092Srdivacky // FIXME: extended qualifiers? 1925199482Srdivacky typedef llvm::SmallVector<unsigned, 4> QualifierVector; 1926199482Srdivacky QualifierVector QualifierUnion; 1927199482Srdivacky typedef llvm::SmallVector<std::pair<const Type *, const Type *>, 4> 1928199482Srdivacky ContainingClassVector; 1929199482Srdivacky ContainingClassVector MemberOfClass; 1930199482Srdivacky QualType Composite1 = Context.getCanonicalType(T1), 1931199482Srdivacky Composite2 = Context.getCanonicalType(T2); 1932198092Srdivacky do { 1933198092Srdivacky const PointerType *Ptr1, *Ptr2; 1934198092Srdivacky if ((Ptr1 = Composite1->getAs<PointerType>()) && 1935198092Srdivacky (Ptr2 = Composite2->getAs<PointerType>())) { 1936198092Srdivacky Composite1 = Ptr1->getPointeeType(); 1937198092Srdivacky Composite2 = Ptr2->getPointeeType(); 1938198092Srdivacky QualifierUnion.push_back( 1939198092Srdivacky Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); 1940198092Srdivacky MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); 1941198092Srdivacky continue; 1942198092Srdivacky } 1943198092Srdivacky 1944198092Srdivacky const MemberPointerType *MemPtr1, *MemPtr2; 1945198092Srdivacky if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && 1946198092Srdivacky (MemPtr2 = Composite2->getAs<MemberPointerType>())) { 1947198092Srdivacky Composite1 = MemPtr1->getPointeeType(); 1948198092Srdivacky Composite2 = MemPtr2->getPointeeType(); 1949198092Srdivacky QualifierUnion.push_back( 1950198092Srdivacky Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); 1951198092Srdivacky MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), 1952198092Srdivacky MemPtr2->getClass())); 1953198092Srdivacky continue; 1954198092Srdivacky } 1955198092Srdivacky 1956198092Srdivacky // FIXME: block pointer types? 1957198092Srdivacky 1958198092Srdivacky // Cannot unwrap any more types. 1959198092Srdivacky break; 1960198092Srdivacky } while (true); 1961198092Srdivacky 1962198092Srdivacky // Rewrap the composites as pointers or member pointers with the union CVRs. 1963199482Srdivacky ContainingClassVector::reverse_iterator MOC 1964199482Srdivacky = MemberOfClass.rbegin(); 1965199482Srdivacky for (QualifierVector::reverse_iterator 1966199482Srdivacky I = QualifierUnion.rbegin(), 1967199482Srdivacky E = QualifierUnion.rend(); 1968198092Srdivacky I != E; (void)++I, ++MOC) { 1969198092Srdivacky Qualifiers Quals = Qualifiers::fromCVRMask(*I); 1970198092Srdivacky if (MOC->first && MOC->second) { 1971198092Srdivacky // Rebuild member pointer type 1972198092Srdivacky Composite1 = Context.getMemberPointerType( 1973198092Srdivacky Context.getQualifiedType(Composite1, Quals), 1974198092Srdivacky MOC->first); 1975198092Srdivacky Composite2 = Context.getMemberPointerType( 1976198092Srdivacky Context.getQualifiedType(Composite2, Quals), 1977198092Srdivacky MOC->second); 1978198092Srdivacky } else { 1979198092Srdivacky // Rebuild pointer type 1980198092Srdivacky Composite1 1981198092Srdivacky = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); 1982198092Srdivacky Composite2 1983198092Srdivacky = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); 1984198092Srdivacky } 1985193326Sed } 1986193326Sed 1987198092Srdivacky ImplicitConversionSequence E1ToC1 = 1988198092Srdivacky TryImplicitConversion(E1, Composite1, 1989198092Srdivacky /*SuppressUserConversions=*/false, 1990198092Srdivacky /*AllowExplicit=*/false, 1991198092Srdivacky /*ForceRValue=*/false, 1992198092Srdivacky /*InOverloadResolution=*/false); 1993198092Srdivacky ImplicitConversionSequence E2ToC1 = 1994198092Srdivacky TryImplicitConversion(E2, Composite1, 1995198092Srdivacky /*SuppressUserConversions=*/false, 1996198092Srdivacky /*AllowExplicit=*/false, 1997198092Srdivacky /*ForceRValue=*/false, 1998198092Srdivacky /*InOverloadResolution=*/false); 1999198092Srdivacky 2000193326Sed ImplicitConversionSequence E1ToC2, E2ToC2; 2001193326Sed E1ToC2.ConversionKind = ImplicitConversionSequence::BadConversion; 2002193326Sed E2ToC2.ConversionKind = ImplicitConversionSequence::BadConversion; 2003193326Sed if (Context.getCanonicalType(Composite1) != 2004193326Sed Context.getCanonicalType(Composite2)) { 2005198092Srdivacky E1ToC2 = TryImplicitConversion(E1, Composite2, 2006198092Srdivacky /*SuppressUserConversions=*/false, 2007198092Srdivacky /*AllowExplicit=*/false, 2008198092Srdivacky /*ForceRValue=*/false, 2009198092Srdivacky /*InOverloadResolution=*/false); 2010198092Srdivacky E2ToC2 = TryImplicitConversion(E2, Composite2, 2011198092Srdivacky /*SuppressUserConversions=*/false, 2012198092Srdivacky /*AllowExplicit=*/false, 2013198092Srdivacky /*ForceRValue=*/false, 2014198092Srdivacky /*InOverloadResolution=*/false); 2015193326Sed } 2016193326Sed 2017193326Sed bool ToC1Viable = E1ToC1.ConversionKind != 2018193326Sed ImplicitConversionSequence::BadConversion 2019193326Sed && E2ToC1.ConversionKind != 2020193326Sed ImplicitConversionSequence::BadConversion; 2021193326Sed bool ToC2Viable = E1ToC2.ConversionKind != 2022193326Sed ImplicitConversionSequence::BadConversion 2023193326Sed && E2ToC2.ConversionKind != 2024193326Sed ImplicitConversionSequence::BadConversion; 2025193326Sed if (ToC1Viable && !ToC2Viable) { 2026193326Sed if (!PerformImplicitConversion(E1, Composite1, E1ToC1, "converting") && 2027193326Sed !PerformImplicitConversion(E2, Composite1, E2ToC1, "converting")) 2028193326Sed return Composite1; 2029193326Sed } 2030193326Sed if (ToC2Viable && !ToC1Viable) { 2031193326Sed if (!PerformImplicitConversion(E1, Composite2, E1ToC2, "converting") && 2032193326Sed !PerformImplicitConversion(E2, Composite2, E2ToC2, "converting")) 2033193326Sed return Composite2; 2034193326Sed } 2035193326Sed return QualType(); 2036193326Sed} 2037193326Sed 2038193326SedSema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) { 2039198092Srdivacky if (!Context.getLangOptions().CPlusPlus) 2040198092Srdivacky return Owned(E); 2041198092Srdivacky 2042198092Srdivacky const RecordType *RT = E->getType()->getAs<RecordType>(); 2043193326Sed if (!RT) 2044193326Sed return Owned(E); 2045198092Srdivacky 2046193326Sed CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 2047193326Sed if (RD->hasTrivialDestructor()) 2048193326Sed return Owned(E); 2049198092Srdivacky 2050198092Srdivacky if (CallExpr *CE = dyn_cast<CallExpr>(E)) { 2051198092Srdivacky QualType Ty = CE->getCallee()->getType(); 2052198092Srdivacky if (const PointerType *PT = Ty->getAs<PointerType>()) 2053198092Srdivacky Ty = PT->getPointeeType(); 2054198092Srdivacky 2055198092Srdivacky const FunctionType *FTy = Ty->getAs<FunctionType>(); 2056198092Srdivacky if (FTy->getResultType()->isReferenceType()) 2057198092Srdivacky return Owned(E); 2058198092Srdivacky } 2059198092Srdivacky CXXTemporary *Temp = CXXTemporary::Create(Context, 2060193326Sed RD->getDestructor(Context)); 2061193326Sed ExprTemporaries.push_back(Temp); 2062198092Srdivacky if (CXXDestructorDecl *Destructor = 2063198092Srdivacky const_cast<CXXDestructorDecl*>(RD->getDestructor(Context))) 2064198092Srdivacky MarkDeclarationReferenced(E->getExprLoc(), Destructor); 2065193326Sed // FIXME: Add the temporary to the temporaries vector. 2066193326Sed return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); 2067193326Sed} 2068193326Sed 2069198092SrdivackyExpr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr, 2070194613Sed bool ShouldDestroyTemps) { 2071193576Sed assert(SubExpr && "sub expression can't be null!"); 2072198092Srdivacky 2073193576Sed if (ExprTemporaries.empty()) 2074193576Sed return SubExpr; 2075198092Srdivacky 2076193576Sed Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr, 2077198092Srdivacky &ExprTemporaries[0], 2078193576Sed ExprTemporaries.size(), 2079194613Sed ShouldDestroyTemps); 2080193576Sed ExprTemporaries.clear(); 2081198092Srdivacky 2082193576Sed return E; 2083193576Sed} 2084193576Sed 2085198092SrdivackySema::OwningExprResult 2086198092SrdivackySema::ActOnStartCXXMemberReference(Scope *S, ExprArg Base, SourceLocation OpLoc, 2087198092Srdivacky tok::TokenKind OpKind, TypeTy *&ObjectType) { 2088198092Srdivacky // Since this might be a postfix expression, get rid of ParenListExprs. 2089198092Srdivacky Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); 2090198092Srdivacky 2091198092Srdivacky Expr *BaseExpr = (Expr*)Base.get(); 2092198092Srdivacky assert(BaseExpr && "no record expansion"); 2093198092Srdivacky 2094198092Srdivacky QualType BaseType = BaseExpr->getType(); 2095198092Srdivacky if (BaseType->isDependentType()) { 2096198954Srdivacky // If we have a pointer to a dependent type and are using the -> operator, 2097198954Srdivacky // the object type is the type that the pointer points to. We might still 2098198954Srdivacky // have enough information about that type to do something useful. 2099198954Srdivacky if (OpKind == tok::arrow) 2100198954Srdivacky if (const PointerType *Ptr = BaseType->getAs<PointerType>()) 2101198954Srdivacky BaseType = Ptr->getPointeeType(); 2102198954Srdivacky 2103198092Srdivacky ObjectType = BaseType.getAsOpaquePtr(); 2104198092Srdivacky return move(Base); 2105198092Srdivacky } 2106198092Srdivacky 2107198092Srdivacky // C++ [over.match.oper]p8: 2108198092Srdivacky // [...] When operator->returns, the operator-> is applied to the value 2109198092Srdivacky // returned, with the original second operand. 2110198092Srdivacky if (OpKind == tok::arrow) { 2111198092Srdivacky // The set of types we've considered so far. 2112198092Srdivacky llvm::SmallPtrSet<CanQualType,8> CTypes; 2113198092Srdivacky llvm::SmallVector<SourceLocation, 8> Locations; 2114198092Srdivacky CTypes.insert(Context.getCanonicalType(BaseType)); 2115198092Srdivacky 2116198092Srdivacky while (BaseType->isRecordType()) { 2117198092Srdivacky Base = BuildOverloadedArrowExpr(S, move(Base), OpLoc); 2118198092Srdivacky BaseExpr = (Expr*)Base.get(); 2119198092Srdivacky if (BaseExpr == NULL) 2120198092Srdivacky return ExprError(); 2121198092Srdivacky if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(BaseExpr)) 2122198092Srdivacky Locations.push_back(OpCall->getDirectCallee()->getLocation()); 2123198092Srdivacky BaseType = BaseExpr->getType(); 2124198092Srdivacky CanQualType CBaseType = Context.getCanonicalType(BaseType); 2125198092Srdivacky if (!CTypes.insert(CBaseType)) { 2126198092Srdivacky Diag(OpLoc, diag::err_operator_arrow_circular); 2127198092Srdivacky for (unsigned i = 0; i < Locations.size(); i++) 2128198092Srdivacky Diag(Locations[i], diag::note_declared_at); 2129198092Srdivacky return ExprError(); 2130198092Srdivacky } 2131198092Srdivacky } 2132199990Srdivacky 2133199990Srdivacky if (BaseType->isPointerType()) 2134199990Srdivacky BaseType = BaseType->getPointeeType(); 2135198092Srdivacky } 2136198092Srdivacky 2137198092Srdivacky // We could end up with various non-record types here, such as extended 2138198092Srdivacky // vector types or Objective-C interfaces. Just return early and let 2139198092Srdivacky // ActOnMemberReferenceExpr do the work. 2140198092Srdivacky if (!BaseType->isRecordType()) { 2141198092Srdivacky // C++ [basic.lookup.classref]p2: 2142198092Srdivacky // [...] If the type of the object expression is of pointer to scalar 2143198092Srdivacky // type, the unqualified-id is looked up in the context of the complete 2144198092Srdivacky // postfix-expression. 2145198092Srdivacky ObjectType = 0; 2146198092Srdivacky return move(Base); 2147198092Srdivacky } 2148198092Srdivacky 2149199482Srdivacky // The object type must be complete (or dependent). 2150199482Srdivacky if (!BaseType->isDependentType() && 2151199482Srdivacky RequireCompleteType(OpLoc, BaseType, 2152199482Srdivacky PDiag(diag::err_incomplete_member_access))) 2153199482Srdivacky return ExprError(); 2154199482Srdivacky 2155198092Srdivacky // C++ [basic.lookup.classref]p2: 2156198092Srdivacky // If the id-expression in a class member access (5.2.5) is an 2157199482Srdivacky // unqualified-id, and the type of the object expression is of a class 2158198092Srdivacky // type C (or of pointer to a class type C), the unqualified-id is looked 2159198092Srdivacky // up in the scope of class C. [...] 2160198092Srdivacky ObjectType = BaseType.getAsOpaquePtr(); 2161199482Srdivacky 2162198092Srdivacky return move(Base); 2163198092Srdivacky} 2164198092Srdivacky 2165198092SrdivackyCXXMemberCallExpr *Sema::BuildCXXMemberCallExpr(Expr *Exp, 2166198092Srdivacky CXXMethodDecl *Method) { 2167198092Srdivacky MemberExpr *ME = 2168198092Srdivacky new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method, 2169198092Srdivacky SourceLocation(), Method->getType()); 2170198092Srdivacky QualType ResultType; 2171198092Srdivacky if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(Method)) 2172198092Srdivacky ResultType = Conv->getConversionType().getNonReferenceType(); 2173198092Srdivacky else 2174198092Srdivacky ResultType = Method->getResultType().getNonReferenceType(); 2175198092Srdivacky 2176199990Srdivacky MarkDeclarationReferenced(Exp->getLocStart(), Method); 2177199990Srdivacky CXXMemberCallExpr *CE = 2178199990Srdivacky new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, 2179199990Srdivacky Exp->getLocEnd()); 2180198092Srdivacky return CE; 2181198092Srdivacky} 2182198092Srdivacky 2183198092SrdivackySema::OwningExprResult Sema::BuildCXXCastArgument(SourceLocation CastLoc, 2184198092Srdivacky QualType Ty, 2185198092Srdivacky CastExpr::CastKind Kind, 2186198092Srdivacky CXXMethodDecl *Method, 2187198092Srdivacky ExprArg Arg) { 2188198092Srdivacky Expr *From = Arg.takeAs<Expr>(); 2189198092Srdivacky 2190198092Srdivacky switch (Kind) { 2191198092Srdivacky default: assert(0 && "Unhandled cast kind!"); 2192198092Srdivacky case CastExpr::CK_ConstructorConversion: { 2193198092Srdivacky ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); 2194198092Srdivacky 2195198092Srdivacky if (CompleteConstructorCall(cast<CXXConstructorDecl>(Method), 2196198092Srdivacky MultiExprArg(*this, (void **)&From, 1), 2197198092Srdivacky CastLoc, ConstructorArgs)) 2198198092Srdivacky return ExprError(); 2199198398Srdivacky 2200198398Srdivacky OwningExprResult Result = 2201198398Srdivacky BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), 2202198398Srdivacky move_arg(ConstructorArgs)); 2203198398Srdivacky if (Result.isInvalid()) 2204198398Srdivacky return ExprError(); 2205198398Srdivacky 2206198398Srdivacky return MaybeBindToTemporary(Result.takeAs<Expr>()); 2207198092Srdivacky } 2208198092Srdivacky 2209198092Srdivacky case CastExpr::CK_UserDefinedConversion: { 2210198092Srdivacky assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); 2211198092Srdivacky 2212198092Srdivacky // Cast to base if needed. 2213198092Srdivacky if (PerformObjectArgumentInitialization(From, Method)) 2214198092Srdivacky return ExprError(); 2215198092Srdivacky 2216198092Srdivacky // Create an implicit call expr that calls it. 2217198092Srdivacky CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(From, Method); 2218198398Srdivacky return MaybeBindToTemporary(CE); 2219198092Srdivacky } 2220198092Srdivacky } 2221198092Srdivacky} 2222198092Srdivacky 2223193326SedSema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) { 2224193326Sed Expr *FullExpr = Arg.takeAs<Expr>(); 2225193576Sed if (FullExpr) 2226198092Srdivacky FullExpr = MaybeCreateCXXExprWithTemporaries(FullExpr, 2227194613Sed /*ShouldDestroyTemps=*/true); 2228193326Sed 2229198092Srdivacky 2230193326Sed return Owned(FullExpr); 2231193326Sed} 2232198398Srdivacky 2233198398Srdivacky/// \brief Determine whether a reference to the given declaration in the 2234198398Srdivacky/// current context is an implicit member access 2235198398Srdivacky/// (C++ [class.mfct.non-static]p2). 2236198398Srdivacky/// 2237198398Srdivacky/// FIXME: Should Objective-C also use this approach? 2238198398Srdivacky/// 2239198398Srdivacky/// \param D the declaration being referenced from the current scope. 2240198398Srdivacky/// 2241198398Srdivacky/// \param NameLoc the location of the name in the source. 2242198398Srdivacky/// 2243198398Srdivacky/// \param ThisType if the reference to this declaration is an implicit member 2244198398Srdivacky/// access, will be set to the type of the "this" pointer to be used when 2245198398Srdivacky/// building that implicit member access. 2246198398Srdivacky/// 2247198398Srdivacky/// \returns true if this is an implicit member reference (in which case 2248198398Srdivacky/// \p ThisType and \p MemberType will be set), or false if it is not an 2249198398Srdivacky/// implicit member reference. 2250199990Srdivackybool Sema::isImplicitMemberReference(const LookupResult &R, 2251199990Srdivacky QualType &ThisType) { 2252198398Srdivacky // If this isn't a C++ method, then it isn't an implicit member reference. 2253198398Srdivacky CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext); 2254198398Srdivacky if (!MD || MD->isStatic()) 2255198398Srdivacky return false; 2256198398Srdivacky 2257198398Srdivacky // C++ [class.mfct.nonstatic]p2: 2258198398Srdivacky // [...] if name lookup (3.4.1) resolves the name in the 2259198398Srdivacky // id-expression to a nonstatic nontype member of class X or of 2260198398Srdivacky // a base class of X, the id-expression is transformed into a 2261198398Srdivacky // class member access expression (5.2.5) using (*this) (9.3.2) 2262198398Srdivacky // as the postfix-expression to the left of the '.' operator. 2263198398Srdivacky DeclContext *Ctx = 0; 2264199990Srdivacky if (R.isUnresolvableResult()) { 2265199990Srdivacky // FIXME: this is just picking one at random 2266199990Srdivacky Ctx = R.getRepresentativeDecl()->getDeclContext(); 2267199990Srdivacky } else if (FieldDecl *FD = R.getAsSingle<FieldDecl>()) { 2268198398Srdivacky Ctx = FD->getDeclContext(); 2269198398Srdivacky } else { 2270199990Srdivacky for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { 2271199990Srdivacky CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*I); 2272198398Srdivacky FunctionTemplateDecl *FunTmpl = 0; 2273199990Srdivacky if (!Method && (FunTmpl = dyn_cast<FunctionTemplateDecl>(*I))) 2274198398Srdivacky Method = dyn_cast<CXXMethodDecl>(FunTmpl->getTemplatedDecl()); 2275198398Srdivacky 2276198893Srdivacky // FIXME: Do we have to know if there are explicit template arguments? 2277198398Srdivacky if (Method && !Method->isStatic()) { 2278198398Srdivacky Ctx = Method->getParent(); 2279198398Srdivacky break; 2280198398Srdivacky } 2281198398Srdivacky } 2282198398Srdivacky } 2283198398Srdivacky 2284198398Srdivacky if (!Ctx || !Ctx->isRecord()) 2285198398Srdivacky return false; 2286198398Srdivacky 2287198398Srdivacky // Determine whether the declaration(s) we found are actually in a base 2288198398Srdivacky // class. If not, this isn't an implicit member reference. 2289198398Srdivacky ThisType = MD->getThisType(Context); 2290199990Srdivacky 2291199990Srdivacky // FIXME: this doesn't really work for overloaded lookups. 2292198893Srdivacky 2293198398Srdivacky QualType CtxType = Context.getTypeDeclType(cast<CXXRecordDecl>(Ctx)); 2294198398Srdivacky QualType ClassType 2295198398Srdivacky = Context.getTypeDeclType(cast<CXXRecordDecl>(MD->getParent())); 2296198398Srdivacky return Context.hasSameType(CtxType, ClassType) || 2297198398Srdivacky IsDerivedFrom(ClassType, CtxType); 2298198398Srdivacky} 2299198398Srdivacky 2300