SemaExprCXX.cpp revision 203955
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" 15200583Srdivacky#include "SemaInit.h" 16199482Srdivacky#include "Lookup.h" 17198092Srdivacky#include "clang/AST/ASTContext.h" 18198092Srdivacky#include "clang/AST/CXXInheritance.h" 19193326Sed#include "clang/AST/ExprCXX.h" 20198092Srdivacky#include "clang/Basic/PartialDiagnostic.h" 21198092Srdivacky#include "clang/Basic/TargetInfo.h" 22198092Srdivacky#include "clang/Lex/Preprocessor.h" 23193326Sed#include "clang/Parse/DeclSpec.h" 24193326Sed#include "llvm/ADT/STLExtras.h" 25193326Sedusing namespace clang; 26193326Sed 27193326Sed/// ActOnCXXTypeidOfType - Parse typeid( type-id ). 28193326SedAction::OwningExprResult 29193326SedSema::ActOnCXXTypeid(SourceLocation OpLoc, SourceLocation LParenLoc, 30193326Sed bool isType, void *TyOrExpr, SourceLocation RParenLoc) { 31198092Srdivacky if (!StdNamespace) 32193326Sed return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); 33198092Srdivacky 34201361Srdivacky if (isType) { 35201361Srdivacky // C++ [expr.typeid]p4: 36201361Srdivacky // The top-level cv-qualifiers of the lvalue expression or the type-id 37201361Srdivacky // that is the operand of typeid are always ignored. 38198092Srdivacky // FIXME: Preserve type source info. 39201361Srdivacky // FIXME: Preserve the type before we stripped the cv-qualifiers? 40201361Srdivacky QualType T = GetTypeFromParser(TyOrExpr); 41201361Srdivacky if (T.isNull()) 42201361Srdivacky return ExprError(); 43201361Srdivacky 44201361Srdivacky // C++ [expr.typeid]p4: 45201361Srdivacky // If the type of the type-id is a class type or a reference to a class 46201361Srdivacky // type, the class shall be completely-defined. 47201361Srdivacky QualType CheckT = T; 48201361Srdivacky if (const ReferenceType *RefType = CheckT->getAs<ReferenceType>()) 49201361Srdivacky CheckT = RefType->getPointeeType(); 50201361Srdivacky 51201361Srdivacky if (CheckT->getAs<RecordType>() && 52201361Srdivacky RequireCompleteType(OpLoc, CheckT, diag::err_incomplete_typeid)) 53201361Srdivacky return ExprError(); 54201361Srdivacky 55201361Srdivacky TyOrExpr = T.getUnqualifiedType().getAsOpaquePtr(); 56201361Srdivacky } 57198092Srdivacky 58193326Sed IdentifierInfo *TypeInfoII = &PP.getIdentifierTable().get("type_info"); 59199482Srdivacky LookupResult R(*this, TypeInfoII, SourceLocation(), LookupTagName); 60199482Srdivacky LookupQualifiedName(R, StdNamespace); 61200583Srdivacky RecordDecl *TypeInfoRecordDecl = R.getAsSingle<RecordDecl>(); 62193326Sed if (!TypeInfoRecordDecl) 63193326Sed return ExprError(Diag(OpLoc, diag::err_need_header_before_typeid)); 64193326Sed 65193326Sed QualType TypeInfoType = Context.getTypeDeclType(TypeInfoRecordDecl); 66193326Sed 67194711Sed if (!isType) { 68194711Sed bool isUnevaluatedOperand = true; 69194711Sed Expr *E = static_cast<Expr *>(TyOrExpr); 70201361Srdivacky if (E && !E->isTypeDependent()) { 71194711Sed QualType T = E->getType(); 72198092Srdivacky if (const RecordType *RecordT = T->getAs<RecordType>()) { 73194711Sed CXXRecordDecl *RecordD = cast<CXXRecordDecl>(RecordT->getDecl()); 74201361Srdivacky // C++ [expr.typeid]p3: 75203955Srdivacky // [...] If the type of the expression is a class type, the class 76203955Srdivacky // shall be completely-defined. 77203955Srdivacky if (RequireCompleteType(OpLoc, T, diag::err_incomplete_typeid)) 78203955Srdivacky return ExprError(); 79203955Srdivacky 80203955Srdivacky // C++ [expr.typeid]p3: 81201361Srdivacky // When typeid is applied to an expression other than an lvalue of a 82201361Srdivacky // polymorphic class type [...] [the] expression is an unevaluated 83201361Srdivacky // operand. [...] 84201361Srdivacky if (RecordD->isPolymorphic() && E->isLvalue(Context) == Expr::LV_Valid) 85194711Sed isUnevaluatedOperand = false; 86194711Sed } 87201361Srdivacky 88201361Srdivacky // C++ [expr.typeid]p4: 89201361Srdivacky // [...] If the type of the type-id is a reference to a possibly 90201361Srdivacky // cv-qualified type, the result of the typeid expression refers to a 91201361Srdivacky // std::type_info object representing the cv-unqualified referenced 92201361Srdivacky // type. 93201361Srdivacky if (T.hasQualifiers()) { 94201361Srdivacky ImpCastExprToType(E, T.getUnqualifiedType(), CastExpr::CK_NoOp, 95201361Srdivacky E->isLvalue(Context)); 96201361Srdivacky TyOrExpr = E; 97201361Srdivacky } 98194711Sed } 99198092Srdivacky 100199990Srdivacky // If this is an unevaluated operand, clear out the set of 101199990Srdivacky // declaration references we have been computing and eliminate any 102199990Srdivacky // temporaries introduced in its computation. 103194711Sed if (isUnevaluatedOperand) 104199990Srdivacky ExprEvalContexts.back().Context = Unevaluated; 105194711Sed } 106198092Srdivacky 107193326Sed return Owned(new (Context) CXXTypeidExpr(isType, TyOrExpr, 108193326Sed TypeInfoType.withConst(), 109193326Sed SourceRange(OpLoc, RParenLoc))); 110193326Sed} 111193326Sed 112193326Sed/// ActOnCXXBoolLiteral - Parse {true,false} literals. 113193326SedAction::OwningExprResult 114193326SedSema::ActOnCXXBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { 115193326Sed assert((Kind == tok::kw_true || Kind == tok::kw_false) && 116193326Sed "Unknown C++ Boolean value!"); 117193326Sed return Owned(new (Context) CXXBoolLiteralExpr(Kind == tok::kw_true, 118193326Sed Context.BoolTy, OpLoc)); 119193326Sed} 120193326Sed 121193326Sed/// ActOnCXXNullPtrLiteral - Parse 'nullptr'. 122193326SedAction::OwningExprResult 123193326SedSema::ActOnCXXNullPtrLiteral(SourceLocation Loc) { 124193326Sed return Owned(new (Context) CXXNullPtrLiteralExpr(Context.NullPtrTy, Loc)); 125193326Sed} 126193326Sed 127193326Sed/// ActOnCXXThrow - Parse throw expressions. 128193326SedAction::OwningExprResult 129193326SedSema::ActOnCXXThrow(SourceLocation OpLoc, ExprArg E) { 130193326Sed Expr *Ex = E.takeAs<Expr>(); 131193326Sed if (Ex && !Ex->isTypeDependent() && CheckCXXThrowOperand(OpLoc, Ex)) 132193326Sed return ExprError(); 133193326Sed return Owned(new (Context) CXXThrowExpr(Ex, Context.VoidTy, OpLoc)); 134193326Sed} 135193326Sed 136193326Sed/// CheckCXXThrowOperand - Validate the operand of a throw. 137193326Sedbool Sema::CheckCXXThrowOperand(SourceLocation ThrowLoc, Expr *&E) { 138193326Sed // C++ [except.throw]p3: 139201361Srdivacky // A throw-expression initializes a temporary object, called the exception 140201361Srdivacky // object, the type of which is determined by removing any top-level 141201361Srdivacky // cv-qualifiers from the static type of the operand of throw and adjusting 142201361Srdivacky // the type from "array of T" or "function returning T" to "pointer to T" 143201361Srdivacky // or "pointer to function returning T", [...] 144201361Srdivacky if (E->getType().hasQualifiers()) 145201361Srdivacky ImpCastExprToType(E, E->getType().getUnqualifiedType(), CastExpr::CK_NoOp, 146201361Srdivacky E->isLvalue(Context) == Expr::LV_Valid); 147201361Srdivacky 148193326Sed DefaultFunctionArrayConversion(E); 149193326Sed 150193326Sed // If the type of the exception would be an incomplete type or a pointer 151193326Sed // to an incomplete type other than (cv) void the program is ill-formed. 152193326Sed QualType Ty = E->getType(); 153193326Sed int isPointer = 0; 154198092Srdivacky if (const PointerType* Ptr = Ty->getAs<PointerType>()) { 155193326Sed Ty = Ptr->getPointeeType(); 156193326Sed isPointer = 1; 157193326Sed } 158193326Sed if (!isPointer || !Ty->isVoidType()) { 159193326Sed if (RequireCompleteType(ThrowLoc, Ty, 160198092Srdivacky PDiag(isPointer ? diag::err_throw_incomplete_ptr 161198092Srdivacky : diag::err_throw_incomplete) 162198092Srdivacky << E->getSourceRange())) 163193326Sed return true; 164193326Sed } 165193326Sed 166193326Sed // FIXME: Construct a temporary here. 167193326Sed return false; 168193326Sed} 169193326Sed 170193326SedAction::OwningExprResult Sema::ActOnCXXThis(SourceLocation ThisLoc) { 171193326Sed /// C++ 9.3.2: In the body of a non-static member function, the keyword this 172193326Sed /// is a non-lvalue expression whose value is the address of the object for 173193326Sed /// which the function is called. 174193326Sed 175193326Sed if (!isa<FunctionDecl>(CurContext)) 176193326Sed return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); 177193326Sed 178193326Sed if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CurContext)) 179193326Sed if (MD->isInstance()) 180193326Sed return Owned(new (Context) CXXThisExpr(ThisLoc, 181202379Srdivacky MD->getThisType(Context), 182202379Srdivacky /*isImplicit=*/false)); 183193326Sed 184193326Sed return ExprError(Diag(ThisLoc, diag::err_invalid_this_use)); 185193326Sed} 186193326Sed 187193326Sed/// ActOnCXXTypeConstructExpr - Parse construction of a specified type. 188193326Sed/// Can be interpreted either as function-style casting ("int(x)") 189193326Sed/// or class type construction ("ClassType(x,y,z)") 190193326Sed/// or creation of a value-initialized type ("int()"). 191193326SedAction::OwningExprResult 192193326SedSema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, 193193326Sed SourceLocation LParenLoc, 194193326Sed MultiExprArg exprs, 195193326Sed SourceLocation *CommaLocs, 196193326Sed SourceLocation RParenLoc) { 197203955Srdivacky if (!TypeRep) 198203955Srdivacky return ExprError(); 199203955Srdivacky 200202879Srdivacky TypeSourceInfo *TInfo; 201202879Srdivacky QualType Ty = GetTypeFromParser(TypeRep, &TInfo); 202202879Srdivacky if (!TInfo) 203202879Srdivacky TInfo = Context.getTrivialTypeSourceInfo(Ty, SourceLocation()); 204193326Sed unsigned NumExprs = exprs.size(); 205193326Sed Expr **Exprs = (Expr**)exprs.get(); 206193326Sed SourceLocation TyBeginLoc = TypeRange.getBegin(); 207193326Sed SourceRange FullRange = SourceRange(TyBeginLoc, RParenLoc); 208193326Sed 209193326Sed if (Ty->isDependentType() || 210193326Sed CallExpr::hasAnyTypeDependentArguments(Exprs, NumExprs)) { 211193326Sed exprs.release(); 212198092Srdivacky 213198092Srdivacky return Owned(CXXUnresolvedConstructExpr::Create(Context, 214198092Srdivacky TypeRange.getBegin(), Ty, 215193326Sed LParenLoc, 216193326Sed Exprs, NumExprs, 217193326Sed RParenLoc)); 218193326Sed } 219193326Sed 220198092Srdivacky if (Ty->isArrayType()) 221198092Srdivacky return ExprError(Diag(TyBeginLoc, 222198092Srdivacky diag::err_value_init_for_array_type) << FullRange); 223198092Srdivacky if (!Ty->isVoidType() && 224198092Srdivacky RequireCompleteType(TyBeginLoc, Ty, 225198092Srdivacky PDiag(diag::err_invalid_incomplete_type_use) 226198092Srdivacky << FullRange)) 227198092Srdivacky return ExprError(); 228198893Srdivacky 229198092Srdivacky if (RequireNonAbstractType(TyBeginLoc, Ty, 230198092Srdivacky diag::err_allocation_of_abstract_type)) 231198092Srdivacky return ExprError(); 232198092Srdivacky 233198092Srdivacky 234193326Sed // C++ [expr.type.conv]p1: 235193326Sed // If the expression list is a single expression, the type conversion 236193326Sed // expression is equivalent (in definedness, and if defined in meaning) to the 237193326Sed // corresponding cast expression. 238193326Sed // 239193326Sed if (NumExprs == 1) { 240198092Srdivacky CastExpr::CastKind Kind = CastExpr::CK_Unknown; 241198092Srdivacky CXXMethodDecl *Method = 0; 242198092Srdivacky if (CheckCastTypes(TypeRange, Ty, Exprs[0], Kind, Method, 243198092Srdivacky /*FunctionalStyle=*/true)) 244193326Sed return ExprError(); 245198092Srdivacky 246193326Sed exprs.release(); 247198092Srdivacky if (Method) { 248198092Srdivacky OwningExprResult CastArg 249198092Srdivacky = BuildCXXCastArgument(TypeRange.getBegin(), Ty.getNonReferenceType(), 250198092Srdivacky Kind, Method, Owned(Exprs[0])); 251198092Srdivacky if (CastArg.isInvalid()) 252198092Srdivacky return ExprError(); 253198092Srdivacky 254198092Srdivacky Exprs[0] = CastArg.takeAs<Expr>(); 255198092Srdivacky } 256198092Srdivacky 257193326Sed return Owned(new (Context) CXXFunctionalCastExpr(Ty.getNonReferenceType(), 258202879Srdivacky TInfo, TyBeginLoc, Kind, 259198092Srdivacky Exprs[0], RParenLoc)); 260193326Sed } 261193326Sed 262198092Srdivacky if (const RecordType *RT = Ty->getAs<RecordType>()) { 263193326Sed CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl()); 264193326Sed 265198092Srdivacky if (NumExprs > 1 || !Record->hasTrivialConstructor() || 266198092Srdivacky !Record->hasTrivialDestructor()) { 267203955Srdivacky InitializedEntity Entity = InitializedEntity::InitializeTemporary(Ty); 268203955Srdivacky InitializationKind Kind 269203955Srdivacky = NumExprs ? InitializationKind::CreateDirect(TypeRange.getBegin(), 270203955Srdivacky LParenLoc, RParenLoc) 271203955Srdivacky : InitializationKind::CreateValue(TypeRange.getBegin(), 272203955Srdivacky LParenLoc, RParenLoc); 273203955Srdivacky InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); 274203955Srdivacky OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, 275203955Srdivacky move(exprs)); 276193326Sed 277203955Srdivacky // FIXME: Improve AST representation? 278203955Srdivacky return move(Result); 279193326Sed } 280193326Sed 281193326Sed // Fall through to value-initialize an object of class type that 282193326Sed // doesn't have a user-declared default constructor. 283193326Sed } 284193326Sed 285193326Sed // C++ [expr.type.conv]p1: 286193326Sed // If the expression list specifies more than a single value, the type shall 287193326Sed // be a class with a suitably declared constructor. 288193326Sed // 289193326Sed if (NumExprs > 1) 290193326Sed return ExprError(Diag(CommaLocs[0], 291193326Sed diag::err_builtin_func_cast_more_than_one_arg) 292193326Sed << FullRange); 293193326Sed 294193326Sed assert(NumExprs == 0 && "Expected 0 expressions"); 295193326Sed // C++ [expr.type.conv]p2: 296193326Sed // The expression T(), where T is a simple-type-specifier for a non-array 297193326Sed // complete object type or the (possibly cv-qualified) void type, creates an 298193326Sed // rvalue of the specified type, which is value-initialized. 299193326Sed // 300193326Sed exprs.release(); 301193326Sed return Owned(new (Context) CXXZeroInitValueExpr(Ty, TyBeginLoc, RParenLoc)); 302193326Sed} 303193326Sed 304193326Sed 305193326Sed/// ActOnCXXNew - Parsed a C++ 'new' expression (C++ 5.3.4), as in e.g.: 306193326Sed/// @code new (memory) int[size][4] @endcode 307193326Sed/// or 308193326Sed/// @code ::new Foo(23, "hello") @endcode 309193326Sed/// For the interpretation of this heap of arguments, consult the base version. 310193326SedAction::OwningExprResult 311193326SedSema::ActOnCXXNew(SourceLocation StartLoc, bool UseGlobal, 312193326Sed SourceLocation PlacementLParen, MultiExprArg PlacementArgs, 313193326Sed SourceLocation PlacementRParen, bool ParenTypeId, 314193326Sed Declarator &D, SourceLocation ConstructorLParen, 315193326Sed MultiExprArg ConstructorArgs, 316198092Srdivacky SourceLocation ConstructorRParen) { 317193326Sed Expr *ArraySize = 0; 318193326Sed // If the specified type is an array, unwrap it and save the expression. 319193326Sed if (D.getNumTypeObjects() > 0 && 320193326Sed D.getTypeObject(0).Kind == DeclaratorChunk::Array) { 321193326Sed DeclaratorChunk &Chunk = D.getTypeObject(0); 322193326Sed if (Chunk.Arr.hasStatic) 323193326Sed return ExprError(Diag(Chunk.Loc, diag::err_static_illegal_in_new) 324193326Sed << D.getSourceRange()); 325193326Sed if (!Chunk.Arr.NumElts) 326193326Sed return ExprError(Diag(Chunk.Loc, diag::err_array_new_needs_size) 327193326Sed << D.getSourceRange()); 328198893Srdivacky 329198893Srdivacky if (ParenTypeId) { 330198893Srdivacky // Can't have dynamic array size when the type-id is in parentheses. 331198893Srdivacky Expr *NumElts = (Expr *)Chunk.Arr.NumElts; 332198893Srdivacky if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && 333198893Srdivacky !NumElts->isIntegerConstantExpr(Context)) { 334198893Srdivacky Diag(D.getTypeObject(0).Loc, diag::err_new_paren_array_nonconst) 335198893Srdivacky << NumElts->getSourceRange(); 336198893Srdivacky return ExprError(); 337198893Srdivacky } 338198893Srdivacky } 339198893Srdivacky 340193326Sed ArraySize = static_cast<Expr*>(Chunk.Arr.NumElts); 341198893Srdivacky D.DropFirstTypeObject(); 342193326Sed } 343193326Sed 344198092Srdivacky // Every dimension shall be of constant size. 345198893Srdivacky if (ArraySize) { 346198893Srdivacky for (unsigned I = 0, N = D.getNumTypeObjects(); I < N; ++I) { 347198092Srdivacky if (D.getTypeObject(I).Kind != DeclaratorChunk::Array) 348198092Srdivacky break; 349193326Sed 350198092Srdivacky DeclaratorChunk::ArrayTypeInfo &Array = D.getTypeObject(I).Arr; 351198092Srdivacky if (Expr *NumElts = (Expr *)Array.NumElts) { 352198092Srdivacky if (!NumElts->isTypeDependent() && !NumElts->isValueDependent() && 353198092Srdivacky !NumElts->isIntegerConstantExpr(Context)) { 354198092Srdivacky Diag(D.getTypeObject(I).Loc, diag::err_new_array_nonconst) 355198092Srdivacky << NumElts->getSourceRange(); 356198092Srdivacky return ExprError(); 357198092Srdivacky } 358198092Srdivacky } 359193326Sed } 360193326Sed } 361198893Srdivacky 362200583Srdivacky //FIXME: Store TypeSourceInfo in CXXNew expression. 363200583Srdivacky TypeSourceInfo *TInfo = 0; 364200583Srdivacky QualType AllocType = GetTypeForDeclarator(D, /*Scope=*/0, &TInfo); 365198092Srdivacky if (D.isInvalidType()) 366198092Srdivacky return ExprError(); 367199990Srdivacky 368198092Srdivacky return BuildCXXNew(StartLoc, UseGlobal, 369193326Sed PlacementLParen, 370198092Srdivacky move(PlacementArgs), 371193326Sed PlacementRParen, 372193326Sed ParenTypeId, 373198092Srdivacky AllocType, 374193326Sed D.getSourceRange().getBegin(), 375193326Sed D.getSourceRange(), 376193326Sed Owned(ArraySize), 377193326Sed ConstructorLParen, 378193326Sed move(ConstructorArgs), 379193326Sed ConstructorRParen); 380193326Sed} 381193326Sed 382198092SrdivackySema::OwningExprResult 383193326SedSema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal, 384193326Sed SourceLocation PlacementLParen, 385193326Sed MultiExprArg PlacementArgs, 386193326Sed SourceLocation PlacementRParen, 387198092Srdivacky bool ParenTypeId, 388193326Sed QualType AllocType, 389193326Sed SourceLocation TypeLoc, 390193326Sed SourceRange TypeRange, 391193326Sed ExprArg ArraySizeE, 392193326Sed SourceLocation ConstructorLParen, 393193326Sed MultiExprArg ConstructorArgs, 394193326Sed SourceLocation ConstructorRParen) { 395193326Sed if (CheckAllocatedType(AllocType, TypeLoc, TypeRange)) 396193326Sed return ExprError(); 397193326Sed 398193326Sed QualType ResultType = Context.getPointerType(AllocType); 399193326Sed 400193326Sed // That every array dimension except the first is constant was already 401193326Sed // checked by the type check above. 402193326Sed 403193326Sed // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral 404193326Sed // or enumeration type with a non-negative value." 405193326Sed Expr *ArraySize = (Expr *)ArraySizeE.get(); 406193326Sed if (ArraySize && !ArraySize->isTypeDependent()) { 407193326Sed QualType SizeType = ArraySize->getType(); 408193326Sed if (!SizeType->isIntegralType() && !SizeType->isEnumeralType()) 409193326Sed return ExprError(Diag(ArraySize->getSourceRange().getBegin(), 410193326Sed diag::err_array_size_not_integral) 411193326Sed << SizeType << ArraySize->getSourceRange()); 412193326Sed // Let's see if this is a constant < 0. If so, we reject it out of hand. 413193326Sed // We don't care about special rules, so we tell the machinery it's not 414193326Sed // evaluated - it gives us a result in more cases. 415193326Sed if (!ArraySize->isValueDependent()) { 416193326Sed llvm::APSInt Value; 417193326Sed if (ArraySize->isIntegerConstantExpr(Value, Context, 0, false)) { 418193326Sed if (Value < llvm::APSInt( 419198092Srdivacky llvm::APInt::getNullValue(Value.getBitWidth()), 420198092Srdivacky Value.isUnsigned())) 421193326Sed return ExprError(Diag(ArraySize->getSourceRange().getBegin(), 422193326Sed diag::err_typecheck_negative_array_size) 423193326Sed << ArraySize->getSourceRange()); 424193326Sed } 425193326Sed } 426198092Srdivacky 427198398Srdivacky ImpCastExprToType(ArraySize, Context.getSizeType(), 428198398Srdivacky CastExpr::CK_IntegralCast); 429193326Sed } 430193326Sed 431193326Sed FunctionDecl *OperatorNew = 0; 432193326Sed FunctionDecl *OperatorDelete = 0; 433193326Sed Expr **PlaceArgs = (Expr**)PlacementArgs.get(); 434193326Sed unsigned NumPlaceArgs = PlacementArgs.size(); 435199990Srdivacky 436193326Sed if (!AllocType->isDependentType() && 437193326Sed !Expr::hasAnyTypeDependentArguments(PlaceArgs, NumPlaceArgs) && 438193326Sed FindAllocationFunctions(StartLoc, 439193326Sed SourceRange(PlacementLParen, PlacementRParen), 440193326Sed UseGlobal, AllocType, ArraySize, PlaceArgs, 441193326Sed NumPlaceArgs, OperatorNew, OperatorDelete)) 442193326Sed return ExprError(); 443199990Srdivacky llvm::SmallVector<Expr *, 8> AllPlaceArgs; 444199990Srdivacky if (OperatorNew) { 445199990Srdivacky // Add default arguments, if any. 446199990Srdivacky const FunctionProtoType *Proto = 447199990Srdivacky OperatorNew->getType()->getAs<FunctionProtoType>(); 448199990Srdivacky VariadicCallType CallType = 449199990Srdivacky Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; 450199990Srdivacky bool Invalid = GatherArgumentsForCall(PlacementLParen, OperatorNew, 451199990Srdivacky Proto, 1, PlaceArgs, NumPlaceArgs, 452199990Srdivacky AllPlaceArgs, CallType); 453199990Srdivacky if (Invalid) 454199990Srdivacky return ExprError(); 455199990Srdivacky 456199990Srdivacky NumPlaceArgs = AllPlaceArgs.size(); 457199990Srdivacky if (NumPlaceArgs > 0) 458199990Srdivacky PlaceArgs = &AllPlaceArgs[0]; 459199990Srdivacky } 460199990Srdivacky 461193326Sed bool Init = ConstructorLParen.isValid(); 462193326Sed // --- Choosing a constructor --- 463193326Sed CXXConstructorDecl *Constructor = 0; 464193326Sed Expr **ConsArgs = (Expr**)ConstructorArgs.get(); 465193326Sed unsigned NumConsArgs = ConstructorArgs.size(); 466199482Srdivacky ASTOwningVector<&ActionBase::DeleteExpr> ConvertedConstructorArgs(*this); 467199482Srdivacky 468201361Srdivacky if (!AllocType->isDependentType() && 469201361Srdivacky !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) { 470201361Srdivacky // C++0x [expr.new]p15: 471201361Srdivacky // A new-expression that creates an object of type T initializes that 472201361Srdivacky // object as follows: 473201361Srdivacky InitializationKind Kind 474201361Srdivacky // - If the new-initializer is omitted, the object is default- 475201361Srdivacky // initialized (8.5); if no initialization is performed, 476201361Srdivacky // the object has indeterminate value 477201361Srdivacky = !Init? InitializationKind::CreateDefault(TypeLoc) 478201361Srdivacky // - Otherwise, the new-initializer is interpreted according to the 479201361Srdivacky // initialization rules of 8.5 for direct-initialization. 480201361Srdivacky : InitializationKind::CreateDirect(TypeLoc, 481201361Srdivacky ConstructorLParen, 482201361Srdivacky ConstructorRParen); 483201361Srdivacky 484201361Srdivacky InitializedEntity Entity 485201361Srdivacky = InitializedEntity::InitializeNew(StartLoc, AllocType); 486201361Srdivacky InitializationSequence InitSeq(*this, Entity, Kind, ConsArgs, NumConsArgs); 487201361Srdivacky OwningExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, 488201361Srdivacky move(ConstructorArgs)); 489201361Srdivacky if (FullInit.isInvalid()) 490193326Sed return ExprError(); 491201361Srdivacky 492201361Srdivacky // FullInit is our initializer; walk through it to determine if it's a 493201361Srdivacky // constructor call, which CXXNewExpr handles directly. 494201361Srdivacky if (Expr *FullInitExpr = (Expr *)FullInit.get()) { 495201361Srdivacky if (CXXBindTemporaryExpr *Binder 496201361Srdivacky = dyn_cast<CXXBindTemporaryExpr>(FullInitExpr)) 497201361Srdivacky FullInitExpr = Binder->getSubExpr(); 498201361Srdivacky if (CXXConstructExpr *Construct 499201361Srdivacky = dyn_cast<CXXConstructExpr>(FullInitExpr)) { 500201361Srdivacky Constructor = Construct->getConstructor(); 501201361Srdivacky for (CXXConstructExpr::arg_iterator A = Construct->arg_begin(), 502201361Srdivacky AEnd = Construct->arg_end(); 503201361Srdivacky A != AEnd; ++A) 504201361Srdivacky ConvertedConstructorArgs.push_back(A->Retain()); 505201361Srdivacky } else { 506201361Srdivacky // Take the converted initializer. 507201361Srdivacky ConvertedConstructorArgs.push_back(FullInit.release()); 508201361Srdivacky } 509201361Srdivacky } else { 510201361Srdivacky // No initialization required. 511201361Srdivacky } 512201361Srdivacky 513201361Srdivacky // Take the converted arguments and use them for the new expression. 514198092Srdivacky NumConsArgs = ConvertedConstructorArgs.size(); 515198092Srdivacky ConsArgs = (Expr **)ConvertedConstructorArgs.take(); 516193326Sed } 517201361Srdivacky 518193326Sed // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16) 519198398Srdivacky 520193326Sed PlacementArgs.release(); 521193326Sed ConstructorArgs.release(); 522193326Sed ArraySizeE.release(); 523203955Srdivacky return Owned(new (Context) CXXNewExpr(Context, UseGlobal, OperatorNew, 524203955Srdivacky PlaceArgs, NumPlaceArgs, ParenTypeId, 525203955Srdivacky ArraySize, Constructor, Init, 526203955Srdivacky ConsArgs, NumConsArgs, OperatorDelete, 527203955Srdivacky ResultType, StartLoc, 528203955Srdivacky Init ? ConstructorRParen : 529203955Srdivacky SourceLocation())); 530193326Sed} 531193326Sed 532193326Sed/// CheckAllocatedType - Checks that a type is suitable as the allocated type 533193326Sed/// in a new-expression. 534193326Sed/// dimension off and stores the size expression in ArraySize. 535193326Sedbool Sema::CheckAllocatedType(QualType AllocType, SourceLocation Loc, 536198092Srdivacky SourceRange R) { 537193326Sed // C++ 5.3.4p1: "[The] type shall be a complete object type, but not an 538193326Sed // abstract class type or array thereof. 539193326Sed if (AllocType->isFunctionType()) 540193326Sed return Diag(Loc, diag::err_bad_new_type) 541193326Sed << AllocType << 0 << R; 542193326Sed else if (AllocType->isReferenceType()) 543193326Sed return Diag(Loc, diag::err_bad_new_type) 544193326Sed << AllocType << 1 << R; 545193326Sed else if (!AllocType->isDependentType() && 546193326Sed RequireCompleteType(Loc, AllocType, 547198092Srdivacky PDiag(diag::err_new_incomplete_type) 548198092Srdivacky << R)) 549193326Sed return true; 550193326Sed else if (RequireNonAbstractType(Loc, AllocType, 551193326Sed diag::err_allocation_of_abstract_type)) 552193326Sed return true; 553193326Sed 554193326Sed return false; 555193326Sed} 556193326Sed 557193326Sed/// FindAllocationFunctions - Finds the overloads of operator new and delete 558193326Sed/// that are appropriate for the allocation. 559193326Sedbool Sema::FindAllocationFunctions(SourceLocation StartLoc, SourceRange Range, 560193326Sed bool UseGlobal, QualType AllocType, 561193326Sed bool IsArray, Expr **PlaceArgs, 562193326Sed unsigned NumPlaceArgs, 563193326Sed FunctionDecl *&OperatorNew, 564198092Srdivacky FunctionDecl *&OperatorDelete) { 565193326Sed // --- Choosing an allocation function --- 566193326Sed // C++ 5.3.4p8 - 14 & 18 567193326Sed // 1) If UseGlobal is true, only look in the global scope. Else, also look 568193326Sed // in the scope of the allocated class. 569193326Sed // 2) If an array size is given, look for operator new[], else look for 570193326Sed // operator new. 571193326Sed // 3) The first argument is always size_t. Append the arguments from the 572193326Sed // placement form. 573193326Sed // FIXME: Also find the appropriate delete operator. 574193326Sed 575193326Sed llvm::SmallVector<Expr*, 8> AllocArgs(1 + NumPlaceArgs); 576193326Sed // We don't care about the actual value of this argument. 577193326Sed // FIXME: Should the Sema create the expression and embed it in the syntax 578193326Sed // tree? Or should the consumer just recalculate the value? 579198092Srdivacky IntegerLiteral Size(llvm::APInt::getNullValue( 580198092Srdivacky Context.Target.getPointerWidth(0)), 581198092Srdivacky Context.getSizeType(), 582198092Srdivacky SourceLocation()); 583198092Srdivacky AllocArgs[0] = &Size; 584193326Sed std::copy(PlaceArgs, PlaceArgs + NumPlaceArgs, AllocArgs.begin() + 1); 585193326Sed 586193326Sed DeclarationName NewName = Context.DeclarationNames.getCXXOperatorName( 587193326Sed IsArray ? OO_Array_New : OO_New); 588193326Sed if (AllocType->isRecordType() && !UseGlobal) { 589198092Srdivacky CXXRecordDecl *Record 590198092Srdivacky = cast<CXXRecordDecl>(AllocType->getAs<RecordType>()->getDecl()); 591193326Sed // FIXME: We fail to find inherited overloads. 592193326Sed if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], 593193326Sed AllocArgs.size(), Record, /*AllowMissing=*/true, 594193326Sed OperatorNew)) 595193326Sed return true; 596193326Sed } 597193326Sed if (!OperatorNew) { 598193326Sed // Didn't find a member overload. Look for a global one. 599193326Sed DeclareGlobalNewDelete(); 600193326Sed DeclContext *TUDecl = Context.getTranslationUnitDecl(); 601193326Sed if (FindAllocationOverload(StartLoc, Range, NewName, &AllocArgs[0], 602193326Sed AllocArgs.size(), TUDecl, /*AllowMissing=*/false, 603193326Sed OperatorNew)) 604193326Sed return true; 605193326Sed } 606193326Sed 607193326Sed // FindAllocationOverload can change the passed in arguments, so we need to 608193326Sed // copy them back. 609193326Sed if (NumPlaceArgs > 0) 610193326Sed std::copy(&AllocArgs[1], AllocArgs.end(), PlaceArgs); 611198092Srdivacky 612193326Sed return false; 613193326Sed} 614193326Sed 615193326Sed/// FindAllocationOverload - Find an fitting overload for the allocation 616193326Sed/// function in the specified scope. 617193326Sedbool Sema::FindAllocationOverload(SourceLocation StartLoc, SourceRange Range, 618193326Sed DeclarationName Name, Expr** Args, 619193326Sed unsigned NumArgs, DeclContext *Ctx, 620198092Srdivacky bool AllowMissing, FunctionDecl *&Operator) { 621199482Srdivacky LookupResult R(*this, Name, StartLoc, LookupOrdinaryName); 622199482Srdivacky LookupQualifiedName(R, Ctx); 623198092Srdivacky if (R.empty()) { 624193326Sed if (AllowMissing) 625193326Sed return false; 626193326Sed return Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) 627193326Sed << Name << Range; 628193326Sed } 629193326Sed 630198092Srdivacky // FIXME: handle ambiguity 631198092Srdivacky 632203955Srdivacky OverloadCandidateSet Candidates(StartLoc); 633198092Srdivacky for (LookupResult::iterator Alloc = R.begin(), AllocEnd = R.end(); 634198092Srdivacky Alloc != AllocEnd; ++Alloc) { 635193326Sed // Even member operator new/delete are implicitly treated as 636193326Sed // static, so don't use AddMemberCandidate. 637203955Srdivacky 638203955Srdivacky if (FunctionTemplateDecl *FnTemplate = 639203955Srdivacky dyn_cast<FunctionTemplateDecl>((*Alloc)->getUnderlyingDecl())) { 640203955Srdivacky AddTemplateOverloadCandidate(FnTemplate, Alloc.getAccess(), 641203955Srdivacky /*ExplicitTemplateArgs=*/0, Args, NumArgs, 642203955Srdivacky Candidates, 643203955Srdivacky /*SuppressUserConversions=*/false); 644198092Srdivacky continue; 645203955Srdivacky } 646203955Srdivacky 647203955Srdivacky FunctionDecl *Fn = cast<FunctionDecl>((*Alloc)->getUnderlyingDecl()); 648203955Srdivacky AddOverloadCandidate(Fn, Alloc.getAccess(), Args, NumArgs, Candidates, 649203955Srdivacky /*SuppressUserConversions=*/false); 650193326Sed } 651193326Sed 652193326Sed // Do the resolution. 653193326Sed OverloadCandidateSet::iterator Best; 654194613Sed switch(BestViableFunction(Candidates, StartLoc, Best)) { 655193326Sed case OR_Success: { 656193326Sed // Got one! 657193326Sed FunctionDecl *FnDecl = Best->Function; 658193326Sed // The first argument is size_t, and the first parameter must be size_t, 659193326Sed // too. This is checked on declaration and can be assumed. (It can't be 660193326Sed // asserted on, though, since invalid decls are left in there.) 661199990Srdivacky // Whatch out for variadic allocator function. 662199990Srdivacky unsigned NumArgsInFnDecl = FnDecl->getNumParams(); 663199990Srdivacky for (unsigned i = 0; (i < NumArgs && i < NumArgsInFnDecl); ++i) { 664193326Sed if (PerformCopyInitialization(Args[i], 665193326Sed FnDecl->getParamDecl(i)->getType(), 666201361Srdivacky AA_Passing)) 667193326Sed return true; 668193326Sed } 669193326Sed Operator = FnDecl; 670193326Sed return false; 671193326Sed } 672193326Sed 673193326Sed case OR_No_Viable_Function: 674193326Sed Diag(StartLoc, diag::err_ovl_no_viable_function_in_call) 675193326Sed << Name << Range; 676202379Srdivacky PrintOverloadCandidates(Candidates, OCD_AllCandidates, Args, NumArgs); 677193326Sed return true; 678193326Sed 679193326Sed case OR_Ambiguous: 680193326Sed Diag(StartLoc, diag::err_ovl_ambiguous_call) 681193326Sed << Name << Range; 682202379Srdivacky PrintOverloadCandidates(Candidates, OCD_ViableCandidates, Args, NumArgs); 683193326Sed return true; 684193326Sed 685193326Sed case OR_Deleted: 686193326Sed Diag(StartLoc, diag::err_ovl_deleted_call) 687193326Sed << Best->Function->isDeleted() 688193326Sed << Name << Range; 689202379Srdivacky PrintOverloadCandidates(Candidates, OCD_AllCandidates, Args, NumArgs); 690193326Sed return true; 691193326Sed } 692193326Sed assert(false && "Unreachable, bad result from BestViableFunction"); 693193326Sed return true; 694193326Sed} 695193326Sed 696193326Sed 697193326Sed/// DeclareGlobalNewDelete - Declare the global forms of operator new and 698193326Sed/// delete. These are: 699193326Sed/// @code 700193326Sed/// void* operator new(std::size_t) throw(std::bad_alloc); 701193326Sed/// void* operator new[](std::size_t) throw(std::bad_alloc); 702193326Sed/// void operator delete(void *) throw(); 703193326Sed/// void operator delete[](void *) throw(); 704193326Sed/// @endcode 705193326Sed/// Note that the placement and nothrow forms of new are *not* implicitly 706193326Sed/// declared. Their use requires including \<new\>. 707198092Srdivackyvoid Sema::DeclareGlobalNewDelete() { 708193326Sed if (GlobalNewDeleteDeclared) 709193326Sed return; 710198092Srdivacky 711198092Srdivacky // C++ [basic.std.dynamic]p2: 712198092Srdivacky // [...] The following allocation and deallocation functions (18.4) are 713198092Srdivacky // implicitly declared in global scope in each translation unit of a 714198092Srdivacky // program 715198092Srdivacky // 716198092Srdivacky // void* operator new(std::size_t) throw(std::bad_alloc); 717198092Srdivacky // void* operator new[](std::size_t) throw(std::bad_alloc); 718198092Srdivacky // void operator delete(void*) throw(); 719198092Srdivacky // void operator delete[](void*) throw(); 720198092Srdivacky // 721198092Srdivacky // These implicit declarations introduce only the function names operator 722198092Srdivacky // new, operator new[], operator delete, operator delete[]. 723198092Srdivacky // 724198092Srdivacky // Here, we need to refer to std::bad_alloc, so we will implicitly declare 725198092Srdivacky // "std" or "bad_alloc" as necessary to form the exception specification. 726198092Srdivacky // However, we do not make these implicit declarations visible to name 727198092Srdivacky // lookup. 728198092Srdivacky if (!StdNamespace) { 729198092Srdivacky // The "std" namespace has not yet been defined, so build one implicitly. 730198092Srdivacky StdNamespace = NamespaceDecl::Create(Context, 731198092Srdivacky Context.getTranslationUnitDecl(), 732198092Srdivacky SourceLocation(), 733198092Srdivacky &PP.getIdentifierTable().get("std")); 734198092Srdivacky StdNamespace->setImplicit(true); 735198092Srdivacky } 736198092Srdivacky 737198092Srdivacky if (!StdBadAlloc) { 738198092Srdivacky // The "std::bad_alloc" class has not yet been declared, so build it 739198092Srdivacky // implicitly. 740198092Srdivacky StdBadAlloc = CXXRecordDecl::Create(Context, TagDecl::TK_class, 741198092Srdivacky StdNamespace, 742198092Srdivacky SourceLocation(), 743198092Srdivacky &PP.getIdentifierTable().get("bad_alloc"), 744198092Srdivacky SourceLocation(), 0); 745198092Srdivacky StdBadAlloc->setImplicit(true); 746198092Srdivacky } 747198092Srdivacky 748193326Sed GlobalNewDeleteDeclared = true; 749193326Sed 750193326Sed QualType VoidPtr = Context.getPointerType(Context.VoidTy); 751193326Sed QualType SizeT = Context.getSizeType(); 752201361Srdivacky bool AssumeSaneOperatorNew = getLangOptions().AssumeSaneOperatorNew; 753193326Sed 754193326Sed DeclareGlobalAllocationFunction( 755193326Sed Context.DeclarationNames.getCXXOperatorName(OO_New), 756201361Srdivacky VoidPtr, SizeT, AssumeSaneOperatorNew); 757193326Sed DeclareGlobalAllocationFunction( 758193326Sed Context.DeclarationNames.getCXXOperatorName(OO_Array_New), 759201361Srdivacky VoidPtr, SizeT, AssumeSaneOperatorNew); 760193326Sed DeclareGlobalAllocationFunction( 761193326Sed Context.DeclarationNames.getCXXOperatorName(OO_Delete), 762193326Sed Context.VoidTy, VoidPtr); 763193326Sed DeclareGlobalAllocationFunction( 764193326Sed Context.DeclarationNames.getCXXOperatorName(OO_Array_Delete), 765193326Sed Context.VoidTy, VoidPtr); 766193326Sed} 767193326Sed 768193326Sed/// DeclareGlobalAllocationFunction - Declares a single implicit global 769193326Sed/// allocation function if it doesn't already exist. 770193326Sedvoid Sema::DeclareGlobalAllocationFunction(DeclarationName Name, 771201361Srdivacky QualType Return, QualType Argument, 772201361Srdivacky bool AddMallocAttr) { 773193326Sed DeclContext *GlobalCtx = Context.getTranslationUnitDecl(); 774193326Sed 775193326Sed // Check if this function is already declared. 776193326Sed { 777193326Sed DeclContext::lookup_iterator Alloc, AllocEnd; 778195341Sed for (llvm::tie(Alloc, AllocEnd) = GlobalCtx->lookup(Name); 779193326Sed Alloc != AllocEnd; ++Alloc) { 780203955Srdivacky // Only look at non-template functions, as it is the predefined, 781203955Srdivacky // non-templated allocation function we are trying to declare here. 782203955Srdivacky if (FunctionDecl *Func = dyn_cast<FunctionDecl>(*Alloc)) { 783203955Srdivacky QualType InitialParamType = 784201361Srdivacky Context.getCanonicalType( 785203955Srdivacky Func->getParamDecl(0)->getType().getUnqualifiedType()); 786203955Srdivacky // FIXME: Do we need to check for default arguments here? 787203955Srdivacky if (Func->getNumParams() == 1 && InitialParamType == Argument) 788203955Srdivacky return; 789203955Srdivacky } 790193326Sed } 791193326Sed } 792193326Sed 793198092Srdivacky QualType BadAllocType; 794198092Srdivacky bool HasBadAllocExceptionSpec 795198092Srdivacky = (Name.getCXXOverloadedOperator() == OO_New || 796198092Srdivacky Name.getCXXOverloadedOperator() == OO_Array_New); 797198092Srdivacky if (HasBadAllocExceptionSpec) { 798198092Srdivacky assert(StdBadAlloc && "Must have std::bad_alloc declared"); 799198092Srdivacky BadAllocType = Context.getTypeDeclType(StdBadAlloc); 800198092Srdivacky } 801198092Srdivacky 802198092Srdivacky QualType FnType = Context.getFunctionType(Return, &Argument, 1, false, 0, 803198092Srdivacky true, false, 804198092Srdivacky HasBadAllocExceptionSpec? 1 : 0, 805198092Srdivacky &BadAllocType); 806193326Sed FunctionDecl *Alloc = 807193326Sed FunctionDecl::Create(Context, GlobalCtx, SourceLocation(), Name, 808200583Srdivacky FnType, /*TInfo=*/0, FunctionDecl::None, false, true); 809193326Sed Alloc->setImplicit(); 810201361Srdivacky 811201361Srdivacky if (AddMallocAttr) 812201361Srdivacky Alloc->addAttr(::new (Context) MallocAttr()); 813201361Srdivacky 814193326Sed ParmVarDecl *Param = ParmVarDecl::Create(Context, Alloc, SourceLocation(), 815200583Srdivacky 0, Argument, /*TInfo=*/0, 816198092Srdivacky VarDecl::None, 0); 817203955Srdivacky Alloc->setParams(&Param, 1); 818193326Sed 819193326Sed // FIXME: Also add this declaration to the IdentifierResolver, but 820193326Sed // make sure it is at the end of the chain to coincide with the 821193326Sed // global scope. 822195341Sed ((DeclContext *)TUScope->getEntity())->addDecl(Alloc); 823193326Sed} 824193326Sed 825199482Srdivackybool Sema::FindDeallocationFunction(SourceLocation StartLoc, CXXRecordDecl *RD, 826199482Srdivacky DeclarationName Name, 827199482Srdivacky FunctionDecl* &Operator) { 828199482Srdivacky LookupResult Found(*this, Name, StartLoc, LookupOrdinaryName); 829199482Srdivacky // Try to find operator delete/operator delete[] in class scope. 830199482Srdivacky LookupQualifiedName(Found, RD); 831199482Srdivacky 832199482Srdivacky if (Found.isAmbiguous()) 833199482Srdivacky return true; 834199482Srdivacky 835199482Srdivacky for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); 836199482Srdivacky F != FEnd; ++F) { 837199482Srdivacky if (CXXMethodDecl *Delete = dyn_cast<CXXMethodDecl>(*F)) 838199482Srdivacky if (Delete->isUsualDeallocationFunction()) { 839199482Srdivacky Operator = Delete; 840199482Srdivacky return false; 841199482Srdivacky } 842199482Srdivacky } 843199482Srdivacky 844199482Srdivacky // We did find operator delete/operator delete[] declarations, but 845199482Srdivacky // none of them were suitable. 846199482Srdivacky if (!Found.empty()) { 847199482Srdivacky Diag(StartLoc, diag::err_no_suitable_delete_member_function_found) 848199482Srdivacky << Name << RD; 849199482Srdivacky 850199482Srdivacky for (LookupResult::iterator F = Found.begin(), FEnd = Found.end(); 851199482Srdivacky F != FEnd; ++F) { 852199482Srdivacky Diag((*F)->getLocation(), 853199482Srdivacky diag::note_delete_member_function_declared_here) 854199482Srdivacky << Name; 855199482Srdivacky } 856199482Srdivacky 857199482Srdivacky return true; 858199482Srdivacky } 859199482Srdivacky 860199482Srdivacky // Look for a global declaration. 861199482Srdivacky DeclareGlobalNewDelete(); 862199482Srdivacky DeclContext *TUDecl = Context.getTranslationUnitDecl(); 863199482Srdivacky 864199482Srdivacky CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation()); 865199482Srdivacky Expr* DeallocArgs[1]; 866199482Srdivacky DeallocArgs[0] = &Null; 867199482Srdivacky if (FindAllocationOverload(StartLoc, SourceRange(), Name, 868199482Srdivacky DeallocArgs, 1, TUDecl, /*AllowMissing=*/false, 869199482Srdivacky Operator)) 870199482Srdivacky return true; 871199482Srdivacky 872199482Srdivacky assert(Operator && "Did not find a deallocation function!"); 873199482Srdivacky return false; 874199482Srdivacky} 875199482Srdivacky 876193326Sed/// ActOnCXXDelete - Parsed a C++ 'delete' expression (C++ 5.3.5), as in: 877193326Sed/// @code ::delete ptr; @endcode 878193326Sed/// or 879193326Sed/// @code delete [] ptr; @endcode 880193326SedAction::OwningExprResult 881193326SedSema::ActOnCXXDelete(SourceLocation StartLoc, bool UseGlobal, 882198092Srdivacky bool ArrayForm, ExprArg Operand) { 883198092Srdivacky // C++ [expr.delete]p1: 884198092Srdivacky // The operand shall have a pointer type, or a class type having a single 885198092Srdivacky // conversion function to a pointer type. The result has type void. 886198092Srdivacky // 887193326Sed // DR599 amends "pointer type" to "pointer to object type" in both cases. 888193326Sed 889198092Srdivacky FunctionDecl *OperatorDelete = 0; 890198092Srdivacky 891193326Sed Expr *Ex = (Expr *)Operand.get(); 892193326Sed if (!Ex->isTypeDependent()) { 893193326Sed QualType Type = Ex->getType(); 894193326Sed 895198092Srdivacky if (const RecordType *Record = Type->getAs<RecordType>()) { 896198092Srdivacky llvm::SmallVector<CXXConversionDecl *, 4> ObjectPtrConversions; 897198092Srdivacky CXXRecordDecl *RD = cast<CXXRecordDecl>(Record->getDecl()); 898202879Srdivacky const UnresolvedSetImpl *Conversions = RD->getVisibleConversionFunctions(); 899198092Srdivacky 900202879Srdivacky for (UnresolvedSetImpl::iterator I = Conversions->begin(), 901199990Srdivacky E = Conversions->end(); I != E; ++I) { 902198092Srdivacky // Skip over templated conversion functions; they aren't considered. 903199990Srdivacky if (isa<FunctionTemplateDecl>(*I)) 904198092Srdivacky continue; 905198092Srdivacky 906199990Srdivacky CXXConversionDecl *Conv = cast<CXXConversionDecl>(*I); 907198092Srdivacky 908198092Srdivacky QualType ConvType = Conv->getConversionType().getNonReferenceType(); 909198092Srdivacky if (const PointerType *ConvPtrType = ConvType->getAs<PointerType>()) 910198092Srdivacky if (ConvPtrType->getPointeeType()->isObjectType()) 911198092Srdivacky ObjectPtrConversions.push_back(Conv); 912198092Srdivacky } 913198092Srdivacky if (ObjectPtrConversions.size() == 1) { 914198092Srdivacky // We have a single conversion to a pointer-to-object type. Perform 915198092Srdivacky // that conversion. 916198092Srdivacky Operand.release(); 917198092Srdivacky if (!PerformImplicitConversion(Ex, 918198092Srdivacky ObjectPtrConversions.front()->getConversionType(), 919201361Srdivacky AA_Converting)) { 920198092Srdivacky Operand = Owned(Ex); 921198092Srdivacky Type = Ex->getType(); 922198092Srdivacky } 923198092Srdivacky } 924198092Srdivacky else if (ObjectPtrConversions.size() > 1) { 925198092Srdivacky Diag(StartLoc, diag::err_ambiguous_delete_operand) 926198092Srdivacky << Type << Ex->getSourceRange(); 927198092Srdivacky for (unsigned i= 0; i < ObjectPtrConversions.size(); i++) { 928198092Srdivacky CXXConversionDecl *Conv = ObjectPtrConversions[i]; 929202379Srdivacky NoteOverloadCandidate(Conv); 930198092Srdivacky } 931198092Srdivacky return ExprError(); 932198092Srdivacky } 933193326Sed } 934193326Sed 935193326Sed if (!Type->isPointerType()) 936193326Sed return ExprError(Diag(StartLoc, diag::err_delete_operand) 937193326Sed << Type << Ex->getSourceRange()); 938193326Sed 939198092Srdivacky QualType Pointee = Type->getAs<PointerType>()->getPointeeType(); 940193326Sed if (Pointee->isFunctionType() || Pointee->isVoidType()) 941193326Sed return ExprError(Diag(StartLoc, diag::err_delete_operand) 942193326Sed << Type << Ex->getSourceRange()); 943193326Sed else if (!Pointee->isDependentType() && 944198092Srdivacky RequireCompleteType(StartLoc, Pointee, 945198092Srdivacky PDiag(diag::warn_delete_incomplete) 946198092Srdivacky << Ex->getSourceRange())) 947193326Sed return ExprError(); 948193326Sed 949198092Srdivacky // C++ [expr.delete]p2: 950198092Srdivacky // [Note: a pointer to a const type can be the operand of a 951198092Srdivacky // delete-expression; it is not necessary to cast away the constness 952198092Srdivacky // (5.2.11) of the pointer expression before it is used as the operand 953198092Srdivacky // of the delete-expression. ] 954198092Srdivacky ImpCastExprToType(Ex, Context.getPointerType(Context.VoidTy), 955198092Srdivacky CastExpr::CK_NoOp); 956198092Srdivacky 957198092Srdivacky // Update the operand. 958198092Srdivacky Operand.take(); 959198092Srdivacky Operand = ExprArg(*this, Ex); 960198092Srdivacky 961198092Srdivacky DeclarationName DeleteName = Context.DeclarationNames.getCXXOperatorName( 962198092Srdivacky ArrayForm ? OO_Array_Delete : OO_Delete); 963198092Srdivacky 964199482Srdivacky if (const RecordType *RT = Pointee->getAs<RecordType>()) { 965199482Srdivacky CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 966199482Srdivacky 967199482Srdivacky if (!UseGlobal && 968199482Srdivacky FindDeallocationFunction(StartLoc, RD, DeleteName, OperatorDelete)) 969199482Srdivacky return ExprError(); 970198092Srdivacky 971199482Srdivacky if (!RD->hasTrivialDestructor()) 972199482Srdivacky if (const CXXDestructorDecl *Dtor = RD->getDestructor(Context)) 973198092Srdivacky MarkDeclarationReferenced(StartLoc, 974198092Srdivacky const_cast<CXXDestructorDecl*>(Dtor)); 975198092Srdivacky } 976199482Srdivacky 977198092Srdivacky if (!OperatorDelete) { 978199482Srdivacky // Look for a global declaration. 979198092Srdivacky DeclareGlobalNewDelete(); 980198092Srdivacky DeclContext *TUDecl = Context.getTranslationUnitDecl(); 981198092Srdivacky if (FindAllocationOverload(StartLoc, SourceRange(), DeleteName, 982198092Srdivacky &Ex, 1, TUDecl, /*AllowMissing=*/false, 983198092Srdivacky OperatorDelete)) 984198092Srdivacky return ExprError(); 985198092Srdivacky } 986198092Srdivacky 987193326Sed // FIXME: Check access and ambiguity of operator delete and destructor. 988193326Sed } 989193326Sed 990193326Sed Operand.release(); 991193326Sed return Owned(new (Context) CXXDeleteExpr(Context.VoidTy, UseGlobal, ArrayForm, 992198092Srdivacky OperatorDelete, Ex, StartLoc)); 993193326Sed} 994193326Sed 995199990Srdivacky/// \brief Check the use of the given variable as a C++ condition in an if, 996199990Srdivacky/// while, do-while, or switch statement. 997199990SrdivackyAction::OwningExprResult Sema::CheckConditionVariable(VarDecl *ConditionVar) { 998199990Srdivacky QualType T = ConditionVar->getType(); 999199990Srdivacky 1000199990Srdivacky // C++ [stmt.select]p2: 1001199990Srdivacky // The declarator shall not specify a function or an array. 1002199990Srdivacky if (T->isFunctionType()) 1003199990Srdivacky return ExprError(Diag(ConditionVar->getLocation(), 1004199990Srdivacky diag::err_invalid_use_of_function_type) 1005199990Srdivacky << ConditionVar->getSourceRange()); 1006199990Srdivacky else if (T->isArrayType()) 1007199990Srdivacky return ExprError(Diag(ConditionVar->getLocation(), 1008199990Srdivacky diag::err_invalid_use_of_array_type) 1009199990Srdivacky << ConditionVar->getSourceRange()); 1010193326Sed 1011199990Srdivacky return Owned(DeclRefExpr::Create(Context, 0, SourceRange(), ConditionVar, 1012199990Srdivacky ConditionVar->getLocation(), 1013199990Srdivacky ConditionVar->getType().getNonReferenceType())); 1014193326Sed} 1015193326Sed 1016193326Sed/// CheckCXXBooleanCondition - Returns true if a conversion to bool is invalid. 1017193326Sedbool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { 1018193326Sed // C++ 6.4p4: 1019193326Sed // The value of a condition that is an initialized declaration in a statement 1020193326Sed // other than a switch statement is the value of the declared variable 1021193326Sed // implicitly converted to type bool. If that conversion is ill-formed, the 1022193326Sed // program is ill-formed. 1023193326Sed // The value of a condition that is an expression is the value of the 1024193326Sed // expression, implicitly converted to bool. 1025193326Sed // 1026193326Sed return PerformContextuallyConvertToBool(CondExpr); 1027193326Sed} 1028193326Sed 1029193326Sed/// Helper function to determine whether this is the (deprecated) C++ 1030193326Sed/// conversion from a string literal to a pointer to non-const char or 1031193326Sed/// non-const wchar_t (for narrow and wide string literals, 1032193326Sed/// respectively). 1033198092Srdivackybool 1034193326SedSema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { 1035193326Sed // Look inside the implicit cast, if it exists. 1036193326Sed if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) 1037193326Sed From = Cast->getSubExpr(); 1038193326Sed 1039193326Sed // A string literal (2.13.4) that is not a wide string literal can 1040193326Sed // be converted to an rvalue of type "pointer to char"; a wide 1041193326Sed // string literal can be converted to an rvalue of type "pointer 1042193326Sed // to wchar_t" (C++ 4.2p2). 1043193326Sed if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) 1044198092Srdivacky if (const PointerType *ToPtrType = ToType->getAs<PointerType>()) 1045198092Srdivacky if (const BuiltinType *ToPointeeType 1046198092Srdivacky = ToPtrType->getPointeeType()->getAs<BuiltinType>()) { 1047193326Sed // This conversion is considered only when there is an 1048193326Sed // explicit appropriate pointer target type (C++ 4.2p2). 1049198092Srdivacky if (!ToPtrType->getPointeeType().hasQualifiers() && 1050193326Sed ((StrLit->isWide() && ToPointeeType->isWideCharType()) || 1051193326Sed (!StrLit->isWide() && 1052193326Sed (ToPointeeType->getKind() == BuiltinType::Char_U || 1053193326Sed ToPointeeType->getKind() == BuiltinType::Char_S)))) 1054193326Sed return true; 1055193326Sed } 1056193326Sed 1057193326Sed return false; 1058193326Sed} 1059193326Sed 1060193326Sed/// PerformImplicitConversion - Perform an implicit conversion of the 1061193326Sed/// expression From to the type ToType. Returns true if there was an 1062193326Sed/// error, false otherwise. The expression From is replaced with the 1063193326Sed/// converted expression. Flavor is the kind of conversion we're 1064193326Sed/// performing, used in the error message. If @p AllowExplicit, 1065193326Sed/// explicit user-defined conversions are permitted. @p Elidable should be true 1066193326Sed/// when called for copies which may be elided (C++ 12.8p15). C++0x overload 1067193326Sed/// resolution works differently in that case. 1068193326Sedbool 1069193326SedSema::PerformImplicitConversion(Expr *&From, QualType ToType, 1070201361Srdivacky AssignmentAction Action, bool AllowExplicit, 1071198092Srdivacky bool Elidable) { 1072193326Sed ImplicitConversionSequence ICS; 1073201361Srdivacky return PerformImplicitConversion(From, ToType, Action, AllowExplicit, 1074198092Srdivacky Elidable, ICS); 1075198092Srdivacky} 1076198092Srdivacky 1077198092Srdivackybool 1078198092SrdivackySema::PerformImplicitConversion(Expr *&From, QualType ToType, 1079201361Srdivacky AssignmentAction Action, bool AllowExplicit, 1080198092Srdivacky bool Elidable, 1081198092Srdivacky ImplicitConversionSequence& ICS) { 1082202379Srdivacky ICS.setBad(); 1083202379Srdivacky ICS.Bad.init(BadConversionSequence::no_conversion, From, ToType); 1084193326Sed if (Elidable && getLangOptions().CPlusPlus0x) { 1085198092Srdivacky ICS = TryImplicitConversion(From, ToType, 1086198092Srdivacky /*SuppressUserConversions=*/false, 1087198092Srdivacky AllowExplicit, 1088198092Srdivacky /*ForceRValue=*/true, 1089198092Srdivacky /*InOverloadResolution=*/false); 1090193326Sed } 1091202379Srdivacky if (ICS.isBad()) { 1092198092Srdivacky ICS = TryImplicitConversion(From, ToType, 1093198092Srdivacky /*SuppressUserConversions=*/false, 1094198092Srdivacky AllowExplicit, 1095198092Srdivacky /*ForceRValue=*/false, 1096198092Srdivacky /*InOverloadResolution=*/false); 1097193326Sed } 1098201361Srdivacky return PerformImplicitConversion(From, ToType, ICS, Action); 1099193326Sed} 1100193326Sed 1101193326Sed/// PerformImplicitConversion - Perform an implicit conversion of the 1102193326Sed/// expression From to the type ToType using the pre-computed implicit 1103193326Sed/// conversion sequence ICS. Returns true if there was an error, false 1104193326Sed/// otherwise. The expression From is replaced with the converted 1105201361Srdivacky/// expression. Action is the kind of conversion we're performing, 1106193326Sed/// used in the error message. 1107193326Sedbool 1108193326SedSema::PerformImplicitConversion(Expr *&From, QualType ToType, 1109193326Sed const ImplicitConversionSequence &ICS, 1110201361Srdivacky AssignmentAction Action, bool IgnoreBaseAccess) { 1111202379Srdivacky switch (ICS.getKind()) { 1112193326Sed case ImplicitConversionSequence::StandardConversion: 1113201361Srdivacky if (PerformImplicitConversion(From, ToType, ICS.Standard, Action, 1114199482Srdivacky IgnoreBaseAccess)) 1115193326Sed return true; 1116193326Sed break; 1117193326Sed 1118198092Srdivacky case ImplicitConversionSequence::UserDefinedConversion: { 1119198092Srdivacky 1120198092Srdivacky FunctionDecl *FD = ICS.UserDefined.ConversionFunction; 1121198092Srdivacky CastExpr::CastKind CastKind = CastExpr::CK_Unknown; 1122198092Srdivacky QualType BeforeToType; 1123198092Srdivacky if (const CXXConversionDecl *Conv = dyn_cast<CXXConversionDecl>(FD)) { 1124198092Srdivacky CastKind = CastExpr::CK_UserDefinedConversion; 1125198092Srdivacky 1126198092Srdivacky // If the user-defined conversion is specified by a conversion function, 1127198092Srdivacky // the initial standard conversion sequence converts the source type to 1128198092Srdivacky // the implicit object parameter of the conversion function. 1129198092Srdivacky BeforeToType = Context.getTagDeclType(Conv->getParent()); 1130198092Srdivacky } else if (const CXXConstructorDecl *Ctor = 1131198092Srdivacky dyn_cast<CXXConstructorDecl>(FD)) { 1132198092Srdivacky CastKind = CastExpr::CK_ConstructorConversion; 1133199482Srdivacky // Do no conversion if dealing with ... for the first conversion. 1134199990Srdivacky if (!ICS.UserDefined.EllipsisConversion) { 1135199482Srdivacky // If the user-defined conversion is specified by a constructor, the 1136199482Srdivacky // initial standard conversion sequence converts the source type to the 1137199482Srdivacky // type required by the argument of the constructor 1138199990Srdivacky BeforeToType = Ctor->getParamDecl(0)->getType().getNonReferenceType(); 1139199990Srdivacky } 1140198092Srdivacky } 1141198092Srdivacky else 1142198092Srdivacky assert(0 && "Unknown conversion function kind!"); 1143199482Srdivacky // Whatch out for elipsis conversion. 1144199482Srdivacky if (!ICS.UserDefined.EllipsisConversion) { 1145199482Srdivacky if (PerformImplicitConversion(From, BeforeToType, 1146201361Srdivacky ICS.UserDefined.Before, AA_Converting, 1147199482Srdivacky IgnoreBaseAccess)) 1148199482Srdivacky return true; 1149199482Srdivacky } 1150198092Srdivacky 1151198092Srdivacky OwningExprResult CastArg 1152198092Srdivacky = BuildCXXCastArgument(From->getLocStart(), 1153198092Srdivacky ToType.getNonReferenceType(), 1154198092Srdivacky CastKind, cast<CXXMethodDecl>(FD), 1155198092Srdivacky Owned(From)); 1156198092Srdivacky 1157198092Srdivacky if (CastArg.isInvalid()) 1158198092Srdivacky return true; 1159199990Srdivacky 1160199990Srdivacky From = CastArg.takeAs<Expr>(); 1161199990Srdivacky 1162199990Srdivacky return PerformImplicitConversion(From, ToType, ICS.UserDefined.After, 1163201361Srdivacky AA_Converting, IgnoreBaseAccess); 1164198398Srdivacky } 1165202379Srdivacky 1166202379Srdivacky case ImplicitConversionSequence::AmbiguousConversion: 1167202379Srdivacky DiagnoseAmbiguousConversion(ICS, From->getExprLoc(), 1168202379Srdivacky PDiag(diag::err_typecheck_ambiguous_condition) 1169202379Srdivacky << From->getSourceRange()); 1170202379Srdivacky return true; 1171198398Srdivacky 1172193326Sed case ImplicitConversionSequence::EllipsisConversion: 1173193326Sed assert(false && "Cannot perform an ellipsis conversion"); 1174193326Sed return false; 1175193326Sed 1176193326Sed case ImplicitConversionSequence::BadConversion: 1177193326Sed return true; 1178193326Sed } 1179193326Sed 1180193326Sed // Everything went well. 1181193326Sed return false; 1182193326Sed} 1183193326Sed 1184193326Sed/// PerformImplicitConversion - Perform an implicit conversion of the 1185193326Sed/// expression From to the type ToType by following the standard 1186193326Sed/// conversion sequence SCS. Returns true if there was an error, false 1187193326Sed/// otherwise. The expression From is replaced with the converted 1188193326Sed/// expression. Flavor is the context in which we're performing this 1189193326Sed/// conversion, for use in error messages. 1190198092Srdivackybool 1191193326SedSema::PerformImplicitConversion(Expr *&From, QualType ToType, 1192193326Sed const StandardConversionSequence& SCS, 1193201361Srdivacky AssignmentAction Action, bool IgnoreBaseAccess) { 1194193326Sed // Overall FIXME: we are recomputing too many types here and doing far too 1195193326Sed // much extra work. What this means is that we need to keep track of more 1196193326Sed // information that is computed when we try the implicit conversion initially, 1197193326Sed // so that we don't need to recompute anything here. 1198193326Sed QualType FromType = From->getType(); 1199193326Sed 1200193326Sed if (SCS.CopyConstructor) { 1201193326Sed // FIXME: When can ToType be a reference type? 1202193326Sed assert(!ToType->isReferenceType()); 1203198092Srdivacky if (SCS.Second == ICK_Derived_To_Base) { 1204198092Srdivacky ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); 1205198092Srdivacky if (CompleteConstructorCall(cast<CXXConstructorDecl>(SCS.CopyConstructor), 1206198092Srdivacky MultiExprArg(*this, (void **)&From, 1), 1207198092Srdivacky /*FIXME:ConstructLoc*/SourceLocation(), 1208198092Srdivacky ConstructorArgs)) 1209198092Srdivacky return true; 1210198092Srdivacky OwningExprResult FromResult = 1211198092Srdivacky BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), 1212198092Srdivacky ToType, SCS.CopyConstructor, 1213198092Srdivacky move_arg(ConstructorArgs)); 1214198092Srdivacky if (FromResult.isInvalid()) 1215198092Srdivacky return true; 1216198092Srdivacky From = FromResult.takeAs<Expr>(); 1217198092Srdivacky return false; 1218198092Srdivacky } 1219198092Srdivacky OwningExprResult FromResult = 1220198092Srdivacky BuildCXXConstructExpr(/*FIXME:ConstructLoc*/SourceLocation(), 1221198092Srdivacky ToType, SCS.CopyConstructor, 1222198092Srdivacky MultiExprArg(*this, (void**)&From, 1)); 1223198092Srdivacky 1224198092Srdivacky if (FromResult.isInvalid()) 1225198092Srdivacky return true; 1226198092Srdivacky 1227198092Srdivacky From = FromResult.takeAs<Expr>(); 1228193326Sed return false; 1229193326Sed } 1230193326Sed 1231193326Sed // Perform the first implicit conversion. 1232193326Sed switch (SCS.First) { 1233193326Sed case ICK_Identity: 1234193326Sed case ICK_Lvalue_To_Rvalue: 1235193326Sed // Nothing to do. 1236193326Sed break; 1237193326Sed 1238193326Sed case ICK_Array_To_Pointer: 1239193326Sed FromType = Context.getArrayDecayedType(FromType); 1240198092Srdivacky ImpCastExprToType(From, FromType, CastExpr::CK_ArrayToPointerDecay); 1241193326Sed break; 1242193326Sed 1243193326Sed case ICK_Function_To_Pointer: 1244193326Sed if (Context.getCanonicalType(FromType) == Context.OverloadTy) { 1245193326Sed FunctionDecl *Fn = ResolveAddressOfOverloadedFunction(From, ToType, true); 1246193326Sed if (!Fn) 1247193326Sed return true; 1248193326Sed 1249193326Sed if (DiagnoseUseOfDecl(Fn, From->getSourceRange().getBegin())) 1250193326Sed return true; 1251193326Sed 1252198398Srdivacky From = FixOverloadedFunctionReference(From, Fn); 1253193326Sed FromType = From->getType(); 1254198398Srdivacky 1255198398Srdivacky // If there's already an address-of operator in the expression, we have 1256198398Srdivacky // the right type already, and the code below would just introduce an 1257198398Srdivacky // invalid additional pointer level. 1258198398Srdivacky if (FromType->isPointerType() || FromType->isMemberFunctionPointerType()) 1259198398Srdivacky break; 1260193326Sed } 1261193326Sed FromType = Context.getPointerType(FromType); 1262198092Srdivacky ImpCastExprToType(From, FromType, CastExpr::CK_FunctionToPointerDecay); 1263193326Sed break; 1264193326Sed 1265193326Sed default: 1266193326Sed assert(false && "Improper first standard conversion"); 1267193326Sed break; 1268193326Sed } 1269193326Sed 1270193326Sed // Perform the second implicit conversion 1271193326Sed switch (SCS.Second) { 1272193326Sed case ICK_Identity: 1273198092Srdivacky // If both sides are functions (or pointers/references to them), there could 1274198092Srdivacky // be incompatible exception declarations. 1275198092Srdivacky if (CheckExceptionSpecCompatibility(From, ToType)) 1276198092Srdivacky return true; 1277198092Srdivacky // Nothing else to do. 1278193326Sed break; 1279193326Sed 1280200583Srdivacky case ICK_NoReturn_Adjustment: 1281200583Srdivacky // If both sides are functions (or pointers/references to them), there could 1282200583Srdivacky // be incompatible exception declarations. 1283200583Srdivacky if (CheckExceptionSpecCompatibility(From, ToType)) 1284200583Srdivacky return true; 1285200583Srdivacky 1286200583Srdivacky ImpCastExprToType(From, Context.getNoReturnType(From->getType(), false), 1287200583Srdivacky CastExpr::CK_NoOp); 1288200583Srdivacky break; 1289200583Srdivacky 1290193326Sed case ICK_Integral_Promotion: 1291198398Srdivacky case ICK_Integral_Conversion: 1292198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_IntegralCast); 1293198398Srdivacky break; 1294198398Srdivacky 1295193326Sed case ICK_Floating_Promotion: 1296198398Srdivacky case ICK_Floating_Conversion: 1297198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_FloatingCast); 1298198398Srdivacky break; 1299198398Srdivacky 1300193326Sed case ICK_Complex_Promotion: 1301193326Sed case ICK_Complex_Conversion: 1302198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); 1303198398Srdivacky break; 1304198398Srdivacky 1305193326Sed case ICK_Floating_Integral: 1306198398Srdivacky if (ToType->isFloatingType()) 1307198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_IntegralToFloating); 1308198398Srdivacky else 1309198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_FloatingToIntegral); 1310198398Srdivacky break; 1311198398Srdivacky 1312193326Sed case ICK_Complex_Real: 1313198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_Unknown); 1314198398Srdivacky break; 1315198398Srdivacky 1316193326Sed case ICK_Compatible_Conversion: 1317198398Srdivacky ImpCastExprToType(From, ToType, CastExpr::CK_NoOp); 1318193326Sed break; 1319193326Sed 1320198092Srdivacky case ICK_Pointer_Conversion: { 1321193326Sed if (SCS.IncompatibleObjC) { 1322193326Sed // Diagnose incompatible Objective-C conversions 1323198092Srdivacky Diag(From->getSourceRange().getBegin(), 1324193326Sed diag::ext_typecheck_convert_incompatible_pointer) 1325201361Srdivacky << From->getType() << ToType << Action 1326193326Sed << From->getSourceRange(); 1327193326Sed } 1328193326Sed 1329198092Srdivacky 1330198092Srdivacky CastExpr::CastKind Kind = CastExpr::CK_Unknown; 1331199482Srdivacky if (CheckPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) 1332193326Sed return true; 1333198092Srdivacky ImpCastExprToType(From, ToType, Kind); 1334193326Sed break; 1335198092Srdivacky } 1336198092Srdivacky 1337198092Srdivacky case ICK_Pointer_Member: { 1338198092Srdivacky CastExpr::CastKind Kind = CastExpr::CK_Unknown; 1339199482Srdivacky if (CheckMemberPointerConversion(From, ToType, Kind, IgnoreBaseAccess)) 1340193326Sed return true; 1341198092Srdivacky if (CheckExceptionSpecCompatibility(From, ToType)) 1342198092Srdivacky return true; 1343198092Srdivacky ImpCastExprToType(From, ToType, Kind); 1344193326Sed break; 1345198092Srdivacky } 1346199990Srdivacky case ICK_Boolean_Conversion: { 1347199990Srdivacky CastExpr::CastKind Kind = CastExpr::CK_Unknown; 1348199990Srdivacky if (FromType->isMemberPointerType()) 1349199990Srdivacky Kind = CastExpr::CK_MemberPointerToBoolean; 1350199990Srdivacky 1351199990Srdivacky ImpCastExprToType(From, Context.BoolTy, Kind); 1352193326Sed break; 1353199990Srdivacky } 1354193326Sed 1355199482Srdivacky case ICK_Derived_To_Base: 1356199482Srdivacky if (CheckDerivedToBaseConversion(From->getType(), 1357199482Srdivacky ToType.getNonReferenceType(), 1358199482Srdivacky From->getLocStart(), 1359199482Srdivacky From->getSourceRange(), 1360199482Srdivacky IgnoreBaseAccess)) 1361199482Srdivacky return true; 1362199482Srdivacky ImpCastExprToType(From, ToType.getNonReferenceType(), 1363199482Srdivacky CastExpr::CK_DerivedToBase); 1364199482Srdivacky break; 1365199482Srdivacky 1366193326Sed default: 1367193326Sed assert(false && "Improper second standard conversion"); 1368193326Sed break; 1369193326Sed } 1370193326Sed 1371193326Sed switch (SCS.Third) { 1372193326Sed case ICK_Identity: 1373193326Sed // Nothing to do. 1374193326Sed break; 1375193326Sed 1376193326Sed case ICK_Qualification: 1377193326Sed // FIXME: Not sure about lvalue vs rvalue here in the presence of rvalue 1378193326Sed // references. 1379198092Srdivacky ImpCastExprToType(From, ToType.getNonReferenceType(), 1380198398Srdivacky CastExpr::CK_NoOp, 1381193326Sed ToType->isLValueReferenceType()); 1382193326Sed break; 1383199482Srdivacky 1384193326Sed default: 1385193326Sed assert(false && "Improper second standard conversion"); 1386193326Sed break; 1387193326Sed } 1388193326Sed 1389193326Sed return false; 1390193326Sed} 1391193326Sed 1392193326SedSema::OwningExprResult Sema::ActOnUnaryTypeTrait(UnaryTypeTrait OTT, 1393193326Sed SourceLocation KWLoc, 1394193326Sed SourceLocation LParen, 1395193326Sed TypeTy *Ty, 1396193326Sed SourceLocation RParen) { 1397198092Srdivacky QualType T = GetTypeFromParser(Ty); 1398193326Sed 1399198092Srdivacky // According to http://gcc.gnu.org/onlinedocs/gcc/Type-Traits.html 1400198092Srdivacky // all traits except __is_class, __is_enum and __is_union require a the type 1401198092Srdivacky // to be complete. 1402198092Srdivacky if (OTT != UTT_IsClass && OTT != UTT_IsEnum && OTT != UTT_IsUnion) { 1403198092Srdivacky if (RequireCompleteType(KWLoc, T, 1404198092Srdivacky diag::err_incomplete_type_used_in_type_trait_expr)) 1405198092Srdivacky return ExprError(); 1406198092Srdivacky } 1407198092Srdivacky 1408193326Sed // There is no point in eagerly computing the value. The traits are designed 1409193326Sed // to be used from type trait templates, so Ty will be a template parameter 1410193326Sed // 99% of the time. 1411198092Srdivacky return Owned(new (Context) UnaryTypeTraitExpr(KWLoc, OTT, T, 1412198092Srdivacky RParen, Context.BoolTy)); 1413193326Sed} 1414193326Sed 1415193326SedQualType Sema::CheckPointerToMemberOperands( 1416198092Srdivacky Expr *&lex, Expr *&rex, SourceLocation Loc, bool isIndirect) { 1417193326Sed const char *OpSpelling = isIndirect ? "->*" : ".*"; 1418193326Sed // C++ 5.5p2 1419193326Sed // The binary operator .* [p3: ->*] binds its second operand, which shall 1420193326Sed // be of type "pointer to member of T" (where T is a completely-defined 1421193326Sed // class type) [...] 1422193326Sed QualType RType = rex->getType(); 1423198092Srdivacky const MemberPointerType *MemPtr = RType->getAs<MemberPointerType>(); 1424193326Sed if (!MemPtr) { 1425193326Sed Diag(Loc, diag::err_bad_memptr_rhs) 1426193326Sed << OpSpelling << RType << rex->getSourceRange(); 1427193326Sed return QualType(); 1428198092Srdivacky } 1429193326Sed 1430193326Sed QualType Class(MemPtr->getClass(), 0); 1431193326Sed 1432193326Sed // C++ 5.5p2 1433193326Sed // [...] to its first operand, which shall be of class T or of a class of 1434193326Sed // which T is an unambiguous and accessible base class. [p3: a pointer to 1435193326Sed // such a class] 1436193326Sed QualType LType = lex->getType(); 1437193326Sed if (isIndirect) { 1438198092Srdivacky if (const PointerType *Ptr = LType->getAs<PointerType>()) 1439193326Sed LType = Ptr->getPointeeType().getNonReferenceType(); 1440193326Sed else { 1441193326Sed Diag(Loc, diag::err_bad_memptr_lhs) 1442198893Srdivacky << OpSpelling << 1 << LType 1443198893Srdivacky << CodeModificationHint::CreateReplacement(SourceRange(Loc), ".*"); 1444193326Sed return QualType(); 1445193326Sed } 1446193326Sed } 1447193326Sed 1448199482Srdivacky if (!Context.hasSameUnqualifiedType(Class, LType)) { 1449198092Srdivacky CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/false, 1450198092Srdivacky /*DetectVirtual=*/false); 1451193326Sed // FIXME: Would it be useful to print full ambiguity paths, or is that 1452193326Sed // overkill? 1453193326Sed if (!IsDerivedFrom(LType, Class, Paths) || 1454193326Sed Paths.isAmbiguous(Context.getCanonicalType(Class))) { 1455193326Sed Diag(Loc, diag::err_bad_memptr_lhs) << OpSpelling 1456202879Srdivacky << (int)isIndirect << lex->getType(); 1457193326Sed return QualType(); 1458193326Sed } 1459202879Srdivacky // Cast LHS to type of use. 1460202879Srdivacky QualType UseType = isIndirect ? Context.getPointerType(Class) : Class; 1461202879Srdivacky bool isLValue = !isIndirect && lex->isLvalue(Context) == Expr::LV_Valid; 1462202879Srdivacky ImpCastExprToType(lex, UseType, CastExpr::CK_DerivedToBase, isLValue); 1463193326Sed } 1464193326Sed 1465199512Srdivacky if (isa<CXXZeroInitValueExpr>(rex->IgnoreParens())) { 1466199512Srdivacky // Diagnose use of pointer-to-member type which when used as 1467199512Srdivacky // the functional cast in a pointer-to-member expression. 1468199512Srdivacky Diag(Loc, diag::err_pointer_to_member_type) << isIndirect; 1469199512Srdivacky return QualType(); 1470199512Srdivacky } 1471193326Sed // C++ 5.5p2 1472193326Sed // The result is an object or a function of the type specified by the 1473193326Sed // second operand. 1474193326Sed // The cv qualifiers are the union of those in the pointer and the left side, 1475193326Sed // in accordance with 5.5p5 and 5.2.5. 1476193326Sed // FIXME: This returns a dereferenced member function pointer as a normal 1477193326Sed // function type. However, the only operation valid on such functions is 1478193326Sed // calling them. There's also a GCC extension to get a function pointer to the 1479193326Sed // thing, which is another complication, because this type - unlike the type 1480193326Sed // that is the result of this expression - takes the class as the first 1481193326Sed // argument. 1482193326Sed // We probably need a "MemberFunctionClosureType" or something like that. 1483193326Sed QualType Result = MemPtr->getPointeeType(); 1484198092Srdivacky Result = Context.getCVRQualifiedType(Result, LType.getCVRQualifiers()); 1485193326Sed return Result; 1486193326Sed} 1487193326Sed 1488193326Sed/// \brief Get the target type of a standard or user-defined conversion. 1489193326Sedstatic QualType TargetType(const ImplicitConversionSequence &ICS) { 1490202379Srdivacky switch (ICS.getKind()) { 1491202379Srdivacky case ImplicitConversionSequence::StandardConversion: 1492203955Srdivacky return ICS.Standard.getToType(2); 1493202379Srdivacky case ImplicitConversionSequence::UserDefinedConversion: 1494203955Srdivacky return ICS.UserDefined.After.getToType(2); 1495202379Srdivacky case ImplicitConversionSequence::AmbiguousConversion: 1496202379Srdivacky return ICS.Ambiguous.getToType(); 1497202379Srdivacky case ImplicitConversionSequence::EllipsisConversion: 1498202379Srdivacky case ImplicitConversionSequence::BadConversion: 1499202379Srdivacky llvm_unreachable("function not valid for ellipsis or bad conversions"); 1500202379Srdivacky } 1501202379Srdivacky return QualType(); // silence warnings 1502193326Sed} 1503193326Sed 1504193326Sed/// \brief Try to convert a type to another according to C++0x 5.16p3. 1505193326Sed/// 1506193326Sed/// This is part of the parameter validation for the ? operator. If either 1507193326Sed/// value operand is a class type, the two operands are attempted to be 1508193326Sed/// converted to each other. This function does the conversion in one direction. 1509193326Sed/// It emits a diagnostic and returns true only if it finds an ambiguous 1510193326Sed/// conversion. 1511193326Sedstatic bool TryClassUnification(Sema &Self, Expr *From, Expr *To, 1512193326Sed SourceLocation QuestionLoc, 1513198092Srdivacky ImplicitConversionSequence &ICS) { 1514193326Sed // C++0x 5.16p3 1515193326Sed // The process for determining whether an operand expression E1 of type T1 1516193326Sed // can be converted to match an operand expression E2 of type T2 is defined 1517193326Sed // as follows: 1518193326Sed // -- If E2 is an lvalue: 1519193326Sed if (To->isLvalue(Self.Context) == Expr::LV_Valid) { 1520193326Sed // E1 can be converted to match E2 if E1 can be implicitly converted to 1521193326Sed // type "lvalue reference to T2", subject to the constraint that in the 1522193326Sed // conversion the reference must bind directly to E1. 1523193326Sed if (!Self.CheckReferenceInit(From, 1524193326Sed Self.Context.getLValueReferenceType(To->getType()), 1525198092Srdivacky To->getLocStart(), 1526198092Srdivacky /*SuppressUserConversions=*/false, 1527198092Srdivacky /*AllowExplicit=*/false, 1528198092Srdivacky /*ForceRValue=*/false, 1529198092Srdivacky &ICS)) 1530193326Sed { 1531202379Srdivacky assert((ICS.isStandard() || ICS.isUserDefined()) && 1532193326Sed "expected a definite conversion"); 1533193326Sed bool DirectBinding = 1534202379Srdivacky ICS.isStandard() ? ICS.Standard.DirectBinding 1535202379Srdivacky : ICS.UserDefined.After.DirectBinding; 1536193326Sed if (DirectBinding) 1537193326Sed return false; 1538193326Sed } 1539193326Sed } 1540202379Srdivacky ICS.setBad(); 1541193326Sed // -- If E2 is an rvalue, or if the conversion above cannot be done: 1542193326Sed // -- if E1 and E2 have class type, and the underlying class types are 1543193326Sed // the same or one is a base class of the other: 1544193326Sed QualType FTy = From->getType(); 1545193326Sed QualType TTy = To->getType(); 1546198092Srdivacky const RecordType *FRec = FTy->getAs<RecordType>(); 1547198092Srdivacky const RecordType *TRec = TTy->getAs<RecordType>(); 1548193326Sed bool FDerivedFromT = FRec && TRec && Self.IsDerivedFrom(FTy, TTy); 1549193326Sed if (FRec && TRec && (FRec == TRec || 1550193326Sed FDerivedFromT || Self.IsDerivedFrom(TTy, FTy))) { 1551193326Sed // E1 can be converted to match E2 if the class of T2 is the 1552193326Sed // same type as, or a base class of, the class of T1, and 1553193326Sed // [cv2 > cv1]. 1554193326Sed if ((FRec == TRec || FDerivedFromT) && TTy.isAtLeastAsQualifiedAs(FTy)) { 1555193326Sed // Could still fail if there's no copy constructor. 1556193326Sed // FIXME: Is this a hard error then, or just a conversion failure? The 1557193326Sed // standard doesn't say. 1558198092Srdivacky ICS = Self.TryCopyInitialization(From, TTy, 1559198092Srdivacky /*SuppressUserConversions=*/false, 1560198092Srdivacky /*ForceRValue=*/false, 1561198092Srdivacky /*InOverloadResolution=*/false); 1562193326Sed } 1563193326Sed } else { 1564193326Sed // -- Otherwise: E1 can be converted to match E2 if E1 can be 1565193326Sed // implicitly converted to the type that expression E2 would have 1566193326Sed // if E2 were converted to an rvalue. 1567193326Sed // First find the decayed type. 1568193326Sed if (TTy->isFunctionType()) 1569193326Sed TTy = Self.Context.getPointerType(TTy); 1570198092Srdivacky else if (TTy->isArrayType()) 1571193326Sed TTy = Self.Context.getArrayDecayedType(TTy); 1572193326Sed 1573193326Sed // Now try the implicit conversion. 1574193326Sed // FIXME: This doesn't detect ambiguities. 1575198092Srdivacky ICS = Self.TryImplicitConversion(From, TTy, 1576198092Srdivacky /*SuppressUserConversions=*/false, 1577198092Srdivacky /*AllowExplicit=*/false, 1578198092Srdivacky /*ForceRValue=*/false, 1579198092Srdivacky /*InOverloadResolution=*/false); 1580193326Sed } 1581193326Sed return false; 1582193326Sed} 1583193326Sed 1584193326Sed/// \brief Try to find a common type for two according to C++0x 5.16p5. 1585193326Sed/// 1586193326Sed/// This is part of the parameter validation for the ? operator. If either 1587193326Sed/// value operand is a class type, overload resolution is used to find a 1588193326Sed/// conversion to a common type. 1589193326Sedstatic bool FindConditionalOverload(Sema &Self, Expr *&LHS, Expr *&RHS, 1590193326Sed SourceLocation Loc) { 1591193326Sed Expr *Args[2] = { LHS, RHS }; 1592203955Srdivacky OverloadCandidateSet CandidateSet(Loc); 1593198398Srdivacky Self.AddBuiltinOperatorCandidates(OO_Conditional, Loc, Args, 2, CandidateSet); 1594193326Sed 1595193326Sed OverloadCandidateSet::iterator Best; 1596194613Sed switch (Self.BestViableFunction(CandidateSet, Loc, Best)) { 1597200583Srdivacky case OR_Success: 1598193326Sed // We found a match. Perform the conversions on the arguments and move on. 1599193326Sed if (Self.PerformImplicitConversion(LHS, Best->BuiltinTypes.ParamTypes[0], 1600201361Srdivacky Best->Conversions[0], Sema::AA_Converting) || 1601193326Sed Self.PerformImplicitConversion(RHS, Best->BuiltinTypes.ParamTypes[1], 1602201361Srdivacky Best->Conversions[1], Sema::AA_Converting)) 1603193326Sed break; 1604193326Sed return false; 1605193326Sed 1606200583Srdivacky case OR_No_Viable_Function: 1607193326Sed Self.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) 1608193326Sed << LHS->getType() << RHS->getType() 1609193326Sed << LHS->getSourceRange() << RHS->getSourceRange(); 1610193326Sed return true; 1611193326Sed 1612200583Srdivacky case OR_Ambiguous: 1613193326Sed Self.Diag(Loc, diag::err_conditional_ambiguous_ovl) 1614193326Sed << LHS->getType() << RHS->getType() 1615193326Sed << LHS->getSourceRange() << RHS->getSourceRange(); 1616193326Sed // FIXME: Print the possible common types by printing the return types of 1617193326Sed // the viable candidates. 1618193326Sed break; 1619193326Sed 1620200583Srdivacky case OR_Deleted: 1621193326Sed assert(false && "Conditional operator has only built-in overloads"); 1622193326Sed break; 1623193326Sed } 1624193326Sed return true; 1625193326Sed} 1626193326Sed 1627193326Sed/// \brief Perform an "extended" implicit conversion as returned by 1628193326Sed/// TryClassUnification. 1629193326Sed/// 1630193326Sed/// TryClassUnification generates ICSs that include reference bindings. 1631193326Sed/// PerformImplicitConversion is not suitable for this; it chokes if the 1632193326Sed/// second part of a standard conversion is ICK_DerivedToBase. This function 1633193326Sed/// handles the reference binding specially. 1634193326Sedstatic bool ConvertForConditional(Sema &Self, Expr *&E, 1635198092Srdivacky const ImplicitConversionSequence &ICS) { 1636202379Srdivacky if (ICS.isStandard() && ICS.Standard.ReferenceBinding) { 1637193326Sed assert(ICS.Standard.DirectBinding && 1638193326Sed "TryClassUnification should never generate indirect ref bindings"); 1639193326Sed // FIXME: CheckReferenceInit should be able to reuse the ICS instead of 1640193326Sed // redoing all the work. 1641193326Sed return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( 1642198092Srdivacky TargetType(ICS)), 1643198092Srdivacky /*FIXME:*/E->getLocStart(), 1644198092Srdivacky /*SuppressUserConversions=*/false, 1645198092Srdivacky /*AllowExplicit=*/false, 1646198092Srdivacky /*ForceRValue=*/false); 1647193326Sed } 1648202379Srdivacky if (ICS.isUserDefined() && ICS.UserDefined.After.ReferenceBinding) { 1649193326Sed assert(ICS.UserDefined.After.DirectBinding && 1650193326Sed "TryClassUnification should never generate indirect ref bindings"); 1651193326Sed return Self.CheckReferenceInit(E, Self.Context.getLValueReferenceType( 1652198092Srdivacky TargetType(ICS)), 1653198092Srdivacky /*FIXME:*/E->getLocStart(), 1654198092Srdivacky /*SuppressUserConversions=*/false, 1655198092Srdivacky /*AllowExplicit=*/false, 1656198092Srdivacky /*ForceRValue=*/false); 1657193326Sed } 1658201361Srdivacky if (Self.PerformImplicitConversion(E, TargetType(ICS), ICS, Sema::AA_Converting)) 1659193326Sed return true; 1660193326Sed return false; 1661193326Sed} 1662193326Sed 1663193326Sed/// \brief Check the operands of ?: under C++ semantics. 1664193326Sed/// 1665193326Sed/// See C++ [expr.cond]. Note that LHS is never null, even for the GNU x ?: y 1666193326Sed/// extension. In this case, LHS == Cond. (But they're not aliases.) 1667193326SedQualType Sema::CXXCheckConditionalOperands(Expr *&Cond, Expr *&LHS, Expr *&RHS, 1668193326Sed SourceLocation QuestionLoc) { 1669193326Sed // FIXME: Handle C99's complex types, vector types, block pointers and Obj-C++ 1670193326Sed // interface pointers. 1671193326Sed 1672193326Sed // C++0x 5.16p1 1673193326Sed // The first expression is contextually converted to bool. 1674193326Sed if (!Cond->isTypeDependent()) { 1675193326Sed if (CheckCXXBooleanCondition(Cond)) 1676193326Sed return QualType(); 1677193326Sed } 1678193326Sed 1679193326Sed // Either of the arguments dependent? 1680193326Sed if (LHS->isTypeDependent() || RHS->isTypeDependent()) 1681193326Sed return Context.DependentTy; 1682193326Sed 1683198954Srdivacky CheckSignCompare(LHS, RHS, QuestionLoc, diag::warn_mixed_sign_conditional); 1684198954Srdivacky 1685193326Sed // C++0x 5.16p2 1686193326Sed // If either the second or the third operand has type (cv) void, ... 1687193326Sed QualType LTy = LHS->getType(); 1688193326Sed QualType RTy = RHS->getType(); 1689193326Sed bool LVoid = LTy->isVoidType(); 1690193326Sed bool RVoid = RTy->isVoidType(); 1691193326Sed if (LVoid || RVoid) { 1692193326Sed // ... then the [l2r] conversions are performed on the second and third 1693193326Sed // operands ... 1694203955Srdivacky DefaultFunctionArrayLvalueConversion(LHS); 1695203955Srdivacky DefaultFunctionArrayLvalueConversion(RHS); 1696193326Sed LTy = LHS->getType(); 1697193326Sed RTy = RHS->getType(); 1698193326Sed 1699193326Sed // ... and one of the following shall hold: 1700193326Sed // -- The second or the third operand (but not both) is a throw- 1701193326Sed // expression; the result is of the type of the other and is an rvalue. 1702193326Sed bool LThrow = isa<CXXThrowExpr>(LHS); 1703193326Sed bool RThrow = isa<CXXThrowExpr>(RHS); 1704193326Sed if (LThrow && !RThrow) 1705193326Sed return RTy; 1706193326Sed if (RThrow && !LThrow) 1707193326Sed return LTy; 1708193326Sed 1709193326Sed // -- Both the second and third operands have type void; the result is of 1710193326Sed // type void and is an rvalue. 1711193326Sed if (LVoid && RVoid) 1712193326Sed return Context.VoidTy; 1713193326Sed 1714193326Sed // Neither holds, error. 1715193326Sed Diag(QuestionLoc, diag::err_conditional_void_nonvoid) 1716193326Sed << (LVoid ? RTy : LTy) << (LVoid ? 0 : 1) 1717193326Sed << LHS->getSourceRange() << RHS->getSourceRange(); 1718193326Sed return QualType(); 1719193326Sed } 1720193326Sed 1721193326Sed // Neither is void. 1722193326Sed 1723193326Sed // C++0x 5.16p3 1724193326Sed // Otherwise, if the second and third operand have different types, and 1725193326Sed // either has (cv) class type, and attempt is made to convert each of those 1726193326Sed // operands to the other. 1727193326Sed if (Context.getCanonicalType(LTy) != Context.getCanonicalType(RTy) && 1728193326Sed (LTy->isRecordType() || RTy->isRecordType())) { 1729193326Sed ImplicitConversionSequence ICSLeftToRight, ICSRightToLeft; 1730193326Sed // These return true if a single direction is already ambiguous. 1731193326Sed if (TryClassUnification(*this, LHS, RHS, QuestionLoc, ICSLeftToRight)) 1732193326Sed return QualType(); 1733193326Sed if (TryClassUnification(*this, RHS, LHS, QuestionLoc, ICSRightToLeft)) 1734193326Sed return QualType(); 1735193326Sed 1736202379Srdivacky bool HaveL2R = !ICSLeftToRight.isBad(); 1737202379Srdivacky bool HaveR2L = !ICSRightToLeft.isBad(); 1738193326Sed // If both can be converted, [...] the program is ill-formed. 1739193326Sed if (HaveL2R && HaveR2L) { 1740193326Sed Diag(QuestionLoc, diag::err_conditional_ambiguous) 1741193326Sed << LTy << RTy << LHS->getSourceRange() << RHS->getSourceRange(); 1742193326Sed return QualType(); 1743193326Sed } 1744193326Sed 1745193326Sed // If exactly one conversion is possible, that conversion is applied to 1746193326Sed // the chosen operand and the converted operands are used in place of the 1747193326Sed // original operands for the remainder of this section. 1748193326Sed if (HaveL2R) { 1749193326Sed if (ConvertForConditional(*this, LHS, ICSLeftToRight)) 1750193326Sed return QualType(); 1751193326Sed LTy = LHS->getType(); 1752193326Sed } else if (HaveR2L) { 1753193326Sed if (ConvertForConditional(*this, RHS, ICSRightToLeft)) 1754193326Sed return QualType(); 1755193326Sed RTy = RHS->getType(); 1756193326Sed } 1757193326Sed } 1758193326Sed 1759193326Sed // C++0x 5.16p4 1760193326Sed // If the second and third operands are lvalues and have the same type, 1761193326Sed // the result is of that type [...] 1762193326Sed bool Same = Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy); 1763193326Sed if (Same && LHS->isLvalue(Context) == Expr::LV_Valid && 1764193326Sed RHS->isLvalue(Context) == Expr::LV_Valid) 1765193326Sed return LTy; 1766193326Sed 1767193326Sed // C++0x 5.16p5 1768193326Sed // Otherwise, the result is an rvalue. If the second and third operands 1769193326Sed // do not have the same type, and either has (cv) class type, ... 1770193326Sed if (!Same && (LTy->isRecordType() || RTy->isRecordType())) { 1771193326Sed // ... overload resolution is used to determine the conversions (if any) 1772193326Sed // to be applied to the operands. If the overload resolution fails, the 1773193326Sed // program is ill-formed. 1774193326Sed if (FindConditionalOverload(*this, LHS, RHS, QuestionLoc)) 1775193326Sed return QualType(); 1776193326Sed } 1777193326Sed 1778193326Sed // C++0x 5.16p6 1779193326Sed // LValue-to-rvalue, array-to-pointer, and function-to-pointer standard 1780193326Sed // conversions are performed on the second and third operands. 1781203955Srdivacky DefaultFunctionArrayLvalueConversion(LHS); 1782203955Srdivacky DefaultFunctionArrayLvalueConversion(RHS); 1783193326Sed LTy = LHS->getType(); 1784193326Sed RTy = RHS->getType(); 1785193326Sed 1786193326Sed // After those conversions, one of the following shall hold: 1787193326Sed // -- The second and third operands have the same type; the result 1788193326Sed // is of that type. 1789193326Sed if (Context.getCanonicalType(LTy) == Context.getCanonicalType(RTy)) 1790193326Sed return LTy; 1791193326Sed 1792193326Sed // -- The second and third operands have arithmetic or enumeration type; 1793193326Sed // the usual arithmetic conversions are performed to bring them to a 1794193326Sed // common type, and the result is of that type. 1795193326Sed if (LTy->isArithmeticType() && RTy->isArithmeticType()) { 1796193326Sed UsualArithmeticConversions(LHS, RHS); 1797193326Sed return LHS->getType(); 1798193326Sed } 1799193326Sed 1800193326Sed // -- The second and third operands have pointer type, or one has pointer 1801193326Sed // type and the other is a null pointer constant; pointer conversions 1802193326Sed // and qualification conversions are performed to bring them to their 1803193326Sed // composite pointer type. The result is of the composite pointer type. 1804202379Srdivacky // -- The second and third operands have pointer to member type, or one has 1805202379Srdivacky // pointer to member type and the other is a null pointer constant; 1806202379Srdivacky // pointer to member conversions and qualification conversions are 1807202379Srdivacky // performed to bring them to a common type, whose cv-qualification 1808202379Srdivacky // shall match the cv-qualification of either the second or the third 1809202379Srdivacky // operand. The result is of the common type. 1810193326Sed QualType Composite = FindCompositePointerType(LHS, RHS); 1811193326Sed if (!Composite.isNull()) 1812193326Sed return Composite; 1813200583Srdivacky 1814200583Srdivacky // Similarly, attempt to find composite type of twp objective-c pointers. 1815200583Srdivacky Composite = FindCompositeObjCPointerType(LHS, RHS, QuestionLoc); 1816200583Srdivacky if (!Composite.isNull()) 1817200583Srdivacky return Composite; 1818193326Sed 1819193326Sed Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) 1820193326Sed << LHS->getType() << RHS->getType() 1821193326Sed << LHS->getSourceRange() << RHS->getSourceRange(); 1822193326Sed return QualType(); 1823193326Sed} 1824193326Sed 1825193326Sed/// \brief Find a merged pointer type and convert the two expressions to it. 1826193326Sed/// 1827198092Srdivacky/// This finds the composite pointer type (or member pointer type) for @p E1 1828198092Srdivacky/// and @p E2 according to C++0x 5.9p2. It converts both expressions to this 1829198092Srdivacky/// type and returns it. 1830193326Sed/// It does not emit diagnostics. 1831193326SedQualType Sema::FindCompositePointerType(Expr *&E1, Expr *&E2) { 1832193326Sed assert(getLangOptions().CPlusPlus && "This function assumes C++"); 1833193326Sed QualType T1 = E1->getType(), T2 = E2->getType(); 1834193326Sed 1835200583Srdivacky if (!T1->isAnyPointerType() && !T1->isMemberPointerType() && 1836200583Srdivacky !T2->isAnyPointerType() && !T2->isMemberPointerType()) 1837198092Srdivacky return QualType(); 1838198092Srdivacky 1839193326Sed // C++0x 5.9p2 1840193326Sed // Pointer conversions and qualification conversions are performed on 1841193326Sed // pointer operands to bring them to their composite pointer type. If 1842193326Sed // one operand is a null pointer constant, the composite pointer type is 1843193326Sed // the type of the other operand. 1844198092Srdivacky if (E1->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { 1845198398Srdivacky if (T2->isMemberPointerType()) 1846198398Srdivacky ImpCastExprToType(E1, T2, CastExpr::CK_NullToMemberPointer); 1847198398Srdivacky else 1848198398Srdivacky ImpCastExprToType(E1, T2, CastExpr::CK_IntegralToPointer); 1849193326Sed return T2; 1850193326Sed } 1851198092Srdivacky if (E2->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { 1852198398Srdivacky if (T1->isMemberPointerType()) 1853198398Srdivacky ImpCastExprToType(E2, T1, CastExpr::CK_NullToMemberPointer); 1854198398Srdivacky else 1855198398Srdivacky ImpCastExprToType(E2, T1, CastExpr::CK_IntegralToPointer); 1856193326Sed return T1; 1857193326Sed } 1858198092Srdivacky 1859198092Srdivacky // Now both have to be pointers or member pointers. 1860199482Srdivacky if ((!T1->isPointerType() && !T1->isMemberPointerType()) || 1861199482Srdivacky (!T2->isPointerType() && !T2->isMemberPointerType())) 1862193326Sed return QualType(); 1863193326Sed 1864193326Sed // Otherwise, of one of the operands has type "pointer to cv1 void," then 1865193326Sed // the other has type "pointer to cv2 T" and the composite pointer type is 1866193326Sed // "pointer to cv12 void," where cv12 is the union of cv1 and cv2. 1867193326Sed // Otherwise, the composite pointer type is a pointer type similar to the 1868193326Sed // type of one of the operands, with a cv-qualification signature that is 1869193326Sed // the union of the cv-qualification signatures of the operand types. 1870193326Sed // In practice, the first part here is redundant; it's subsumed by the second. 1871193326Sed // What we do here is, we build the two possible composite types, and try the 1872193326Sed // conversions in both directions. If only one works, or if the two composite 1873193326Sed // types are the same, we have succeeded. 1874198092Srdivacky // FIXME: extended qualifiers? 1875199482Srdivacky typedef llvm::SmallVector<unsigned, 4> QualifierVector; 1876199482Srdivacky QualifierVector QualifierUnion; 1877199482Srdivacky typedef llvm::SmallVector<std::pair<const Type *, const Type *>, 4> 1878199482Srdivacky ContainingClassVector; 1879199482Srdivacky ContainingClassVector MemberOfClass; 1880199482Srdivacky QualType Composite1 = Context.getCanonicalType(T1), 1881199482Srdivacky Composite2 = Context.getCanonicalType(T2); 1882198092Srdivacky do { 1883198092Srdivacky const PointerType *Ptr1, *Ptr2; 1884198092Srdivacky if ((Ptr1 = Composite1->getAs<PointerType>()) && 1885198092Srdivacky (Ptr2 = Composite2->getAs<PointerType>())) { 1886198092Srdivacky Composite1 = Ptr1->getPointeeType(); 1887198092Srdivacky Composite2 = Ptr2->getPointeeType(); 1888198092Srdivacky QualifierUnion.push_back( 1889198092Srdivacky Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); 1890198092Srdivacky MemberOfClass.push_back(std::make_pair((const Type *)0, (const Type *)0)); 1891198092Srdivacky continue; 1892198092Srdivacky } 1893198092Srdivacky 1894198092Srdivacky const MemberPointerType *MemPtr1, *MemPtr2; 1895198092Srdivacky if ((MemPtr1 = Composite1->getAs<MemberPointerType>()) && 1896198092Srdivacky (MemPtr2 = Composite2->getAs<MemberPointerType>())) { 1897198092Srdivacky Composite1 = MemPtr1->getPointeeType(); 1898198092Srdivacky Composite2 = MemPtr2->getPointeeType(); 1899198092Srdivacky QualifierUnion.push_back( 1900198092Srdivacky Composite1.getCVRQualifiers() | Composite2.getCVRQualifiers()); 1901198092Srdivacky MemberOfClass.push_back(std::make_pair(MemPtr1->getClass(), 1902198092Srdivacky MemPtr2->getClass())); 1903198092Srdivacky continue; 1904198092Srdivacky } 1905198092Srdivacky 1906198092Srdivacky // FIXME: block pointer types? 1907198092Srdivacky 1908198092Srdivacky // Cannot unwrap any more types. 1909198092Srdivacky break; 1910198092Srdivacky } while (true); 1911198092Srdivacky 1912198092Srdivacky // Rewrap the composites as pointers or member pointers with the union CVRs. 1913199482Srdivacky ContainingClassVector::reverse_iterator MOC 1914199482Srdivacky = MemberOfClass.rbegin(); 1915199482Srdivacky for (QualifierVector::reverse_iterator 1916199482Srdivacky I = QualifierUnion.rbegin(), 1917199482Srdivacky E = QualifierUnion.rend(); 1918198092Srdivacky I != E; (void)++I, ++MOC) { 1919198092Srdivacky Qualifiers Quals = Qualifiers::fromCVRMask(*I); 1920198092Srdivacky if (MOC->first && MOC->second) { 1921198092Srdivacky // Rebuild member pointer type 1922198092Srdivacky Composite1 = Context.getMemberPointerType( 1923198092Srdivacky Context.getQualifiedType(Composite1, Quals), 1924198092Srdivacky MOC->first); 1925198092Srdivacky Composite2 = Context.getMemberPointerType( 1926198092Srdivacky Context.getQualifiedType(Composite2, Quals), 1927198092Srdivacky MOC->second); 1928198092Srdivacky } else { 1929198092Srdivacky // Rebuild pointer type 1930198092Srdivacky Composite1 1931198092Srdivacky = Context.getPointerType(Context.getQualifiedType(Composite1, Quals)); 1932198092Srdivacky Composite2 1933198092Srdivacky = Context.getPointerType(Context.getQualifiedType(Composite2, Quals)); 1934198092Srdivacky } 1935193326Sed } 1936193326Sed 1937198092Srdivacky ImplicitConversionSequence E1ToC1 = 1938198092Srdivacky TryImplicitConversion(E1, Composite1, 1939198092Srdivacky /*SuppressUserConversions=*/false, 1940198092Srdivacky /*AllowExplicit=*/false, 1941198092Srdivacky /*ForceRValue=*/false, 1942198092Srdivacky /*InOverloadResolution=*/false); 1943198092Srdivacky ImplicitConversionSequence E2ToC1 = 1944198092Srdivacky TryImplicitConversion(E2, Composite1, 1945198092Srdivacky /*SuppressUserConversions=*/false, 1946198092Srdivacky /*AllowExplicit=*/false, 1947198092Srdivacky /*ForceRValue=*/false, 1948198092Srdivacky /*InOverloadResolution=*/false); 1949198092Srdivacky 1950193326Sed ImplicitConversionSequence E1ToC2, E2ToC2; 1951202379Srdivacky E1ToC2.setBad(); 1952202379Srdivacky E2ToC2.setBad(); 1953193326Sed if (Context.getCanonicalType(Composite1) != 1954193326Sed Context.getCanonicalType(Composite2)) { 1955198092Srdivacky E1ToC2 = TryImplicitConversion(E1, Composite2, 1956198092Srdivacky /*SuppressUserConversions=*/false, 1957198092Srdivacky /*AllowExplicit=*/false, 1958198092Srdivacky /*ForceRValue=*/false, 1959198092Srdivacky /*InOverloadResolution=*/false); 1960198092Srdivacky E2ToC2 = TryImplicitConversion(E2, Composite2, 1961198092Srdivacky /*SuppressUserConversions=*/false, 1962198092Srdivacky /*AllowExplicit=*/false, 1963198092Srdivacky /*ForceRValue=*/false, 1964198092Srdivacky /*InOverloadResolution=*/false); 1965193326Sed } 1966193326Sed 1967202379Srdivacky bool ToC1Viable = !E1ToC1.isBad() && !E2ToC1.isBad(); 1968202379Srdivacky bool ToC2Viable = !E1ToC2.isBad() && !E2ToC2.isBad(); 1969193326Sed if (ToC1Viable && !ToC2Viable) { 1970201361Srdivacky if (!PerformImplicitConversion(E1, Composite1, E1ToC1, Sema::AA_Converting) && 1971201361Srdivacky !PerformImplicitConversion(E2, Composite1, E2ToC1, Sema::AA_Converting)) 1972193326Sed return Composite1; 1973193326Sed } 1974193326Sed if (ToC2Viable && !ToC1Viable) { 1975201361Srdivacky if (!PerformImplicitConversion(E1, Composite2, E1ToC2, Sema::AA_Converting) && 1976201361Srdivacky !PerformImplicitConversion(E2, Composite2, E2ToC2, Sema::AA_Converting)) 1977193326Sed return Composite2; 1978193326Sed } 1979193326Sed return QualType(); 1980193326Sed} 1981193326Sed 1982193326SedSema::OwningExprResult Sema::MaybeBindToTemporary(Expr *E) { 1983198092Srdivacky if (!Context.getLangOptions().CPlusPlus) 1984198092Srdivacky return Owned(E); 1985198092Srdivacky 1986201361Srdivacky assert(!isa<CXXBindTemporaryExpr>(E) && "Double-bound temporary?"); 1987201361Srdivacky 1988198092Srdivacky const RecordType *RT = E->getType()->getAs<RecordType>(); 1989193326Sed if (!RT) 1990193326Sed return Owned(E); 1991198092Srdivacky 1992203955Srdivacky // If this is the result of a call expression, our source might 1993203955Srdivacky // actually be a reference, in which case we shouldn't bind. 1994198092Srdivacky if (CallExpr *CE = dyn_cast<CallExpr>(E)) { 1995198092Srdivacky QualType Ty = CE->getCallee()->getType(); 1996198092Srdivacky if (const PointerType *PT = Ty->getAs<PointerType>()) 1997198092Srdivacky Ty = PT->getPointeeType(); 1998198092Srdivacky 1999198092Srdivacky const FunctionType *FTy = Ty->getAs<FunctionType>(); 2000198092Srdivacky if (FTy->getResultType()->isReferenceType()) 2001198092Srdivacky return Owned(E); 2002198092Srdivacky } 2003203955Srdivacky 2004203955Srdivacky // That should be enough to guarantee that this type is complete. 2005203955Srdivacky // If it has a trivial destructor, we can avoid the extra copy. 2006203955Srdivacky CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl()); 2007203955Srdivacky if (RD->hasTrivialDestructor()) 2008203955Srdivacky return Owned(E); 2009203955Srdivacky 2010198092Srdivacky CXXTemporary *Temp = CXXTemporary::Create(Context, 2011193326Sed RD->getDestructor(Context)); 2012193326Sed ExprTemporaries.push_back(Temp); 2013198092Srdivacky if (CXXDestructorDecl *Destructor = 2014198092Srdivacky const_cast<CXXDestructorDecl*>(RD->getDestructor(Context))) 2015198092Srdivacky MarkDeclarationReferenced(E->getExprLoc(), Destructor); 2016193326Sed // FIXME: Add the temporary to the temporaries vector. 2017193326Sed return Owned(CXXBindTemporaryExpr::Create(Context, Temp, E)); 2018193326Sed} 2019193326Sed 2020201361SrdivackyExpr *Sema::MaybeCreateCXXExprWithTemporaries(Expr *SubExpr) { 2021193576Sed assert(SubExpr && "sub expression can't be null!"); 2022198092Srdivacky 2023200583Srdivacky unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; 2024200583Srdivacky assert(ExprTemporaries.size() >= FirstTemporary); 2025200583Srdivacky if (ExprTemporaries.size() == FirstTemporary) 2026193576Sed return SubExpr; 2027198092Srdivacky 2028193576Sed Expr *E = CXXExprWithTemporaries::Create(Context, SubExpr, 2029200583Srdivacky &ExprTemporaries[FirstTemporary], 2030201361Srdivacky ExprTemporaries.size() - FirstTemporary); 2031200583Srdivacky ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, 2032200583Srdivacky ExprTemporaries.end()); 2033198092Srdivacky 2034193576Sed return E; 2035193576Sed} 2036193576Sed 2037201361SrdivackySema::OwningExprResult 2038201361SrdivackySema::MaybeCreateCXXExprWithTemporaries(OwningExprResult SubExpr) { 2039201361Srdivacky if (SubExpr.isInvalid()) 2040201361Srdivacky return ExprError(); 2041201361Srdivacky 2042201361Srdivacky return Owned(MaybeCreateCXXExprWithTemporaries(SubExpr.takeAs<Expr>())); 2043201361Srdivacky} 2044201361Srdivacky 2045201361SrdivackyFullExpr Sema::CreateFullExpr(Expr *SubExpr) { 2046201361Srdivacky unsigned FirstTemporary = ExprEvalContexts.back().NumTemporaries; 2047201361Srdivacky assert(ExprTemporaries.size() >= FirstTemporary); 2048201361Srdivacky 2049201361Srdivacky unsigned NumTemporaries = ExprTemporaries.size() - FirstTemporary; 2050201361Srdivacky CXXTemporary **Temporaries = 2051201361Srdivacky NumTemporaries == 0 ? 0 : &ExprTemporaries[FirstTemporary]; 2052201361Srdivacky 2053201361Srdivacky FullExpr E = FullExpr::Create(Context, SubExpr, Temporaries, NumTemporaries); 2054201361Srdivacky 2055201361Srdivacky ExprTemporaries.erase(ExprTemporaries.begin() + FirstTemporary, 2056201361Srdivacky ExprTemporaries.end()); 2057201361Srdivacky 2058201361Srdivacky return E; 2059201361Srdivacky} 2060201361Srdivacky 2061198092SrdivackySema::OwningExprResult 2062198092SrdivackySema::ActOnStartCXXMemberReference(Scope *S, ExprArg Base, SourceLocation OpLoc, 2063198092Srdivacky tok::TokenKind OpKind, TypeTy *&ObjectType) { 2064198092Srdivacky // Since this might be a postfix expression, get rid of ParenListExprs. 2065198092Srdivacky Base = MaybeConvertParenListExprToParenExpr(S, move(Base)); 2066198092Srdivacky 2067198092Srdivacky Expr *BaseExpr = (Expr*)Base.get(); 2068198092Srdivacky assert(BaseExpr && "no record expansion"); 2069198092Srdivacky 2070198092Srdivacky QualType BaseType = BaseExpr->getType(); 2071198092Srdivacky if (BaseType->isDependentType()) { 2072198954Srdivacky // If we have a pointer to a dependent type and are using the -> operator, 2073198954Srdivacky // the object type is the type that the pointer points to. We might still 2074198954Srdivacky // have enough information about that type to do something useful. 2075198954Srdivacky if (OpKind == tok::arrow) 2076198954Srdivacky if (const PointerType *Ptr = BaseType->getAs<PointerType>()) 2077198954Srdivacky BaseType = Ptr->getPointeeType(); 2078198954Srdivacky 2079198092Srdivacky ObjectType = BaseType.getAsOpaquePtr(); 2080198092Srdivacky return move(Base); 2081198092Srdivacky } 2082198092Srdivacky 2083198092Srdivacky // C++ [over.match.oper]p8: 2084198092Srdivacky // [...] When operator->returns, the operator-> is applied to the value 2085198092Srdivacky // returned, with the original second operand. 2086198092Srdivacky if (OpKind == tok::arrow) { 2087198092Srdivacky // The set of types we've considered so far. 2088198092Srdivacky llvm::SmallPtrSet<CanQualType,8> CTypes; 2089198092Srdivacky llvm::SmallVector<SourceLocation, 8> Locations; 2090198092Srdivacky CTypes.insert(Context.getCanonicalType(BaseType)); 2091198092Srdivacky 2092198092Srdivacky while (BaseType->isRecordType()) { 2093198092Srdivacky Base = BuildOverloadedArrowExpr(S, move(Base), OpLoc); 2094198092Srdivacky BaseExpr = (Expr*)Base.get(); 2095198092Srdivacky if (BaseExpr == NULL) 2096198092Srdivacky return ExprError(); 2097198092Srdivacky if (CXXOperatorCallExpr *OpCall = dyn_cast<CXXOperatorCallExpr>(BaseExpr)) 2098198092Srdivacky Locations.push_back(OpCall->getDirectCallee()->getLocation()); 2099198092Srdivacky BaseType = BaseExpr->getType(); 2100198092Srdivacky CanQualType CBaseType = Context.getCanonicalType(BaseType); 2101198092Srdivacky if (!CTypes.insert(CBaseType)) { 2102198092Srdivacky Diag(OpLoc, diag::err_operator_arrow_circular); 2103198092Srdivacky for (unsigned i = 0; i < Locations.size(); i++) 2104198092Srdivacky Diag(Locations[i], diag::note_declared_at); 2105198092Srdivacky return ExprError(); 2106198092Srdivacky } 2107198092Srdivacky } 2108199990Srdivacky 2109199990Srdivacky if (BaseType->isPointerType()) 2110199990Srdivacky BaseType = BaseType->getPointeeType(); 2111198092Srdivacky } 2112198092Srdivacky 2113198092Srdivacky // We could end up with various non-record types here, such as extended 2114198092Srdivacky // vector types or Objective-C interfaces. Just return early and let 2115198092Srdivacky // ActOnMemberReferenceExpr do the work. 2116198092Srdivacky if (!BaseType->isRecordType()) { 2117198092Srdivacky // C++ [basic.lookup.classref]p2: 2118198092Srdivacky // [...] If the type of the object expression is of pointer to scalar 2119198092Srdivacky // type, the unqualified-id is looked up in the context of the complete 2120198092Srdivacky // postfix-expression. 2121198092Srdivacky ObjectType = 0; 2122198092Srdivacky return move(Base); 2123198092Srdivacky } 2124198092Srdivacky 2125199482Srdivacky // The object type must be complete (or dependent). 2126199482Srdivacky if (!BaseType->isDependentType() && 2127199482Srdivacky RequireCompleteType(OpLoc, BaseType, 2128199482Srdivacky PDiag(diag::err_incomplete_member_access))) 2129199482Srdivacky return ExprError(); 2130199482Srdivacky 2131198092Srdivacky // C++ [basic.lookup.classref]p2: 2132198092Srdivacky // If the id-expression in a class member access (5.2.5) is an 2133199482Srdivacky // unqualified-id, and the type of the object expression is of a class 2134198092Srdivacky // type C (or of pointer to a class type C), the unqualified-id is looked 2135198092Srdivacky // up in the scope of class C. [...] 2136198092Srdivacky ObjectType = BaseType.getAsOpaquePtr(); 2137199482Srdivacky 2138198092Srdivacky return move(Base); 2139198092Srdivacky} 2140198092Srdivacky 2141198092SrdivackyCXXMemberCallExpr *Sema::BuildCXXMemberCallExpr(Expr *Exp, 2142198092Srdivacky CXXMethodDecl *Method) { 2143200583Srdivacky if (PerformObjectArgumentInitialization(Exp, Method)) 2144200583Srdivacky assert(0 && "Calling BuildCXXMemberCallExpr with invalid call?"); 2145200583Srdivacky 2146198092Srdivacky MemberExpr *ME = 2147198092Srdivacky new (Context) MemberExpr(Exp, /*IsArrow=*/false, Method, 2148198092Srdivacky SourceLocation(), Method->getType()); 2149200583Srdivacky QualType ResultType = Method->getResultType().getNonReferenceType(); 2150199990Srdivacky MarkDeclarationReferenced(Exp->getLocStart(), Method); 2151199990Srdivacky CXXMemberCallExpr *CE = 2152199990Srdivacky new (Context) CXXMemberCallExpr(Context, ME, 0, 0, ResultType, 2153199990Srdivacky Exp->getLocEnd()); 2154198092Srdivacky return CE; 2155198092Srdivacky} 2156198092Srdivacky 2157198092SrdivackySema::OwningExprResult Sema::BuildCXXCastArgument(SourceLocation CastLoc, 2158198092Srdivacky QualType Ty, 2159198092Srdivacky CastExpr::CastKind Kind, 2160198092Srdivacky CXXMethodDecl *Method, 2161198092Srdivacky ExprArg Arg) { 2162198092Srdivacky Expr *From = Arg.takeAs<Expr>(); 2163198092Srdivacky 2164198092Srdivacky switch (Kind) { 2165198092Srdivacky default: assert(0 && "Unhandled cast kind!"); 2166198092Srdivacky case CastExpr::CK_ConstructorConversion: { 2167198092Srdivacky ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); 2168198092Srdivacky 2169198092Srdivacky if (CompleteConstructorCall(cast<CXXConstructorDecl>(Method), 2170198092Srdivacky MultiExprArg(*this, (void **)&From, 1), 2171198092Srdivacky CastLoc, ConstructorArgs)) 2172198092Srdivacky return ExprError(); 2173198398Srdivacky 2174198398Srdivacky OwningExprResult Result = 2175198398Srdivacky BuildCXXConstructExpr(CastLoc, Ty, cast<CXXConstructorDecl>(Method), 2176198398Srdivacky move_arg(ConstructorArgs)); 2177198398Srdivacky if (Result.isInvalid()) 2178198398Srdivacky return ExprError(); 2179198398Srdivacky 2180198398Srdivacky return MaybeBindToTemporary(Result.takeAs<Expr>()); 2181198092Srdivacky } 2182198092Srdivacky 2183198092Srdivacky case CastExpr::CK_UserDefinedConversion: { 2184198092Srdivacky assert(!From->getType()->isPointerType() && "Arg can't have pointer type!"); 2185200583Srdivacky 2186198092Srdivacky // Create an implicit call expr that calls it. 2187198092Srdivacky CXXMemberCallExpr *CE = BuildCXXMemberCallExpr(From, Method); 2188198398Srdivacky return MaybeBindToTemporary(CE); 2189198092Srdivacky } 2190198092Srdivacky } 2191198092Srdivacky} 2192198092Srdivacky 2193193326SedSema::OwningExprResult Sema::ActOnFinishFullExpr(ExprArg Arg) { 2194193326Sed Expr *FullExpr = Arg.takeAs<Expr>(); 2195193576Sed if (FullExpr) 2196201361Srdivacky FullExpr = MaybeCreateCXXExprWithTemporaries(FullExpr); 2197193326Sed 2198193326Sed return Owned(FullExpr); 2199193326Sed} 2200