From: Eli Friedman Date: Sun, 31 Jan 2010 20:58:15 +0000 (+0000) Subject: Switch expressions like T() and T(1,2) over to new-style initialization. I'm X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6997aae42800d95a1189a6186af438feb19ecc54;p=clang Switch expressions like T() and T(1,2) over to new-style initialization. I'm not quite sure what we want to do about the AST representation; comments welcome. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@94967 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/CodeGen/CGExprCXX.cpp b/lib/CodeGen/CGExprCXX.cpp index d7e6dbfa36..4253d18c39 100644 --- a/lib/CodeGen/CGExprCXX.cpp +++ b/lib/CodeGen/CGExprCXX.cpp @@ -60,7 +60,7 @@ static bool canDevirtualizeMemberFunctionCalls(const Expr *Base) { } // We can always devirtualize calls on temporary object expressions. - if (isa(Base)) + if (isa(Base)) return true; // And calls on bound temporaries. diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h index 6f1ab14778..7f4806d305 100644 --- a/lib/Sema/Sema.h +++ b/lib/Sema/Sema.h @@ -1924,12 +1924,6 @@ public: bool RequiresZeroInit = false, bool BaseInitialization = false); - OwningExprResult BuildCXXTemporaryObjectExpr(CXXConstructorDecl *Cons, - QualType writtenTy, - SourceLocation tyBeginLoc, - MultiExprArg Args, - SourceLocation rParenLoc); - OwningExprResult BuildCXXCastArgument(SourceLocation CastLoc, QualType Ty, CastExpr::CastKind Kind, @@ -1983,14 +1977,6 @@ public: Expr **Args, unsigned NumArgs, SourceLocation Loc, InitializationKind Kind); - - CXXConstructorDecl * - PerformInitializationByConstructor(QualType ClassType, - MultiExprArg ArgsPtr, - SourceLocation Loc, SourceRange Range, - DeclarationName InitEntity, - InitializationKind Kind, - ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs); bool CompleteConstructorCall(CXXConstructorDecl *Constructor, MultiExprArg ArgsPtr, diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp index f23f702d05..bba0e5d5f7 100644 --- a/lib/Sema/SemaDeclCXX.cpp +++ b/lib/Sema/SemaDeclCXX.cpp @@ -3965,22 +3965,6 @@ Sema::BuildCXXConstructExpr(SourceLocation ConstructLoc, QualType DeclInitType, RequiresZeroInit, BaseInitialization)); } -Sema::OwningExprResult -Sema::BuildCXXTemporaryObjectExpr(CXXConstructorDecl *Constructor, - QualType Ty, - SourceLocation TyBeginLoc, - MultiExprArg Args, - SourceLocation RParenLoc) { - unsigned NumExprs = Args.size(); - Expr **Exprs = (Expr **)Args.release(); - - MarkDeclarationReferenced(TyBeginLoc, Constructor); - return Owned(new (Context) CXXTemporaryObjectExpr(Context, Constructor, Ty, - TyBeginLoc, Exprs, - NumExprs, RParenLoc)); -} - - bool Sema::InitializeVarWithConstructor(VarDecl *VD, CXXConstructorDecl *Constructor, MultiExprArg Exprs) { @@ -4218,92 +4202,6 @@ Sema::TryInitializationByConstructor(QualType ClassType, return 0; } -/// \brief Perform initialization by constructor (C++ [dcl.init]p14), which -/// may occur as part of direct-initialization or copy-initialization. -/// -/// \param ClassType the type of the object being initialized, which must have -/// class type. -/// -/// \param ArgsPtr the arguments provided to initialize the object -/// -/// \param Loc the source location where the initialization occurs -/// -/// \param Range the source range that covers the entire initialization -/// -/// \param InitEntity the name of the entity being initialized, if known -/// -/// \param Kind the type of initialization being performed -/// -/// \param ConvertedArgs a vector that will be filled in with the -/// appropriately-converted arguments to the constructor (if initialization -/// succeeded). -/// -/// \returns the constructor used to initialize the object, if successful. -/// Otherwise, emits a diagnostic and returns NULL. -CXXConstructorDecl * -Sema::PerformInitializationByConstructor(QualType ClassType, - MultiExprArg ArgsPtr, - SourceLocation Loc, SourceRange Range, - DeclarationName InitEntity, - InitializationKind Kind, - ASTOwningVector<&ActionBase::DeleteExpr> &ConvertedArgs) { - - // Build the overload candidate set - Expr **Args = (Expr **)ArgsPtr.get(); - unsigned NumArgs = ArgsPtr.size(); - OverloadCandidateSet CandidateSet; - AddConstructorInitializationCandidates(*this, ClassType, Args, NumArgs, Kind, - CandidateSet); - - OverloadCandidateSet::iterator Best; - switch (BestViableFunction(CandidateSet, Loc, Best)) { - case OR_Success: - // We found a constructor. Break out so that we can convert the arguments - // appropriately. - break; - - case OR_No_Viable_Function: - if (InitEntity) - Diag(Loc, diag::err_ovl_no_viable_function_in_init) - << InitEntity << Range; - else - Diag(Loc, diag::err_ovl_no_viable_function_in_init) - << ClassType << Range; - PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); - return 0; - - case OR_Ambiguous: - if (InitEntity) - Diag(Loc, diag::err_ovl_ambiguous_init) << InitEntity << Range; - else - Diag(Loc, diag::err_ovl_ambiguous_init) << ClassType << Range; - PrintOverloadCandidates(CandidateSet, OCD_ViableCandidates, Args, NumArgs); - return 0; - - case OR_Deleted: - if (InitEntity) - Diag(Loc, diag::err_ovl_deleted_init) - << Best->Function->isDeleted() - << InitEntity << Range; - else { - const CXXRecordDecl *RD = - cast(ClassType->getAs()->getDecl()); - Diag(Loc, diag::err_ovl_deleted_init) - << Best->Function->isDeleted() - << RD->getDeclName() << Range; - } - PrintOverloadCandidates(CandidateSet, OCD_AllCandidates, Args, NumArgs); - return 0; - } - - // Convert the arguments, fill in default arguments, etc. - CXXConstructorDecl *Constructor = cast(Best->Function); - if (CompleteConstructorCall(Constructor, move(ArgsPtr), Loc, ConvertedArgs)) - return 0; - - return Constructor; -} - /// \brief Given a constructor and the set of arguments provided for the /// constructor, convert the arguments and add any required default arguments /// to form a proper call to this constructor. diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 847991d6f5..e944f328ed 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -263,29 +263,18 @@ Sema::ActOnCXXTypeConstructExpr(SourceRange TypeRange, TypeTy *TypeRep, if (NumExprs > 1 || !Record->hasTrivialConstructor() || !Record->hasTrivialDestructor()) { - ASTOwningVector<&ActionBase::DeleteExpr> ConstructorArgs(*this); - - CXXConstructorDecl *Constructor - = PerformInitializationByConstructor(Ty, move(exprs), - TypeRange.getBegin(), - SourceRange(TypeRange.getBegin(), - RParenLoc), - DeclarationName(), - InitializationKind::CreateDirect(TypeRange.getBegin(), - LParenLoc, - RParenLoc), - ConstructorArgs); - - if (!Constructor) - return ExprError(); - - OwningExprResult Result = - BuildCXXTemporaryObjectExpr(Constructor, Ty, TyBeginLoc, - move_arg(ConstructorArgs), RParenLoc); - if (Result.isInvalid()) - return ExprError(); - - return MaybeBindToTemporary(Result.takeAs()); + InitializedEntity Entity = InitializedEntity::InitializeTemporary(Ty); + InitializationKind Kind + = NumExprs ? InitializationKind::CreateDirect(TypeRange.getBegin(), + LParenLoc, RParenLoc) + : InitializationKind::CreateValue(TypeRange.getBegin(), + LParenLoc, RParenLoc); + InitializationSequence InitSeq(*this, Entity, Kind, Exprs, NumExprs); + OwningExprResult Result = InitSeq.Perform(*this, Entity, Kind, + move(exprs)); + + // FIXME: Improve AST representation? + return move(Result); } // Fall through to value-initialize an object of class type that